Nebula
Loading...
Searching...
No Matches
Input::ActionMap Class Reference

#include <actionmap.h>

Detailed Description

An action is a name for something the game asks for, such as "Jump".

A binding is one control that can answer for it: a key with its modifiers, a mouse button, a game pad button, a game pad axis, or the mouse movement and wheel. Code asks for the action by name and does not care which control answered.

Two files:

  • The definitions file holds the actions and their default bindings. It is part of the project and is never written.
  • The user file holds what one player changed. Save() writes it.

Load() reads the definitions and then the user file over them. A player file only needs the actions the player changed.

Save() writes every action as it is now. It does not write a list of changes, so the player file can be read on its own.

The default paths are proj:work/keymap.json and user:keymap.json. The user path is the player's own directory, because a shipped build cannot write to its install directory.

The file, as Save() writes it and as Load() reads it:

{ "version": 1, "players": [ { "player": 0, "devices": [ { "device": "keyboardmouse" }, { "device": "gamepad", "deviceIndex": 0 } ] }, { "player": 1, "devices": [ { "device": "gamepad", "deviceIndex": 1 } ] } ], "actions": { "Jump": { "kind": "digital", "bindings": [ { "source": "key", "code": "Space", "mods": "", "scale": 1.0 }, { "source": "key", "code": "E", "mods": "ctrl", "scale": 1.0 }, { "source": "gamepadButton", "code": "AButton", "scale": 1.0 } ] }, "MoveForward": { "kind": "analog", "deadzone": 0.15, "sensitivity": 1.0, "bindings": [ { "source": "key", "code": "W", "scale": 1.0 }, { "source": "gamepadAxis", "code": "LeftThumbYAxis", "scale": -1.0 }, { "source": "mouseWheel", "code": "wheel", "scale": 1.0 } ] } } }

A binding can leave out kind, deadzone, sensitivity, mods, scale and a player's deviceIndex. code is required, because it names the control. Each source names it its own way: Key names a key, MouseButton names a mouse button, GamePadBase names a pad button or axis, and the mouse uses x, y or wheel.

A player's devices are a list. deviceIndex is the game pad slot that device reads, and it is the player number when the file leaves it out. The keyboard and mouse always use 0, because the input server has one of each. A player with one device can also write it as a single device name.

Defaults: kind is digital, deadzone is 0.15 for an analog action and 0 for a digital one, sensitivity and scale are 1.0, and deviceIndex is the player number. A digital action cannot use mouseMove or mouseWheel, because those only answer with a value.

An action id is the index of the action in the map, and the reads take that id. The ids follow the order the actions were added in. A reload changes bindings but never the order, so an id you keep is still the same action. A name the map does not have answers with InvalidAction, and a read on an invalid id answers with nothing.

The state is one entry per action per player. A player is a slot with one or more devices on it. A binding only answers for a player whose slot has that device, so a pad binding does not answer for a player who only has the keyboard. A player can hold both, which is the usual case for a single character. Every player reads the same actions.

The state is built once per frame, after the window events have been pumped. The input server polls the devices and then calls Evaluate(). A read before that point sees the frame being built.

An edge is the frame a control changed. The map uses the device's own edge flags when it has them. The keyboard keeps a key's pressed state for one frame after the release event, so the release edge lands in the frame of the event and the held state clears in the frame after. A source with no edge flags, such as an axis, answers with the change of its value.

Inherits Core::RefCounted.

Public Member Functions

 ActionMap ()
 constructor
virtual ~ActionMap ()
 destructor
void Open ()
 Open the map. This allocates the state and the tables.
void Close ()
 close the map
bool IsOpen () const
 return true if open
void SetDefaultPath (const Util::String &path)
 The two files.
const Util::String & GetDefaultPath () const
void SetUserPath (const Util::String &path)
const Util::String & GetUserPath () const
ActionId Register (const Util::String &name, ActionKind kind, float deadzone=0.0f)
 Add an action.
void SetAction (ActionId action, ActionKind kind, float deadzone)
 Change an action's kind and deadzone. The reads use both from the next frame.
bool RenameAction (ActionId action, const Util::String &name)
 Change an action's name.
bool RemoveAction (ActionId action)
 Drop an action.
bool IsRemoved (ActionId action) const
 true when the action was removed. Its id is still a slot in the map, but nothing answers for it
