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

UserProfile container resource. More...

#include <Users.hpp>

Inheritance diagram for ecstasy::integration::user_action::Users:
Collaboration diagram for ecstasy::integration::user_action::Users:

Classes

struct  UserActionLink
 Helper type instead of using std::pair. More...
 

Public Member Functions

 Users (size_t count=1)
 Construct a new Users resource.
 
 ~Users ()=default
 Default destructor.
 
void updateBindings ()
 Update the internal bindings associative maps.
 
void handleEvent (Registry &registry, const event::Event &event) const
 Call action listeners associated to the event if any.
 
constexpr UserProfilegetUserProfile (size_t index=0)
 Get the User Profile corresponding to the given index.
 
constexpr const UserProfilegetUserProfile (size_t index=0) const
 Get the User Profile corresponding to the given index.
 
- Public Member Functions inherited from ecstasy::IResource
virtual ~IResource ()=default
 Default destructor.
 

Static Public Member Functions

static void updateBindings (Registry &registry)
 Fetch the Users resource in the registry if available and call updateBindings().
 

Private Member Functions

template<typename T >
void removeOutdatedBindings (std::unordered_multimap< T, UserActionLink > &map)
 
template<typename T >
void addBindingIfMissing (const UserProfile &user, const ActionBinding &binding, T input, std::unordered_multimap< T, UserActionLink > &map)
 

Static Private Member Functions

static void callActionListeners (Registry &registry, Action action)
 
template<typename T >
static void callListenersFromMap (Registry &registry, const std::unordered_multimap< T, Users::UserActionLink > &map, T key, float value)
 

Private Attributes

std::unordered_multimap< event::Mouse::Button, UserActionLink_mouseButtonToAction
 
std::unordered_multimap< event::Keyboard::Key, UserActionLink_keyToAction
 
std::unordered_multimap< event::Gamepad::Button, UserActionLink_gamepadButtonToAction
 
std::unordered_multimap< event::Gamepad::Axis, UserActionLink_gamepadAxisToAction
 
std::vector< UserProfile_users
 

Friends

class UsersTester
 

Detailed Description

UserProfile container resource.

Manage all the user profiles and their associated actions and 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-12-02)

Definition at line 40 of file Users.hpp.

Constructor & Destructor Documentation

◆ Users()

ecstasy::integration::user_action::Users::Users ( size_t  count = 1)

Construct a new Users resource.

Parameters
[in]countNumber of available users.
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 20 of file Users.cpp.

20 : _users(count)
21 {
22 for (size_t i = 1; i < count; i++)
23 _users[i].setId(i);
24 }
std::vector< UserProfile > _users
Definition Users.hpp:184
T count(T... args)

◆ ~Users()

ecstasy::integration::user_action::Users::~Users ( )
default

Default destructor.

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)

Member Function Documentation

◆ addBindingIfMissing()

template<typename T >
void ecstasy::integration::user_action::Users::addBindingIfMissing ( const UserProfile user,
const ActionBinding binding,
input,
std::unordered_multimap< T, UserActionLink > &  map 
)
inlineprivate

Definition at line 145 of file Users.hpp.

147 {
148 bool add = true;
149 auto range = map.equal_range(input);
150
151 for (auto it = range.first; it != range.second; ++it) {
152 if (it->second.userId == user.getId() && it->second.actionId == binding.actionId) {
153 add = false;
154 break;
155 }
156 }
157 if (add)
158 map.insert({input, UserActionLink{binding.actionId, user.getId()}});
159 }
T equal_range(T... args)

◆ callActionListeners()

void ecstasy::integration::user_action::Users::callActionListeners ( Registry registry,
Action  action 
)
staticprivate

Definition at line 61 of file Users.cpp.

62 {
63 if (registry.hasResource<PendingActions>())
64 registry.getResource<PendingActions>().get()->push(action);
65
66 for (auto [entity, maybeListener, maybeListeners] :
67 registry.select<Entities, Maybe<ActionListener>, Maybe<ActionListeners>>()
68 .where<Entities, Or<ActionListener, ActionListeners>>()) {
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 }
std::unordered_map< Action::Id, ActionListener::Listener > ActionListeners
Action listeners map.
ecstasy::ObjectWrapper< std::queue< Action > > PendingActions
Queue of Action.
auto constexpr Or(Q1 &firstOperand, Q2 &secondOperand, Qs &...otherOperands)
Binary query modifier which performs a or between at least two queryables.
Definition Or.hpp:159
static constexpr Id All
Constant id matching any action ID.
Definition Action.hpp:30

◆ callListenersFromMap()

template<typename T >
static void ecstasy::integration::user_action::Users::callListenersFromMap ( Registry registry,
const std::unordered_multimap< T, Users::UserActionLink > &  map,
key,
float  value 
)
inlinestaticprivate

Definition at line 167 of file Users.hpp.

169 {
170 auto range = map.equal_range(key);
171 for (auto it = range.first; it != range.second; ++it)
172 callActionListeners(registry, Action{it->second.actionId, it->second.userId, value});
173 }
static void callActionListeners(Registry &registry, Action action)
Definition Users.cpp:61

◆ getUserProfile() [1/2]

constexpr UserProfile & ecstasy::integration::user_action::Users::getUserProfile ( size_t  index = 0)
inlineconstexpr

Get the User Profile corresponding to the given index.

Warning
This method does not perfom bounds checking.
Parameters
[in]indexUser index.
Returns
UserProfile& A reference to the requested user.
Exceptions
std::out_of_rangeIf the index is out of bounds.
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-05)

Definition at line 115 of file Users.hpp.

116 {
117 return _users[index];
118 }

◆ getUserProfile() [2/2]

constexpr const UserProfile & ecstasy::integration::user_action::Users::getUserProfile ( size_t  index = 0) const
inlineconstexpr

Get the User Profile corresponding to the given index.

Warning
This method does not perfom bounds checking.
Parameters
[in]indexUser index.
Returns
UserProfile& A reference to the requested user.
Exceptions
std::out_of_rangeIf the index is out of bounds.
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-05)

Definition at line 121 of file Users.hpp.

122 {
123 return _users[index];
124 }

◆ handleEvent()

void ecstasy::integration::user_action::Users::handleEvent ( Registry registry,
const event::Event event 
) const

Call action listeners associated to the event if any.

Parameters
[in]registryRegistry owning the event.
[in]eventEvent to handle.
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 77 of file Users.cpp.

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
94 callListenersFromMap(registry, _gamepadButtonToAction, e.gamepadButton.button,
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 }
std::unordered_multimap< event::Mouse::Button, UserActionLink > _mouseButtonToAction
Definition Users.hpp:176
std::unordered_multimap< event::Gamepad::Button, UserActionLink > _gamepadButtonToAction
Definition Users.hpp:180
std::unordered_multimap< event::Gamepad::Axis, UserActionLink > _gamepadAxisToAction
Definition Users.hpp:182
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
@ 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.

◆ removeOutdatedBindings()

template<typename T >
void ecstasy::integration::user_action::Users::removeOutdatedBindings ( std::unordered_multimap< T, UserActionLink > &  map)
inlineprivate

Definition at line 130 of file Users.hpp.

131 {
132 for (auto it = map.begin(); it != map.end();) {
133 if (it->second.userId >= _users.size()
134 || !_users[it->second.userId].getActionBindings().contains(
135 ActionBinding(it->second.actionId, it->first)))
136 it = map.erase(it);
137 else
138 ++it;
139 }
140 }

◆ updateBindings() [1/2]

void ecstasy::integration::user_action::Users::updateBindings ( )

Update the internal bindings associative maps.

Note
This should be called when one UserProfile binding changed.
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 33 of file Users.cpp.

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 }
@ 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.
void addBindingIfMissing(const UserProfile &user, const ActionBinding &binding, T input, std::unordered_multimap< T, UserActionLink > &map)
Definition Users.hpp:145
void removeOutdatedBindings(std::unordered_multimap< T, UserActionLink > &map)
Definition Users.hpp:130

◆ updateBindings() [2/2]

void ecstasy::integration::user_action::Users::updateBindings ( Registry registry)
static

Fetch the Users resource in the registry if available and call updateBindings().

Parameters
[in]registryRegistry owning the Users resource.
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 26 of file Users.cpp.

27 {
28 if (!registry.hasResource<Users>()) [[unlikely]]
29 return;
30 registry.getResource<Users>().get().updateBindings();
31 }
Users(size_t count=1)
Construct a new Users resource.
Definition Users.cpp:20

Friends And Related Symbol Documentation

◆ UsersTester

friend class UsersTester
friend

Definition at line 187 of file Users.hpp.

Member Data Documentation

◆ _gamepadAxisToAction

std::unordered_multimap<event::Gamepad::Axis, UserActionLink> ecstasy::integration::user_action::Users::_gamepadAxisToAction
private

Definition at line 182 of file Users.hpp.

◆ _gamepadButtonToAction

std::unordered_multimap<event::Gamepad::Button, UserActionLink> ecstasy::integration::user_action::Users::_gamepadButtonToAction
private

Definition at line 180 of file Users.hpp.

◆ _keyToAction

std::unordered_multimap<event::Keyboard::Key, UserActionLink> ecstasy::integration::user_action::Users::_keyToAction
private

Definition at line 178 of file Users.hpp.

◆ _mouseButtonToAction

std::unordered_multimap<event::Mouse::Button, UserActionLink> ecstasy::integration::user_action::Users::_mouseButtonToAction
private

Definition at line 176 of file Users.hpp.

◆ _users

std::vector<UserProfile> ecstasy::integration::user_action::Users::_users
private

Definition at line 184 of file Users.hpp.


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