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_data.hpp
Go to the documentation of this file.
1
10
16
17#ifndef FOOLSENGINE_INPUTSYSTEMDATA_H
18#define FOOLSENGINE_INPUTSYSTEMDATA_H
19
20#include <set>
21#include <map>
22#include <string>
23#include <functional>
24
25#include <nlohmann/json.hpp>
26#include <glm/glm.hpp>
27
28namespace ToyMaker {
29
37 using ActionName = ::std::string;
38
44 using ContextName = ::std::string;
45
47
53 enum class DeviceType: uint8_t {
59 };
60
66 enum class ControlType: uint8_t {
73 };
74
88 typedef uint8_t AxisFilterType;
89
97 typedef uint8_t InputAttributesValueType;
98
104 typedef ::std::pair<DeviceType, ControlType> InputSourceType;
105
111 struct InputAttributesType {
112 InputAttributesType() = default;
113 InputAttributesType(InputAttributesValueType value) : mValue{value} {}
114 operator InputAttributesValueType() const { return mValue; }
115 InputAttributesValueType mValue {0};
116 };
117
123 extern const ::std::map<InputSourceType, InputAttributesType> kInputSourceTypeAttributes;
124
133 /*
134 * Mask for the first two bits containing
135 * the number of axes in the value produced by
136 * an input device
137 */
138 N_AXES=0x3,
144 };
145
156
161 uint8_t mDevice {0};
162
167 uint32_t mControl {0};
168
179
189 bool operator==(const InputSourceDescription& other) const {
190 return !(*this < other) && !(other < *this);
191 }
192
202 bool operator<(const InputSourceDescription& other) const {
203 return (
204 static_cast<uint8_t>(mDeviceType) < static_cast<uint8_t>(other.mDeviceType)
205 || (
206 mDeviceType == other.mDeviceType && (
207 mDevice < other.mDevice
208 || (
209 mDevice == other.mDevice
210 && (
211 static_cast<uint8_t>(mControlType) < static_cast<uint8_t>(other.mControlType)
212 || mControl < other.mControl
213 )
214 )
215 )
216 )
217 );
218 }
219
226 operator bool() const {
227 // Must have both a device type and a control type
228 // to be considered a valid input source
229 return !(
232 );
233 }
234 };
235
243 SIMPLE=0x0, //< Sign Index
244 X_POS=0x1, //< 0b 00 01
245 X_NEG=0x5, //< 0b 01 01
246 Y_POS=0x2, //< 0b 00 10
247 Y_NEG=0x6, //< 0b 01 10
248 Z_POS=0x3, //< 0b 00 11
249 Z_NEG=0x7, //< 0b 01 11
250 X_CHANGE_POS=0x9, //< 0b 10 01
251 X_CHANGE_NEG=0xD, //< 0b 11 01
252 Y_CHANGE_POS=0xA, //< 0b 10 10
253 Y_CHANGE_NEG=0xE, //< 0b 11 10
254 Z_CHANGE_POS=0xB, //< 0b 10 11
255 Z_CHANGE_NEG=0xF, //< 0b 11 11
256 };
257
264 ID=0x3,
265 SIGN=0x4,
266 CHANGE=0x8,
267 };
268
274 struct InputFilter {
275
281
288
298 bool operator==(const InputFilter& other) const {
299 return !(*this < other) && !(other < *this);
300 }
301
309 bool operator<(const InputFilter& other) const {
310 return (
311 mControl < other.mControl
312 || (
313 mControl == other.mControl
314 && (
316 )
317 )
318 );
319 }
320
329 operator bool() const {
330 return mControl;
331 }
332 };
333
346 struct InputCombo {
351 enum Trigger: uint8_t {
352 ON_PRESS, //< Main control is pressed
353 ON_RELEASE, //< Main control was pressed and is now released
354 ON_CHANGE, //< Something has changed w.r.t. the main control
355 ON_BUTTON_PRESS, //< (mainly for pointers and analog sticks) Main control that doubles as a button is pressed
356 ON_BUTTON_RELEASE, //< (mainly for pointers and analog sticks) Main control that doubles as a button was pressed and is now released
357 ON_BUTTON_CHANGE, //< (mainly for pointers and analog sticks) Main control that doubles as a button has just been pressed or released
358 };
359
365
372
379
384 Trigger mTrigger { ON_CHANGE };
385
390 double mDeadzone { 0.f };
391
396 double mThreshold { .7f };
397
404 operator bool() const {
405 return mMainControl;
406 }
407
415 bool operator==(const InputCombo& other) const {
416 return !(*this < other) && !(other < *this);
417 }
418
426 bool operator<(const InputCombo& other) const {
427 return (
429 || (
431 && (
432 mModifier1 < other.mModifier1
433 || (
434 mModifier1 == other.mModifier1
435 && (
436 mModifier2 < other.mModifier2
437 || (
438 mModifier2 == other.mModifier2
439 && (
440 static_cast<uint8_t>(mTrigger) < static_cast<uint8_t>(other.mTrigger)
441 || (
442 mTrigger == other.mTrigger
443 && (
444 mThreshold < other.mThreshold
445 )
446 )
447 )
448 )
449 )
450 )
451 )
452 )
453 );
454 }
455 };
456
467 uint32_t mTimestamp {};
468
473 bool mActivated { false };
474
479 float mAxisValue { 0.f };
480
485 float mButtonValue { 0.f };
486 };
487
493 enum class ActionValueType: uint8_t {
494 STATE, //< A value that represents the state of an action in the present moment (like mouse positions, or tablet pen pressure)
495 CHANGE, //< A value that represents a recent change (like mouse motions, or button presses and releases)
496 };
497
503 using QualifiedActionName = ::std::pair<ContextName, ActionName>;
504
517 ActionDefinition(const QualifiedActionName& contextActionNamePair):
518 mName{contextActionNamePair.second},
519 mContext{contextActionNamePair.first}
520 {}
521
526
531 ::std::string mName {};
532
540
548
553 ::std::string mContext {};
554
564 inline bool operator==(const ActionDefinition& other) const {
565 return !(*this < other) && !(other < *this);
566 }
567
575 inline bool operator<(const ActionDefinition& other) const {
576 return (mContext < other.mContext || (
577 mContext == other.mContext
578 && mName < other.mName
579 ));
580 }
581
587 operator QualifiedActionName() const {
588 return {mContext, mName};
589 }
590 };
591
597 enum class ActionType {
598 BUTTON, //< It's either on or off
599 ONE_AXIS, //< It's a single value between 0 and 1 or -1 and 1
600 TWO_AXIS, //< It's two values each between 0 and 1 or -1 and 1
601 THREE_AXIS, //< It's three values each between 0 and 1 or -1 and 1
602 };
603
609 enum class ActionTrigger: uint8_t {
610 UPDATE, //< The trigger condition for the associated input combo was met.
611 RESET, //< The trigger condition was met, but has now been failed.
612 };
613
624 ActionTrigger mTriggeredBy { ActionTrigger::UPDATE };
625
630 uint32_t mTimestamp {};
631
636 uint32_t mDuration { 0 };
637
643 bool mActivated {false};
644
649 ActionType mType{ ActionType::BUTTON };
650 };
651
658
669 CommonActionData mCommonData { .mType{ActionType::ONE_AXIS} };
670
675 double mValue { 0.f };
676 };
677
688 CommonActionData mCommonData { .mType {ActionType::TWO_AXIS} };
689
694 glm::dvec2 mValue { 0.f };
695 };
696
707 CommonActionData mCommonData { .mType{ActionType::THREE_AXIS} };
708
713 glm::dvec3 mValue { 0.f };
714 };
715
726 static constexpr ActionType toType[4] {
727 ActionType::BUTTON, ActionType::ONE_AXIS,
728 ActionType::TWO_AXIS, ActionType::THREE_AXIS
729 };
730
736 ActionData(ActionType actionType): mCommonData{ .mType{actionType} } {
737 // Regardless of the type, all the data that corresponds
738 // to the action value should be initialized with 0
739 mThreeAxisActionData.mValue = glm::vec3{0.f};
740 }
741
747
754 mSimpleData = simpleData;
755 }
756
762 ActionData(OneAxisActionData oneAxisActionData): ActionData{ActionType::ONE_AXIS} {
763 mOneAxisActionData = oneAxisActionData;
764 }
765
771 ActionData(TwoAxisActionData twoAxisActionData): ActionData{ActionType::TWO_AXIS} {
772 mTwoAxisActionData = twoAxisActionData;
773 }
774
779 ActionData(ThreeAxisActionData threeAxisActionData): ActionData{ActionType::THREE_AXIS} {
780 mThreeAxisActionData = threeAxisActionData;
781 }
782
788 ActionData(uint8_t nAxes): ActionData{ toType[nAxes] } {}
789
790
796
802
808
814
820 };
821
826 NLOHMANN_JSON_SERIALIZE_ENUM ( DeviceType, {
827 {DeviceType::NA, "na"},
828 {DeviceType::MOUSE, "mouse"},
829 {DeviceType::KEYBOARD, "keyboard"},
830 {DeviceType::TOUCH, "touch"},
831 {DeviceType::CONTROLLER, "controller"},
832 });
833
838 NLOHMANN_JSON_SERIALIZE_ENUM(ControlType, {
839 {ControlType::NA, "na"},
840 {ControlType::AXIS, "axis"},
841 {ControlType::MOTION, "motion"},
842 {ControlType::POINT, "point"},
843 {ControlType::BUTTON, "button"},
844 {ControlType::RADIO, "radio"},
845 });
846
851 NLOHMANN_JSON_SERIALIZE_ENUM( AxisFilter, {
852 {AxisFilter::SIMPLE, "simple"},
853 {AxisFilter::X_POS, "+x"},
854 {AxisFilter::X_NEG, "-x"},
855 {AxisFilter::Y_POS, "+y"},
856 {AxisFilter::Y_NEG, "-y"},
857 {AxisFilter::Z_POS, "+z"},
858 {AxisFilter::Z_NEG, "-z"},
859 {AxisFilter::X_CHANGE_POS, "+dx"},
860 {AxisFilter::X_CHANGE_NEG, "-dx"},
861 {AxisFilter::Y_CHANGE_POS, "+dy"},
862 {AxisFilter::Y_CHANGE_NEG, "-dy"},
863 {AxisFilter::Z_CHANGE_POS, "+dz"},
864 {AxisFilter::Z_CHANGE_NEG, "-dz"},
865 });
866
871 NLOHMANN_JSON_SERIALIZE_ENUM( InputCombo::Trigger, {
872 {InputCombo::Trigger::ON_PRESS, "on-press"},
873 {InputCombo::Trigger::ON_RELEASE, "on-release"},
874 {InputCombo::Trigger::ON_CHANGE, "on-change"},
875 {InputCombo::Trigger::ON_BUTTON_PRESS, "on-button-press"},
876 {InputCombo::Trigger::ON_BUTTON_RELEASE, "on-button-release"},
877 {InputCombo::Trigger::ON_BUTTON_CHANGE, "on-button-change"},
878 });
879
884 NLOHMANN_JSON_SERIALIZE_ENUM( ActionValueType, {
885 {ActionValueType::STATE, "state"},
886 {ActionValueType::CHANGE, "change"},
887 });
888
893 void to_json(nlohmann::json& json, const ToyMaker::InputAttributesType& inputAttributes);
894
899 void from_json(const nlohmann::json& json, ToyMaker::InputAttributesType& inputAttributes);
900
905 void to_json(nlohmann::json& json, const ToyMaker::InputSourceDescription& inputSourceDescription);
906
911 void from_json(const nlohmann::json& json, ToyMaker::InputSourceDescription& inputSourceDescription);
912
917 void to_json(nlohmann::json& json, const ToyMaker::InputFilter& inputFilter);
918
923 void from_json(const nlohmann::json& json, ToyMaker::InputFilter& inputFilter);
924
929 void to_json(nlohmann::json& json, const ToyMaker::InputCombo& inputCombo);
930
935 void from_json(const nlohmann::json& json, ToyMaker::InputCombo& inputCombo);
936
941 void to_json(nlohmann::json& json, const ToyMaker::ActionDefinition& actionDefinition);
942
947 void from_json(const nlohmann::json& json, ToyMaker::ActionDefinition& actionDefinition);
948}
949
950namespace std {
951 template<>
952 struct hash<ToyMaker::InputSourceDescription> {
953 size_t operator() (const ToyMaker::InputSourceDescription& definition) const {
954 return (
955 (( (hash<uint32_t>{}(definition.mControl))
956 ^ (hash<uint8_t>{}(definition.mDevice) << 1) >> 1)
957 ^ (hash<uint8_t>{}(static_cast<uint8_t>(definition.mDeviceType)) << 1) >> 1)
958 ^ (hash<uint8_t>{}(static_cast<uint8_t>(definition.mControlType) << 1))
959 );
960 }
961 };
962
963 template<>
964 struct hash<ToyMaker::ActionDefinition> {
965 size_t operator() (const ToyMaker::ActionDefinition& definition) const {
966 return hash<std::string>{}(definition.mName);
967 }
968 };
969
970 template<>
971 struct hash<ToyMaker::InputFilter> {
972 size_t operator() (const ToyMaker::InputFilter& inputFilter) const {
973 return (
974 (hash<ToyMaker::InputSourceDescription>{}(inputFilter.mControl)
975 ^ (hash<uint8_t>{}(inputFilter.mAxisFilter) << 1))
976 );
977 }
978 };
979
980 template<>
981 struct hash<ToyMaker::InputCombo> {
982 size_t operator() (const ToyMaker::InputCombo& inputBind) const {
983 return (
984 (((hash<ToyMaker::InputFilter>{}(inputBind.mMainControl)
985 ^ (hash<ToyMaker::InputFilter>{}(inputBind.mModifier1) << 1) >> 1)
986 ^ (hash<ToyMaker::InputFilter>{}(inputBind.mModifier2) << 1) >> 1)
987 ^ (hash<ToyMaker::InputCombo::Trigger>{}(inputBind.mTrigger) << 1) >> 1)
988 ^ (hash<float>{}(inputBind.mThreshold) << 1)
989 );
990 }
991 };
992}
993
994#endif
::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
ActionValueType
The type of value associated with this action.
Definition input_data.hpp:493
DeviceType
The type of input device that was responsible for creating the signal which will be mapped to an acti...
Definition input_data.hpp:53
AxisFilterMask
Important values used with AxisFilterType for determining the type, direction, and sign,...
Definition input_data.hpp:263
::std::pair< DeviceType, ControlType > InputSourceType
A composite type which uniquely identifies a control attached to the platform running this applicatio...
Definition input_data.hpp:104
InputAttributes
A collection of a few important input attribute value type values and masks.
Definition input_data.hpp:132
uint8_t InputAttributesValueType
A type that is quite possibly unnecessary now that DeviceType and ControlType exist.
Definition input_data.hpp:97
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
ControlType
A single device may have multiple buttons and other controls, each of which will correspond to a type...
Definition input_data.hpp:66
ActionType
A seemingly redundant type that is a part of the ActionData struct and not the ActionDefinition struc...
Definition input_data.hpp:597
ActionTrigger
Helps describe what InputCombo related event was responsible for signaling this action.
Definition input_data.hpp:609
uint8_t AxisFilterType
A type with multiple uses.
Definition input_data.hpp:88
const ::std::map< InputSourceType, InputAttributesType > kInputSourceTypeAttributes
A mapping from each type of input control to the attributes associated with it.
Definition input_manager.cpp:13
CommonActionData SimpleActionData
Actions that ultimately act like a single button value, where mActivated is the state of the button.
Definition input_data.hpp:657
@ TOUCH
Definition input_data.hpp:58
@ MOUSE
Definition input_data.hpp:55
@ CONTROLLER
Definition input_data.hpp:57
@ NA
Definition input_data.hpp:54
@ KEYBOARD
Definition input_data.hpp:56
@ HAS_NEGATIVE
Definition input_data.hpp:139
@ HAS_STATE_VALUE
Definition input_data.hpp:142
@ HAS_BUTTON_VALUE
Definition input_data.hpp:141
@ STATE_IS_LOCATION
Definition input_data.hpp:143
@ N_AXES
Definition input_data.hpp:138
@ HAS_CHANGE_VALUE
Definition input_data.hpp:140
@ SIMPLE
V- lines up with the bit representing sign in actionAttributes.
Definition input_data.hpp:243
@ BUTTON
Definition input_data.hpp:71
@ AXIS
Definition input_data.hpp:68
@ POINT
Definition input_data.hpp:70
@ NA
Definition input_data.hpp:67
@ RADIO
Definition input_data.hpp:72
@ MOTION
Definition input_data.hpp:69
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
STL namespace.
The definition of a single action, including whether it represents state or change,...
Definition input_data.hpp:511
ActionDefinition()=default
Construct a new (empty) action definition object.
bool operator==(const ActionDefinition &other) const
Compares two action definitions for equality.
Definition input_data.hpp:564
::std::string mContext
The name of the context the action belongs to.
Definition input_data.hpp:553
ActionDefinition(const QualifiedActionName &contextActionNamePair)
Construct a new action definition object.
Definition input_data.hpp:517
::std::string mName
The name of the action.
Definition input_data.hpp:531
bool operator<(const ActionDefinition &other) const
Provides an implementation of the less than operator, mainly for use by the equality operator.
Definition input_data.hpp:575
ActionValueType mValueType
I'm not yet sure where this is used, since mAttributes already exists.
Definition input_data.hpp:547
InputAttributesType mAttributes
The same as in an InputSource, describes the type of data (normalized) this action is expected to hav...
Definition input_data.hpp:539
A struct containing meta-info that will be present for all types of actions.
Definition input_data.hpp:619
ActionType mType
The type of value associated with this action.
Definition input_data.hpp:649
ActionTrigger mTriggeredBy
The condition which caused this action to be signalled.
Definition input_data.hpp:624
bool mActivated
I'm not sure about this one.
Definition input_data.hpp:643
uint32_t mDuration
Unused for now, but presumably the duration an active input has been active.
Definition input_data.hpp:636
uint32_t mTimestamp
The time at which the action was signalled.
Definition input_data.hpp:630
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
double mThreshold
The threshold (on a main control that produces continuous values, like analog sticks and triggers) be...
Definition input_data.hpp:396
Trigger
The action on the main control (provided any modifiers are active) that activates this combo.
Definition input_data.hpp:351
InputFilter mMainControl
Axis value corresponding to this combo may be sampled from this control.
Definition input_data.hpp:364
InputFilter mModifier1
A single axis of a single input source that must be considered active in order for this combo to be c...
Definition input_data.hpp:371
double mDeadzone
Some device controls, like analog sticks, wear out over time producing false positives for input even...
Definition input_data.hpp:390
bool operator<(const InputCombo &other) const
Mainly supports the equality operator, and allows sorting for InputCombos to work.
Definition input_data.hpp:426
Trigger mTrigger
The actual event on the main control that causes the value mapped to this InputCombo to change.
Definition input_data.hpp:384
bool operator==(const InputCombo &other) const
Compares to InputCombos for equality, for use in certain containers like maps.
Definition input_data.hpp:415
InputFilter mModifier2
A single axis of a single input source that must be considered active in order for this combo to be c...
Definition input_data.hpp:378
Filter that uniquely defines ONE axis of one control of one input belonging to one device.
Definition input_data.hpp:274
InputSourceDescription mControl
The control, one of whose axes is being filtered for, uniquely described by this InputFilter.
Definition input_data.hpp:280
bool operator==(const InputFilter &other) const
Compares two input filters for equality.
Definition input_data.hpp:298
bool operator<(const InputFilter &other) const
A less than operator, mainly used for sorting and the equality operator.
Definition input_data.hpp:309
AxisFilter mAxisFilter
The axis of the control (and its value type) being filtered for.
Definition input_data.hpp:287
Identifies a single control, such as a button, trigger, or joystick, on a single device.
Definition input_data.hpp:150
uint32_t mControl
The ID of the control on a single device, if the device has multiple controls (buttons,...
Definition input_data.hpp:167
bool operator<(const InputSourceDescription &other) const
Compares one description to another, for use by the equality operator.
Definition input_data.hpp:202
uint8_t mDevice
The ID of a device, assuming several of the same devices can be connected to a single platform.
Definition input_data.hpp:161
bool operator==(const InputSourceDescription &other) const
Compares one description to another for equality.
Definition input_data.hpp:189
ControlType mControlType
The type of control belonging to this device, described by this object.
Definition input_data.hpp:178
DeviceType mDeviceType
The type of device described by this object.
Definition input_data.hpp:173
InputAttributesType mAttributes
The attributes of the input control, queryable using values from InputAttributes.
Definition input_data.hpp:155
Actions that have just one axis of data, eg., the accelerator on a car.
Definition input_data.hpp:664
double mValue
The actual value of the axis of this action.
Definition input_data.hpp:675
CommonActionData mCommonData
Common metadata belonging to this action.
Definition input_data.hpp:669
Actions described by 3 axes (I can't think of any examples for this)
Definition input_data.hpp:702
glm::dvec3 mValue
Three float values, normalized (or not, for values representing location)
Definition input_data.hpp:713
CommonActionData mCommonData
Common action metadata.
Definition input_data.hpp:707
Actions that have two axes of data. (Pointer locations, movement direction input, pitch+roll,...
Definition input_data.hpp:683
CommonActionData mCommonData
Common action metadata.
Definition input_data.hpp:688
glm::dvec2 mValue
Two float values, normalized (or not, for location states)
Definition input_data.hpp:694
An input state that hasn't yet been mapped to its corresponding action.
Definition input_data.hpp:462
float mButtonValue
In devices where a control also doubles as a button (like analog sticks, pointer clicks),...
Definition input_data.hpp:485
uint32_t mTimestamp
The time at which this input state was recorded.
Definition input_data.hpp:467
float mAxisValue
The value of the axis of the control of the combo that this value represents.
Definition input_data.hpp:479
bool mActivated
Per its combo's main control, whether this value should be considered "active".
Definition input_data.hpp:473
ActionData(TwoAxisActionData twoAxisActionData)
Construct a new action data object based on already existing TwoAxisActionData.
Definition input_data.hpp:771
ActionData(ActionType actionType)
Construct a new action data object of a particular type. Called prior to all other ActionData constru...
Definition input_data.hpp:736
TwoAxisActionData mTwoAxisActionData
Two axis action data.
Definition input_data.hpp:813
static constexpr ActionType toType[4]
Array of action types so that 0 - BUTTON, 1 - ONE_AXIS, and so on.
Definition input_data.hpp:726
ActionData(uint8_t nAxes)
Construct a new Action Data object with nAxes axes.
Definition input_data.hpp:788
ActionData(ThreeAxisActionData threeAxisActionData)
Construct a new THREE_AXIS action data object based on already existing ThreeAxisActionData.
Definition input_data.hpp:779
OneAxisActionData mOneAxisActionData
One axis action data.
Definition input_data.hpp:807
ActionData(SimpleActionData simpleData)
Construct a new SIMPLE action data object, based on already existing SimpleActionData.
Definition input_data.hpp:753
ThreeAxisActionData mThreeAxisActionData
Three axis action data.
Definition input_data.hpp:819
CommonActionData mCommonData
Common or simple data.
Definition input_data.hpp:795
ActionData()
Construct a new SIMPLE action data object.
Definition input_data.hpp:746
SimpleActionData mSimpleData
Common or simple data.
Definition input_data.hpp:801
ActionData(OneAxisActionData oneAxisActionData)
Construct a new ONE_AXIS action data object based on already existing OneAxisActionData.
Definition input_data.hpp:762