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

119 lines
3.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. #ifndef ANTKEEER_INPUT_EVENT_ROUTER_HPP
  20. #define ANTKEEER_INPUT_EVENT_ROUTER_HPP
  21. #include "event/input-events.hpp"
  22. #include "event/event-handler.hpp"
  23. #include "event/event-dispatcher.hpp"
  24. #include <list>
  25. #include <map>
  26. namespace input {
  27. class control;
  28. class mapping;
  29. class key_mapping;
  30. class mouse_motion_mapping;
  31. class mouse_wheel_mapping;
  32. class mouse_button_mapping;
  33. class game_controller_axis_mapping;
  34. class game_controller_button_mapping;
  35. enum class mouse_motion_axis;
  36. enum class mouse_wheel_axis;
  37. /**
  38. * Uses input mappings to route input events to controls.
  39. */
  40. class event_router:
  41. public event_handler<key_pressed_event>,
  42. public event_handler<key_released_event>,
  43. public event_handler<mouse_moved_event>,
  44. public event_handler<mouse_wheel_scrolled_event>,
  45. public event_handler<mouse_button_pressed_event>,
  46. public event_handler<mouse_button_released_event>,
  47. public event_handler<game_controller_axis_moved_event>,
  48. public event_handler<game_controller_button_pressed_event>,
  49. public event_handler<game_controller_button_released_event>
  50. {
  51. public:
  52. /**
  53. * Creates an input router and subscribes it to the input events of the specified event dispatcher.
  54. */
  55. event_router();
  56. /**
  57. * Destroys an input router and unsubscribes it from input events.
  58. */
  59. ~event_router();
  60. /**
  61. * Adds an input mapping to the router.
  62. *
  63. * @param mapping Input mapping to add.
  64. */
  65. void add_mapping(const mapping& mapping);
  66. /**
  67. * Removes all input mappings from the router that are associated with the specified control.
  68. *
  69. * @param control control with which associated input mappings should be removed.
  70. */
  71. void remove_mappings(control* control);
  72. /**
  73. * Sets the event dispatcher to which this input event router will subscribe itself.
  74. */
  75. void set_event_dispatcher(event_dispatcher* event_dispatcher);
  76. /**
  77. * Removes all input mappings from the router.
  78. */
  79. void remove_mappings();
  80. /// Returns a list of mappings for the specified control, or nullptr if the control is unmapped.
  81. const std::list<mapping*>* get_mappings(control* control) const;
  82. private:
  83. virtual void handle_event(const key_pressed_event& event);
  84. virtual void handle_event(const key_released_event& event);
  85. virtual void handle_event(const mouse_moved_event& event);
  86. virtual void handle_event(const mouse_wheel_scrolled_event& event);
  87. virtual void handle_event(const mouse_button_pressed_event& event);
  88. virtual void handle_event(const mouse_button_released_event& event);
  89. virtual void handle_event(const game_controller_axis_moved_event& event);
  90. virtual void handle_event(const game_controller_button_pressed_event& event);
  91. virtual void handle_event(const game_controller_button_released_event& event);
  92. event_dispatcher* event_dispatcher;
  93. std::map<control*, std::list<mapping*>> controls;
  94. std::list<key_mapping*> key_mappings;
  95. std::list<mouse_motion_mapping*> mouse_motion_mappings;
  96. std::list<mouse_wheel_mapping*> mouse_wheel_mappings;
  97. std::list<mouse_button_mapping*> mouse_button_mappings;
  98. std::list<game_controller_axis_mapping*> game_controller_axis_mappings;
  99. std::list<game_controller_button_mapping*> game_controller_button_mappings;
  100. };
  101. } // namespace input
  102. #endif // ANTKEEER_INPUT_EVENT_ROUTER_HPP