void AddBinding (ActionId action, SourceType source, IndexT code, Input::Modifier::Code mods, float scale=1.0f)
 Add a binding. A rebinding screen calls this after ClearBindings.
bool SetBinding (ActionId action, IndexT bindingIndex, SourceType source, IndexT code, Input::Modifier::Code mods, float scale)
 Put a control on a binding the action already has. Returns false when the index is not one in use.
bool RemoveBinding (ActionId action, IndexT bindingIndex)
 Drop one binding. The bindings after it move up, so their indexes change.
void ClearBindings (ActionId action)
 Drop every binding an action has, so the next ones added are the only ones.
ActionId FindAction (const Util::String &name) const
 the action a name refers to, or InvalidAction when the map does not have it
SizeT GetNumActions () const
 the number of slots the map has, including the ones a removed action left
const ActionEntry & GetAction (ActionId action) const
 the action and one of its bindings, by index
const ActionBinding & GetBinding (ActionId action, IndexT bindingIndex) const
IndexT GetBindingCount (ActionId action) const
 how many bindings an action has
void SetPlayerDevice (IndexT playerIndex, DeviceType device, IndexT deviceIndex)
 Put one device on a player slot and drop the devices it had.
bool AddPlayerDevice (IndexT playerIndex, DeviceType device, IndexT deviceIndex)
 Add a device to a player slot without removing the devices it already has.
DeviceType GetPlayerDevice (IndexT playerIndex) const
 the first device on a slot, which is the device the player is reported as
bool PlayerHasDevice (IndexT playerIndex, DeviceType device) const
 true when a player can answer from this device
IndexT GetPlayerDeviceIndex (IndexT playerIndex, DeviceType device) const
 the slot a player reads a device from, or InvalidIndex when the player does not have that device
bool Load ()
 Read the definitions and then the user file over them. Returns false when neither file had anything.
bool LoadFrom (const Util::String &path)
 Read one file over the map as it is now, without reading the other path.
bool Save ()
 Write every action as it is now, to the user file.
bool SaveTo (const Util::String &path)
 Write every action as it is now, to a file.
void Evaluate ()
 build this frame's state. Called by the input server after the events have been pumped
const ActionState & GetState (ActionId action, IndexT playerIndex) const
 the state for one action and one player
bool IsHeld (ActionId action, IndexT playerIndex) const
bool IsPressed (ActionId action, IndexT playerIndex) const
bool IsReleased (ActionId action, IndexT playerIndex) const
float GetValue (ActionId action, IndexT playerIndex) const
 the analog value of one action. A stick or a trigger answers with this.
float GetAxis (ActionId negativeAction, ActionId positiveAction, IndexT playerIndex) const
 two actions as one axis: the positive action's value minus the negative one's
Math::vec2 GetAxis2 (ActionId negativeX, ActionId positiveX, ActionId negativeY, ActionId positiveY, IndexT playerIndex) const
 two axes as a pair
IndexT GetLastActionPlayer (ActionId action) const
 the player that last triggered an action, or InvalidIndex when none has
Public Member Functions inherited from Core::RefCounted
 RefCounted ()
 constructor
int GetRefCount () const
 get the current refcount
void AddRef ()
 increment refcount by one
void Release ()
 decrement refcount and destroy object if refcount is zero
bool IsInstanceOf (const Rtti &rtti) const
 return true if this object is instance of given class
bool IsInstanceOf (const Util::String &className) const
 return true if this object is instance of given class by string
bool IsInstanceOf (const Util::FourCC &classFourCC) const
 return true if this object is instance of given class by fourcc
bool IsA (const Rtti &rtti) const
 return true if this object is instance of given class, or a derived class
bool IsA (const Util::String &rttiName) const
 return true if this object is instance of given class, or a derived class, by string
bool IsA (const Util::FourCC &rttiFourCC) const
 return true if this object is instance of given class, or a derived class, by fourcc
const Util::String & GetClassName () const
 get the class name
Util::FourCC GetClassFourCC () const
 get the class FourCC code

Static Public Member Functions

static Util::String SourceToString (SourceType source)
 The names the file writes a control with. The editor's keymap modal draws a binding with these.
static Util::String KindToString (ActionKind kind)
static Util::String CodeToString (SourceType source, IndexT code)
 A binding's code as the name the file writes it with.
