Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

264 lignes
7.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Benjamin Litzelmann
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #pragma once
  11. namespace lol
  12. {
  13. const String g_name_max("MAX");
  14. const String g_name_mouse("Mouse");
  15. const String g_name_keyboard("Keyboard");
  16. static String g_name_joystick()
  17. {
  18. return String("Joystick");
  19. }
  20. static String g_name_joystick(const uint64_t num)
  21. {
  22. return String::format("Joystick%d", (int)num);
  23. }
  24. # define _SC(id, str, name) const String g_name_key_##name(#name);
  25. # include "input/keys.h"
  26. //Mouse default buttons/axis
  27. const String g_name_mouse_key_left("Left");
  28. const String g_name_mouse_key_middle("Middle");
  29. const String g_name_mouse_key_right("Right");
  30. const String g_name_mouse_key_in_screen("InScreen");
  31. const String g_name_mouse_axis_x("X");
  32. const String g_name_mouse_axis_y("Y");
  33. const String g_name_mouse_axis_xpixel("XPixel");
  34. const String g_name_mouse_axis_ypixel("YPixel");
  35. const String g_name_mouse_axis_scroll("Scroll");
  36. const String g_name_mouse_cursor("Cursor");
  37. //Xbox default buttons/axis
  38. const String g_name_xbox_key_dpad_up("DPadUp");
  39. const String g_name_xbox_key_dpad_down("DPadDown");
  40. const String g_name_xbox_key_dpad_left("DPadLeft");
  41. const String g_name_xbox_key_dpad_right("DPadRight");
  42. const String g_name_xbox_key_left_thumb("LeftThumb");
  43. const String g_name_xbox_key_right_thumb("RightThumb");
  44. const String g_name_xbox_key_left_shoulder("LeftShoulder");
  45. const String g_name_xbox_key_right_shoulder("Rightshoulder");
  46. const String g_name_xbox_key_a("A");
  47. const String g_name_xbox_key_b("B");
  48. const String g_name_xbox_key_x("X");
  49. const String g_name_xbox_key_y("Y");
  50. const String g_name_xbox_key_start("Start");
  51. const String g_name_xbox_key_back("Back");
  52. const String g_name_xbox_axis_left_x("Axis1");
  53. const String g_name_xbox_axis_left_y("Axis2");
  54. const String g_name_xbox_axis_right_x("Axis3");
  55. const String g_name_xbox_axis_right_y("Axis4");
  56. const String g_name_xbox_axis_left_trigger("Axis5");
  57. const String g_name_xbox_axis_right_trigger("Axis6");
  58. class InputDevice
  59. {
  60. public:
  61. /** Gets the name of this input device */
  62. const String& GetName() const
  63. {
  64. return m_name;
  65. }
  66. /** Gets the index of the corresponding key, needed to call GetKey */
  67. ptrdiff_t GetKeyIndex(String const &name) const
  68. {
  69. return GetItemIndex(name, m_keynames);
  70. }
  71. /** Gets the index of the corresponding axis, needed to call GetAxis */
  72. ptrdiff_t GetAxisIndex(String const &name) const
  73. {
  74. return GetItemIndex(name, m_axisnames);
  75. }
  76. /** Gets the index of the corresponding cursor, needed to call GetCursor */
  77. ptrdiff_t GetCursorIndex(String const &name) const
  78. {
  79. return GetItemIndex(name, m_cursornames);
  80. }
  81. /** Gets the current state of the given key, true being pressed and
  82. * false being released */
  83. bool GetKey(ptrdiff_t index) const
  84. {
  85. return m_keys[index];
  86. }
  87. /** Gets the latest contents of text input. */
  88. String GetText();
  89. bool IsTextInputActive();
  90. void SetTextInputActive(bool status);
  91. /** Gets the current value of the given axis. Devices should try to
  92. * clamp this value between -1 and 1, though it is not guaranteed. */
  93. float GetAxis(ptrdiff_t index) const
  94. {
  95. return m_axis[index].m1 * m_axis[index].m2;
  96. }
  97. /** Gets the current value of the given cursor, 0,0 being the bottom-left
  98. * corner and 1,1 being the top-right corner */
  99. vec2 GetCursor(ptrdiff_t index) const
  100. {
  101. return m_cursors[index].m1;
  102. }
  103. /** Gets the coordinate of the pixel the cursor is currently over,
  104. * 0,0 being the bottom-left corner. */
  105. ivec2 GetCursorPixel(ptrdiff_t index) const
  106. {
  107. return m_cursors[index].m2;
  108. }
  109. /** Sets a per-device-axis sensitivity factor. The value returned by
  110. * the operating system will be multiplied by this value before being
  111. * returned by GetAxis */
  112. void SetAxisSensitivity(ptrdiff_t index, float sensitivity)
  113. {
  114. m_axis[index].m2 = sensitivity;
  115. }
  116. /** Gets the per-device-axis sensitivity factor. The value returned by
  117. * the operating system will be multiplied by this value before being
  118. * returned by GetAxis */
  119. float GetAxisSensitivity(ptrdiff_t index) const
  120. {
  121. return m_axis[index].m2;
  122. }
  123. /** Gets a list of the name of all available keys in this device */
  124. const array<String>& GetAllKeys() const
  125. {
  126. return m_keynames;
  127. }
  128. /** Gets a list of the name of all available axis in this device */
  129. const array<String>& GetAllAxis() const
  130. {
  131. return m_axisnames;
  132. }
  133. /** Gets a list of the name of all available cursors in this device */
  134. const array<String>& GetAllCursors() const
  135. {
  136. return m_cursornames;
  137. }
  138. /** Gets a list of the name of all available input devices */
  139. static array<String> GetAvailableDevices();
  140. /** Gets an input device by its name */
  141. static InputDevice* Get(String const &name)
  142. {
  143. return GetDevice(name);
  144. }
  145. /** Default helpers */
  146. static InputDevice* GetKeyboard()
  147. {
  148. return GetDevice(g_name_keyboard);
  149. }
  150. static InputDevice* GetMouse()
  151. {
  152. return GetDevice(g_name_mouse);
  153. }
  154. static int GetJoystickCount()
  155. {
  156. return joystick_count;
  157. }
  158. static InputDevice* GetJoystick(const uint64_t num)
  159. {
  160. return GetDevice(g_name_joystick(num));
  161. }
  162. /** Sets whether the mouse cursor should be captured. */
  163. static void CaptureMouse(bool activated)
  164. {
  165. m_capturemouse = activated;
  166. }
  167. protected:
  168. // TODO: hide all of this in a InputDeviceData?
  169. String m_name;
  170. array<String> m_keynames;
  171. array<String> m_axisnames;
  172. array<String> m_cursornames;
  173. /** Key states (pressed/released) */
  174. array<bool> m_keys;
  175. /** Text input state */
  176. String m_text;
  177. bool m_input_active;
  178. /** Axis states (value and sensitivity) */
  179. array<float, float> m_axis;
  180. /** Cursor position */
  181. array<vec2, ivec2> m_cursors;
  182. static bool m_capturemouse;
  183. InputDevice(String const &name)
  184. : m_name(name),
  185. m_input_active(false)
  186. {
  187. devices.push_unique(this);
  188. }
  189. ~InputDevice()
  190. {
  191. for (int i = 0; i < devices.count(); ++i)
  192. {
  193. if (devices[i] == this)
  194. {
  195. devices.remove(i);
  196. return;
  197. }
  198. }
  199. }
  200. private:
  201. static array<InputDevice*> devices;
  202. static int joystick_count;
  203. template <typename... T>
  204. ptrdiff_t GetItemIndex(String const &name, const array<String, T...>& a) const
  205. {
  206. for (int i = 0; i < a.count(); ++i)
  207. {
  208. if (a[i] == name)
  209. return i;
  210. }
  211. return -1;
  212. }
  213. static InputDevice* GetDevice(String const &name)
  214. {
  215. //Count the device types. TODO: Multi mouse/keyboard
  216. if (name.contains(g_name_joystick())) joystick_count++;
  217. for (int i = 0; i < devices.count(); ++i)
  218. {
  219. if (devices[i]->m_name == name)
  220. return devices[i];
  221. }
  222. return nullptr;
  223. }
  224. };
  225. } /* namespace lol */