Cogs.Core
InputManager.h
1#pragma once
2
3#include "IInputProvider.h"
4
5#include "Foundation/StringView.h"
6
7#include "Foundation/Platform/Keyboard.h"
8#include "Foundation/Platform/Gestures.h"
9
10namespace Cogs
11{
12 namespace Core
13 {
14 class Context;
15 class ViewContext;
16
18 {
19 enum EValues
20 {
21 Mouse = 0,
23 Gamepad0,
24 Gamepad1,
25 Gamepad2,
26 Gamepad3,
27 LeftHandController,
28 RightHandController,
29 Custom
30 };
31 };
32
36 class COGSCORE_DLL_API InputManager
37 {
38 public:
40 explicit InputManager(ViewContext* viewContext);
41
44
45 // Disable copy constructors to avoid issues with vector of unique_ptr
46 InputManager(const InputManager&) = delete;
47 InputManager& operator=(const InputManager&) = delete;
48
50 void initialize();
51
53 void addProvider(std::unique_ptr<IInputProvider> provider);
54
56 void createAxisMapping(const InputIdentifier & axisId, const StringView & name);
57
59 void createActionMapping(const InputIdentifier & actionId, const StringView & name);
60
62 float getAxisValue(const StringView & name) const;
63
65 bool getActionState(const StringView & name) const;
66
68 void update();
69
70 inline Cogs::Core::InputIdentifier getAxis(std::string driver, std::string device, std::string axis)
71 {
72 return InputIdentifier{ { driver, device }, axis };
73 }
74
75 size_t getAxisIndex(InputDeviceState * device, const StringView & inputAxis) const;
76
77 void addAxisMapping(const StringView & name, const StringView & device, const StringView & inputAxis, float scale = 1.0f, int flags = 0);
78 void addAxisMapping(const StringView & name, InputDeviceState * device, const StringView & inputAxis, float scale = 1.0f, int flags = 0);
79
80 size_t getButtonIndex(InputDeviceState * device, const StringView & inputAction) const;
81
82 void addActionMapping(const StringView & name, const StringView & device, const StringView & inputAction, int placeholder = 0);
83 void addActionMapping(const StringView & name, InputDeviceState * device, const StringView & inputAction, int placeholder = 0);
84
85 void gainedFocus(double timestamp_ms);
86 void lostFocus(double timestamp_ms);
87
88 void triggerPointerPress(PointerType pointerType, PointerId pointerId, MouseButton button, const glm::ivec2& position, double timestamp_ms);
89 void triggerPointerRelease(PointerType pointerType, PointerId pointerId, MouseButton button, const glm::ivec2& position, double timestamp_ms);
90 void triggerPointerMove(PointerType pointerType, PointerId pointerId, const glm::ivec2& position, double timestamp_ms);
91 void triggerMouseWheel(int32_t deltaValue, double timestamp_ms);
92
93 void triggerKeyDown(Key key, double timestamp_ms);
94 void triggerKeyUp(Key key, double timestamp_ms);
95 void triggerKeyChar(std::string ch, double timestamp_ms);
96
97 // TODO: Trigger controller input
98
99 InputDeviceState & getDevice(InputDevices::EValues deviceId);
100 ViewContext * getView() const { return view; }
101
102 private:
103 void updateConnections();
104 void updateInputs();
105 void updateActions();
106 void updateAxes();
107
111 double checkTimeStamp(double timestamp_ms);
112
113 void readInputConfig(const StringView & path);
114
115 InputDeviceState * getDevice(const StringView & name);
116
117 ViewContext* view = nullptr;
118
119 double prevTimeStamp = 0.0;
120
121 static constexpr size_t NoAxis = static_cast<size_t>(-1);
122 static constexpr size_t NoButton = static_cast<size_t>(-1);
123
124 static constexpr size_t ErrorAxis = static_cast<size_t>(-2);
125 static constexpr size_t ErrorButton = static_cast<size_t>(-2);
126
128 {
129 std::string axisName;
130
131 size_t deviceId;
132 size_t axisId;
133
134 float scale = 1.0f;
135
136 int flags = 0;
137 };
138
140 {
141 std::string name;
142
143 float value = 0;
144
145 std::vector<SourceAxisMapping> inputs;
146 };
147
149 {
150 std::string actionName;
151
152 size_t deviceId;
153 size_t buttonId;
154 // Modifiers etc.
155 };
156
158 {
159 std::string name;
160
161 bool value = false;
162
163 std::vector<SourceActionMapping> inputs;
164 };
165
166 std::vector<InputDeviceState> deviceStates;
167
168 std::unordered_map<size_t, InputAxisMapping> axisMappings;
169 std::unordered_map<size_t, InputActionMapping> actionMappings;
170
171 std::vector<std::unique_ptr<IInputProvider>> inputProviders;
172
173 std::vector<InputDeviceIdentifier> pendingDevices;
174
175 friend void inputInspector(Context * context, bool * show);
176 };
177 }
178}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Input manager responsible for handling input from various devices, including mouse,...
Definition: InputManager.h:37
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Identifier for an input device axis.