Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
InputManager.OpenTK.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 
4 #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGL && SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
5 
6 using System;
7 using OpenTK.Input;
8 using SiliconStudio.Paradox.Games;
9 using GameWindow = OpenTK.GameWindow;
11 
12 namespace SiliconStudio.Paradox.Input
13 {
14  public partial class InputManager
15  {
16  private GameWindow gameWindow;
17 
18  public void InitializeFromOpenTK(GameContext gameContext)
19  {
20  gameWindow = (OpenTK.GameWindow)gameContext.Control;
21 
22  gameWindow.Keyboard.KeyDown += Keyboard_KeyDown;
23  gameWindow.Keyboard.KeyUp += Keyboard_KeyUp;
24  gameWindow.Mouse.ButtonDown += Mouse_ButtonDown;
25  gameWindow.Mouse.ButtonUp += Mouse_ButtonUp;
26  gameWindow.Mouse.Move += Mouse_Move;
27  gameWindow.Resize += GameWindowOnResize;
28 
29  GameWindowOnResize(null, EventArgs.Empty);
30  }
31 
32  private void GameWindowOnResize(object sender, EventArgs eventArgs)
33  {
34  ControlHeight = gameWindow.Height;
35  ControlWidth = gameWindow.Width;
36  }
37 
38  void Mouse_Move(object sender, MouseMoveEventArgs e)
39  {
40  CurrentMousePosition = new Vector2(e.X / ControlWidth, e.Y / ControlHeight);
41 
42  // trigger touch move events
43  foreach (MouseButton button in Enum.GetValues(typeof(MouseButton)))
44  {
45  var buttonId = (int)button;
46  if (MouseButtonCurrentlyDown[buttonId])
47  HandlePointerEvents(buttonId, CurrentMousePosition, PointerState.Move, PointerType.Mouse);
48  }
49  }
50 
51  void Mouse_ButtonUp(object sender, MouseButtonEventArgs e)
52  {
53  var button = ConvertMouseButtonFromOpenTK(e.Button);
54  var buttonId = (int)button;
55 
56  // the mouse events series has been interrupted because out of the window.
57  if (!MouseButtonCurrentlyDown[buttonId])
58  return;
59 
60  CurrentMousePosition = new Vector2(e.X / ControlWidth, e.Y / ControlHeight);
61  var mouseInputEvent = new MouseInputEvent { Type = InputEventType.Up, MouseButton = button };
62  lock (MouseInputEvents)
63  MouseInputEvents.Add(mouseInputEvent);
64 
65  MouseButtonCurrentlyDown[buttonId] = false;
66  HandlePointerEvents(buttonId, CurrentMousePosition, PointerState.Up, PointerType.Mouse);
67  }
68 
69  void Mouse_ButtonDown(object sender, MouseButtonEventArgs e)
70  {
71  var button = ConvertMouseButtonFromOpenTK(e.Button);
72  var buttonId = (int)button;
73 
74  CurrentMousePosition = new Vector2(e.X / ControlWidth, e.Y / ControlHeight);
75  var mouseInputEvent = new MouseInputEvent { Type = InputEventType.Down, MouseButton = button };
76  lock (MouseInputEvents)
77  MouseInputEvents.Add(mouseInputEvent);
78 
79  MouseButtonCurrentlyDown[buttonId] = true;
80  HandlePointerEvents(buttonId, CurrentMousePosition, PointerState.Down, PointerType.Mouse);
81  }
82 
83  void Keyboard_KeyUp(object sender, KeyboardKeyEventArgs arg)
84  {
85  lock (KeyboardInputEvents)
86  KeyboardInputEvents.Add(new KeyboardInputEvent { Key = ConvertKeyFromOpenTK(arg.Key), Type = InputEventType.Up });
87  }
88 
89  void Keyboard_KeyDown(object sender, KeyboardKeyEventArgs arg)
90  {
91  lock (KeyboardInputEvents)
92  KeyboardInputEvents.Add(new KeyboardInputEvent { Key = ConvertKeyFromOpenTK(arg.Key), Type = InputEventType.Down });
93  }
94 
95  private Keys ConvertKeyFromOpenTK(Key key)
96  {
97  switch (key)
98  {
99  case Key.Number0: return Keys.D0;
100  case Key.Number1: return Keys.D1;
101  case Key.Number2: return Keys.D2;
102  case Key.Number3: return Keys.D3;
103  case Key.Number4: return Keys.D4;
104  case Key.Number5: return Keys.D5;
105  case Key.Number6: return Keys.D6;
106  case Key.Number7: return Keys.D7;
107  case Key.Number8: return Keys.D8;
108  case Key.Number9: return Keys.D9;
109  case Key.A: return Keys.A;
110  case Key.B: return Keys.B;
111  case Key.C: return Keys.C;
112  case Key.D: return Keys.D;
113  case Key.E: return Keys.E;
114  case Key.F: return Keys.F;
115  case Key.G: return Keys.G;
116  case Key.H: return Keys.H;
117  case Key.I: return Keys.I;
118  case Key.J: return Keys.J;
119  case Key.K: return Keys.K;
120  case Key.L: return Keys.L;
121  case Key.M: return Keys.M;
122  case Key.N: return Keys.N;
123  case Key.O: return Keys.O;
124  case Key.P: return Keys.P;
125  case Key.Q: return Keys.Q;
126  case Key.R: return Keys.R;
127  case Key.S: return Keys.S;
128  case Key.T: return Keys.T;
129  case Key.U: return Keys.U;
130  case Key.V: return Keys.V;
131  case Key.W: return Keys.W;
132  case Key.X: return Keys.X;
133  case Key.Y: return Keys.Y;
134  case Key.Z: return Keys.Z;
135  case Key.F1: return Keys.F1;
136  case Key.F2: return Keys.F2;
137  case Key.F3: return Keys.F3;
138  case Key.F4: return Keys.F4;
139  case Key.F5: return Keys.F5;
140  case Key.F6: return Keys.F6;
141  case Key.F7: return Keys.F7;
142  case Key.F8: return Keys.F8;
143  case Key.F9: return Keys.F9;
144  case Key.F10: return Keys.F10;
145  case Key.F11: return Keys.F11;
146  case Key.F12: return Keys.F12;
147  case Key.Space: return Keys.Space;
148  case Key.AltLeft: return Keys.LeftAlt;
149  case Key.AltRight: return Keys.RightAlt;
150  case Key.ShiftLeft: return Keys.LeftShift;
151  case Key.ShiftRight: return Keys.RightShift;
152  case Key.ControlLeft: return Keys.LeftCtrl;
153  case Key.ControlRight: return Keys.RightCtrl;
154  case Key.Enter: return Keys.Enter;
155  case Key.BackSpace: return Keys.Back;
156  case Key.Tab: return Keys.Tab;
157  case Key.Insert: return Keys.Insert;
158  case Key.Delete: return Keys.Delete;
159  case Key.Home: return Keys.Home;
160  case Key.End: return Keys.End;
161  case Key.PageUp: return Keys.PageUp;
162  case Key.PageDown: return Keys.PageDown;
163  case Key.Up: return Keys.Up;
164  case Key.Down: return Keys.Down;
165  case Key.Left: return Keys.Left;
166  case Key.Right: return Keys.Right;
167  default:
168  return (Keys)(-1);
169  }
170  }
171 
172  private MouseButton ConvertMouseButtonFromOpenTK(OpenTK.Input.MouseButton mouseButton)
173  {
174  switch (mouseButton)
175  {
176  case OpenTK.Input.MouseButton.Left:
177  return MouseButton.Left;
178  case OpenTK.Input.MouseButton.Right:
179  return MouseButton.Right;
180  case OpenTK.Input.MouseButton.Middle:
181  return MouseButton.Middle;
182  case OpenTK.Input.MouseButton.Button1:
183  return MouseButton.Extended1;
184  case OpenTK.Input.MouseButton.Button2:
185  return MouseButton.Extended2;
186  }
187  return (MouseButton)(-1);
188  }
189  }
190 }
191 #endif
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
PointerType
Type of a pointer device.
Definition: PointerType.cs:8
MouseButton
Mouse buttons.
Definition: MouseButton.cs:10
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}.
document false