Input.cs 11 KB

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