Input.cs 11 KB

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