Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
actions.hpp
Go to the documentation of this file.
1
4#pragma once
5
7#include <aegis2d/platform/export.hpp>
10
11#include <cstddef>
12#include <initializer_list>
13#include <optional>
14#include <span>
15#include <string>
16#include <string_view>
17#include <vector>
18
20
22using ActionId = std::size_t;
23
38class AEGIS2D_PLATFORM_EXPORT ActionMap {
39public:
48 ActionId add(std::string name, std::span<const Key> defaults);
49
55 ActionId add(std::string name, std::initializer_list<Key> defaults);
56
65 ActionId add(std::string name, std::span<const Key> keys,
66 std::span<const GamepadButton> buttons);
67
75 ActionId add(std::string name, std::initializer_list<Key> keys,
76 std::initializer_list<GamepadButton> buttons);
77
80 [[nodiscard]] std::size_t size() const noexcept;
81
85 [[nodiscard]] std::optional<ActionId> find(std::string_view name) const;
86
91 [[nodiscard]] std::string_view name(ActionId action) const;
92
97 [[nodiscard]] std::span<const Key> keys(ActionId action) const;
98
103 [[nodiscard]] std::span<const Key> default_keys(ActionId action) const;
104
108 [[nodiscard]] std::optional<ActionId> action_of(Key key) const noexcept;
109
114 [[nodiscard]] std::span<const GamepadButton> buttons(ActionId action) const;
115
120 [[nodiscard]] std::span<const GamepadButton> default_buttons(ActionId action) const;
121
125 [[nodiscard]] std::optional<ActionId> action_of(GamepadButton button) const noexcept;
126
133 std::optional<ActionId> bind(ActionId action, Key key);
134
141 std::optional<ActionId> bind(ActionId action, GamepadButton button);
142
149 std::optional<ActionId> rebind(ActionId action, Key key);
150
156 std::optional<ActionId> rebind(ActionId action, GamepadButton button);
157
163 bool unbind(ActionId action, Key key);
164
170 bool unbind(ActionId action, GamepadButton button);
171
176 void clear(ActionId action);
177
182 void reset(ActionId action);
183
185 void reset_all();
186
193 void save(core::Config& settings, std::string_view section) const;
194
203 std::size_t load(const core::Config& settings, std::string_view section);
204
211 void save_buttons(core::Config& settings, std::string_view section) const;
212
217 std::size_t load_buttons(const core::Config& settings, std::string_view section);
218
224 [[nodiscard]] bool held(ActionId action, const Keyboard& keyboard) const;
225
231 [[nodiscard]] bool pressed(ActionId action, const Keyboard& keyboard) const;
232
238 [[nodiscard]] bool released(ActionId action, const Keyboard& keyboard) const;
239
246 [[nodiscard]] bool held(ActionId action, const Keyboard& keyboard,
247 const Gamepad& gamepad) const;
248
256 [[nodiscard]] bool pressed(ActionId action, const Keyboard& keyboard,
257 const Gamepad& gamepad) const;
258
266 [[nodiscard]] bool released(ActionId action, const Keyboard& keyboard,
267 const Gamepad& gamepad) const;
268
269private:
270 struct Entry {
271 std::string name;
272 std::vector<Key> default_keys;
273 std::vector<Key> keys;
274 std::vector<GamepadButton> default_buttons;
275 std::vector<GamepadButton> buttons;
276 };
277
278 [[nodiscard]] const Entry& entry(ActionId action) const;
279 [[nodiscard]] Entry& entry(ActionId action);
280
281 std::vector<Entry> entries_;
282};
283
284} // namespace aegis2d::platform
Actions of the game and the keys and gamepad buttons that trigger them.
Definition actions.hpp:38
ActionId add(std::string name, std::initializer_list< Key > defaults)
Declares an action, bound to its default keys.
ActionId add(std::string name, std::span< const Key > defaults)
Declares an action, bound to its default keys.
ActionId add(std::string name, std::initializer_list< Key > keys, std::initializer_list< GamepadButton > buttons)
Declares an action, bound to its default keys and gamepad buttons.
std::size_t size() const noexcept
Number of actions declared.
ActionId add(std::string name, std::span< const Key > keys, std::span< const GamepadButton > buttons)
Declares an action, bound to its default keys and gamepad buttons.
Buttons held down, and those pressed or released since the start of the frame, with the position of t...
Definition gamepad.hpp:139
Keys held down, and those pressed or released since the start of the frame.
Definition keyboard.hpp:150
JSON configuration with dotted-path lookups, which can be changed and saved.
Gamepad buttons, sticks and triggers, with no SDL type.
Keyboard keys, their names, and the state of the keyboard during a frame.
Window and input on top of SDL3, independent of the graphics API (client only).
Definition actions.hpp:19
GamepadButton
Buttons of a gamepad, named by their position on the pad.
Definition gamepad.hpp:23
Key
Keyboard key, independent of SDL.
Definition keyboard.hpp:24
std::size_t ActionId
Identifier of an action in its ActionMap, given by ActionMap::add().
Definition actions.hpp:22
Definition endpoint.hpp:85