ECSTASY
All in the name
Loading...
Searching...
No Matches
Users.cpp
Go to the documentation of this file.
1
11
12#include "Users.hpp"
13#include "ActionListener.hpp"
14#include "PendingActions.hpp"
17
19{
20 Users::Users(size_t count) : _users(count)
21 {
22 for (size_t i = 1; i < count; i++)
23 _users[i].setId(i);
24 }
25
27 {
28 if (!registry.hasResource<Users>()) [[unlikely]]
29 return;
30 registry.getResource<Users>().get().updateBindings();
31 }
32
34 {
39
40 for (UserProfile &user : _users) {
41 auto &bindings = user.getActionBindings().getBindings();
42
43 for (ActionBinding binding : bindings) {
44 switch (binding.type) {
46 addBindingIfMissing(user, binding, binding.mouseButton, _mouseButtonToAction);
47 break;
48 case ActionBinding::Type::Key: addBindingIfMissing(user, binding, binding.key, _keyToAction); break;
50 addBindingIfMissing(user, binding, binding.gamepadButton, _gamepadButtonToAction);
51 break;
53 addBindingIfMissing(user, binding, binding.gamepadAxis, _gamepadAxisToAction);
54 break;
55 default: break;
56 }
57 }
58 }
59 }
60
62 {
63 if (registry.hasResource<PendingActions>())
64 registry.getResource<PendingActions>().get()->push(action);
65
66 for (auto [entity, maybeListener, maybeListeners] :
69 if (maybeListener) {
70 if (maybeListener->get().actionId == Action::All || maybeListener->get().actionId == action.id)
71 maybeListener->get().listener(registry, entity, action);
72 } else if (maybeListeners->get().contains(action.id))
73 maybeListeners->get()[action.id](registry, entity, action);
74 }
75 }
76
77 void Users::handleEvent(Registry &registry, const event::Event &e) const
78 {
79 switch (e.type) {
83 registry, _mouseButtonToAction, e.mouseButton.button, static_cast<float>(e.mouseButton.pressed));
84 break;
85
88 callListenersFromMap(registry, _keyToAction, e.key.key, static_cast<float>(e.key.pressed));
89 break;
90
95 static_cast<float>(e.gamepadButton.pressed));
96 break;
97
101 registry, _gamepadAxisToAction, e.gamepadAxis.axis, static_cast<float>(e.gamepadAxis.value));
102 break;
103 default: break;
104 }
105 }
106} // namespace ecstasy::integration::user_action
Resource holding all the Registry entities.
Definition Entities.hpp:30
Basic object wrapper as a resource.
constexpr T & get() noexcept
Get a reference to the object.
Base of an ECS architecture.
Definition Registry.hpp:82
ResourceReference< const R, Locked > getResource() const
Get the Resource of type R.
Definition Registry.hpp:937
Select< queryable_type_t< C >, queryable_type_t< Cs >... > select()
Starts the creation of a complex query in the registry.
bool hasResource() const
Check whether the registry has the resource of type R.
Definition Registry.hpp:918
Action binding class, represent a binding between an input and a given action.
@ 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.
Represent an application user profile, allowing to configure basic informations like the action bindi...
UserProfile container resource.
Definition Users.hpp:40
std::unordered_multimap< event::Mouse::Button, UserActionLink > _mouseButtonToAction
Definition Users.hpp:176
std::unordered_multimap< event::Gamepad::Button, UserActionLink > _gamepadButtonToAction
Definition Users.hpp:180
void updateBindings()
Update the internal bindings associative maps.
Definition Users.cpp:33
void addBindingIfMissing(const UserProfile &user, const ActionBinding &binding, T input, std::unordered_multimap< T, UserActionLink > &map)
Definition Users.hpp:145
std::unordered_multimap< event::Gamepad::Axis, UserActionLink > _gamepadAxisToAction
Definition Users.hpp:182
Users(size_t count=1)
Construct a new Users resource.
Definition Users.cpp:20
std::unordered_multimap< event::Keyboard::Key, UserActionLink > _keyToAction
Definition Users.hpp:178
static void callListenersFromMap(Registry &registry, const std::unordered_multimap< T, Users::UserActionLink > &map, T key, float value)
Definition Users.hpp:167
void handleEvent(Registry &registry, const event::Event &event) const
Call action listeners associated to the event if any.
Definition Users.cpp:77
static void callActionListeners(Registry &registry, Action action)
Definition Users.cpp:61
void removeOutdatedBindings(std::unordered_multimap< T, UserActionLink > &map)
Definition Users.hpp:130
std::vector< UserProfile > _users
Definition Users.hpp:184
User action integration.
Definition Action.hpp:18
Query modifier which returns a std::optional filled when the data if existing.
Binary query modifier which performs a or between at least two queryables.
Placeholder for Maybe.
Definition Maybe.hpp:31
Placeholder for Or.
Definition Or.hpp:31
GamepadAxisEvent gamepadAxis
Type::GamepadAxis associated event.
Definition Event.hpp:193
KeyEvent key
Type::KeyPressed && Type::KeyReleased associated events.
Definition Event.hpp:189
@ KeyReleased
One of the keyboard key has been released.
@ MouseButtonPressed
One of the mouse button has been pressed.
@ MouseButtonReleased
One of the mouse button has been released.
@ GamepadAxis
One of the gamepads axis value changed.
@ KeyPressed
One of the keyboard key has been pressed.
@ GamepadButtonPressed
One of the gamepads button has been pressed.
@ GamepadButtonReleased
One of the gamepads button has been released.
GamepadButtonEvent gamepadButton
Type::GamepadButtonPressed && Type::GamepadButtonReleased
Definition Event.hpp:191
MouseButtonEvent mouseButton
Type::MouseButtonPressed && Type::MouseButtonReleased associated events.
Definition Event.hpp:186
Type type
Type of the event stored.
Definition Event.hpp:176
bool pressed
Whether the button is pressed (down) or not (up).
bool pressed
Whether the key is pressed (down) or not (up).
Definition KeyEvent.hpp:29
Keyboard::Key key
Target key.
Definition KeyEvent.hpp:27
bool pressed
Whether it was pressed or released.
Describe an action changed event.
Definition Action.hpp:25
static constexpr Id All
Constant id matching any action ID.
Definition Action.hpp:30