Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InputManager.Android.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_ANDROID
4 
5 using System;
6 using Android.Views;
7 using OpenTK.Platform.Android;
8 using SiliconStudio.Core;
9 using SiliconStudio.Core.Mathematics;
10 
11 namespace SiliconStudio.Paradox.Input
12 {
13  public partial class InputManager
14  {
15  private AndroidGameView gameView;
16 
17  public InputManager(IServiceRegistry registry) : base(registry)
18  {
19  HasKeyboard = true;
20  HasMouse = false;
21  HasPointer = true;
22  }
23 
24  public override void Initialize()
25  {
26  var viewListener = new ViewListener(this);
27  gameView = Game.Context.Control;
28  gameView.SetOnTouchListener(viewListener);
29  gameView.SetOnKeyListener(viewListener);
30  gameView.Resize += GameViewOnResize;
31 
32  GameViewOnResize(null, EventArgs.Empty);
33  }
34 
35  private void GameViewOnResize(object sender, EventArgs eventArgs)
36  {
37  ControlWidth = gameView.Size.Width;
38  ControlHeight = gameView.Size.Height;
39  }
40 
41  private bool OnTouch(MotionEvent e)
42  {
43  PointerState state;
44  switch (e.ActionMasked)
45  {
46  case MotionEventActions.Cancel:
47  state = PointerState.Cancel;
48  break;
49  case MotionEventActions.Move:
50  state = PointerState.Move;
51  break;
52  case MotionEventActions.Outside:
53  state = PointerState.Out;
54  break;
55  case MotionEventActions.Down:
56  case MotionEventActions.PointerDown:
57  state = PointerState.Down;
58  break;
59  case MotionEventActions.Up:
60  case MotionEventActions.PointerUp:
61  state = PointerState.Up;
62  break;
63  default:
64  // Not handled
65  return false;
66  }
67 
68  var startIndex = 0;
69  var endIndex = e.PointerCount;
70 
71  if (state == PointerState.Down || state == PointerState.Up || state == PointerState.Out)
72  {
73  startIndex = e.ActionIndex;
74  endIndex = startIndex + 1;
75  }
76 
77  for (var i = startIndex; i < endIndex; ++i)
78  {
79  var pointerId = e.GetPointerId(i);
80  var pixelPosition = new Vector2(e.GetX(i), e.GetY(i));
81 
82  if(MultiTouchEnabled || pointerId == 0) // manually drop multi-touch events when disabled
83  HandlePointerEvents(pointerId, NormalizeScreenPosition(pixelPosition), state);
84  }
85 
86  return true;
87  }
88 
89  private bool OnKey(Keycode keyCode, Android.Views.KeyEvent e)
90  {
91  lock (KeyboardInputEvents)
92  {
93  KeyboardInputEvents.Add(new KeyboardInputEvent
94  {
95  Key = ConvertKeyFromAndroid(keyCode),
96  Type = e.Action == KeyEventActions.Down ? InputEventType.Down : InputEventType.Up,
97  });
98  }
99  return true;
100  }
101 
102  private Keys ConvertKeyFromAndroid(Keycode key)
103  {
104  switch (key)
105  {
106  case Keycode.Num0: return Keys.D0;
107  case Keycode.Num1: return Keys.D1;
108  case Keycode.Num2: return Keys.D2;
109  case Keycode.Num3: return Keys.D3;
110  case Keycode.Num4: return Keys.D4;
111  case Keycode.Num5: return Keys.D5;
112  case Keycode.Num6: return Keys.D6;
113  case Keycode.Num7: return Keys.D7;
114  case Keycode.Num8: return Keys.D8;
115  case Keycode.Num9: return Keys.D9;
116  case Keycode.A: return Keys.A;
117  case Keycode.B: return Keys.B;
118  case Keycode.C: return Keys.C;
119  case Keycode.D: return Keys.D;
120  case Keycode.E: return Keys.E;
121  case Keycode.F: return Keys.F;
122  case Keycode.G: return Keys.G;
123  case Keycode.H: return Keys.H;
124  case Keycode.I: return Keys.I;
125  case Keycode.J: return Keys.J;
126  case Keycode.K: return Keys.K;
127  case Keycode.L: return Keys.L;
128  case Keycode.M: return Keys.M;
129  case Keycode.N: return Keys.N;
130  case Keycode.O: return Keys.O;
131  case Keycode.P: return Keys.P;
132  case Keycode.Q: return Keys.Q;
133  case Keycode.R: return Keys.R;
134  case Keycode.S: return Keys.S;
135  case Keycode.T: return Keys.T;
136  case Keycode.U: return Keys.U;
137  case Keycode.V: return Keys.V;
138  case Keycode.W: return Keys.W;
139  case Keycode.X: return Keys.X;
140  case Keycode.Y: return Keys.Y;
141  case Keycode.Z: return Keys.Z;
142  case Keycode.AltLeft: return Keys.LeftAlt;
143  case Keycode.AltRight: return Keys.RightAlt;
144  case Keycode.ShiftLeft: return Keys.LeftShift;
145  case Keycode.ShiftRight: return Keys.RightShift;
146  case Keycode.Enter: return Keys.Enter;
147  case Keycode.Back: return Keys.Back;
148  case Keycode.Tab: return Keys.Tab;
149  case Keycode.Del: return Keys.Delete;
150  case Keycode.PageUp: return Keys.PageUp;
151  case Keycode.PageDown: return Keys.PageDown;
152  case Keycode.DpadUp: return Keys.Up;
153  case Keycode.DpadDown: return Keys.Down;
154  case Keycode.DpadLeft: return Keys.Right;
155  case Keycode.DpadRight: return Keys.Right;
156  default:
157  return (Keys)(-1);
158  }
159  }
160 
161  class ViewListener : Java.Lang.Object, View.IOnTouchListener, View.IOnKeyListener
162  {
163  private readonly InputManager inputManager;
164 
165  public ViewListener(InputManager inputManager)
166  {
167  this.inputManager = inputManager;
168  }
169 
170  public bool OnTouch(View v, MotionEvent e)
171  {
172  return inputManager.OnTouch(e);
173  }
174 
175  public bool OnKey(View v, Keycode keyCode, Android.Views.KeyEvent e)
176  {
177  return inputManager.OnKey(keyCode, e);
178  }
179  }
180 
181  // No easy way to enable/disable multi-touch on android so we drop them manually in OnTouch function
182  public override bool MultiTouchEnabled { get; set; }
183  }
184 }
185 #endif
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
SiliconStudio.Core.IServiceRegistry IServiceRegistry
Definition: ModelRenderer.cs:9
SiliconStudio.Paradox.Input.Keys Keys
PointerState
State of a pointer event.
Definition: PointerState.cs:8
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.