Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

259 строки
7.1 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. /** Gets the current value of the given axis. Devices should try to
  90. * clamp this value between -1 and 1, though it is not guaranteed. */
  91. float GetAxis(ptrdiff_t index) const
  92. {
  93. return m_axis[index].m1 * m_axis[index].m2;
  94. }
  95. /** Gets the current value of the given cursor, 0,0 being the bottom-left
  96. * corner and 1,1 being the top-right corner */
  97. vec2 GetCursor(ptrdiff_t index) const
  98. {
  99. return m_cursors[index].m1;
  100. }
  101. /** Gets the coordinate of the pixel the cursor is currently over,
  102. * 0,0 being the bottom-left corner. */
  103. ivec2 GetCursorPixel(ptrdiff_t index) const
  104. {
  105. return m_cursors[index].m2;
  106. }
  107. /** Sets a per-device-axis sensitivity factor. The value returned by
  108. * the operating system will be multiplied by this value before being
  109. * returned by GetAxis */
  110. void SetAxisSensitivity(ptrdiff_t index, float sensitivity)
  111. {
  112. m_axis[index].m2 = sensitivity;
  113. }
  114. /** Gets the per-device-axis sensitivity factor. The value returned by
  115. * the operating system will be multiplied by this value before being
  116. * returned by GetAxis */
  117. float GetAxisSensitivity(ptrdiff_t index) const
  118. {
  119. return m_axis[index].m2;
  120. }
  121. /** Gets a list of the name of all available keys in this device */
  122. const array<String>& GetAllKeys() const
  123. {
  124. return m_keynames;
  125. }
  126. /** Gets a list of the name of all available axis in this device */
  127. const array<String>& GetAllAxis() const
  128. {
  129. return m_axisnames;
  130. }
  131. /** Gets a list of the name of all available cursors in this device */
  132. const array<String>& GetAllCursors() const
  133. {
  134. return m_cursornames;
  135. }
  136. /** Gets a list of the name of all available input devices */
  137. static array<String> GetAvailableDevices();
  138. /** Gets an input device by its name */
  139. static InputDevice* Get(String const &name)
  140. {
  141. return GetDevice(name);
  142. }
  143. /** Default helpers */
  144. static InputDevice* GetKeyboard()
  145. {
  146. return GetDevice(g_name_keyboard);
  147. }
  148. static InputDevice* GetMouse()
  149. {
  150. return GetDevice(g_name_mouse);
  151. }
  152. static int GetJoystickCount()
  153. {
  154. return joystick_count;
  155. }
  156. static InputDevice* GetJoystick(const uint64_t num)
  157. {
  158. return GetDevice(g_name_joystick(num));
  159. }
  160. /** Sets whether the mouse cursor should be captured. */
  161. static void CaptureMouse(bool activated)
  162. {
  163. m_capturemouse = activated;
  164. }
  165. protected:
  166. // TODO: hide all of this in a InputDeviceData?
  167. String m_name;
  168. array<String> m_keynames;
  169. array<String> m_axisnames;
  170. array<String> m_cursornames;
  171. /** Key states (pressed/released) */
  172. array<bool> m_keys;
  173. /** Text input state */
  174. String m_text;
  175. /** Axis states (value and sensitivity) */
  176. array<float, float> m_axis;
  177. /** Cursor position */
  178. array<vec2, ivec2> m_cursors;
  179. static bool m_capturemouse;
  180. InputDevice(String const &name) : m_name(name)
  181. {
  182. devices.push_unique(this);
  183. }
  184. ~InputDevice()
  185. {
  186. for (int i = 0; i < devices.count(); ++i)
  187. {
  188. if (devices[i] == this)
  189. {
  190. devices.remove(i);
  191. return;
  192. }
  193. }
  194. }
  195. private:
  196. static array<InputDevice*> devices;
  197. static int joystick_count;
  198. template <typename... T>
  199. ptrdiff_t GetItemIndex(String const &name, const array<String, T...>& a) const
  200. {
  201. for (int i = 0; i < a.count(); ++i)
  202. {
  203. if (a[i] == name)
  204. return i;
  205. }
  206. return -1;
  207. }
  208. static InputDevice* GetDevice(String const &name)
  209. {
  210. //Count the device types. TODO: Multi mouse/keyboard
  211. if (name.contains(g_name_joystick())) joystick_count++;
  212. for (int i = 0; i < devices.count(); ++i)
  213. {
  214. if (devices[i]->m_name == name)
  215. return devices[i];
  216. }
  217. return nullptr;
  218. }
  219. };
  220. } /* namespace lol */