💿🐜 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.

341 lines
10 KiB

  1. /*
  2. * Copyright (C) 2020 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 "input-event-router.hpp"
  20. #include "control.hpp"
  21. #include "input-mapping.hpp"
  22. #include "mouse.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. input_event_router::input_event_router():
  25. event_dispatcher(nullptr)
  26. {}
  27. input_event_router::~input_event_router()
  28. {
  29. remove_mappings();
  30. set_event_dispatcher(nullptr);
  31. }
  32. void input_event_router::add_mapping(const input_mapping& mapping)
  33. {
  34. control* control = mapping.control;
  35. switch (mapping.get_type())
  36. {
  37. case input_mapping_type::key:
  38. {
  39. ::key_mapping* key_mapping = new ::key_mapping(static_cast<const ::key_mapping&>(mapping));
  40. key_mappings.push_back(key_mapping);
  41. controls[control].push_back(key_mapping);
  42. break;
  43. }
  44. case input_mapping_type::mouse_motion:
  45. {
  46. ::mouse_motion_mapping* mouse_motion_mapping = new ::mouse_motion_mapping(static_cast<const ::mouse_motion_mapping&>(mapping));
  47. mouse_motion_mappings.push_back(mouse_motion_mapping);
  48. controls[control].push_back(mouse_motion_mapping);
  49. break;
  50. }
  51. case input_mapping_type::mouse_wheel:
  52. {
  53. ::mouse_wheel_mapping* mouse_wheel_mapping = new ::mouse_wheel_mapping(static_cast<const ::mouse_wheel_mapping&>(mapping));
  54. mouse_wheel_mappings.push_back(mouse_wheel_mapping);
  55. controls[control].push_back(mouse_wheel_mapping);
  56. break;
  57. }
  58. case input_mapping_type::mouse_button:
  59. {
  60. ::mouse_button_mapping* mouse_button_mapping = new ::mouse_button_mapping(static_cast<const ::mouse_button_mapping&>(mapping));
  61. mouse_button_mappings.push_back(mouse_button_mapping);
  62. controls[control].push_back(mouse_button_mapping);
  63. break;
  64. }
  65. case input_mapping_type::game_controller_axis:
  66. {
  67. ::game_controller_axis_mapping* game_controller_axis_mapping = new ::game_controller_axis_mapping(static_cast<const ::game_controller_axis_mapping&>(mapping));
  68. game_controller_axis_mappings.push_back(game_controller_axis_mapping);
  69. controls[control].push_back(game_controller_axis_mapping);
  70. break;
  71. }
  72. case input_mapping_type::game_controller_button:
  73. {
  74. ::game_controller_button_mapping* game_controller_button_mapping = new ::game_controller_button_mapping(static_cast<const ::game_controller_button_mapping&>(mapping));
  75. game_controller_button_mappings.push_back(game_controller_button_mapping);
  76. controls[control].push_back(game_controller_button_mapping);
  77. break;
  78. }
  79. default:
  80. break;
  81. }
  82. }
  83. void input_event_router::remove_mappings(control* control)
  84. {
  85. auto it = controls.find(control);
  86. if (it != controls.end())
  87. {
  88. for (input_mapping* mapping: it->second)
  89. {
  90. switch (mapping->get_type())
  91. {
  92. case input_mapping_type::key:
  93. key_mappings.remove(static_cast<key_mapping*>(mapping));
  94. break;
  95. case input_mapping_type::mouse_motion:
  96. mouse_motion_mappings.remove(static_cast<mouse_motion_mapping*>(mapping));
  97. break;
  98. case input_mapping_type::mouse_wheel:
  99. mouse_wheel_mappings.remove(static_cast<mouse_wheel_mapping*>(mapping));
  100. break;
  101. case input_mapping_type::mouse_button:
  102. mouse_button_mappings.remove(static_cast<mouse_button_mapping*>(mapping));
  103. break;
  104. case input_mapping_type::game_controller_axis:
  105. game_controller_axis_mappings.remove(static_cast<game_controller_axis_mapping*>(mapping));
  106. break;
  107. case input_mapping_type::game_controller_button:
  108. game_controller_button_mappings.remove(static_cast<game_controller_button_mapping*>(mapping));
  109. break;
  110. default:
  111. break;
  112. }
  113. delete mapping;
  114. }
  115. controls.erase(it);
  116. }
  117. }
  118. void input_event_router::set_event_dispatcher(::event_dispatcher* event_dispatcher)
  119. {
  120. if (this->event_dispatcher)
  121. {
  122. this->event_dispatcher->unsubscribe<key_pressed_event>(this);
  123. this->event_dispatcher->unsubscribe<key_released_event>(this);
  124. this->event_dispatcher->unsubscribe<mouse_moved_event>(this);
  125. this->event_dispatcher->unsubscribe<mouse_wheel_scrolled_event>(this);
  126. this->event_dispatcher->unsubscribe<mouse_button_pressed_event>(this);
  127. this->event_dispatcher->unsubscribe<mouse_button_released_event>(this);
  128. this->event_dispatcher->unsubscribe<game_controller_axis_moved_event>(this);
  129. this->event_dispatcher->unsubscribe<game_controller_button_pressed_event>(this);
  130. this->event_dispatcher->unsubscribe<game_controller_button_released_event>(this);
  131. }
  132. this->event_dispatcher = event_dispatcher;
  133. if (event_dispatcher)
  134. {
  135. event_dispatcher->subscribe<key_pressed_event>(this);
  136. event_dispatcher->subscribe<key_released_event>(this);
  137. event_dispatcher->subscribe<mouse_moved_event>(this);
  138. event_dispatcher->subscribe<mouse_wheel_scrolled_event>(this);
  139. event_dispatcher->subscribe<mouse_button_pressed_event>(this);
  140. event_dispatcher->subscribe<mouse_button_released_event>(this);
  141. event_dispatcher->subscribe<game_controller_axis_moved_event>(this);
  142. event_dispatcher->subscribe<game_controller_button_pressed_event>(this);
  143. event_dispatcher->subscribe<game_controller_button_released_event>(this);
  144. }
  145. }
  146. void input_event_router::remove_mappings()
  147. {
  148. for (auto it = controls.begin(); it != controls.end(); ++it)
  149. {
  150. for (input_mapping* mapping: it->second)
  151. {
  152. delete mapping;
  153. }
  154. }
  155. controls.clear();
  156. key_mappings.clear();
  157. mouse_motion_mappings.clear();
  158. mouse_wheel_mappings.clear();
  159. mouse_button_mappings.clear();
  160. game_controller_axis_mappings.clear();
  161. game_controller_button_mappings.clear();
  162. }
  163. const std::list<input_mapping*>* input_event_router::get_mappings(control* control) const
  164. {
  165. auto it = controls.find(control);
  166. if (it == controls.end())
  167. {
  168. return nullptr;
  169. }
  170. return &it->second;
  171. }
  172. void input_event_router::handle_event(const key_pressed_event& event)
  173. {
  174. for (const key_mapping* mapping: key_mappings)
  175. {
  176. if ((!mapping->keyboard || mapping->keyboard == event.keyboard) && mapping->scancode == event.scancode)
  177. {
  178. mapping->control->set_current_value(1.0f);
  179. }
  180. }
  181. }
  182. void input_event_router::handle_event(const key_released_event& event)
  183. {
  184. for (const key_mapping* mapping: key_mappings)
  185. {
  186. if ((!mapping->keyboard || mapping->keyboard == event.keyboard) && mapping->scancode == event.scancode)
  187. {
  188. mapping->control->set_current_value(0.0f);
  189. }
  190. }
  191. }
  192. void input_event_router::handle_event(const mouse_moved_event& event)
  193. {
  194. for (const mouse_motion_mapping* mapping: mouse_motion_mappings)
  195. {
  196. if (!mapping->mouse || mapping->mouse == event.mouse)
  197. {
  198. if (mapping->axis == mouse_motion_axis::negative_x && event.dx < 0)
  199. {
  200. mapping->control->set_temporary_value(-event.dx);
  201. }
  202. else if (mapping->axis == mouse_motion_axis::positive_x && event.dx > 0)
  203. {
  204. mapping->control->set_temporary_value(event.dx);
  205. }
  206. else if (mapping->axis == mouse_motion_axis::negative_y && event.dy < 0)
  207. {
  208. mapping->control->set_temporary_value(-event.dy);
  209. }
  210. else if (mapping->axis == mouse_motion_axis::positive_y && event.dy > 0)
  211. {
  212. mapping->control->set_temporary_value(event.dy);
  213. }
  214. }
  215. }
  216. }
  217. void input_event_router::handle_event(const mouse_wheel_scrolled_event& event)
  218. {
  219. for (const mouse_wheel_mapping* mapping: mouse_wheel_mappings)
  220. {
  221. if (!mapping->mouse || mapping->mouse == event.mouse)
  222. {
  223. if (mapping->axis == mouse_wheel_axis::negative_x && event.x < 0)
  224. {
  225. mapping->control->set_temporary_value(-event.x);
  226. }
  227. else if (mapping->axis == mouse_wheel_axis::positive_x && event.x > 0)
  228. {
  229. mapping->control->set_temporary_value(event.x);
  230. }
  231. else if (mapping->axis == mouse_wheel_axis::negative_y && event.y < 0)
  232. {
  233. mapping->control->set_temporary_value(-event.y);
  234. }
  235. else if (mapping->axis == mouse_wheel_axis::positive_y && event.y > 0)
  236. {
  237. mapping->control->set_temporary_value(event.y);
  238. }
  239. }
  240. }
  241. }
  242. void input_event_router::handle_event(const mouse_button_pressed_event& event)
  243. {
  244. for (const mouse_button_mapping* mapping: mouse_button_mappings)
  245. {
  246. if ((!mapping->mouse || mapping->mouse == event.mouse) && mapping->button == event.button)
  247. {
  248. mapping->control->set_current_value(1.0f);
  249. }
  250. }
  251. }
  252. void input_event_router::handle_event(const mouse_button_released_event& event)
  253. {
  254. for (const mouse_button_mapping* mapping: mouse_button_mappings)
  255. {
  256. if ((!mapping->mouse || mapping->mouse == event.mouse) && mapping->button == event.button)
  257. {
  258. mapping->control->set_current_value(0.0f);
  259. }
  260. }
  261. }
  262. void input_event_router::handle_event(const game_controller_axis_moved_event& event)
  263. {
  264. for (const game_controller_axis_mapping* mapping: game_controller_axis_mappings)
  265. {
  266. if ((!mapping->game_controller || mapping->game_controller == event.game_controller) && mapping->axis == event.axis)
  267. {
  268. if (mapping->negative && event.value >= 0.0f || !mapping->negative && event.value <= 0.0f)
  269. {
  270. mapping->control->set_current_value(0.0f);
  271. }
  272. else
  273. {
  274. mapping->control->set_current_value(std::abs(event.value));
  275. }
  276. }
  277. }
  278. }
  279. void input_event_router::handle_event(const game_controller_button_pressed_event& event)
  280. {
  281. for (const game_controller_button_mapping* mapping: game_controller_button_mappings)
  282. {
  283. if ((!mapping->game_controller || mapping->game_controller == event.game_controller) && mapping->button == event.button)
  284. {
  285. mapping->control->set_current_value(1.0f);
  286. }
  287. }
  288. }
  289. void input_event_router::handle_event(const game_controller_button_released_event& event)
  290. {
  291. for (const game_controller_button_mapping* mapping: game_controller_button_mappings)
  292. {
  293. if ((!mapping->game_controller || mapping->game_controller == event.game_controller) && mapping->button == event.button)
  294. {
  295. mapping->control->set_current_value(0.0f);
  296. }
  297. }
  298. }