InputManager.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "InputManager.h"
  24. #include "OS.h"
  25. #include "Log.h"
  26. #include "OsEventBuffer.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. InputManager::InputManager()
  31. {
  32. }
  33. //-----------------------------------------------------------------------------
  34. InputManager::~InputManager()
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. Keyboard* InputManager::keyboard()
  39. {
  40. return &m_keyboard;
  41. }
  42. //-----------------------------------------------------------------------------
  43. Mouse* InputManager::mouse()
  44. {
  45. return &m_mouse;
  46. }
  47. //-----------------------------------------------------------------------------
  48. Touch* InputManager::touch()
  49. {
  50. return &m_touch;
  51. }
  52. //-----------------------------------------------------------------------------
  53. Accelerometer* InputManager::accelerometer()
  54. {
  55. return &m_accelerometer;
  56. }
  57. //-----------------------------------------------------------------------------
  58. void InputManager::register_mouse_listener(MouseListener* listener)
  59. {
  60. m_event_dispatcher.add_mouse_listener(listener);
  61. }
  62. //-----------------------------------------------------------------------------
  63. void InputManager::register_keyboard_listener(KeyboardListener* listener)
  64. {
  65. m_event_dispatcher.add_keyboard_listener(listener);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void InputManager::register_touch_listener(TouchListener* listener)
  69. {
  70. m_event_dispatcher.add_touch_listener(listener);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void InputManager::register_accelerometer_listener(AccelerometerListener* listener)
  74. {
  75. m_event_dispatcher.add_accelerometer_listener(listener);
  76. }
  77. //-----------------------------------------------------------------------------
  78. EventDispatcher* InputManager::get_event_dispatcher()
  79. {
  80. return &m_event_dispatcher;
  81. }
  82. //-----------------------------------------------------------------------------
  83. void InputManager::frame(uint64_t frame_count)
  84. {
  85. void* event;
  86. uint32_t event_type;
  87. size_t event_size;
  88. // Update input devices
  89. m_keyboard.m_current_frame = frame_count;
  90. m_mouse.m_current_frame = frame_count;
  91. while (1)
  92. {
  93. if ((event = os_event_buffer()->get_next_event(event_type, event_size)) == NULL)
  94. {
  95. break;
  96. }
  97. switch (event_type)
  98. {
  99. case OSET_NONE:
  100. {
  101. return;
  102. }
  103. case OSET_BUTTON_PRESS:
  104. case OSET_BUTTON_RELEASE:
  105. {
  106. MouseEvent mouse_event;
  107. mouse_event.x = ((OsMouseEvent*)event)->x;
  108. mouse_event.y = ((OsMouseEvent*)event)->y;
  109. mouse_event.button = ((OsMouseEvent*)event)->button == 0 ? MB_LEFT : ((OsMouseEvent*)event)->button == 1 ? MB_MIDDLE : MB_RIGHT;
  110. mouse_event.wheel = 0.0f;
  111. if (event_type == OSET_BUTTON_PRESS)
  112. {
  113. m_mouse.update(frame_count, mouse_event.button, true);
  114. m_event_dispatcher.button_pressed(mouse_event);
  115. }
  116. else
  117. {
  118. m_mouse.update(frame_count, mouse_event.button, false);
  119. m_event_dispatcher.button_released(mouse_event);
  120. }
  121. break;
  122. }
  123. case OSET_KEY_PRESS:
  124. case OSET_KEY_RELEASE:
  125. {
  126. KeyboardEvent keyboard_event;
  127. keyboard_event.key = (KeyCode) ((OsKeyboardEvent*)event)->key;
  128. keyboard_event.modifier = (uint8_t) ((OsKeyboardEvent*)event)->modifier;
  129. m_keyboard.m_modifier = keyboard_event.modifier;
  130. if (event_type == OSET_KEY_PRESS)
  131. {
  132. m_keyboard.update(frame_count, keyboard_event.key, true);
  133. m_event_dispatcher.key_pressed(keyboard_event);
  134. }
  135. else
  136. {
  137. m_keyboard.update(frame_count, keyboard_event.key, false);
  138. m_event_dispatcher.key_released(keyboard_event);
  139. }
  140. break;
  141. }
  142. case OSET_TOUCH_DOWN:
  143. case OSET_TOUCH_UP:
  144. {
  145. TouchEvent touch_event;
  146. touch_event.pointer_id = ((OsTouchEvent*)event)->pointer_id;
  147. touch_event.x = ((OsTouchEvent*)event)->x;
  148. touch_event.y = ((OsTouchEvent*)event)->y;
  149. m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
  150. m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
  151. // FIXME
  152. m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
  153. m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
  154. if (event_type == OSET_TOUCH_DOWN)
  155. {
  156. m_touch.m_pointers[touch_event.pointer_id].up = false;
  157. m_event_dispatcher.touch_down(touch_event);
  158. }
  159. else
  160. {
  161. m_touch.m_pointers[touch_event.pointer_id].up = true;
  162. m_event_dispatcher.touch_up(touch_event);
  163. }
  164. break;
  165. }
  166. case OSET_TOUCH_MOVE:
  167. {
  168. TouchEvent touch_event;
  169. touch_event.pointer_id = ((OsTouchEvent*)event)->pointer_id;
  170. touch_event.x = ((OsTouchEvent*)event)->x;
  171. touch_event.y = ((OsTouchEvent*)event)->y;
  172. m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
  173. m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
  174. // FIXME
  175. m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
  176. m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
  177. m_event_dispatcher.touch_move(touch_event);
  178. break;
  179. }
  180. // case OSET_ACCELEROMETER:
  181. // {
  182. // AccelerometerEvent sensor_event;
  183. // sensor_event.x = event.data_a.float_value;
  184. // sensor_event.y = event.data_b.float_value;
  185. // sensor_event.z = event.data_c.float_value;
  186. // m_accelerometer.m_orientation.x = sensor_event.x;
  187. // m_accelerometer.m_orientation.y = sensor_event.y;
  188. // m_accelerometer.m_orientation.z = sensor_event.z;
  189. // m_event_dispatcher.accelerometer_changed(sensor_event);
  190. // break;
  191. // }
  192. default:
  193. {
  194. break;
  195. }
  196. }
  197. }
  198. }
  199. } // namespace crown