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

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