Static Public Member Functions inherited from Core::RefCounted
static void DumpRefCountingLeaks ()
 dump refcounting leaks, call at end of application (NEBULA_DEBUG builds only!)

Private Member Functions

 __DeclareClass (ActionMap)
 __DeclareInterfaceSingleton (ActionMap)
float ReadBinding (const ActionEntry &action, const ActionBinding &binding, IndexT playerIndex, const Ptr< Input::Keyboard > &keyboard, const Ptr< Input::Mouse > &mouse, const Ptr< Input::GamePad > &gamePad, bool &outDown, bool &outUp) const
 what one binding answers for one player this frame, and the edge the device reported for it
IndexT ReadFile (const Util::String &path)
 Read one file and put it over the map as it is now. Returns the number of actions it applied.
void RebuildIndex ()
 Build the name to id lookup from the actions that are still there.

Static Private Member Functions

static SourceType SourceFromString (const Util::String &str)
static DeviceType DeviceFromString (const Util::String &str)
static Util::String DeviceToString (DeviceType device)
static ActionKind KindFromString (const Util::String &str)
static IndexT CodeFromString (SourceType source, const Util::String &str)

Private Attributes

bool isOpen
Util::String defaultPath
Util::String userPath
Util::Array< ActionEntry > actions
Util::FixedArray< ActionBinding > bindings
Util::FixedArray< ActionState > states
Util::FixedArray< ActionPrev > prev
Util::FixedArray< IndexT > lastPlayer
Util::FixedArray< PlayerSlot > players
Util::Dictionary< Util::StringAtom, ActionId > actionIndex

Additional Inherited Members

Protected Member Functions inherited from Core::RefCounted
virtual ~RefCounted ()
 destructor (called when refcount reaches zero)

Constructor & Destructor Documentation

◆ ActionMap()

Input::ActionMap::ActionMap ( )

constructor

◆ ~ActionMap()

Input::ActionMap::~ActionMap ( )
virtual

destructor

Member Function Documentation

◆ __DeclareClass()

Input::ActionMap::__DeclareClass ( ActionMap )
private

◆ __DeclareInterfaceSingleton()

Input::ActionMap::__DeclareInterfaceSingleton ( ActionMap )
private

◆ AddBinding()

void Input::ActionMap::AddBinding ( ActionId action,
SourceType source,
IndexT code,
Input::Modifier::Code mods,
float scale = 1.0f )

Add a binding. A rebinding screen calls this after ClearBindings.

◆ AddPlayerDevice()

bool Input::ActionMap::AddPlayerDevice ( IndexT playerIndex,
DeviceType device,
IndexT deviceIndex )

Add a device to a player slot without removing the devices it already has.

Returns false when the slot is full.

◆ ClearBindings()

void Input::ActionMap::ClearBindings ( ActionId action)

Drop every binding an action has, so the next ones added are the only ones.

◆ Close()

void Input::ActionMap::Close ( )

close the map

◆ CodeFromString()

IndexT Input::ActionMap::CodeFromString ( SourceType source,
const Util::String & str )
staticprivate

◆ CodeToString()

Util::String Input::ActionMap::CodeToString ( SourceType source,
IndexT code )
static

A binding's code as the name the file writes it with.

The code comes from the code set its source uses.

◆ DeviceFromString()

DeviceType Input::ActionMap::DeviceFromString ( const Util::String & str)
staticprivate

◆ DeviceToString()

Util::String Input::ActionMap::DeviceToString ( DeviceType device)
staticprivate

◆ Evaluate()

void Input::ActionMap::Evaluate ( )

build this frame's state. Called by the input server after the events have been pumped

Build this frame's state.

The player loop is outside the action loop, because one player's device is read once and used for every action. The state uses the same stride as the reads.

◆ FindAction()

ActionId Input::ActionMap::FindAction ( const Util::String & name) const

the action a name refers to, or InvalidAction when the map does not have it

◆ GetAction()

const ActionEntry & Input::ActionMap::GetAction ( ActionId action) const

the action and one of its bindings, by index

◆ GetAxis()

float Input::ActionMap::GetAxis ( ActionId negativeAction,
ActionId positiveAction,
IndexT playerIndex ) const

two actions as one axis: the positive action's value minus the negative one's

Two actions as one axis.

A key answers with 1 and a stick with its deadzoned value, so one call works for both.

◆ GetAxis2()

