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
ToyMaker::ActionContext Class Reference

A container for actions that make sense within a given context. More...

#include <input_system.hpp>

Public Member Functions

 ActionContext (InputManager &inputManager, const ContextName &name)
 Construct a new action context.
 
 ActionContext (InputManager &&inputManager, const ContextName &name)=delete
 Construct a new action context.
 
::std::vector<::std::pair< ActionDefinition, ActionData > > getTriggeredActions ()
 Returns a list of triggered actions following input mapping in this context.
 
void registerAction (const ActionName &name, InputAttributesType attributes)
 Creates an action and specifies its attributes.
 
void registerAction (const nlohmann::json &actionParameters)
 Creates an action and specifies its attributes based on its JSON description.
 
void unregisterAction (const ActionName &name)
 Removes an action from this context.
 
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.
 
void registerInputBind (const nlohmann::json &inputBindParameters)
 Register a binding from an input-sign-axis-modifiers combination to a specific axis of an action.
 
void unregisterInputBind (const InputCombo &inputCombo)
 Remove the binding from this input-sign-axis-modifier combination to whatever action it's bound to.
 
void unregisterInputBinds (const ActionName &forAction)
 Removes all input binds associated with a particular action.
 
void unregisterInputBinds ()
 Removes all input combo -> action-axis bindings.
 
bool propagateAllowed ()
 Checks whether this context allows propagation to lower priority contexts.
 
void setPropagateAllowed (bool allowPropagate)
 Enables or disables input propagation to lower priority contexts.
 
bool enabled ()
 Checks whether this context is active and able to process input events.
 
void setEnabled (bool enable)
 Enable or disable this context, allowing it to or preventing it from receiving input events.
 

Static Public Member Functions

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.
 

Private Member Functions

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.
 
void resetActionData (uint32_t timestamp)
 Sets all action data to 0.f or false for ALL actions, and queues related RESET actions.
 
void mapToAction (const UnmappedInputValue &inputValue, const InputCombo &inputCombo)
 Maps the given input value to its assigned action state.
 

Private Attributes

InputManagermInputManager
 
const ContextName mName
 The name of this action context.
 
bool mEnabled { true }
 Determines whether this action context is active and allowed to process any bound input events.
 
bool mPropagateInput { false }
 Determines whether, after mapping an input event to its corresponding action, other contexts waiting for the input event are allowed to have a go at processing it also.
 
::std::unordered_map< ActionDefinition, ActionDatamActions {}
 All actions defined for this context and their most recently triggered state.
 
::std::vector<::std::pair< ActionDefinition, ActionData > > mPendingTriggeredActions {}
 Action state changes that have recently been triggered, in the order that they were triggered.
 
::std::unordered_map< ActionDefinition, ::std::set< InputCombo > > mActionToInputBinds {}
 All input bindings associated with a specific action.
 
::std::unordered_map< InputCombo, ::std::pair< AxisFilter, ActionDefinition > > mInputBindToAction {}
 Mapping from unmapped input controls, provided by the input manager, to their associated action definitions.
 

Friends

class InputManager
 

Detailed Description

A container for actions that make sense within a given context.

Different contexts might have different requirements, even with the same set of inputs. For example, it might make sense to have a "slash" action in the game world, but in the context of a game menu, slash cannot have meaning.

Action Contexts turn those requirements into a container object, where the semantics of an input event are indicated by the action and context, and are one degree removed from the inputs themselves.

This can be useful, for example, when a game requires input for character movement across multiple platforms. For a console, or when a controller is present, it would make sense to query the value of the left analog stick. However, no such control exists on a keyboard, which is the most common input device for a PC.

Action Contexts, among other things, allow it so that multiple mappings to the same type of input are possible, and the handlers of the input don't have to reason about differences in platforms. In the example above, that mapping might look something like this:

W ______
A|S|D \___.--> Character : Move (2 Axes) --> (Move handlers)
<keyboard> /
/
/
(( L ))----/
<controller>

This allows developers to reason about input somewhat uniformly during game development. The input mappings themselves are a matter of wiring inputs to high level actions during configuration, separate from game logic.

In this case, all a game programmer need know is that they want signed non-location state input on 2 axes, and that they want it for "Move".

Constructor & Destructor Documentation

◆ ActionContext() [1/2]

ToyMaker::ActionContext::ActionContext ( InputManager & inputManager,
const ContextName & name )
inline

