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

111 lines
3.7 KiB

  1. /*
  2. * Copyright (C) 2023 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_CONTROL_MAP_HPP
  20. #define ANTKEEER_INPUT_CONTROL_MAP_HPP
  21. #include "event/subscription.hpp"
  22. #include "event/queue.hpp"
  23. #include "input/control.hpp"
  24. #include "input/input-events.hpp"
  25. #include "input/mapping.hpp"
  26. #include <memory>
  27. #include <tuple>
  28. #include <unordered_map>
  29. #include <vector>
  30. namespace input {
  31. /**
  32. * Maps input to a set of contextually-related controls.
  33. */
  34. class control_map
  35. {
  36. public:
  37. /**
  38. * Connects the input event signals of an event queue to the control map.
  39. *
  40. * @param queue Event queue to connect.
  41. */
  42. void connect(::event::queue& queue);
  43. /**
  44. * Disconnects all input event signals from the control map.
  45. */
  46. void disconnect();
  47. /**
  48. * Maps input to a control.
  49. *
  50. * @param control Control to which input will be mapped.
  51. * @param mapping Input mapping to add.
  52. */
  53. /// @{
  54. void add_mapping(control& control, const mapping& mapping);
  55. void add_mapping(control& control, gamepad_axis_mapping&& mapping);
  56. void add_mapping(control& control, gamepad_button_mapping&& mapping);
  57. void add_mapping(control& control, key_mapping&& mapping);
  58. void add_mapping(control& control, mouse_button_mapping&& mapping);
  59. void add_mapping(control& control, mouse_motion_mapping&& mapping);
  60. void add_mapping(control& control, mouse_scroll_mapping&& mapping);
  61. /// @}
  62. /**
  63. * Unmaps input from a control.
  64. *
  65. * @param control Control from which input will be unmapped.
  66. * @param type Type of input mapping to remove.
  67. */
  68. void remove_mappings(control& control, mapping_type type);
  69. /**
  70. * Unmaps all input from a control.
  71. *
  72. * @param control Control from which input will be unmapped.
  73. */
  74. void remove_mappings(control& control);
  75. /**
  76. * Unmaps all input from all controls in the control map.
  77. */
  78. void remove_mappings();
  79. private:
  80. void handle_gamepad_axis_moved(const gamepad_axis_moved_event& event);
  81. void handle_gamepad_button_pressed(const gamepad_button_pressed_event& event);
  82. void handle_gamepad_button_released(const gamepad_button_released_event& event);
  83. void handle_key_pressed(const key_pressed_event& event);
  84. void handle_key_released(const key_released_event& event);
  85. void handle_mouse_button_pressed(const mouse_button_pressed_event& event);
  86. void handle_mouse_button_released(const mouse_button_released_event& event);
  87. void handle_mouse_moved(const mouse_moved_event& event);
  88. void handle_mouse_scrolled(const mouse_scrolled_event& event);
  89. std::vector<std::shared_ptr<::event::subscription>> subscriptions;
  90. std::vector<std::tuple<control*, gamepad_axis_mapping>> gamepad_axis_mappings;
  91. std::vector<std::tuple<control*, gamepad_button_mapping>> gamepad_button_mappings;
  92. std::vector<std::tuple<control*, key_mapping>> key_mappings;
  93. std::vector<std::tuple<control*, mouse_button_mapping>> mouse_button_mappings;
  94. std::vector<std::tuple<control*, mouse_motion_mapping>> mouse_motion_mappings;
  95. std::vector<std::tuple<control*, mouse_scroll_mapping>> mouse_scroll_mappings;
  96. };
  97. } // namespace input
  98. #endif // ANTKEEER_INPUT_CONTROL_MAP_HPP