input_manager.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "core/error/error.inl"
  6. #include "core/math/constants.h"
  7. #include "core/math/vector3.inl"
  8. #include "device/input_device.h"
  9. #include "device/input_manager.h"
  10. namespace crown
  11. {
  12. static const char *s_keyboard_button_names[] =
  13. {
  14. "tab", // KeyboardButton::TAB
  15. "enter", // KeyboardButton::ENTER
  16. "escape", // KeyboardButton::ESCAPE
  17. "space", // KeyboardButton::SPACE
  18. "backspace", // KeyboardButton::BACKSPACE
  19. "num_lock", // KeyboardButton::NUM_LOCK
  20. "numpad_enter", // KeyboardButton::NUMPAD_ENTER
  21. "numpad_.", // KeyboardButton::NUMPAD_DELETE
  22. "numpad_*", // KeyboardButton::NUMPAD_MULTIPLY
  23. "numpad_+", // KeyboardButton::NUMPAD_ADD
  24. "numpad_-", // KeyboardButton::NUMPAD_SUBTRACT
  25. "numpad_/", // KeyboardButton::NUMPAD_DIVIDE
  26. "numpad_0", // KeyboardButton::NUMPAD_0
  27. "numpad_1", // KeyboardButton::NUMPAD_1
  28. "numpad_2", // KeyboardButton::NUMPAD_2
  29. "numpad_3", // KeyboardButton::NUMPAD_3
  30. "numpad_4", // KeyboardButton::NUMPAD_4
  31. "numpad_5", // KeyboardButton::NUMPAD_5
  32. "numpad_6", // KeyboardButton::NUMPAD_6
  33. "numpad_7", // KeyboardButton::NUMPAD_7
  34. "numpad_8", // KeyboardButton::NUMPAD_8
  35. "numpad_9", // KeyboardButton::NUMPAD_9
  36. "f1", // KeyboardButton::F1
  37. "f2", // KeyboardButton::F2
  38. "f3", // KeyboardButton::F3
  39. "f4", // KeyboardButton::F4
  40. "f5", // KeyboardButton::F5
  41. "f6", // KeyboardButton::F6
  42. "f7", // KeyboardButton::F7
  43. "f8", // KeyboardButton::F8
  44. "f9", // KeyboardButton::F9
  45. "f10", // KeyboardButton::F10
  46. "f11", // KeyboardButton::F11
  47. "f12", // KeyboardButton::F12
  48. "home", // KeyboardButton::HOME
  49. "left", // KeyboardButton::LEFT
  50. "up", // KeyboardButton::UP
  51. "right", // KeyboardButton::RIGHT
  52. "down", // KeyboardButton::DOWN
  53. "page_up", // KeyboardButton::PAGE_UP
  54. "page_down", // KeyboardButton::PAGE_DOWN
  55. "ins", // KeyboardButton::INS
  56. "del", // KeyboardButton::DEL
  57. "end", // KeyboardButton::END
  58. "ctrl_left", // KeyboardButton::CTRL_LEFT
  59. "ctrl_right", // KeyboardButton::CTRL_RIGHT
  60. "shift_left", // KeyboardButton::SHIFT_LEFT
  61. "shift_right", // KeyboardButton::SHIFT_RIGHT
  62. "caps_lock", // KeyboardButton::CAPS_LOCK
  63. "alt_left", // KeyboardButton::ALT_LEFT
  64. "alt_right", // KeyboardButton::ALT_RIGHT
  65. "super_left", // KeyboardButton::SUPER_LEFT
  66. "super_right", // KeyboardButton::SUPER_RIGHT
  67. "0", // KeyboardButton::NUMBER_0
  68. "1", // KeyboardButton::NUMBER_1
  69. "2", // KeyboardButton::NUMBER_2
  70. "3", // KeyboardButton::NUMBER_3
  71. "4", // KeyboardButton::NUMBER_4
  72. "5", // KeyboardButton::NUMBER_5
  73. "6", // KeyboardButton::NUMBER_6
  74. "7", // KeyboardButton::NUMBER_7
  75. "8", // KeyboardButton::NUMBER_8
  76. "9", // KeyboardButton::NUMBER_9
  77. "a", // KeyboardButton::A
  78. "b", // KeyboardButton::B
  79. "c", // KeyboardButton::C
  80. "d", // KeyboardButton::D
  81. "e", // KeyboardButton::E
  82. "f", // KeyboardButton::F
  83. "g", // KeyboardButton::G
  84. "h", // KeyboardButton::H
  85. "i", // KeyboardButton::I
  86. "j", // KeyboardButton::J
  87. "k", // KeyboardButton::K
  88. "l", // KeyboardButton::L
  89. "m", // KeyboardButton::M
  90. "n", // KeyboardButton::N
  91. "o", // KeyboardButton::O
  92. "p", // KeyboardButton::P
  93. "q", // KeyboardButton::Q
  94. "r", // KeyboardButton::R
  95. "s", // KeyboardButton::S
  96. "t", // KeyboardButton::T
  97. "u", // KeyboardButton::U
  98. "v", // KeyboardButton::V
  99. "w", // KeyboardButton::W
  100. "x", // KeyboardButton::X
  101. "y", // KeyboardButton::Y
  102. "z" // KeyboardButton::Z
  103. };
  104. CE_STATIC_ASSERT(countof(s_keyboard_button_names) == KeyboardButton::COUNT);
  105. static const char *s_mouse_button_names[] =
  106. {
  107. "left", // MouseButton::LEFT
  108. "middle", // MouseButton::MIDDLE
  109. "right", // MouseButton::RIGHT
  110. "extra_1", // MouseButton::EXTRA_1
  111. "extra_2" // MouseButton::EXTRA_2
  112. };
  113. CE_STATIC_ASSERT(countof(s_mouse_button_names) == MouseButton::COUNT);
  114. static const char *s_mouse_axis_names[] =
  115. {
  116. "cursor", // MouseAxis::CURSOR
  117. "cursor_delta", // MouseAxis::CURSOR_DELTA
  118. "wheel" // MouseAxis::WHEEL
  119. };
  120. CE_STATIC_ASSERT(countof(s_mouse_axis_names) == MouseAxis::COUNT);
  121. static const char *s_touch_button_names[] =
  122. {
  123. "pointer_0", // TouchButton::POINTER_0
  124. "pointer_1", // TouchButton::POINTER_1
  125. "pointer_2", // TouchButton::POINTER_2
  126. "pointer_3" // TouchButton::POINTER_3
  127. };
  128. CE_STATIC_ASSERT(countof(s_touch_button_names) == TouchButton::COUNT);
  129. static const char *s_touch_axis_names[] =
  130. {
  131. "pointer_0", // TouchAxis::POINTER_0
  132. "pointer_1", // TouchAxis::POINTER_1
  133. "pointer_2", // TouchAxis::POINTER_2
  134. "pointer_3" // TouchAxis::POINTER_3
  135. };
  136. CE_STATIC_ASSERT(countof(s_touch_axis_names) == TouchAxis::COUNT);
  137. static const char *s_pad_button_names[] =
  138. {
  139. "up", // JoypadButton::UP
  140. "down", // JoypadButton::DOWN
  141. "left", // JoypadButton::LEFT
  142. "right", // JoypadButton::RIGHT
  143. "start", // JoypadButton::START
  144. "back", // JoypadButton::BACK
  145. "guide", // JoypadButton::GUIDE
  146. "thumb_left", // JoypadButton::THUMB_LEFT
  147. "thumb_right", // JoypadButton::THUMB_RIGHT
  148. "shoulder_left", // JoypadButton::SHOULDER_LEFT
  149. "shoulder_right", // JoypadButton::SHOULDER_RIGHT
  150. "a", // JoypadButton::A
  151. "b", // JoypadButton::B
  152. "x", // JoypadButton::X
  153. "y" // JoypadButton::Y
  154. };
  155. CE_STATIC_ASSERT(countof(s_pad_button_names) == JoypadButton::COUNT);
  156. static const char *s_pad_axis_names[] =
  157. {
  158. "left", // JoypadAxis::LEFT
  159. "right", // JoypadAxis::RIGHT
  160. "trigger_left", // JoypadAxis::TRIGGER_LEFT
  161. "trigger_right" // JoypadAxis::TRIGGER_RIGHT
  162. };
  163. CE_STATIC_ASSERT(countof(s_pad_axis_names) == JoypadAxis::COUNT);
  164. InputManager::InputManager(Allocator &a)
  165. : _allocator(&a)
  166. , _keyboard(NULL)
  167. , _mouse(NULL)
  168. , _touch(NULL)
  169. , _mouse_last_x(INT16_MAX)
  170. , _mouse_last_y(INT16_MAX)
  171. , _has_delta_axis_event(false)
  172. , _num_events(0)
  173. {
  174. _keyboard = input_device::create(*_allocator
  175. , "Keyboard"
  176. , KeyboardButton::COUNT
  177. , 0
  178. , s_keyboard_button_names
  179. , NULL
  180. );
  181. _mouse = input_device::create(*_allocator
  182. , "Mouse"
  183. , MouseButton::COUNT
  184. , MouseAxis::COUNT
  185. , s_mouse_button_names
  186. , s_mouse_axis_names
  187. );
  188. _touch = input_device::create(*_allocator
  189. , "Touch"
  190. , TouchButton::COUNT
  191. , TouchAxis::COUNT
  192. , s_touch_button_names
  193. , s_touch_axis_names
  194. );
  195. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i) {
  196. _joypad[i] = input_device::create(*_allocator
  197. , "Joypad"
  198. , JoypadButton::COUNT
  199. , JoypadAxis::COUNT
  200. , s_pad_button_names
  201. , s_pad_axis_names
  202. );
  203. _joypad[i]->set_deadzone(JoypadAxis::LEFT, DeadzoneMode::CIRCULAR, 0.24f);
  204. _joypad[i]->set_deadzone(JoypadAxis::RIGHT, DeadzoneMode::CIRCULAR, 0.27f);
  205. _joypad[i]->set_deadzone(JoypadAxis::TRIGGER_LEFT, DeadzoneMode::CIRCULAR, 0.12f);
  206. _joypad[i]->set_deadzone(JoypadAxis::TRIGGER_RIGHT, DeadzoneMode::CIRCULAR, 0.12f);
  207. }
  208. _keyboard->_connected = true;
  209. _mouse->_connected = true;
  210. _touch->_connected = true;
  211. }
  212. InputManager::~InputManager()
  213. {
  214. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  215. input_device::destroy(*_allocator, *_joypad[i]);
  216. input_device::destroy(*_allocator, *_touch);
  217. input_device::destroy(*_allocator, *_mouse);
  218. input_device::destroy(*_allocator, *_keyboard);
  219. }
  220. InputDevice *InputManager::device_from_type(u16 type, u16 num)
  221. {
  222. switch (type) {
  223. case InputDeviceType::KEYBOARD:
  224. return _keyboard;
  225. case InputDeviceType::MOUSE:
  226. return _mouse;
  227. case InputDeviceType::TOUCHSCREEN:
  228. return _touch;
  229. case InputDeviceType::JOYPAD:
  230. return _joypad[num];
  231. default:
  232. CE_FATAL("Unknown device type: %d", type);
  233. return NULL;
  234. }
  235. }
  236. InputDevice *InputManager::keyboard()
  237. {
  238. return _keyboard;
  239. }
  240. InputDevice *InputManager::mouse()
  241. {
  242. return _mouse;
  243. }
  244. InputDevice *InputManager::touch()
  245. {
  246. return _touch;
  247. }
  248. u8 InputManager::num_joypads()
  249. {
  250. return countof(_joypad);
  251. }
  252. InputDevice *InputManager::joypad(u8 i)
  253. {
  254. CE_ASSERT(i < CROWN_MAX_JOYPADS, "Index out of bounds");
  255. return _joypad[i];
  256. }
  257. /// Applies the deadzone settings for the axis @a id to @a axis and returns its value.
  258. static Vector3 deadzone(const InputDevice *dev, u8 id, const Vector3 &axis)
  259. {
  260. Vector3 out;
  261. switch (dev->_deadzone_mode[id]) {
  262. case DeadzoneMode::RAW:
  263. out = axis;
  264. break;
  265. case DeadzoneMode::INDEPENDENT:
  266. out.x = fabs(axis.x) < dev->_deadzone_size[id] ? 0.0f : axis.x;
  267. out.y = fabs(axis.y) < dev->_deadzone_size[id] ? 0.0f : axis.y;
  268. out.z = fabs(axis.z) < dev->_deadzone_size[id] ? 0.0f : axis.z;
  269. break;
  270. case DeadzoneMode::CIRCULAR:
  271. if (length(axis) < dev->_deadzone_size[id]) {
  272. out = VECTOR3_ZERO;
  273. } else {
  274. const f32 size = 1.0f - dev->_deadzone_size[id];
  275. const f32 size_inv = 1.0f / size;
  276. const f32 axis_len = length(axis);
  277. out = axis;
  278. out = normalize(out) * (axis_len - dev->_deadzone_size[id]) * size_inv;
  279. }
  280. break;
  281. default:
  282. CE_FATAL("Unknown deadzone mode");
  283. out = axis;
  284. break;
  285. }
  286. return out;
  287. }
  288. void InputManager::read(const OsEvent &event)
  289. {
  290. InputDevice *dev;
  291. InputEvent input_ev;
  292. switch (event.type) {
  293. case OsEventType::BUTTON: {
  294. const ButtonEvent ev = event.button;
  295. dev = device_from_type(ev.device_id, ev.device_num);
  296. if (CE_UNLIKELY(dev == NULL))
  297. return;
  298. // Publish the event.
  299. dev->set_button(ev.button_num, ev.pressed);
  300. input_ev.id = ev.button_num;
  301. input_ev.type = ev.pressed ? InputEventType::BUTTON_PRESSED : InputEventType::BUTTON_RELEASED;
  302. input_ev.value = VECTOR3_ZERO;
  303. input_ev.device = dev;
  304. _events[_num_events++] = input_ev;
  305. break;
  306. }
  307. case OsEventType::AXIS: {
  308. const AxisEvent ev = event.axis;
  309. dev = device_from_type(ev.device_id, ev.device_num);
  310. if (CE_UNLIKELY(dev == NULL))
  311. return;
  312. Vector3 axis;
  313. if (ev.device_id == InputDeviceType::MOUSE) {
  314. if (ev.axis_num == MouseAxis::CURSOR_DELTA) {
  315. const Vector3 delta = _has_delta_axis_event ?
  316. dev->axis(MouseAxis::CURSOR_DELTA)
  317. : VECTOR3_ZERO
  318. ;
  319. axis.x = delta.x + ev.axis_x;
  320. axis.y = delta.y + ev.axis_y;
  321. axis.z = 0.0f;
  322. _has_delta_axis_event = true;
  323. } else {
  324. axis.x = ev.axis_x;
  325. axis.y = ev.axis_y;
  326. axis.z = ev.axis_z;
  327. }
  328. } else if (ev.device_id == InputDeviceType::JOYPAD) {
  329. axis.x = (f32)ev.axis_x / (f32)INT16_MAX;
  330. axis.y = (f32)ev.axis_y / (f32)INT16_MAX;
  331. axis.z = (f32)ev.axis_z / (f32)INT16_MAX;
  332. axis = deadzone(dev, ev.axis_num, axis);
  333. } else {
  334. axis.x = ev.axis_x;
  335. axis.y = ev.axis_y;
  336. axis.z = ev.axis_z;
  337. }
  338. // Publish the event.
  339. dev->set_axis(ev.axis_num, axis.x, axis.y, axis.z);
  340. input_ev.id = ev.axis_num;
  341. input_ev.type = InputEventType::AXIS_CHANGED;
  342. input_ev.value = axis;
  343. input_ev.device = dev;
  344. _events[_num_events++] = input_ev;
  345. break;
  346. }
  347. case OsEventType::STATUS: {
  348. const StatusEvent ev = event.status;
  349. dev = device_from_type(ev.device_id, ev.device_num);
  350. if (CE_UNLIKELY(dev == NULL))
  351. return;
  352. dev->_connected = ev.connected;
  353. break;
  354. }
  355. default:
  356. CE_FATAL("Unknown input event type");
  357. break;
  358. }
  359. }
  360. void InputManager::update()
  361. {
  362. _keyboard->update();
  363. if (!_has_delta_axis_event) {
  364. _mouse->set_axis(MouseAxis::CURSOR_DELTA
  365. , 0
  366. , 0
  367. , 0
  368. );
  369. }
  370. _has_delta_axis_event = false;
  371. _mouse->update();
  372. _touch->update();
  373. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  374. _joypad[i]->update();
  375. _num_events = 0;
  376. }
  377. } // namespace crown