Math::vec2 Input::ActionMap::GetAxis2 ( ActionId negativeX,
ActionId positiveX,
ActionId negativeY,
ActionId positiveY,
IndexT playerIndex ) const

two axes as a pair

◆ GetBinding()

const ActionBinding & Input::ActionMap::GetBinding ( ActionId action,
IndexT bindingIndex ) const

◆ GetBindingCount()

IndexT Input::ActionMap::GetBindingCount ( ActionId action) const

how many bindings an action has

◆ GetDefaultPath()

const Util::String & Input::ActionMap::GetDefaultPath ( ) const

◆ GetLastActionPlayer()

IndexT Input::ActionMap::GetLastActionPlayer ( ActionId action) const

the player that last triggered an action, or InvalidIndex when none has

◆ GetNumActions()

SizeT Input::ActionMap::GetNumActions ( ) const

the number of slots the map has, including the ones a removed action left

◆ GetPlayerDevice()

DeviceType Input::ActionMap::GetPlayerDevice ( IndexT playerIndex) const

the first device on a slot, which is the device the player is reported as

◆ GetPlayerDeviceIndex()

IndexT Input::ActionMap::GetPlayerDeviceIndex ( IndexT playerIndex,
DeviceType device ) const

the slot a player reads a device from, or InvalidIndex when the player does not have that device

◆ GetState()

const ActionState & Input::ActionMap::GetState ( ActionId action,
IndexT playerIndex ) const

the state for one action and one player

The state entry for one action and one player.

The reads below take one field of the same entry.

◆ GetUserPath()

const Util::String & Input::ActionMap::GetUserPath ( ) const

◆ GetValue()

float Input::ActionMap::GetValue ( ActionId action,
IndexT playerIndex ) const

the analog value of one action. A stick or a trigger answers with this.

◆ IsHeld()

bool Input::ActionMap::IsHeld ( ActionId action,
IndexT playerIndex ) const

◆ IsOpen()

bool Input::ActionMap::IsOpen ( ) const

return true if open

◆ IsPressed()

bool Input::ActionMap::IsPressed ( ActionId action,
IndexT playerIndex ) const

◆ IsReleased()

bool Input::ActionMap::IsReleased ( ActionId action,
IndexT playerIndex ) const

◆ IsRemoved()

bool Input::ActionMap::IsRemoved ( ActionId action) const

true when the action was removed. Its id is still a slot in the map, but nothing answers for it

◆ KindFromString()

ActionKind Input::ActionMap::KindFromString ( const Util::String & str)
staticprivate

◆ KindToString()

Util::String Input::ActionMap::KindToString ( ActionKind kind)
static

◆ Load()

bool Input::ActionMap::Load ( )

Read the definitions and then the user file over them. Returns false when neither file had anything.

Read the definitions, then the player's file over them.

An action only the definitions have keeps the bindings the definitions gave it. An action the player's file also has takes that file's bindings.

A name the map does not have answers with InvalidAction, so a caller that resolves a name must check it. A reload only adds actions at the end, so an id you keep is still the same action.

◆ LoadFrom()

bool Input::ActionMap::LoadFrom ( const Util::String & path)

Read one file over the map as it is now, without reading the other path.

The editor's keymap modal reloads the definitions with this.

◆ Open()

void Input::ActionMap::Open ( )

Open the map. This allocates the state and the tables.

◆ PlayerHasDevice()

bool Input::ActionMap::PlayerHasDevice ( IndexT playerIndex,
DeviceType device ) const

true when a player can answer from this device

◆ ReadBinding()

float Input::ActionMap::ReadBinding ( const ActionEntry & action,
const ActionBinding & binding,
IndexT playerIndex,
const Ptr< Input::Keyboard > & keyboard,
const Ptr< Input::Mouse > & mouse,
const Ptr< Input::GamePad > & gamePad,
bool & outDown,
bool & outUp ) const
private

what one binding answers for one player this frame, and the edge the device reported for it

What one binding answers for one player this frame.

A binding only answers for a player who has that device, so a pad binding answers nothing for a player on the keyboard.

The mouse movement comes from the handler, with the sign the handler built it with. That sign depends on which event path the window took. A binding's scale can turn it into the direction the game wants.

◆ ReadFile()

IndexT Input::ActionMap::ReadFile ( const Util::String & path)
private

Read one file and put it over the map as it is now. Returns the number of actions it applied.

