Nebula
Loading...
Searching...
No Matches
actionmap.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
88//------------------------------------------------------------------------------
89#include "core/refcounted.h"
90#include "core/singleton.h"
91#include "core/ptr.h"
92#include "input/key.h"
93#include "input/modifier.h"
94#include "input/mousebutton.h"
95#include "input/gamepad.h"
96#include "input/keyboard.h"
97#include "input/mouse.h"
98#include "math/vec2.h"
99#include "util/array.h"
100#include "util/dictionary.h"
101#include "util/fixedarray.h"
102#include "util/string.h"
103#include "util/stringatom.h"
104
105namespace Input
106{
107
110constexpr ActionId InvalidAction = InvalidIndex;
111
114constexpr IndexT MaxActions = 256;
115constexpr IndexT MaxPlayers = 4;
118
125
136
142
152
165
169{
170 bool held = false;
171 bool down = false;
172 bool released = false;
173 float value = 0.0f;
174};
175
179{
180 bool held = false;
181 bool edge = false;
182};
183
191
199
200//------------------------------------------------------------------------------
204{
207public:
209 ActionMap();
211 virtual ~ActionMap();
212
214 void Open();
216 void Close();
218 bool IsOpen() const;
219
222 void SetDefaultPath(const Util::String& path);
223 const Util::String& GetDefaultPath() const;
224 void SetUserPath(const Util::String& path);
225 const Util::String& GetUserPath() const;
226
229 ActionId Register(const Util::String& name, ActionKind kind, float deadzone = 0.0f);
231 void SetAction(ActionId action, ActionKind kind, float deadzone);
235 bool RenameAction(ActionId action, const Util::String& name);
239 bool RemoveAction(ActionId action);
241 bool IsRemoved(ActionId action) const;
243 void AddBinding(ActionId action, SourceType source, IndexT code, Input::Modifier::Code mods, float scale = 1.0f);
245 bool SetBinding(ActionId action, IndexT bindingIndex, SourceType source, IndexT code, Input::Modifier::Code mods, float scale);
247 bool RemoveBinding(ActionId action, IndexT bindingIndex);
249 void ClearBindings(ActionId action);
250
252 ActionId FindAction(const Util::String& name) const;
254 SizeT GetNumActions() const;
255
257 const ActionEntry& GetAction(ActionId action) const;
258 const ActionBinding& GetBinding(ActionId action, IndexT bindingIndex) const;
260 IndexT GetBindingCount(ActionId action) const;
261
264 void SetPlayerDevice(IndexT playerIndex, DeviceType device, IndexT deviceIndex);
267 bool AddPlayerDevice(IndexT playerIndex, DeviceType device, IndexT deviceIndex);
269 DeviceType GetPlayerDevice(IndexT playerIndex) const;
271 bool PlayerHasDevice(IndexT playerIndex, DeviceType device) const;
273 IndexT GetPlayerDeviceIndex(IndexT playerIndex, DeviceType device) const;
274
276 bool Load();
279 bool LoadFrom(const Util::String& path);
281 bool Save();
284 bool SaveTo(const Util::String& path);
285
287 void Evaluate();
288
290 const ActionState& GetState(ActionId action, IndexT playerIndex) const;
291
292 bool IsHeld(ActionId action, IndexT playerIndex) const;
293 bool IsPressed(ActionId action, IndexT playerIndex) const;
294 bool IsReleased(ActionId action, IndexT playerIndex) const;
295
297 float GetValue(ActionId action, IndexT playerIndex) const;
298
300 float GetAxis(ActionId negativeAction, ActionId positiveAction, IndexT playerIndex) const;
302 Math::vec2 GetAxis2(ActionId negativeX, ActionId positiveX, ActionId negativeY, ActionId positiveY, IndexT playerIndex) const;
303
305 IndexT GetLastActionPlayer(ActionId action) const;
306
310 static Util::String CodeToString(SourceType source, IndexT code);
311
312private:
314 float ReadBinding(const ActionEntry& action, const ActionBinding& binding, IndexT playerIndex,
315 const Ptr<Input::Keyboard>& keyboard, const Ptr<Input::Mouse>& mouse, const Ptr<Input::GamePad>& gamePad,
316 bool& outDown, bool& outUp) const;
317
318 static SourceType SourceFromString(const Util::String& str);
319 static DeviceType DeviceFromString(const Util::String& str);
321 static ActionKind KindFromString(const Util::String& str);
322 static IndexT CodeFromString(SourceType source, const Util::String& str);
323
325 IndexT ReadFile(const Util::String& path);
326
329 void RebuildIndex();
330
331 bool isOpen;
341};
342
343//------------------------------------------------------------------------------
347inline ActionId
349{
350 if (!ActionMap::HasInstance())
351 return InvalidIndex;
352 return ActionMap::Instance()->FindAction(name);
353}
354
355//------------------------------------------------------------------------------
358inline bool
359IsActionHeld(ActionId action, IndexT playerIndex = 0)
360{
361 if (!ActionMap::HasInstance())
362 return false;
363 return ActionMap::Instance()->IsHeld(action, playerIndex);
364}
365
366//------------------------------------------------------------------------------
369inline bool
370IsActionPressed(ActionId action, IndexT playerIndex = 0)
371{
372 if (!ActionMap::HasInstance())
373 return false;
374 return ActionMap::Instance()->IsPressed(action, playerIndex);
375}
376
377//------------------------------------------------------------------------------
380inline bool
381IsActionReleased(ActionId action, IndexT playerIndex = 0)
382{
383 if (!ActionMap::HasInstance())
384 return false;
385 return ActionMap::Instance()->IsReleased(action, playerIndex);
386}
387
388//------------------------------------------------------------------------------
391inline float
392GetActionValue(ActionId action, IndexT playerIndex = 0)
393{
394 if (!ActionMap::HasInstance())
395 return 0.0f;
396 return ActionMap::Instance()->GetValue(action, playerIndex);
397}
398
399//------------------------------------------------------------------------------
402inline float
403GetAxis(ActionId negativeAction, ActionId positiveAction, IndexT playerIndex = 0)
404{
405 if (!ActionMap::HasInstance())
406 return 0.0f;
407 return ActionMap::Instance()->GetAxis(negativeAction, positiveAction, playerIndex);
408}
409
410//------------------------------------------------------------------------------
413inline Math::vec2
414GetAxis2(ActionId negativeX, ActionId positiveX, ActionId negativeY, ActionId positiveY, IndexT playerIndex = 0)
415{
416 if (!ActionMap::HasInstance())
417 return Math::vec2(0.0f, 0.0f);
418 return ActionMap::Instance()->GetAxis2(negativeX, positiveX, negativeY, positiveY, playerIndex);
419}
420
421//------------------------------------------------------------------------------
424inline IndexT
426{
427 if (!ActionMap::HasInstance())
428 return InvalidIndex;
429 return ActionMap::Instance()->GetLastActionPlayer(action);
430}
431
432} // namespace Input
433//------------------------------------------------------------------------------
The common base class of Nebula.
Definition refcounted.h:45
__DeclareClass(ActionMap)
Util::FixedArray< PlayerSlot > players
Definition actionmap.h:339
void RebuildIndex()
Build the name to id lookup from the actions that are still there.
Definition actionmap.cc:220
bool RenameAction(ActionId action, const Util::String &name)
Change an action's name.
Definition actionmap.cc:189
SizeT GetNumActions() const
the number of slots the map has, including the ones a removed action left
Definition actionmap.cc:388
void Close()
close the map
Definition actionmap.cc:77
bool IsHeld(ActionId action, IndexT playerIndex) const
Definition actionmap.cc:1169
static Util::String KindToString(ActionKind kind)
Definition actionmap.cc:578
Util::FixedArray< ActionPrev > prev
Definition actionmap.h:337
ActionMap()
constructor
Definition actionmap.cc:24
const ActionEntry & GetAction(ActionId action) const
the action and one of its bindings, by index
Definition actionmap.cc:397
const Util::String & GetDefaultPath() const
Definition actionmap.cc:113
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.
Definition actionmap.cc:638
bool isOpen
Definition actionmap.h:331
IndexT GetBindingCount(ActionId action) const
how many bindings an action has
Definition actionmap.cc:418
bool SaveTo(const Util::String &path)
Write every action as it is now, to a file.
Definition actionmap.cc:901
const ActionBinding & GetBinding(ActionId action, IndexT bindingIndex) const
Definition actionmap.cc:407
const ActionState & GetState(ActionId action, IndexT playerIndex) const
the state for one action and one player
Definition actionmap.cc:1157
Util::String defaultPath
Definition actionmap.h:332
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
Definition actionmap.cc:1010
static Util::String CodeToString(SourceType source, IndexT code)
A binding's code as the name the file writes it with.
Definition actionmap.cc:588
Math::vec2 GetAxis2(ActionId negativeX, ActionId positiveX, ActionId negativeY, ActionId positiveY, IndexT playerIndex) const
two axes as a pair
Definition actionmap.cc:1236
void ClearBindings(ActionId action)
Drop every binding an action has, so the next ones added are the only ones.
Definition actionmap.cc:357
IndexT GetLastActionPlayer(ActionId action) const
the player that last triggered an action, or InvalidIndex when none has
Definition actionmap.cc:1245
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.
Definition actionmap.cc:310
ActionId Register(const Util::String &name, ActionKind kind, float deadzone=0.0f)
Add an action.
Definition actionmap.cc:143
Util::FixedArray< IndexT > lastPlayer
Definition actionmap.h:338
bool RemoveBinding(ActionId action, IndexT bindingIndex)
Drop one binding. The bindings after it move up, so their indexes change.
Definition actionmap.cc:333
void Open()
Open the map. This allocates the state and the tables.
Definition actionmap.cc:53
static ActionKind KindFromString(const Util::String &str)
Definition actionmap.cc:567
const Util::String & GetUserPath() const
Definition actionmap.cc:131
static DeviceType DeviceFromString(const Util::String &str)
Definition actionmap.cc:542
bool RemoveAction(ActionId action)
Drop an action.
Definition actionmap.cc:240
Util::Dictionary< Util::StringAtom, ActionId > actionIndex
Definition actionmap.h:340
static IndexT CodeFromString(SourceType source, const Util::String &str)
Definition actionmap.cc:606
ActionId FindAction(const Util::String &name) const
the action a name refers to, or InvalidAction when the map does not have it
Definition actionmap.cc:376
bool LoadFrom(const Util::String &path)
Read one file over the map as it is now, without reading the other path.
Definition actionmap.cc:880
void Evaluate()
build this frame's state. Called by the input server after the events have been pumped
Definition actionmap.cc:1074
Util::String userPath
Definition actionmap.h:333
static Util::String SourceToString(SourceType source)
The names the file writes a control with. The editor's keymap modal draws a binding with these.
Definition actionmap.cc:524
bool IsOpen() const
return true if open
Definition actionmap.cc:95
void SetUserPath(const Util::String &path)
Definition actionmap.cc:122
DeviceType GetPlayerDevice(IndexT playerIndex) const
the first device on a slot, which is the device the player is reported as
Definition actionmap.cc:460
bool IsReleased(ActionId action, IndexT playerIndex) const
Definition actionmap.cc:1197
float GetValue(ActionId action, IndexT playerIndex) const
the analog value of one action. A stick or a trigger answers with this.
Definition actionmap.cc:1211
void SetDefaultPath(const Util::String &path)
The two files.
Definition actionmap.cc:104
void SetAction(ActionId action, ActionKind kind, float deadzone)
Change an action's kind and deadzone. The reads use both from the next frame.
Definition actionmap.cc:173
bool Load()
Read the definitions and then the user file over them. Returns false when neither file had anything.
Definition actionmap.cc:867
void SetPlayerDevice(IndexT playerIndex, DeviceType device, IndexT deviceIndex)
Put one device on a player slot and drop the devices it had.
Definition actionmap.cc:430
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
Definition actionmap.cc:270
Util::Array< ActionEntry > actions
Definition actionmap.h:334
float GetAxis(ActionId negativeAction, ActionId positiveAction, IndexT playerIndex) const
two actions as one axis: the positive action's value minus the negative one's
Definition actionmap.cc:1227
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
Definition actionmap.cc:492
Util::FixedArray< ActionBinding > bindings
Definition actionmap.h:335
__DeclareInterfaceSingleton(ActionMap)
bool AddPlayerDevice(IndexT playerIndex, DeviceType device, IndexT deviceIndex)
Add a device to a player slot without removing the devices it already has.
Definition actionmap.cc:442
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.
Definition actionmap.cc:282
bool IsPressed(ActionId action, IndexT playerIndex) const
Definition actionmap.cc:1183
static SourceType SourceFromString(const Util::String &str)
Definition actionmap.cc:509
bool Save()
Write every action as it is now, to the user file.
Definition actionmap.cc:892
bool PlayerHasDevice(IndexT playerIndex, DeviceType device) const
true when a player can answer from this device
Definition actionmap.cc:474
static Util::String DeviceToString(DeviceType device)
Definition actionmap.cc:553
Util::FixedArray< ActionState > states
Definition actionmap.h:336
Nebula's smart pointer class which manages the life time of RefCounted objects.
Definition ptr.h:38
Nebula's dynamic array class.
Definition array.h:61
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:35
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
A StringAtom.
Definition stringatom.h:22
Nebula's universal string class.
Definition string.h:50
uint32_t Code
Definition modifier.h:21
constexpr Code NoModifier
Definition modifier.h:23
FIXME!
ActionId FindAction(const Util::String &name)
The reads as code uses them.
Definition actionmap.h:348
constexpr IndexT MaxBindingsPerAction
Definition actionmap.h:116
Math::vec2 GetAxis2(ActionId negativeX, ActionId positiveX, ActionId negativeY, ActionId positiveY, IndexT playerIndex=0)
Definition actionmap.h:414
bool IsActionHeld(ActionId action, IndexT playerIndex=0)
Definition actionmap.h:359
float GetAxis(ActionId negativeAction, ActionId positiveAction, IndexT playerIndex=0)
Definition actionmap.h:403
constexpr IndexT MaxPlayers
Definition actionmap.h:115
IndexT ActionId
The index of an action in the map. A name the map does not have answers with InvalidAction.
Definition actionmap.h:109
constexpr ActionId InvalidAction
Definition actionmap.h:110
IndexT GetLastActionPlayer(ActionId action)
Definition actionmap.h:425
constexpr IndexT MaxDevicesPerPlayer
Definition actionmap.h:117
bool IsActionReleased(ActionId action, IndexT playerIndex=0)
Definition actionmap.h:381
constexpr IndexT MaxActions
The size of the state block.
Definition actionmap.h:114
SourceType
Definition actionmap.h:127
@ GamePadAxisSource
Definition actionmap.h:132
@ KeySource
Definition actionmap.h:129
@ MouseButtonSource
Definition actionmap.h:130
@ MouseWheelSource
Definition actionmap.h:134
@ InvalidSource
Definition actionmap.h:128
@ GamePadButtonSource
Definition actionmap.h:131
@ MouseMoveSource
Definition actionmap.h:133
bool IsActionPressed(ActionId action, IndexT playerIndex=0)
Definition actionmap.h:370
DeviceType
Definition actionmap.h:120
@ InvalidDevice
Definition actionmap.h:121
@ KeyboardMouseDevice
Definition actionmap.h:122
@ GamePadDevice
Definition actionmap.h:123
float GetActionValue(ActionId action, IndexT playerIndex=0)
Definition actionmap.h:392
ActionKind
Definition actionmap.h:138
@ DigitalAction
Definition actionmap.h:139
@ AnalogAction
Definition actionmap.h:140
One control.
Definition actionmap.h:146
Input::Modifier::Code mods
Definition actionmap.h:149
SourceType source
Definition actionmap.h:147
IndexT code
Definition actionmap.h:148
float scale
Definition actionmap.h:150
An action as the map holds it.
Definition actionmap.h:156
float deadzone
Definition actionmap.h:159
float sensitivity
Definition actionmap.h:160
bool removed
true when the action was removed
Definition actionmap.h:163
Util::StringAtom name
Definition actionmap.h:157
IndexT numBindings
Definition actionmap.h:161
ActionKind kind
Definition actionmap.h:158
What an edge is measured against.
Definition actionmap.h:179
bool edge
Definition actionmap.h:181
bool held
Definition actionmap.h:180
One action for one player, as this frame built it.
Definition actionmap.h:169
bool released
Definition actionmap.h:172
bool held
Definition actionmap.h:170
bool down
Definition actionmap.h:171
float value
Definition actionmap.h:173
One device a player can answer from.
Definition actionmap.h:187
IndexT deviceIndex
Definition actionmap.h:189
DeviceType device
Definition actionmap.h:188
A player as the devices its bindings can answer from.
Definition actionmap.h:195
PlayerDevice devices[MaxDevicesPerPlayer]
Definition actionmap.h:196
IndexT numDevices
Definition actionmap.h:197
A 2-component float vector class.
Definition vec2.h:21
int SizeT
Definition types.h:50
int IndexT
Definition types.h:49