No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

268 líneas
7.7 KiB

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