Read one file and put it over the map as it is now.

An action the file has and the map does not is a new action. An action both have keeps its id and takes the file's bindings. This is why the second file is an override and not a definition.

Return the number of actions the file applied, so the caller can tell a file it read from a file that was not there.

◆ RebuildIndex()

void Input::ActionMap::RebuildIndex ( )
private

Build the name to id lookup from the actions that are still there.

A key in a dictionary cannot be changed or removed, so a rename or a removal builds the whole index again.

◆ Register()

ActionId Input::ActionMap::Register ( const Util::String & name,
ActionKind kind,
float deadzone = 0.0f )

Add an action.

An action gets its id from the order it was registered in, and the id is what the reads take.

The id it returns is the index the reads take, and it does not change on a reload. A slot a removed action left is not used again, so a new action always goes at the end.

An action that already has a name is not registered twice: the call answers with the id it already has, so an id you keep is still the same action after a reload.

◆ RemoveAction()

bool Input::ActionMap::RemoveAction ( ActionId action)

Drop an action.

Drop an action, and return true.

The slot it had is not used again this session, because an id is the index the reads take and the actions after it must not move, so the next action added takes a new slot at the end. Returns false when the action is not one in use.

The slot it had is not used again this session, because an id is the index the reads take and moving the entries after it would move their ids. The next action added takes a new slot at the end. Returns false when the action is not one in use.

◆ RemoveBinding()

bool Input::ActionMap::RemoveBinding ( ActionId action,
IndexT bindingIndex )

Drop one binding. The bindings after it move up, so their indexes change.

◆ RenameAction()

bool Input::ActionMap::RenameAction ( ActionId action,
const Util::String & name )

Change an action's name.

The id does not change, because the id is the index the reads take, so code that already has the id keeps working. Returns false when the name is empty or another action already has it.

◆ Save()

bool Input::ActionMap::Save ( )

Write every action as it is now, to the user file.

◆ SaveTo()

bool Input::ActionMap::SaveTo ( const Util::String & path)

Write every action as it is now, to a file.

The editor's keymap modal writes the definitions with this, because the definitions are the file it edits.

◆ SetAction()

void Input::ActionMap::SetAction ( ActionId action,
ActionKind kind,
float deadzone )

Change an action's kind and deadzone. The reads use both from the next frame.

◆ SetBinding()

bool Input::ActionMap::SetBinding ( ActionId action,
IndexT bindingIndex,
SourceType source,
IndexT code,
Input::Modifier::Code mods,
float scale )

Put a control on a binding the action already has. Returns false when the index is not one in use.

◆ SetDefaultPath()

void Input::ActionMap::SetDefaultPath ( const Util::String & path)

The two files.

The definitions are the project's and are never written. The user file is the player's, and Save() writes it. Load() reads the definitions and then the user file over them.

◆ SetPlayerDevice()

void Input::ActionMap::SetPlayerDevice ( IndexT playerIndex,
DeviceType device,
IndexT deviceIndex )

Put one device on a player slot and drop the devices it had.

That device is the one the player is reported as.

◆ SetUserPath()

void Input::ActionMap::SetUserPath ( const Util::String & path)

◆ SourceFromString()

SourceType Input::ActionMap::SourceFromString ( const Util::String & str)
staticprivate

◆ SourceToString()

Util::String Input::ActionMap::SourceToString ( SourceType source)
static

The names the file writes a control with. The editor's keymap modal draws a binding with these.

Member Data Documentation

◆ actionIndex

Util::Dictionary<Util::StringAtom, ActionId> Input::ActionMap::actionIndex
private

◆ actions

Util::Array<ActionEntry> Input::ActionMap::actions
private

◆ bindings

Util::FixedArray<ActionBinding> Input::ActionMap::bindings
private

◆ defaultPath

Util::String Input::ActionMap::defaultPath
private

◆ isOpen

bool Input::ActionMap::isOpen
private

◆ lastPlayer

Util::FixedArray<IndexT> Input::ActionMap::lastPlayer
private

◆ players

Util::FixedArray<PlayerSlot> Input::ActionMap::players
private

◆ prev

Util::FixedArray<ActionPrev> Input::ActionMap::prev
private

◆ states

Util::FixedArray<ActionState> Input::ActionMap::states
private

◆ userPath

Util::String Input::ActionMap::userPath
private

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