25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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