Nameless Engine
Loading...
Searching...
No Matches
InputManager.h
1#pragma once
2
3// Standard.
4#include <unordered_map>
5#include <string>
6#include <variant>
7#include <optional>
8#include <mutex>
9#include <vector>
10
11// Custom.
12#include "input/KeyboardKey.hpp"
13#include "input/MouseButton.hpp"
14
15namespace ne {
20 public:
21 ActionState() = delete;
22
28 ActionState(std::variant<KeyboardKey, MouseButton> key) {
29 this->key = key;
30 bIsPressed = false;
31 }
32
35
37 std::variant<KeyboardKey, MouseButton> key;
38 };
39
41 class AxisState {
42 public:
43 AxisState() = delete;
44
51 AxisState(KeyboardKey plusKey, KeyboardKey minusKey) {
52 this->plusKey = plusKey;
53 this->minusKey = minusKey;
54
55 bIsPlusKeyPressed = false;
56 bIsMinusKeyPressed = false;
57 }
58
60 KeyboardKey plusKey;
61
63 KeyboardKey minusKey;
64
67
70 };
71
78 // Triggers input events.
79 friend class GameManager;
80
81 public:
82 InputManager() = default;
83 InputManager(const InputManager&) = delete;
84 InputManager& operator=(const InputManager&) = delete;
85
105 [[nodiscard]] std::optional<Error> addActionEvent(
106 unsigned int iActionId, const std::vector<std::variant<KeyboardKey, MouseButton>>& vKeys);
107
129 [[nodiscard]] std::optional<Error> addAxisEvent(
130 unsigned int iAxisEventId, const std::vector<std::pair<KeyboardKey, KeyboardKey>>& vAxis);
131
141 [[nodiscard]] std::optional<Error> modifyActionEventKey(
142 unsigned int iActionId,
143 std::variant<KeyboardKey, MouseButton> oldKey,
144 std::variant<KeyboardKey, MouseButton> newKey);
145
155 [[nodiscard]] std::optional<Error> modifyAxisEventKey(
156 unsigned int iAxisEventId,
157 std::pair<KeyboardKey, KeyboardKey> oldPair,
158 std::pair<KeyboardKey, KeyboardKey> newPair);
159
169 [[nodiscard]] std::optional<Error> saveToFile(std::string_view sFileName);
170
191 [[nodiscard]] std::optional<Error> loadFromFile(std::string_view sFileName);
192
206 std::pair<std::vector<unsigned int>, std::vector<unsigned int>>
207 isKeyUsed(const std::variant<KeyboardKey, MouseButton>& key);
208
219 std::vector<std::variant<KeyboardKey, MouseButton>> getActionEvent(unsigned int iActionId);
220
231 std::vector<std::pair<KeyboardKey, KeyboardKey>> getAxisEvent(unsigned int iAxisEventId);
232
241 float getCurrentAxisEventState(unsigned int iAxisEventId);
242
250 bool removeActionEvent(unsigned int iActionId);
251
259 bool removeAxisEvent(unsigned int iAxisEventId);
260
266 std::unordered_map<unsigned int, std::vector<std::variant<KeyboardKey, MouseButton>>>
268
274 std::unordered_map<unsigned int, std::vector<std::pair<KeyboardKey, KeyboardKey>>> getAllAxisEvents();
275
284 static std::vector<std::string>
285 splitString(const std::string& sStringToSplit, const std::string& sDelimiter);
286
287 private:
305 [[nodiscard]] std::optional<Error> overwriteActionEvent(
306 unsigned int iActionId, const std::vector<std::variant<KeyboardKey, MouseButton>>& vKeys);
307
325 [[nodiscard]] std::optional<Error> overwriteAxisEvent(
326 unsigned int iAxisEventId, const std::vector<std::pair<KeyboardKey, KeyboardKey>>& vAxis);
327
334 std::unordered_map<std::variant<KeyboardKey, MouseButton>, std::vector<unsigned int>> actionEvents;
335
340 std::unordered_map<unsigned int, std::pair<std::vector<ActionState>, bool /* action state */>>
342
350 std::unordered_map<KeyboardKey, std::vector<std::pair<unsigned int, bool>>> axisEvents;
351
356 std::unordered_map<unsigned int, std::pair<std::vector<AxisState>, int /* axis state */>> axisState;
357
359 std::recursive_mutex mtxActionEvents;
360
362 std::recursive_mutex mtxAxisEvents;
363
365 static inline const std::string_view sActionEventSectionName = "action event";
366
368 static inline const std::string_view sAxisEventSectionName = "axis event";
369 };
370} // namespace ne
Definition: InputManager.h:19
bool bIsPressed
Definition: InputManager.h:34
std::variant< KeyboardKey, MouseButton > key
Definition: InputManager.h:37
ActionState(std::variant< KeyboardKey, MouseButton > key)
Definition: InputManager.h:28
Definition: InputManager.h:41
KeyboardKey plusKey
Definition: InputManager.h:60
KeyboardKey minusKey
Definition: InputManager.h:63
bool bIsPlusKeyPressed
Definition: InputManager.h:66
bool bIsMinusKeyPressed
Definition: InputManager.h:69
AxisState(KeyboardKey plusKey, KeyboardKey minusKey)
Definition: InputManager.h:51
Definition: GameManager.h:34
Definition: InputManager.h:77
std::unordered_map< std::variant< KeyboardKey, MouseButton >, std::vector< unsigned int > > actionEvents
Definition: InputManager.h:334
std::optional< Error > loadFromFile(std::string_view sFileName)
Definition: InputManager.cpp:189
static std::vector< std::string > splitString(const std::string &sStringToSplit, const std::string &sDelimiter)
Definition: InputManager.cpp:694
std::optional< Error > saveToFile(std::string_view sFileName)
Definition: InputManager.cpp:135
std::vector< std::variant< KeyboardKey, MouseButton > > getActionEvent(unsigned int iActionId)
Definition: InputManager.cpp:461
std::optional< Error > overwriteAxisEvent(unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis)
Definition: InputManager.cpp:736
std::recursive_mutex mtxActionEvents
Definition: InputManager.h:359
std::unordered_map< KeyboardKey, std::vector< std::pair< unsigned int, bool > > > axisEvents
Definition: InputManager.h:350
std::optional< Error > modifyActionEventKey(unsigned int iActionId, std::variant< KeyboardKey, MouseButton > oldKey, std::variant< KeyboardKey, MouseButton > newKey)
Definition: InputManager.cpp:65
std::optional< Error > overwriteActionEvent(unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys)
Definition: InputManager.cpp:708
std::optional< Error > addActionEvent(unsigned int iActionId, const std::vector< std::variant< KeyboardKey, MouseButton > > &vKeys)
Definition: InputManager.cpp:14
std::optional< Error > addAxisEvent(unsigned int iAxisEventId, const std::vector< std::pair< KeyboardKey, KeyboardKey > > &vAxis)
Definition: InputManager.cpp:40
std::unordered_map< unsigned int, std::vector< std::pair< KeyboardKey, KeyboardKey > > > getAllAxisEvents()
Definition: InputManager.cpp:668
static const std::string_view sAxisEventSectionName
Definition: InputManager.h:368
std::unordered_map< unsigned int, std::vector< std::variant< KeyboardKey, MouseButton > > > getAllActionEvents()
Definition: InputManager.cpp:643
std::optional< Error > modifyAxisEventKey(unsigned int iAxisEventId, std::pair< KeyboardKey, KeyboardKey > oldPair, std::pair< KeyboardKey, KeyboardKey > newPair)
Definition: InputManager.cpp:97
std::unordered_map< unsigned int, std::pair< std::vector< ActionState >, bool > > actionState
Definition: InputManager.h:341
bool removeActionEvent(unsigned int iActionId)
Definition: InputManager.cpp:556
std::pair< std::vector< unsigned int >, std::vector< unsigned int > > isKeyUsed(const std::variant< KeyboardKey, MouseButton > &key)
Definition: InputManager.cpp:433
bool removeAxisEvent(unsigned int iAxisEventId)
Definition: InputManager.cpp:599
float getCurrentAxisEventState(unsigned int iAxisEventId)
Definition: InputManager.cpp:544
static const std::string_view sActionEventSectionName
Definition: InputManager.h:365
std::recursive_mutex mtxAxisEvents
Definition: InputManager.h:362
std::vector< std::pair< KeyboardKey, KeyboardKey > > getAxisEvent(unsigned int iAxisEventId)
Definition: InputManager.cpp:478
std::unordered_map< unsigned int, std::pair< std::vector< AxisState >, int > > axisState
Definition: InputManager.h:356