💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

344 lines
9.9 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "event-router.hpp"
  20. #include "control.hpp"
  21. #include "mapping.hpp"
  22. #include "mouse.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. namespace input {
  25. event_router::event_router():
  26. event_dispatcher(nullptr)
  27. {}
  28. event_router::~event_router()
  29. {
  30. remove_mappings();
  31. set_event_dispatcher(nullptr);
  32. }
  33. void event_router::add_mapping(const mapping& mapping)
  34. {
  35. control* control = mapping.control;
  36. switch (mapping.get_type())
  37. {
  38. case mapping_type::key:
  39. {
  40. input::key_mapping* key_mapping = new input::key_mapping(static_cast<const input::key_mapping&>(mapping));
  41. key_mappings.push_back(key_mapping);
  42. controls[control].push_back(key_mapping);
  43. break;
  44. }
  45. case mapping_type::mouse_motion:
  46. {
  47. input::mouse_motion_mapping* mouse_motion_mapping = new input::mouse_motion_mapping(static_cast<const input::mouse_motion_mapping&>(mapping));
  48. mouse_motion_mappings.push_back(mouse_motion_mapping);
  49. controls[control].push_back(mouse_motion_mapping);
  50. break;
  51. }
  52. case mapping_type::mouse_wheel:
  53. {
  54. input::mouse_wheel_mapping* mouse_wheel_mapping = new input::mouse_wheel_mapping(static_cast<const input::mouse_wheel_mapping&>(mapping));
  55. mouse_wheel_mappings.push_back(mouse_wheel_mapping);
  56. controls[control].push_back(mouse_wheel_mapping);
  57. break;
  58. }
  59. case mapping_type::mouse_button:
  60. {
  61. input::mouse_button_mapping* mouse_button_mapping = new input::mouse_button_mapping(static_cast<const input::mouse_button_mapping&>(mapping));
  62. mouse_button_mappings.push_back(mouse_button_mapping);
  63. controls[control].push_back(mouse_button_mapping);
  64. break;
  65. }
  66. case mapping_type::game_controller_axis:
  67. {
  68. input::game_controller_axis_mapping* game_controller_axis_mapping = new input::game_controller_axis_mapping(static_cast<const input::game_controller_axis_mapping&>(mapping));
  69. game_controller_axis_mappings.push_back(game_controller_axis_mapping);
  70. controls[control].push_back(game_controller_axis_mapping);
  71. break;
  72. }
  73. case mapping_type::game_controller_button:
  74. {
  75. input::game_controller_button_mapping* game_controller_button_mapping = new input::game_controller_button_mapping(static_cast<const input::game_controller_button_mapping&>(mapping));
  76. game_controller_button_mappings.push_back(game_controller_button_mapping);
  77. controls[control].push_back(game_controller_button_mapping);
  78. break;
  79. }
  80. default:
  81. break;
  82. }
  83. }
  84. void event_router::remove_mappings(control* control)
  85. {
  86. auto it = controls.find(control);
  87. if (it != controls.end())
  88. {
  89. for (mapping* mapping: it->second)
  90. {
  91. switch (mapping->get_type())
  92. {
  93. case mapping_type::key:
  94. key_mappings.remove(static_cast<key_mapping*>(mapping));
  95. break;
  96. case mapping_type::mouse_motion:
  97. mouse_motion_mappings.remove(static_cast<mouse_motion_mapping*>(mapping));
  98. break;
  99. case mapping_type::mouse_wheel:
  100. mouse_wheel_mappings.remove(static_cast<mouse_wheel_mapping*>(mapping));
  101. break;
  102. case mapping_type::mouse_button:
  103. mouse_button_mappings.remove(static_cast<mouse_button_mapping*>(mapping));
  104. break;
  105. case mapping_type::game_controller_axis:
  106. game_controller_axis_mappings.remove(static_cast<game_controller_axis_mapping*>(mapping));
  107. break;
  108. case mapping_type::game_controller_button:
  109. game_controller_button_mappings.remove(static_cast<game_controller_button_mapping*>(mapping));
  110. break;
  111. default:
  112. break;
  113. }
  114. delete mapping;
  115. }
  116. controls.erase(it);
  117. }
  118. }
  119. void event_router::set_event_dispatcher(::event_dispatcher* event_dispatcher)
  120. {
  121. if (this->event_dispatcher)
  122. {
  123. this->event_dispatcher->unsubscribe<key_pressed_event>(this);
  124. this->event_dispatcher->unsubscribe<key_released_event>(this);
  125. this->event_dispatcher->unsubscribe<mouse_moved_event>(this);
  126. this->event_dispatcher->unsubscribe<mouse_wheel_scrolled_event>(this);
  127. this->event_dispatcher->unsubscribe<mouse_button_pressed_event>(this);
  128. this->event_dispatcher->unsubscribe<mouse_button_released_event>(this);
  129. this->event_dispatcher->unsubscribe<game_controller_axis_moved_event>(this);
  130. this->event_dispatcher->unsubscribe<game_controller_button_pressed_event>(this);
  131. this->event_dispatcher->unsubscribe<game_controller_button_released_event>(this);
  132. }
  133. this->event_dispatcher = event_dispatcher;
  134. if (event_dispatcher)
  135. {
  136. event_dispatcher->subscribe<key_pressed_event>(this);
  137. event_dispatcher->subscribe<key_released_event>(this);
  138. event_dispatcher->subscribe<mouse_moved_event>(this);
  139. event_dispatcher->subscribe<mouse_wheel_scrolled_event>(this);
  140. event_dispatcher->subscribe<mouse_button_pressed_event>(this);
  141. event_dispatcher->subscribe<mouse_button_released_event>(this);
  142. event_dispatcher->subscribe<game_controller_axis_moved_event>(this);
  143. event_dispatcher->subscribe<game_controller_button_pressed_event>(this);
  144. event_dispatcher->subscribe<game_controller_button_released_event>(this);
  145. }
  146. }
  147. void event_router::remove_mappings()
  148. {
  149. for (auto it = controls.begin(); it != controls.end(); ++it)
  150. {
  151. for (mapping* mapping: it->second)
  152. {
  153. delete mapping;
  154. }
  155. }
  156. controls.clear();
  157. key_mappings.clear();
  158. mouse_motion_mappings.clear();
  159. mouse_wheel_mappings.clear();
  160. mouse_button_mappings.clear();
  161. game_controller_axis_mappings.clear();
  162. game_controller_button_mappings.clear();
  163. }
  164. const std::list<mapping*>* event_router::get_mappings(control* control) const
  165. {
  166. auto it = controls.find(control);
  167. if (it == controls.end())
  168. {
  169. return nullptr;
  170. }
  171. return &it->second;
  172. }
  173. void event_router::handle_event(const key_pressed_event& event)
  174. {
  175. for (const key_mapping* mapping: key_mappings)
  176. {
  177. if ((!mapping->keyboard || mapping->keyboard == event.keyboard) && mapping->scancode == event.scancode)
  178. {
  179. mapping->control->set_current_value(1.0f);
  180. }
  181. }
  182. }
  183. void event_router::handle_event(const key_released_event& event)
  184. {
  185. for (const key_mapping* mapping: key_mappings)
  186. {
  187. if ((!mapping->keyboard || mapping->keyboard == event.keyboard) && mapping->scancode == event.scancode)
  188. {
  189. mapping->control->set_current_value(0.0f);
  190. }
  191. }
  192. }
  193. void event_router::handle_event(const mouse_moved_event& event)
  194. {
  195. for (const mouse_motion_mapping* mapping: mouse_motion_mappings)
  196. {
  197. if (!mapping->mouse || mapping->mouse == event.mouse)
  198. {
  199. if (mapping->axis == mouse_motion_axis::negative_x && event.dx < 0)
  200. {
  201. mapping->control->set_temporary_value(-event.dx);
  202. }
  203. else if (mapping->axis == mouse_motion_axis::positive_x && event.dx > 0)
  204. {
  205. mapping->control->set_temporary_value(event.dx);
  206. }
  207. else if (mapping->axis == mouse_motion_axis::negative_y && event.dy < 0)
  208. {
  209. mapping->control->set_temporary_value(-event.dy);
  210. }
  211. else if (mapping->axis == mouse_motion_axis::positive_y && event.dy > 0)
  212. {
  213. mapping->control->set_temporary_value(event.dy);
  214. }
  215. }
  216. }
  217. }
  218. void event_router::handle_event(const mouse_wheel_scrolled_event& event)
  219. {
  220. for (const mouse_wheel_mapping* mapping: mouse_wheel_mappings)
  221. {
  222. if (!mapping->mouse || mapping->mouse == event.mouse)
  223. {
  224. if (mapping->axis == mouse_wheel_axis::negative_x && event.x < 0)
  225. {
  226. mapping->control->set_temporary_value(-event.x);
  227. }
  228. else if (mapping->axis == mouse_wheel_axis::positive_x && event.x > 0)
  229. {
  230. mapping->control->set_temporary_value(event.x);
  231. }
  232. else if (mapping->axis == mouse_wheel_axis::negative_y && event.y < 0)
  233. {
  234. mapping->control->set_temporary_value(-event.y);
  235. }
  236. else if (mapping->axis == mouse_wheel_axis::positive_y && event.y > 0)
  237. {
  238. mapping->control->set_temporary_value(event.y);
  239. }
  240. }
  241. }
  242. }
  243. void event_router::handle_event(const mouse_button_pressed_event& event)
  244. {
  245. for (const mouse_button_mapping* mapping: mouse_button_mappings)
  246. {
  247. if ((!mapping->mouse || mapping->mouse == event.mouse) && mapping->button == event.button)
  248. {
  249. mapping->control->set_current_value(1.0f);
  250. }
  251. }
  252. }
  253. void event_router::handle_event(const mouse_button_released_event& event)
  254. {
  255. for (const mouse_button_mapping* mapping: mouse_button_mappings)
  256. {
  257. if ((!mapping->mouse || mapping->mouse == event.mouse) && mapping->button == event.button)
  258. {
  259. mapping->control->set_current_value(0.0f);
  260. }
  261. }
  262. }
  263. void event_router::handle_event(const game_controller_axis_moved_event& event)
  264. {
  265. for (const game_controller_axis_mapping* mapping: game_controller_axis_mappings)
  266. {
  267. if ((!mapping->controller || mapping->controller == event.controller) && mapping->axis == event.axis)
  268. {
  269. if (mapping->negative && event.value >= 0.0f || !mapping->negative && event.value <= 0.0f)
  270. {
  271. mapping->control->set_current_value(0.0f);
  272. }
  273. else
  274. {
  275. mapping->control->set_current_value(std::abs(event.value));
  276. }
  277. }
  278. }
  279. }
  280. void event_router::handle_event(const game_controller_button_pressed_event& event)
  281. {
  282. for (const game_controller_button_mapping* mapping: game_controller_button_mappings)
  283. {
  284. if ((!mapping->controller || mapping->controller == event.controller) && mapping->button == event.button)
  285. {
  286. mapping->control->set_current_value(1.0f);
  287. }
  288. }
  289. }
  290. void event_router::handle_event(const game_controller_button_released_event& event)
  291. {
  292. for (const game_controller_button_mapping* mapping: game_controller_button_mappings)
  293. {
  294. if ((!mapping->controller || mapping->controller == event.controller) && mapping->button == event.button)
  295. {
  296. mapping->control->set_current_value(0.0f);
  297. }
  298. }
  299. }
  300. } // namespace input