ECSTASY
All in the name
Loading...
Searching...
No Matches
ecstasy::integration::user_action::ActionBindings Class Reference

Wrapper of a std::vector of ActionBinding. More...

#include <ActionBindings.hpp>

Collaboration diagram for ecstasy::integration::user_action::ActionBindings:

Public Member Functions

 ActionBindings () noexcept=default
 Construct a new empty action binding container.
 
constexpr std::vector< ActionBinding > & getBindings () noexcept
 Get the Bindings.
 
constexpr const std::vector< ActionBinding > & getBindings () const noexcept
 Get the Bindings.
 
toml::table dump () const noexcept
 Dump the bindings as a toml table.
 
bool load (const toml::table &bindings) noexcept
 Load a given toml table representing a list of bindings.
 
bool contains (ActionBinding action) const noexcept
 Check whether action is contained in the internal ActionBinding vector.
 

Private Attributes

std::vector< ActionBinding_bindings
 

Detailed Description

Wrapper of a std::vector of ActionBinding.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-25)

Definition at line 27 of file ActionBindings.hpp.

Constructor & Destructor Documentation

◆ ActionBindings()

ecstasy::integration::user_action::ActionBindings::ActionBindings ( )
defaultnoexcept

Construct a new empty action binding container.

Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-25)

Member Function Documentation

◆ contains()

bool ecstasy::integration::user_action::ActionBindings::contains ( ActionBinding  action) const
noexcept

Check whether action is contained in the internal ActionBinding vector.

Parameters
[in]actionaction to search.
Returns
bool Whether the action is contained or not.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-12-02)

Definition at line 107 of file ActionBindings.cpp.

108 {
109 return std::find(_bindings.cbegin(), _bindings.cend(), binding) != _bindings.cend();
110 }
T find(T... args)

◆ dump()

toml::table ecstasy::integration::user_action::ActionBindings::dump ( ) const
noexcept

Dump the bindings as a toml table.

Returns
toml::table Toml representation of the bindings.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-29)

There is no check for node type because we control the total flow of the toml nodes

The actions are named 'Action-<action_id>'

If there isn't any node for the action we create one

Definition at line 21 of file ActionBindings.cpp.

22 {
23 toml::table table;
24
26 for (ActionBinding action : _bindings) {
28 std::string key = std::string("Action-") + std::to_string(action.actionId);
29 toml::node *nodePtr = table.get(key);
31
33 if (!nodePtr)
34 nodePtr = &table.emplace<toml::array>(key).first->second;
35
36 toml::node &node = *nodePtr;
37 value << action.type << "->";
38 switch (action.type) {
39 case ActionBinding::Type::MouseButton: value << action.mouseButton; break;
40 case ActionBinding::Type::Key: value << action.key; break;
41 case ActionBinding::Type::GamepadButton: value << action.gamepadButton; break;
42 case ActionBinding::Type::GamepadAxis: value << action.gamepadAxis; break;
43 default: continue;
44 }
45 node.as_array()->emplace_back(value.str());
46 }
47
48 return table;
49 }
@ GamepadButton
The action is bound to a gamepad button state.
@ MouseButton
The action is bound to a mouse button state.
@ Key
The action is bound to a keyboard key state.
@ GamepadAxis
The action is bound to a gamepad axis state.
decltype(auto) emplace_back(Args &&... args)
virtual array * as_array() noexcept=0
std::pair< iterator, bool > emplace(KeyType &&key, ValueArgs &&... args)
node * get(std::string_view key) noexcept
T str(T... args)
T to_string(T... args)

◆ getBindings() [1/2]

constexpr const std::vector< ActionBinding > & ecstasy::integration::user_action::ActionBindings::getBindings ( ) const
inlineconstexprnoexcept

Get the Bindings.

Returns
const std::vector<ActionBinding>& A const reference to the internal container.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-25)

Definition at line 58 of file ActionBindings.hpp.

59 {
60 return _bindings;
61 }

◆ getBindings() [2/2]

constexpr std::vector< ActionBinding > & ecstasy::integration::user_action::ActionBindings::getBindings ( )
inlineconstexprnoexcept

Get the Bindings.

Returns
std::vector<ActionBinding>& A reference to the internal container.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-25)

Definition at line 45 of file ActionBindings.hpp.

46 {
47 return _bindings;
48 }

◆ load()

bool ecstasy::integration::user_action::ActionBindings::load ( const toml::table bindings)
noexcept

Load a given toml table representing a list of bindings.

See the format of dump().

Warning
This method clear the internal bindings.
Parameters
[in]bindingsInput bindings.
Returns
bool Whether the load succeed or not.
Author
Andréas Leroux (andre.nosp@m.as.l.nosp@m.eroux.nosp@m.@epi.nosp@m.tech..nosp@m.eu)
Since
1.0.0 (2022-11-30)

Definition at line 51 of file ActionBindings.cpp.

52 {
53 _bindings.clear();
54 const std::regex reActionName("^Action-([0-9]+)$");
55 const std::regex reActionValue("^(\\w+)->(\\w+)$");
56 std::cmatch match;
57
58 for (auto &&[key, value] : bindings) {
59 if (!std::regex_match(key.data(), match, reActionName)) [[unlikely]] {
60 std::cerr << "Invalid action name." << std::endl;
61 break;
62 }
63
64 Action::Id actionId = std::stoul(match[1].str());
65 if (!value.is_array()) [[unlikely]] {
66 std::cerr << "Action '" << key.data() << "' is not a valid toml array." << std::endl;
67 continue;
68 }
69
70 value.as_array()->for_each([this, &match, &reActionValue, &actionId](auto &&input) {
71 if constexpr (toml::is_string<decltype(input)>) {
72 if (!std::regex_match(input.as_string()->get().data(), match, reActionValue))
73 return;
75
76 std::istringstream ss(match[1].str());
77 ss >> type;
78 ss.clear();
79 ss.str(match[2].str());
80
81 switch (type) {
83 _bindings.emplace_back(
84 actionId, util::serialization::Serializer::deserialize<event::Mouse::Button>(ss));
85 break;
87 _bindings.emplace_back(
88 actionId, util::serialization::Serializer::deserialize<event::Keyboard::Key>(ss));
89 break;
91 _bindings.emplace_back(
92 actionId, util::serialization::Serializer::deserialize<event::Gamepad::Button>(ss));
93 break;
95 _bindings.emplace_back(
96 actionId, util::serialization::Serializer::deserialize<event::Gamepad::Axis>(ss));
97 break;
98 default: break;
99 }
100 // clang-format off
101 }});
102 // clang-format on
103 }
104 return true;
105 }
T endl(T... args)
constexpr bool is_string
T regex_match(T... args)
T stoul(T... args)
size_t Id
Action identifier type.
Definition Action.hpp:27

Member Data Documentation

◆ _bindings

std::vector<ActionBinding> ecstasy::integration::user_action::ActionBindings::_bindings
private

Definition at line 101 of file ActionBindings.hpp.


The documentation for this class was generated from the following files: