ECSTASY
All in the name
Loading...
Searching...
No Matches
Users.hpp
Go to the documentation of this file.
1
11
12#ifndef ECSTAS_INTEGRATIONS_USER_ACTION_USERS_HPP_
13#define ECSTAS_INTEGRATIONS_USER_ACTION_USERS_HPP_
14
15#include <vector>
16#include "UserProfile.hpp"
24#include <unordered_map>
25
26namespace
27{
29}
30
32{
40 class Users : public ecstasy::IResource {
41 private:
49 };
50
51 public:
60 Users(size_t count = 1);
61
68 ~Users() = default;
69
78 void updateBindings();
79
88 static void updateBindings(Registry &registry);
89
99 void handleEvent(Registry &registry, const event::Event &event) const;
100
115 [[nodiscard]] constexpr UserProfile &getUserProfile(size_t index = 0)
116 {
117 return _users[index];
118 }
119
121 [[nodiscard]] constexpr const UserProfile &getUserProfile(size_t index = 0) const
122 {
123 return _users[index];
124 }
125
126 private:
129 template <typename T>
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 }
141
144 template <typename T>
145 void addBindingIfMissing(const UserProfile &user, const ActionBinding &binding, T input,
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 }
160
162 static void callActionListeners(Registry &registry, Action action);
163
166 template <typename T>
168 Registry &registry, const std::unordered_multimap<T, Users::UserActionLink> &map, T key, float value)
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 }
174
175 // Link between the mouse buttons and the associated actions.
177 // Link between the keyboard keys and the associated actions.
179 // Link between the gamepad buttons and the associated actions.
181 // Link between the gamepad axis and the associated actions.
183 // User profiles.
185
187 friend class UsersTester;
188 };
189} // namespace ecstasy::integration::user_action
190
191#endif /* !ECSTAS_INTEGRATIONS_USER_ACTION_USERS_HPP_ */
Base class of all registry resources.
Associative Map to store entity components.
Registry class definition.
Base class of all registry resources.
Definition IResource.hpp:33
Base of an ECS architecture.
Definition Registry.hpp:82
Action binding class, represent a binding between an input and a given action.
Represent an application user profile, allowing to configure basic informations like the action bindi...
constexpr Id getId() const noexcept
Get the Id of the profile.
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
constexpr const UserProfile & getUserProfile(size_t index=0) const
Get the User Profile corresponding to the given index.
Definition Users.hpp:121
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()=default
Default destructor.
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
constexpr UserProfile & getUserProfile(size_t index=0)
Get the User Profile corresponding to the given index.
Definition Users.hpp:115
T equal_range(T... args)
Event integration.
Definition Event.hpp:25
User action integration.
Definition Action.hpp:18
Describe an action changed event.
Definition Action.hpp:25
size_t Id
Action identifier type.
Definition Action.hpp:27