2
0

Input.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. namespace BansheeEngine
  7. {
  8. public struct ButtonEvent
  9. {
  10. internal ButtonCode buttonCode;
  11. internal int deviceIdx;
  12. internal ButtonEvent(ButtonCode buttonCode, int deviceIdx)
  13. {
  14. this.buttonCode = buttonCode;
  15. this.deviceIdx = deviceIdx;
  16. }
  17. public ButtonCode Button { get { return buttonCode; } }
  18. public int DeviceIndex { get { return deviceIdx; } }
  19. public bool IsKeyboard { get { return ((int)buttonCode & 0xC0000000) == 0; }}
  20. public bool IsMouse { get { return ((int)buttonCode & 0x80000000) != 0; } }
  21. public bool IsGamepad { get { return ((int)buttonCode & 0x40000000) != 0; } }
  22. };
  23. // Do not change IDs, must match PointerEventButton C++ enum
  24. public enum PointerEventButton
  25. {
  26. Left, Middle, Right, Count
  27. };
  28. public struct PointerEvent
  29. {
  30. internal Vector2I _screenPos;
  31. internal Vector2I _delta;
  32. internal PointerEventButton _button;
  33. internal bool _shift;
  34. internal bool _control;
  35. internal bool _alt;
  36. internal float _mouseWheelScrollAmount;
  37. internal PointerEvent(Vector2I screenPos, Vector2I delta, PointerEventButton button,
  38. bool shift, bool control, bool alt, float mouseWheelScrollAmount)
  39. {
  40. _screenPos = screenPos;
  41. _delta = delta;
  42. _button = button;
  43. _shift = shift;
  44. _control = control;
  45. _alt = alt;
  46. _mouseWheelScrollAmount = mouseWheelScrollAmount;
  47. }
  48. public Vector2I screenPos { get { return _screenPos; } }
  49. public Vector2I delta { get { return _delta; } }
  50. public PointerEventButton button { get { return _button; } }
  51. public bool shift { get { return _shift; } }
  52. public bool control { get { return _control; } }
  53. public bool alt { get { return _alt; } }
  54. public float scrollAmount { get { return _mouseWheelScrollAmount; } }
  55. }
  56. public struct TextInputEvent
  57. {
  58. internal int textChar;
  59. internal TextInputEvent(int textChar)
  60. {
  61. this.textChar = textChar;
  62. }
  63. public int Char { get { return textChar; } }
  64. }
  65. public static class Input
  66. {
  67. public delegate void ButtonEventDelegate(ButtonEvent ev);
  68. public delegate void TextInputEventDelegate(TextInputEvent ev);
  69. public delegate void PointerEventDelegate(PointerEvent ev);
  70. public static event ButtonEventDelegate OnButtonDown;
  71. public static event ButtonEventDelegate OnButtonUp;
  72. public static event TextInputEventDelegate OnCharInput;
  73. public static event PointerEventDelegate OnPointerMoved;
  74. public static event PointerEventDelegate OnPointerPressed;
  75. public static event PointerEventDelegate OnPointerReleased;
  76. public static event PointerEventDelegate OnPointerDoubleClick;
  77. public static float GetAxisValue(InputAxis axis, int deviceIdx = 0)
  78. {
  79. return Internal_GetAxisValue(axis, deviceIdx);
  80. }
  81. public static bool IsButtonHeld(ButtonCode code, int deviceIdx = 0)
  82. {
  83. return Internal_IsButtonHeld(code, deviceIdx);
  84. }
  85. public static bool IsButtonUp(ButtonCode code, int deviceIdx = 0)
  86. {
  87. return Internal_IsButtonUp(code, deviceIdx);
  88. }
  89. public static bool IsButtonDown(ButtonCode code, int deviceIdx = 0)
  90. {
  91. return Internal_IsButtonDown(code, deviceIdx);
  92. }
  93. public static Vector2I PointerPosition
  94. {
  95. get
  96. {
  97. Vector2I value;
  98. Internal_GetPointerPosition(out value);
  99. return value;
  100. }
  101. }
  102. public static Vector2I PointerDelta
  103. {
  104. get
  105. {
  106. Vector2I value;
  107. Internal_GetPointerDelta(out value);
  108. return value;
  109. }
  110. }
  111. private static void Internal_TriggerButtonDown(ButtonCode code, int deviceIdx)
  112. {
  113. ButtonEvent ev = new ButtonEvent(code, deviceIdx);
  114. if (OnButtonDown != null)
  115. OnButtonDown(ev);
  116. }
  117. private static void Internal_TriggerButtonUp(ButtonCode code, int deviceIdx)
  118. {
  119. ButtonEvent ev = new ButtonEvent(code, deviceIdx);
  120. if (OnButtonUp != null)
  121. OnButtonUp(ev);
  122. }
  123. private static void Internal_TriggerCharInput(int textChar)
  124. {
  125. TextInputEvent ev = new TextInputEvent(textChar);
  126. if (OnCharInput != null)
  127. OnCharInput(ev);
  128. }
  129. private static void Internal_TriggerPointerMove(Vector2I screenPos, Vector2I delta, PointerEventButton button, bool shift,
  130. bool ctrl, bool alt, float scrollAmount)
  131. {
  132. PointerEvent ev = new PointerEvent(screenPos, delta, button, shift, ctrl, alt, scrollAmount);
  133. if (OnPointerMoved != null)
  134. OnPointerMoved(ev);
  135. }
  136. private static void Internal_TriggerPointerPressed(Vector2I screenPos, Vector2I delta, PointerEventButton button, bool shift,
  137. bool ctrl, bool alt, float scrollAmount)
  138. {
  139. PointerEvent ev = new PointerEvent(screenPos, delta, button, shift, ctrl, alt, scrollAmount);
  140. if (OnPointerPressed != null)
  141. OnPointerPressed(ev);
  142. }
  143. private static void Internal_TriggerPointerReleased(Vector2I screenPos, Vector2I delta, PointerEventButton button, bool shift,
  144. bool ctrl, bool alt, float scrollAmount)
  145. {
  146. PointerEvent ev = new PointerEvent(screenPos, delta, button, shift, ctrl, alt, scrollAmount);
  147. if (OnPointerReleased != null)
  148. OnPointerReleased(ev);
  149. }
  150. private static void Internal_TriggerPointerDoubleClick(Vector2I screenPos, Vector2I delta, PointerEventButton button, bool shift,
  151. bool ctrl, bool alt, float scrollAmount)
  152. {
  153. PointerEvent ev = new PointerEvent(screenPos, delta, button, shift, ctrl, alt, scrollAmount);
  154. if (OnPointerDoubleClick != null)
  155. OnPointerDoubleClick(ev);
  156. }
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern float Internal_GetAxisValue(InputAxis axis, int deviceIdx);
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern bool Internal_IsButtonHeld(ButtonCode keyCode, int deviceIdx);
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern bool Internal_IsButtonUp(ButtonCode keyCode, int deviceIdx);
  163. [MethodImpl(MethodImplOptions.InternalCall)]
  164. private static extern bool Internal_IsButtonDown(ButtonCode keyCode, int deviceIdx);
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern void Internal_GetPointerPosition(out Vector2I position);
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern void Internal_GetPointerDelta(out Vector2I delta);
  169. }
  170. // Do not change IDs, must match ButtonCode C++ enum
  171. public enum ButtonCode : uint
  172. {
  173. Unassigned = 0x00,
  174. Escape = 0x01,
  175. Num1 = 0x02,
  176. Num2 = 0x03,
  177. Num3 = 0x04,
  178. Num4 = 0x05,
  179. Num5 = 0x06,
  180. Num6 = 0x07,
  181. Num7 = 0x08,
  182. Num8 = 0x09,
  183. Num9 = 0x0A,
  184. Num0 = 0x0B,
  185. Minus = 0x0C,
  186. Equals = 0x0D,
  187. Back = 0x0E,
  188. Tab = 0x0F,
  189. Q = 0x10,
  190. W = 0x11,
  191. E = 0x12,
  192. R = 0x13,
  193. T = 0x14,
  194. Y = 0x15,
  195. U = 0x16,
  196. I = 0x17,
  197. O = 0x18,
  198. P = 0x19,
  199. LeftBracket = 0x1A,
  200. RightBracket = 0x1B,
  201. Return = 0x1C,
  202. LeftControl = 0x1D,
  203. A = 0x1E,
  204. S = 0x1F,
  205. D = 0x20,
  206. F = 0x21,
  207. G = 0x22,
  208. H = 0x23,
  209. J = 0x24,
  210. K = 0x25,
  211. L = 0x26,
  212. Semicolon = 0x27,
  213. Apostrophe = 0x28,
  214. Grave = 0x29,
  215. LeftShift = 0x2A,
  216. Backslash = 0x2B,
  217. Z = 0x2C,
  218. X = 0x2D,
  219. C = 0x2E,
  220. V = 0x2F,
  221. B = 0x30,
  222. N = 0x31,
  223. M = 0x32,
  224. Comma = 0x33,
  225. Period = 0x34,
  226. Slash = 0x35,
  227. RightShift = 0x36,
  228. KeypadMultiply = 0x37,
  229. LeftMenu = 0x38,
  230. Space = 0x39,
  231. CapsLock = 0x3A,
  232. F1 = 0x3B,
  233. F2 = 0x3C,
  234. F3 = 0x3D,
  235. F4 = 0x3E,
  236. F5 = 0x3F,
  237. F6 = 0x40,
  238. F7 = 0x41,
  239. F8 = 0x42,
  240. F9 = 0x43,
  241. F10 = 0x44,
  242. NumLock = 0x45,
  243. ScrollLock = 0x46,
  244. Keypad7 = 0x47,
  245. Keypad8 = 0x48,
  246. Keypad9 = 0x49,
  247. KeypadSubtract = 0x4A,
  248. Keypad4 = 0x4B,
  249. Keypad5 = 0x4C,
  250. Keypad6 = 0x4D,
  251. KeypadAdd = 0x4E,
  252. Keypad1 = 0x4F,
  253. Keypad2 = 0x50,
  254. Keypad3 = 0x51,
  255. Keypad0 = 0x52,
  256. KeypadDecimal = 0x53,
  257. F11 = 0x57,
  258. F12 = 0x58,
  259. F13 = 0x64,
  260. F14 = 0x65,
  261. F15 = 0x66,
  262. KeypadEquals = 0x8D,
  263. At = 0x91,
  264. Colon = 0x92,
  265. NumpadEnter = 0x9C,
  266. RightControl = 0x9D,
  267. KeypadComma = 0xB3,
  268. KeypadDivide = 0xB5,
  269. RightMenu = 0xB8,
  270. Pause = 0xC5,
  271. Home = 0xC7,
  272. Up = 0xC8,
  273. PageUp = 0xC9,
  274. Left = 0xCB,
  275. Right = 0xCD,
  276. End = 0xCF,
  277. Down = 0xD0,
  278. PageDown = 0xD1,
  279. Insert = 0xD2,
  280. Delete = 0xD3,
  281. LeftWindows = 0xDB,
  282. RightWindows = 0xDC,
  283. MouseLeft = 0x800000EE,
  284. MouseRight = 0x800000EF,
  285. MouseMiddle = 0x800000F0,
  286. MouseBtn4 = 0x800000F1,
  287. MouseBtn5 = 0x800000F2,
  288. MouseBtn6 = 0x800000F3,
  289. MouseBtn7 = 0x800000F4,
  290. MouseBtn8 = 0x800000F5,
  291. MouseBtn9 = 0x800000F6,
  292. MouseBtn10 = 0x800000F7,
  293. MouseBtn11 = 0x800000F8,
  294. MouseBtn12 = 0x800000F9,
  295. MouseBtn13 = 0x800000FA,
  296. MouseBtn14 = 0x800000FB,
  297. MouseBtn15 = 0x800000FC,
  298. MouseBtn16 = 0x800000FD,
  299. GamepadA = 0x4000010F,
  300. GamepadB = 0x40000110,
  301. GamepadX = 0x40000111,
  302. GamepadY = 0x40000112,
  303. GamepadLB = 0x40000113,
  304. GamepadRB = 0x40000114,
  305. GamepadLS = 0x40000115,
  306. GamepadRS = 0x40000116,
  307. GamepadBack = 0x40000117,
  308. GamepadStart = 0x40000118,
  309. GamepadDPadLeft = 0x40000119,
  310. GamepadDPadRight = 0x4000011A,
  311. GamepadDPadUp = 0x4000011B,
  312. GamepadDPatDown = 0x4000011C,
  313. GamepadBtn1 = 0x4000011D,
  314. GamepadBtn2 = 0x4000011E,
  315. GamepadBtn3 = 0x4000011F,
  316. GamepadBtn4 = 0x40000120,
  317. GamepadBtn5 = 0x40000121,
  318. GamepadBtn6 = 0x40000122,
  319. GamepadBtn7 = 0x40000123,
  320. GamepadBtn8 = 0x40000124,
  321. GamepadBtn9 = 0x40000125,
  322. GamepadBtn10 = 0x40000126,
  323. GamepadBtn11 = 0x40000127,
  324. GamepadBtn12 = 0x40000128,
  325. GamepadBtn13 = 0x40000129,
  326. GamepadBtn14 = 0x4000012A,
  327. GamepadBtn15 = 0x4000012B,
  328. GamepadBtn16 = 0x4000012C,
  329. Count = 249,
  330. NumKeys = 203, // IMPORTANT: Make sure to update these if you modify the values above
  331. NumMouseButtons = 16,
  332. NumGamepadButtons = 30,
  333. };
  334. // Do not change IDs, must match InputAxis C++ enum
  335. public enum InputAxis
  336. {
  337. MouseX,
  338. MouseY,
  339. MouseZ,
  340. LeftStickX,
  341. LeftStickY,
  342. RightStickX,
  343. RightStickY,
  344. LeftTrigger,
  345. RightTrigger,
  346. Count // Keep at end
  347. };
  348. }