Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
input_system.hpp
Go to the documentation of this file.
1
15
16#ifndef FOOLSENGINE_INPUTSYSTEM_H
17#define FOOLSENGINE_INPUTSYSTEM_H
18
19#include <string>
20#include <map>
21#include <queue>
22#include <memory>
23
24#include <nlohmann/json.hpp>
25#include <SDL2/SDL.h>
26
27#include "input_data.hpp"
28
29namespace ToyMaker {
30 class ActionContext;
31 class ActionDispatch;
32 class InputManager;
33
42 public:
47 enum ActionContextPriority: uint8_t {
48 VERY_LOW=0,
49 LOW=1,
50 DEFAULT=2,
51 HIGH=3,
52 VERY_HIGH=4,
53 TOTAL=5,
54 };
55
62 void queueInput(const SDL_Event& inputEvent);
63
125 void registerInputBind(const nlohmann::json& inputBindingParameters);
126
153 void registerAction(const nlohmann::json& actionParameters);
154
165 void registerActionContext(const ::std::string& name, ActionContextPriority priority=DEFAULT);
166
171 void unregisterActionContext(const ::std::string& name);
172
182 void loadInputConfiguration(const nlohmann::json& inputConfiguration);
183
190 ActionContext& operator[] (const ::std::string& actionContext);
191
196 ::std::vector<::std::pair<ActionDefinition, ActionData>> getTriggeredActions(uint32_t targetTimeMillis);
197
198 private:
199
210 double getRawValue(const InputFilter& inputFilter, const SDL_Event& inputEvent) const;
211
215 void registerInputCombo(const ::std::string& actionContext, const InputCombo& inputCombo);
216
220 void unregisterInputCombo(const ::std::string& actionContext, const InputCombo& inputCombo);
221
225 void unregisterInputCombos(const ::std::string& actionContext);
226
231
235 ::std::unordered_map<ContextName, ::std::pair<ActionContext, ActionContextPriority>> mActionContexts {};
236
241 ::std::unordered_map<InputFilter, double> mRawInputState {};
242
247 ::std::unordered_map<InputFilter, ::std::set<InputCombo>> mInputFilterToCombos {};
248
252 ::std::unordered_map<InputCombo, ::std::array<
253 ::std::set<ContextName>, ActionContextPriority::TOTAL
255
263 ::std::unordered_map<InputCombo, UnmappedInputValue> mInputComboStates {};
264
269 ::std::queue<::std::pair<InputCombo, UnmappedInputValue>> mUnmappedInputs {};
270
276 float mModifierThreshold { .7f };
277
278 friend class ActionContext;
279 };
280
302 public:
303 private:
311 virtual bool handleAction(const ActionData& actionData, const ActionDefinition& actionDefinition) {
312 (void)actionData; // prevent unused parameter warnings
313 (void)actionDefinition; // prevent unused parameter warnings
314 return false;
315 };
316
317 friend class ActionDispatch;
318 };
319
348 public:
355 ActionContext(InputManager& inputManager, const ContextName& name): mInputManager{inputManager}, mName {name} {}
356
363 ActionContext(InputManager&& inputManager, const ContextName& name) = delete;
364
376 static ActionData ApplyInput(const ActionDefinition& actionDefinition, const ActionData& actionData, const AxisFilter targetAxis, const UnmappedInputValue& inputValue);
377
383 ::std::vector<::std::pair<ActionDefinition, ActionData>> getTriggeredActions();
384
391 void registerAction(const ActionName& name, InputAttributesType attributes);
392
400 void registerAction(const nlohmann::json& actionParameters);
401
407 void unregisterAction(const ActionName& name);
408
418 void registerInputBind(const ActionName& forAction, AxisFilter targetAxis, const InputCombo& withInput);
419
425 void registerInputBind(const nlohmann::json& inputBindParameters);
426
432 void unregisterInputBind(const InputCombo& inputCombo);
433
439 void unregisterInputBinds(const ActionName& forAction);
440
446
453 inline bool propagateAllowed() { return mPropagateInput; };
454
455 // Enable or disable input propagation to lower priority contexts
461 inline void setPropagateAllowed(bool allowPropagate) { mPropagateInput = allowPropagate; }
462
467 inline bool enabled() { return mEnabled; }
468
472 inline void setEnabled(bool enable) { mEnabled = enable; }
473
474 private:
481 void resetActionData(const ActionName& forAction, uint32_t timestamp);
482
488 void resetActionData(uint32_t timestamp);
489
496 void mapToAction(const UnmappedInputValue& inputValue, const InputCombo& inputCombo);
497
498 /*
499 * @brief A reference to the input manager that created this
500 * action context.
501 *
502 * (apparently the technical term for this is "dependency injection via constructor")
503 */
504 InputManager& mInputManager;
505
510
515 bool mEnabled { true };
516
521 bool mPropagateInput { false };
522
526 ::std::unordered_map<ActionDefinition, ActionData> mActions {};
527
531 ::std::vector<::std::pair<ActionDefinition, ActionData>> mPendingTriggeredActions {};
532
536 ::std::unordered_map<ActionDefinition, ::std::set<InputCombo>> mActionToInputBinds {};
537
542 ::std::unordered_map<InputCombo, ::std::pair<AxisFilter, ActionDefinition>> mInputBindToAction {};
543
544 friend class InputManager;
545 };
546
587 public:
594 void registerActionHandler(const QualifiedActionName& contextActionPair, ::std::weak_ptr<IActionHandler> actionHandler);
595
602 void unregisterActionHandler(const QualifiedActionName& contextActionPair, ::std::weak_ptr<IActionHandler> actionHandler);
603
609 void unregisterActionHandler(::std::weak_ptr<IActionHandler> actionHandler);
610
618 bool dispatchAction(const ::std::pair<ActionDefinition, ActionData>& pendingAction);
619
620 private:
621
626 ::std::map<QualifiedActionName, ::std::set<::std::weak_ptr<IActionHandler>, ::std::owner_less<::std::weak_ptr<IActionHandler>>>, ::std::less<QualifiedActionName>> mActionHandlers {};
627 };
628}
629
630#endif
A container for actions that make sense within a given context.
Definition input_system.hpp:347
void setEnabled(bool enable)
Enable or disable this context, allowing it to or preventing it from receiving input events.
Definition input_system.hpp:472
::std::vector<::std::pair< ActionDefinition, ActionData > > getTriggeredActions()
Returns a list of triggered actions following input mapping in this context.
Definition action_context.cpp:251
void registerInputBind(const ActionName &forAction, AxisFilter targetAxis, const InputCombo &withInput)
Register a binding from an input-sign-axis-modifiers combination to a specific axis of an action.
Definition action_context.cpp:143
void unregisterInputBinds()
Removes all input combo -> action-axis bindings.
Definition action_context.cpp:192
bool enabled()
Checks whether this context is active and able to process input events.
Definition input_system.hpp:467
void unregisterAction(const ActionName &name)
Removes an action from this context.
Definition action_context.cpp:127
::std::unordered_map< InputCombo, ::std::pair< AxisFilter, ActionDefinition > > mInputBindToAction
Mapping from unmapped input controls, provided by the input manager, to their associated action defin...
Definition input_system.hpp:542
ActionContext(InputManager &inputManager, const ContextName &name)
Construct a new action context.
Definition input_system.hpp:355
static ActionData ApplyInput(const ActionDefinition &actionDefinition, const ActionData &actionData, const AxisFilter targetAxis, const UnmappedInputValue &inputValue)
Returns the result of applying an unmapped input combo value to its target action-axis.
Definition action_context.cpp:7
void mapToAction(const UnmappedInputValue &inputValue, const InputCombo &inputCombo)
Maps the given input value to its assigned action state.
Definition action_context.cpp:216
bool mPropagateInput
Determines whether, after mapping an input event to its corresponding action, other contexts waiting ...
Definition input_system.hpp:521
ActionContext(InputManager &&inputManager, const ContextName &name)=delete
Construct a new action context.
void setPropagateAllowed(bool allowPropagate)
Enables or disables input propagation to lower priority contexts.
Definition input_system.hpp:461
void resetActionData(const ActionName &forAction, uint32_t timestamp)
Sets action data for this action to 0.f or false, and queues a corresponding RESET action.
Definition action_context.cpp:198
::std::unordered_map< ActionDefinition, ActionData > mActions
All actions defined for this context and their most recently triggered state.
Definition input_system.hpp:526
bool propagateAllowed()
Checks whether this context allows propagation to lower priority contexts.
Definition input_system.hpp:453
const ContextName mName
The name of this action context.
Definition input_system.hpp:509
bool mEnabled
Determines whether this action context is active and allowed to process any bound input events.
Definition input_system.hpp:515
void registerAction(const ActionName &name, InputAttributesType attributes)
Creates an action and specifies its attributes.
Definition action_context.cpp:102
::std::unordered_map< ActionDefinition, ::std::set< InputCombo > > mActionToInputBinds
All input bindings associated with a specific action.
Definition input_system.hpp:536
::std::vector<::std::pair< ActionDefinition, ActionData > > mPendingTriggeredActions
Action state changes that have recently been triggered, in the order that they were triggered.
Definition input_system.hpp:531
void unregisterInputBind(const InputCombo &inputCombo)
Remove the binding from this input-sign-axis-modifier combination to whatever action it's bound to.
Definition action_context.cpp:170
An object responsible for tracking action listeners for a given project.
Definition input_system.hpp:586
void unregisterActionHandler(::std::weak_ptr< IActionHandler > actionHandler)
Removes an action handler from all its subscribed actions.
::std::map< QualifiedActionName, ::std::set<::std::weak_ptr< IActionHandler >, ::std::owner_less<::std::weak_ptr< IActionHandler > > >, ::std::less< QualifiedActionName > > mActionHandlers
Pointers to all action handler instances waiting for a particular action.
Definition input_system.hpp:626
bool dispatchAction(const ::std::pair< ActionDefinition, ActionData > &pendingAction)
Sends data for an action to all of that action's registered handlers.
Definition action_context.cpp:269
void unregisterActionHandler(const QualifiedActionName &contextActionPair, ::std::weak_ptr< IActionHandler > actionHandler)
Removes a handler for a particular action.
void registerActionHandler(const QualifiedActionName &contextActionPair, ::std::weak_ptr< IActionHandler > actionHandler)
Registers a handler for an action.
Definition action_context.cpp:257
Class interface for systems that wish to be notified when action events occur in an action context.
Definition input_system.hpp:301
virtual bool handleAction(const ActionData &actionData, const ActionDefinition &actionDefinition)
The action handling function in any class that implements this interface.
Definition input_system.hpp:311
The class that acts as the main interface between the rest of the project and the input system.
Definition input_system.hpp:41
::std::unordered_map< InputFilter, ::std::set< InputCombo > > mInputFilterToCombos
All active input combinations associated with a given input.
Definition input_system.hpp:247
void unregisterInputCombo(const ::std::string &actionContext, const InputCombo &inputCombo)
Remove entry for a specific input within an action context.
Definition input_manager.cpp:724
void queueInput(const SDL_Event &inputEvent)
Maps an event to its internal representation, if one is available.
Definition input_manager.cpp:469
void unregisterInputCombos(const ::std::string &actionContext)
::std::vector<::std::pair< ActionDefinition, ActionData > > getTriggeredActions(uint32_t targetTimeMillis)
Dispatches all mapped inputs received before the target time to action contexts that can handle them.
Definition input_manager.cpp:631
void loadInputConfiguration(const nlohmann::json &inputConfiguration)
Loads a full input configuration based on its JSON description.
Definition input_manager.cpp:571
::std::queue<::std::pair< InputCombo, UnmappedInputValue > > mUnmappedInputs
Queue of input state changes, to be consumed by whichever action contexts require them.
Definition input_system.hpp:269
void registerAction(const nlohmann::json &actionParameters)
Registers an action definition against a defined action context.
Definition input_manager.cpp:596
::std::unordered_map< ContextName, ::std::pair< ActionContext, ActionContextPriority > > mActionContexts
All action context names -> contexts.
Definition input_system.hpp:235
void unregisterInputCombos()
Definition input_manager.cpp:843
void unregisterActionContext(const ::std::string &name)
Removes the action context associated with this name.
Definition input_manager.cpp:616
ActionContextPriority
The priority associated with an action context, which as yet has no real bearing on their evaluation.
Definition input_system.hpp:47
::std::unordered_map< InputCombo, ::std::array< ::std::set< ContextName >, ActionContextPriority::TOTAL > > mInputComboToActionContexts
All action contexts associated with a given input combination, organized by priority.
Definition input_system.hpp:254
void registerInputCombo(const ::std::string &actionContext, const InputCombo &inputCombo)
Register a listener for a certain input combination on behalf of actionContext.
Definition input_manager.cpp:676
ActionContext & operator[](const ::std::string &actionContext)
Retrieves an action context based on its name.
Definition input_manager.cpp:463
void registerInputBind(const nlohmann::json &inputBindingParameters)
The full description of an input binding, to be tracked and signaled by the InputManager.
Definition input_manager.cpp:592
::std::unordered_map< InputCombo, UnmappedInputValue > mInputComboStates
Input combination values, up to the most recently fired input trigger.
Definition input_system.hpp:263
void registerActionContext(const ::std::string &name, ActionContextPriority priority=DEFAULT)
Registers a new action context with a given name.
Definition input_manager.cpp:600
::std::unordered_map< InputFilter, double > mRawInputState
The current, raw state of the control+axis associated with each input filter, each between 0....
Definition input_system.hpp:241
float mModifierThreshold
Button threshold for axes or buttons that map to modifiers, beyond which those modifiers are consider...
Definition input_system.hpp:276
double getRawValue(const InputFilter &inputFilter, const SDL_Event &inputEvent) const
Get the value associated with a single InputFilter, a number between 0 and 1.
Definition input_manager.cpp:94
::std::pair< ContextName, ActionName > QualifiedActionName
An identifier that fully names one action present in the project.
Definition input_data.hpp:503
::std::string ContextName
The name of a context which contains definitions for actions that are valid within it.
Definition input_data.hpp:44
AxisFilter
Enumeration of all possible axis filter values.
Definition input_data.hpp:241
::std::string ActionName
The name of an action whose meaning is known within a specific context.
Definition input_data.hpp:37
A collection of types used by the input system and any entities that have to interact with it.
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
The definition of a single action, including whether it represents state or change,...
Definition input_data.hpp:511
A class that, perhaps just as unnecessarily, stores a value of InputAttributesValueType.
Definition input_data.hpp:111
An input combo that whose value is recorded and mapped to an (axis of an) action value of some kind.
Definition input_data.hpp:346
Filter that uniquely defines ONE axis of one control of one input belonging to one device.
Definition input_data.hpp:274
An input state that hasn't yet been mapped to its corresponding action.
Definition input_data.hpp:462
A union that may contain any one of SimpleActionData, OneAxisActionData, TwoAxisActionData,...
Definition input_data.hpp:721