Antkeeper  0.0.1
action-map.hpp
Go to the documentation of this file.
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 
20 #ifndef ANTKEEER_INPUT_ACTION_MAP_HPP
21 #define ANTKEEER_INPUT_ACTION_MAP_HPP
22 
25 #include <engine/input/action.hpp>
29 #include <engine/input/mapping.hpp>
31 #include <memory>
32 #include <tuple>
33 #include <unordered_map>
34 #include <unordered_set>
35 #include <vector>
36 
37 namespace input {
38 
43 {
44 public:
48  void enable();
49 
53  void disable();
54 
58  void reset();
59 
65  void set_event_dispatcher(event::dispatcher* dispatcher);
66 
74  void add_mapping(action& action, const mapping& mapping);
82 
90 
97 
101  void remove_mappings();
102 
108  [[nodiscard]] std::vector<gamepad_axis_mapping> get_gamepad_axis_mappings(const action& action) const;
109 
115  [[nodiscard]] std::vector<gamepad_button_mapping> get_gamepad_button_mappings(const action& action) const;
116 
122  [[nodiscard]] std::vector<key_mapping> get_key_mappings(const action& action) const;
123 
129  [[nodiscard]] std::vector<mouse_button_mapping> get_mouse_button_mappings(const action& action) const;
130 
136  [[nodiscard]] std::vector<mouse_motion_mapping> get_mouse_motion_mappings(const action& action) const;
137 
143  [[nodiscard]] std::vector<mouse_scroll_mapping> get_mouse_scroll_mappings(const action& action) const;
144 
146  [[nodiscard]] inline constexpr bool is_enabled() const noexcept
147  {
148  return m_enabled;
149  }
150 
151 private:
152  void subscribe();
153  void unsubscribe();
154 
155  void handle_gamepad_axis_moved(const gamepad_axis_moved_event& event);
156  void handle_gamepad_button_pressed(const gamepad_button_pressed_event& event);
157  void handle_gamepad_button_released(const gamepad_button_released_event& event);
158  void handle_key_pressed(const key_pressed_event& event);
159  void handle_key_released(const key_released_event& event);
160  void handle_mouse_button_pressed(const mouse_button_pressed_event& event);
161  void handle_mouse_button_released(const mouse_button_released_event& event);
162  void handle_mouse_moved(const mouse_moved_event& event);
163  void handle_mouse_scrolled(const mouse_scrolled_event& event);
164  void handle_update(const update_event& event);
165 
166  event::dispatcher* m_event_dispatcher{nullptr};
167  bool m_enabled{false};
168  std::unordered_set<action*> m_actions;
169  std::vector<std::shared_ptr<::event::subscription>> m_subscriptions;
170  std::vector<std::tuple<action*, gamepad_axis_mapping>> m_gamepad_axis_mappings;
171  std::vector<std::tuple<action*, gamepad_button_mapping>> m_gamepad_button_mappings;
172  std::vector<std::tuple<action*, key_mapping>> m_key_mappings;
173  std::vector<std::tuple<action*, mouse_button_mapping>> m_mouse_button_mappings;
174  std::vector<std::tuple<action*, mouse_motion_mapping>> m_mouse_motion_mappings;
175  std::vector<std::tuple<action*, mouse_scroll_mapping>> m_mouse_scroll_mappings;
176 };
177 
178 } // namespace input
179 
180 #endif // ANTKEEER_INPUT_ACTION_MAP_HPP
Forwards messages from publishers to subscribers.
Definition: dispatcher.hpp:37
Maps input to a set of contextually-related actions.
Definition: action-map.hpp:43
void add_gamepad_button_mapping(action &action, gamepad_button_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:127
void disable()
Disables the mapping of input events to actions.
Definition: action-map.cpp:41
std::vector< mouse_motion_mapping > get_mouse_motion_mappings(const action &action) const
Returns all of the mouse motion mappings associated with an action.
Definition: action-map.cpp:472
void remove_mappings()
Unmaps all input from all actions in the action map.
Definition: action-map.cpp:258
std::vector< gamepad_button_mapping > get_gamepad_button_mappings(const action &action) const
Returns all of the gamepad button mappings associated with an action.
Definition: action-map.cpp:427
void add_mouse_button_mapping(action &action, mouse_button_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:139
void add_gamepad_axis_mapping(action &action, gamepad_axis_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:121
void set_event_dispatcher(event::dispatcher *dispatcher)
Sets the event dispatcher from which this action map will receive input events.
Definition: action-map.cpp:62
std::vector< key_mapping > get_key_mappings(const action &action) const
Returns all of the key mappings associated with an action.
Definition: action-map.cpp:442
void reset()
Resets the activation states of each action in the action map.
Definition: action-map.cpp:54
void add_mouse_scroll_mapping(action &action, mouse_scroll_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:151
void add_mouse_motion_mapping(action &action, mouse_motion_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:145
std::vector< mouse_button_mapping > get_mouse_button_mappings(const action &action) const
Returns all of the mouse button mappings associated with an action.
Definition: action-map.cpp:457
void add_key_mapping(action &action, key_mapping mapping)
Maps input to an action.
Definition: action-map.cpp:133
std::vector< mouse_scroll_mapping > get_mouse_scroll_mappings(const action &action) const
Returns all of the mouse scroll associated with an action.
Definition: action-map.cpp:487
std::vector< gamepad_axis_mapping > get_gamepad_axis_mappings(const action &action) const
Returns all of the gamepad axis mappings associated with an action.
Definition: action-map.cpp:412
constexpr bool is_enabled() const noexcept
Returns true if the action map is enabled, false otherwise.
Definition: action-map.hpp:146
void enable()
Enables the mapping of input events to actions.
Definition: action-map.cpp:28
void add_mapping(action &action, const mapping &mapping)
Maps input to an action.
Definition: action-map.cpp:87
Evaluates an activation state given input values and publishes events on activation state changes.
Definition: action.hpp:33
Maps a direction along a gamepad axis to a control input value.
Definition: mapping.hpp:61
Maps a gamepad button to a control input value.
Definition: mapping.hpp:95
Maps a keyboard key to a control input value.
Definition: mapping.hpp:125
Abstract base class for input mappings.
Definition: mapping.hpp:43
Maps a mouse button to a control input value.
Definition: mapping.hpp:163
Maps a direction along a mouse motion axis to a control input value.
Definition: mapping.hpp:193
Maps a direction along a mouse scroll axis to a control input value.
Definition: mapping.hpp:227
Publish-subscribe messaging.
Definition: channel.hpp:32
Input devices, events, and mapping.
mapping_type
Input mapping types.
Text and typography.
Definition: bitmap-font.cpp:24
Event generated when a gamepad axis has been moved.
Event generated when a gamepad button has been pressed.
Event generated when a gamepad button has been released.
Event generated when a keyboard key has been pressed.
Event generated when a keyboard key has been released.
Event generated when a mouse button has been pressed.
Event generated when a mouse button has been released.
Event generated when a mouse has been moved.
Event generated when a mouse has been scrolled.
Event generated after input events are polled.