|
Nebula
|
#include <actionmap.h>
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:
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) |
Additional Inherited Members | |
| Protected Member Functions inherited from Core::RefCounted | |
| virtual | ~RefCounted () |
| destructor (called when refcount reaches zero) | |
| Input::ActionMap::ActionMap | ( | ) |
constructor
|
virtual |
destructor
|
private |
|
private |
| 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.
| 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.
| void Input::ActionMap::ClearBindings | ( | ActionId | action | ) |
Drop every binding an action has, so the next ones added are the only ones.
| void Input::ActionMap::Close | ( | ) |
close the map
|
staticprivate |
|
static |
A binding's code as the name the file writes it with.
The code comes from the code set its source uses.
|
staticprivate |
|
staticprivate |
| 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.
| ActionId Input::ActionMap::FindAction | ( | const Util::String & | name | ) | const |
the action a name refers to, or InvalidAction when the map does not have it
| const ActionEntry & Input::ActionMap::GetAction | ( | ActionId | action | ) | const |
the action and one of its bindings, by index
| 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.
| Math::vec2 Input::ActionMap::GetAxis2 | ( | ActionId | negativeX, |
| ActionId | positiveX, | ||
| ActionId | negativeY, | ||
| ActionId | positiveY, | ||
| IndexT | playerIndex ) const |
two axes as a pair
| const ActionBinding & Input::ActionMap::GetBinding | ( | ActionId | action, |
| IndexT | bindingIndex ) const |
| const Util::String & Input::ActionMap::GetDefaultPath | ( | ) | const |
the player that last triggered an action, or InvalidIndex when none has
| SizeT Input::ActionMap::GetNumActions | ( | ) | const |
the number of slots the map has, including the ones a removed action left
| DeviceType Input::ActionMap::GetPlayerDevice | ( | IndexT | playerIndex | ) | const |
the first device on a slot, which is the device the player is reported as
| 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
| 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.
| const Util::String & Input::ActionMap::GetUserPath | ( | ) | const |
the analog value of one action. A stick or a trigger answers with this.
| bool Input::ActionMap::IsOpen | ( | ) | const |
return true if open
| 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
|
staticprivate |
|
static |
| 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.
| 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.
| void Input::ActionMap::Open | ( | ) |
Open the map. This allocates the state and the tables.
| bool Input::ActionMap::PlayerHasDevice | ( | IndexT | playerIndex, |
| DeviceType | device ) const |
true when a player can answer from this device
|
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.
|
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.
|
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.
| 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.
| 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.
Drop one binding. The bindings after it move up, so their indexes change.
| 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.
| bool Input::ActionMap::Save | ( | ) |
Write every action as it is now, to the user file.
| 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.
| 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.
| 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.
| void Input::ActionMap::SetDefaultPath | ( | const Util::String & | path | ) |
| 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.
| void Input::ActionMap::SetUserPath | ( | const Util::String & | path | ) |
|
staticprivate |
|
static |
The names the file writes a control with. The editor's keymap modal draws a binding with these.
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |
|
private |