Construct a new action context.

Parameters
inputManagerThe input manager in charge of this context.
nameThe name of the context.

◆ ActionContext() [2/2]

ToyMaker::ActionContext::ActionContext ( InputManager && inputManager,
const ContextName & name )
delete

Construct a new action context.

Parameters
inputManagerThe input manager in charge of this context.
nameThe name of the context.

Member Function Documentation

◆ ApplyInput()

ActionData ActionContext::ApplyInput ( const ActionDefinition & actionDefinition,
const ActionData & actionData,
const AxisFilter targetAxis,
const UnmappedInputValue & inputValue )
static

Returns the result of applying an unmapped input combo value to its target action-axis.

Parameters
actionDefinitionAction definition of target action (but usually just a context-action name string pair).
actionDataThe previous value of the data associated with the action.
targetAxisThe axis of the action affected.
inputValueThe value associated with the input combo being applied.
Returns
ActionData The action data after the input has been applied.
See also
InputCombo

◆ getTriggeredActions()

std::vector< std::pair< ActionDefinition, ActionData > > ActionContext::getTriggeredActions ( )

Returns a list of triggered actions following input mapping in this context.

Returns
std::vector<::std::pair<ActionDefinition, ActionData>> List of triggered actions after inputs are applied.

◆ mapToAction()

void ActionContext::mapToAction ( const UnmappedInputValue & inputValue,
const InputCombo & inputCombo )
private

Maps the given input value to its assigned action state.

Parameters
inputValueThe value of the input combo to be mapped to an axis of an action belonging to this action's context.
inputComboThe combo whose value is being mapped.

◆ propagateAllowed()

bool ToyMaker::ActionContext::propagateAllowed ( )
inline

Checks whether this context allows propagation to lower priority contexts.

Return values
trueAllows propagation to lower priority contexts;
falseDisallows propagation to lower priority contexts;

◆ registerAction() [1/2]

void ActionContext::registerAction ( const ActionName & name,
InputAttributesType attributes )

Creates an action and specifies its attributes.

Parameters
nameThe name of the action.
attributesThe attributes of the action (no. of axes, state or change, etc.)

◆ registerAction() [2/2]

void ActionContext::registerAction ( const nlohmann::json & actionParameters)

Creates an action and specifies its attributes based on its JSON description.

Parameters
actionParametersThe action's description in JSON.
See also
InputManager::registerAction()

◆ registerInputBind() [1/2]

void ActionContext::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.

Parameters
forActionThe action being mapped to.
targetAxisThe axis of the action affected.
withInputThe combo responsible for changing the action's value.
See also
InputManager::registerInputBind()

◆ registerInputBind() [2/2]

void ActionContext::registerInputBind ( const nlohmann::json & inputBindParameters)

Register a binding from an input-sign-axis-modifiers combination to a specific axis of an action.

Parameters
inputBindParametersA JSON description of the input bind.

◆ resetActionData() [1/2]

void ActionContext::resetActionData ( const ActionName & forAction,
uint32_t timestamp )
private

Sets action data for this action to 0.f or false, and queues a corresponding RESET action.

Parameters
forActionThe action whose vlaue is being reset.
timestampThe time of the reset, in milliseconds since application start.

◆ resetActionData() [2/2]

void ActionContext::resetActionData ( uint32_t timestamp)
private

Sets all action data to 0.f or false for ALL actions, and queues related RESET actions.

Parameters
timestampThe time of the reset, in milliseconds since application start.

◆ setPropagateAllowed()

void ToyMaker::ActionContext::setPropagateAllowed ( bool allowPropagate)
inline

Enables or disables input propagation to lower priority contexts.

Parameters
allowPropagateWhether propagation is permitted (for handled actions)

◆ unregisterAction()

void ActionContext::unregisterAction ( const ActionName & name)

Removes an action from this context.

Parameters
nameThe name of the action being removed.

◆ unregisterInputBind()

void ActionContext::unregisterInputBind ( const InputCombo & inputCombo)

Remove the binding from this input-sign-axis-modifier combination to whatever action it's bound to.

Parameters
inputComboThe combo of the input bind being removed.

◆ unregisterInputBinds()

void ActionContext::unregisterInputBinds ( const ActionName & forAction)

Removes all input binds associated with a particular action.

Parameters
forActionThe action whose input binds are being removed.

The documentation for this class was generated from the following files: