Plasma Engine  2.0
Loading...
Searching...
No Matches
InputManager.h
1#pragma once
2
3#include <Core/Input/InputDevice.h>
4#include <Foundation/Communication/Event.h>
5#include <Foundation/Containers/DynamicArray.h>
6#include <Foundation/Containers/Map.h>
7
9struct PL_CORE_DLL plInputActionConfig
10{
12 enum
13 {
14 MaxInputSlotAlternatives = 3
15 };
16
18
35
37 plString m_sInputSlotTrigger[MaxInputSlotAlternatives];
38
41 float m_fInputSlotScale[MaxInputSlotAlternatives];
42
45 plString m_sFilterByInputSlotX[MaxInputSlotAlternatives];
46
49 plString m_sFilterByInputSlotY[MaxInputSlotAlternatives];
50
55
58
66
70 {
71 ActivateImmediately,
73 RequireKeyUp,
75 };
76
79};
80
90class PL_CORE_DLL plInputManager
91{
92public:
97 static void Update(plTime timeDifference); // [tested]
98
100 static void SetInputSlotDisplayName(plStringView sInputSlot, plStringView sDefaultDisplayName); // [tested]
101
103 static plStringView GetInputSlotDisplayName(plStringView sInputSlot); // [tested]
104
110 static plStringView GetInputSlotDisplayName(plStringView sInputSet, plStringView sAction, plInt32 iTrigger = -1);
111
114 static void SetInputSlotDeadZone(plStringView sInputSlot, float fDeadZone); // [tested]
115
117 static float GetInputSlotDeadZone(plStringView sInputSlot); // [tested]
118
120 static plBitflags<plInputSlotFlags> GetInputSlotFlags(plStringView sInputSlot); // [tested]
121
126 static plKeyState::Enum GetInputSlotState(plStringView sInputSlot, float* pValue = nullptr); // [tested]
127
129 static void RetrieveAllKnownInputSlots(plDynamicArray<plStringView>& out_inputSlots);
130
136 static plUInt32 RetrieveLastCharacter(bool bResetCurrent = true); // [tested]
137
146 static void PollHardware();
147
151 static void ClearInputMapping(plStringView sInputSet, plStringView sInputSlot); // [tested]
152
178 static void SetInputActionConfig(plStringView sInputSet, plStringView sAction, const plInputActionConfig& config, bool bClearPreviousInputMappings); // [tested]
179
182 static plInputActionConfig GetInputActionConfig(plStringView sInputSet, plStringView sAction); // [tested]
183
187 static void RemoveInputAction(plStringView sInputSet, plStringView sAction); // [tested]
188
193 static plKeyState::Enum GetInputActionState(plStringView sInputSet, plStringView sAction, float* pValue = nullptr, plInt8* pTriggeredSlot = nullptr); // [tested]
194
196 static void SetActionDisplayName(plStringView sAction, plStringView sDisplayName); // [tested]
197
199 static const plString GetActionDisplayName(plStringView sAction); // [tested]
200
202 static void GetAllInputSets(plDynamicArray<plString>& out_inputSetNames); // [tested]
203
205 static void GetAllInputActions(plStringView sInputSetName, plDynamicArray<plString>& out_inputActions); // [tested]
206
210 static void SetExclusiveInputSet(plStringView sExclusiveSet) { s_sExclusiveInputSet = sExclusiveSet; }
211
213 static plStringView GetExclusiveInputSet() { return s_sExclusiveInputSet; }
214
224 static void InjectInputSlotValue(plStringView sInputSlot, float fValue); // [tested]
225
230 static plStringView GetPressedInputSlot(plInputSlotFlags::Enum mustHaveFlags, plInputSlotFlags::Enum mustNotHaveFlags); // [tested]
231
233 static plStringView ConvertScanCodeToEngineName(plUInt8 uiScanCode, bool bIsExtendedKey);
234
236 static plStringView GetInputSlotTouchPoint(plUInt32 uiIndex);
237
239 static plStringView GetInputSlotTouchPointPositionX(plUInt32 uiIndex);
240
242 static plStringView GetInputSlotTouchPointPositionY(plUInt32 uiIndex);
243
246 {
252
253 EventType m_EventType = InputSlotChanged;
254 plStringView m_sInputSlot;
255 plStringView m_sInputSet;
256 plStringView m_sInputAction;
257 };
258
259 using plEventInput = plEvent<const InputEventData&>;
260
262 static plEventSubscriptionID AddEventHandler(plEventInput::Handler handler) { return s_InputEvents.AddEventHandler(handler); }
263
265 static void RemoveEventHandler(plEventInput::Handler handler) { s_InputEvents.RemoveEventHandler(handler); }
266 static void RemoveEventHandler(plEventSubscriptionID id) { s_InputEvents.RemoveEventHandler(id); }
267
268private:
269 friend class plInputDevice;
270
275 static void RegisterInputSlot(plStringView sName, plStringView sDefaultDisplayName, plBitflags<plInputSlotFlags> SlotFlags);
276
277private:
279 struct plInputSlot
280 {
281 plInputSlot();
282
283 plString m_sDisplayName;
284 float m_fValue;
285 float m_fValueOld = 0.0f;
286 plKeyState::Enum m_State;
287 float m_fDeadZone;
288 plBitflags<plInputSlotFlags> m_SlotFlags;
289 };
290
292 struct plActionData
293 {
294 plActionData();
295
296 plInputActionConfig m_Config;
297
298 float m_fValue;
299 plKeyState::Enum m_State;
300
301 plInt8 m_iTriggeredViaAlternative;
302 };
303
305 using plInputSetMap = plMap<plString, plActionMap>;
306 using plInputSlotsMap = plMap<plString, plInputSlot>;
307
309 struct InternalData
310 {
311 plInputSetMap s_ActionMapping;
312 plInputSlotsMap s_InputSlots;
313 plMap<plString, plString> s_ActionDisplayNames;
314 plMap<plString, float> s_InjectedInputSlots;
315 };
316
318 static plUInt32 s_uiLastCharacter;
319
320 static bool s_bInputSlotResetRequired;
321
323 static plString s_sExclusiveInputSet;
324
326 static void ResetInputSlotValues();
327
329 static void GatherDeviceInputSlotValues();
330
332 static void UpdateInputSlotStates();
333
335 static void UpdateInputActions(plTime tTimeDifference);
336
338 static void UpdateInputActions(plStringView sInputSet, plActionMap& Actions, plTime tTimeDifference);
339
343 static plActionMap::Iterator GetBestAction(plActionMap& Actions, const plString& sSlot, const plActionMap::Iterator& itFirst);
344
345private:
346 PL_MAKE_SUBSYSTEM_STARTUP_FRIEND(Core, InputManager);
347
348 static void DeallocateInternals();
349 static InternalData& GetInternals();
350
351 static plEventInput s_InputEvents;
352
353 static InternalData* s_pData;
354};
Defines the structure of how actions are organized in a particular context.
Definition ActionMap.h:102
Definition DynamicArray.h:81
Definition Event.h:177
The base class for all input device types.
Definition InputDevice.h:41
The central class to set up and query the state of all input.
Definition InputManager.h:91
static void SetExclusiveInputSet(plStringView sExclusiveSet)
This can be used to pass input exclusively to this input set and no others.
Definition InputManager.h:210
static plStringView GetExclusiveInputSet()
Returns whether any input set gets input exclusively.
Definition InputManager.h:213
static void RemoveEventHandler(plEventInput::Handler handler)
Removes a previously added event handler.
Definition InputManager.h:265
static plEventSubscriptionID AddEventHandler(plEventInput::Handler handler)
Adds an event handler that is called for input events.
Definition InputManager.h:262
Definition Map.h:408
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
The plBitflags class allows you to work with type-safe bitflags.
Definition Bitflags.h:82
A struct that defines how to register an input action.
Definition InputManager.h:10
float m_fFilterYMaxValue
=1; see m_sFilterByInputSlotY
Definition InputManager.h:54
bool m_bApplyTimeScaling
If this is set to true, the value of the action is scaled by the time difference since the last input...
Definition InputManager.h:34
float m_fFilterYMinValue
=0; see m_sFilterByInputSlotY
Definition InputManager.h:53
OnLeaveArea m_OnLeaveArea
=LoseFocus
Definition InputManager.h:77
float m_fFilterXMinValue
=0; see m_sFilterByInputSlotX
Definition InputManager.h:51
float m_fFilterXMaxValue
=1; see m_sFilterByInputSlotX
Definition InputManager.h:52
OnEnterArea
For Input Areas: Describes what happens when any trigger slot is already active will the input slots ...
Definition InputManager.h:70
float m_fFilteredPriority
Definition InputManager.h:56
OnEnterArea m_OnEnterArea
=ActivateImmediately
Definition InputManager.h:78
OnLeaveArea
For Input Areas: Describes what happens when an action is currently triggered, but the input slots us...
Definition InputManager.h:62
@ LoseFocus
The input action will lose focus and thus get 'deactivated' immediately. Ie. it will return plKeyStat...
Definition InputManager.h:63
@ KeepFocus
The input action will keep focus and continue to return plKeyState::Down, until all trigger slots are...
Definition InputManager.h:64
The data that is broadcast when certain events occur.
Definition InputManager.h:246
EventType
Definition InputManager.h:248
@ InputSlotChanged
An input slot has been registered or its state changed.
Definition InputManager.h:249
@ InputActionChanged
An input action has been registered or its state changed.
Definition InputManager.h:250
Enum
Definition Declarations.h:37
Enum
Definition Declarations.h:12
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12