4#ifndef GREM_EVENTS_INPUT_MANAGER_HPP
5#define GREM_EVENTS_INPUT_MANAGER_HPP
38template <
template <
typename>
typename Allocator>
40using Variant = json::VariantBase<std::allocator>;
49template <
typename Action>
50[[nodiscard]]
inline const HashMap<String, uint8_t>& getActionMap() {
51 static const HashMap<String, uint8_t> actionMap = []() -> HashMap<String, uint8_t> {
52 HashMap<String, uint8_t> result{};
61template <
typename Action>
405 class OutputEventBase {
436 template <enumeration Action>
465 : timestamp(timestamp)
466 , outputIndex(outputIndex)
573 loadConfiguration(std::move(fileContents), {}, getOutputIndex, readExtraProperty);
606 template <enumeration Action>
610 std::move(fileContents), filepath,
682 template <enumeration Action>
735 template <enumeration Action>
740 if (outputIndex < actionNames.
size()) {
810 template <enumeration Action>
830 previousState = currentState;
869 polledOutputEvents.clear();
870 polledOutputEvents.swap(pendingOutputEvents);
871 return polledOutputEvents;
887 return polledOutputEvents;
987 polledOutputEvents.clear();
988 pendingOutputEvents.clear();
1029 template <enumeration Action>
1076 template <enumeration Action>
1103 options.preferences = preferences;
1121 options.emitOutputEvents = emitOutputEvents;
1135 return options.preferences;
1147 return initialPreferences;
1169 return !bindings.empty();
1188 auto it = result.
begin();
1189 for (
const auto& [input, outputIndices] : bindings) {
1190 *it++ =
Binding{.input = input, .outputIndices = outputIndices};
1207 if (
const auto it = bindings.find(input); it != bindings.end()) {
1226 if (
const auto it = boundInputs.find(outputIndex); it != boundInputs.end()) {
1239 template <enumeration Action>
1262 return currentState.mousePosition;
1283 return previousState.mousePosition;
1307 if (currentState.mousePosition && previousState.mousePosition) {
1308 return *currentState.mousePosition - *previousState.mousePosition;
1353 return relativeState.transientMouseWheelScroll;
1419 return currentState.touchPosition;
1442 return previousState.touchPosition;
1467 if (currentState.touchPosition && previousState.touchPosition) {
1468 return *currentState.touchPosition - *previousState.touchPosition;
1520 return currentState.touchPressure;
1543 return previousState.touchPressure;
1568 if (currentState.touchPressure && previousState.touchPressure) {
1569 return *currentState.touchPressure - *previousState.touchPressure;
1611 return relativeState.transientPinchScale;
1653 return currentState.controllerLeftStickPosition;
1676 return previousState.controllerLeftStickPosition;
1701 if (currentState.controllerLeftStickPosition && previousState.controllerLeftStickPosition) {
1702 return i32vec2{*currentState.controllerLeftStickPosition} -
i32vec2{*previousState.controllerLeftStickPosition};
1756 return currentState.controllerRightStickPosition;
1779 return previousState.controllerRightStickPosition;
1804 if (currentState.controllerRightStickPosition && previousState.controllerRightStickPosition) {
1805 return i32vec2{*currentState.controllerRightStickPosition} -
i32vec2{*previousState.controllerRightStickPosition};
1858 return currentState.controllerLeftTriggerPosition;
1880 return previousState.controllerLeftTriggerPosition;
1904 if (currentState.controllerLeftTriggerPosition && previousState.controllerLeftTriggerPosition) {
1905 return static_cast<int32_t
>(*currentState.controllerLeftTriggerPosition) -
static_cast<int32_t
>(*previousState.controllerLeftTriggerPosition);
1951 return currentState.controllerRightTriggerPosition;
1973 return previousState.controllerRightTriggerPosition;
1997 if (currentState.controllerRightTriggerPosition && previousState.controllerRightTriggerPosition) {
1998 return static_cast<int32_t
>(*currentState.controllerRightTriggerPosition) -
static_cast<int32_t
>(*previousState.controllerRightTriggerPosition);
2077 return outputIndex <
OUTPUT_COUNT && relativeState.transientOutputPresses[outputIndex];
2094 return outputIndex <
OUTPUT_COUNT && relativeState.transientOutputReleases[outputIndex];
2112 return {.activePresses = 0, .value = 0.0f};
2115 ControlState result = currentState.outputExternalStates[outputIndex];
2116 if (
const auto it = boundInputs.find(outputIndex); it != boundInputs.end()) {
2117 for (
const Input input : it->second) {
2142 return {.activePresses = 0, .value = 0.0f};
2145 ControlState result = previousState.outputExternalStates[outputIndex];
2146 if (
const auto it = boundInputs.find(outputIndex); it != boundInputs.end()) {
2147 for (
const Input input : it->second) {
2172 return {.addedPresses = 0, .motion = 0.0f};
2175 ControlDelta result = relativeState.outputExternalDeltas[outputIndex];
2176 if (
const auto it = boundInputs.find(outputIndex); it != boundInputs.end()) {
2177 for (
const Input input : it->second) {
2569 return inputIndex <
INPUT_COUNT && relativeState.transientInputPresses[inputIndex];
2592 return inputIndex <
INPUT_COUNT && relativeState.transientInputReleases[inputIndex];
2613 return (inputIndex <
INPUT_COUNT) ? currentState.inputStates[inputIndex] :
ControlState{.activePresses = 0, .value = 0.0f};
2634 return (inputIndex <
INPUT_COUNT) ? previousState.inputStates[inputIndex] :
ControlState{.activePresses = 0, .value = 0.0f};
2655 return (inputIndex <
INPUT_COUNT) ? relativeState.inputDeltas[inputIndex] :
ControlDelta{.addedPresses = 0, .motion = 0.0f};
2873 Input inputPositiveZ)
const noexcept {
2912 Input inputPositiveZ)
const noexcept {
2951 Input inputPositiveZ)
const noexcept {
2970 bind(input, {getOutputIndex(actions)...});
2980 template <enumeration Action>
3004 template <enumeration Action>
3006 return isPressed(getOutputIndex(action));
3016 template <enumeration Action>
3028 template <enumeration Action>
3040 template <enumeration Action>
3052 template <enumeration Action>
3064 template <enumeration Action>
3076 template <enumeration Action>
3088 template <enumeration Action>
3092 getOutputIndex(actionNegative),
3093 getOutputIndex(actionPositive));
3103 template <enumeration Action>
3107 getOutputIndex(actionNegative),
3108 getOutputIndex(actionPositive));
3118 template <enumeration Action>
3122 getOutputIndex(actionNegative),
3123 getOutputIndex(actionPositive));
3133 template <enumeration Action>
3137 getOutputIndex(actionNegativeX),
3138 getOutputIndex(actionPositiveX),
3139 getOutputIndex(actionNegativeY),
3140 getOutputIndex(actionPositiveY));
3150 template <enumeration Action>
3154 getOutputIndex(actionNegativeX),
3155 getOutputIndex(actionPositiveX),
3156 getOutputIndex(actionNegativeY),
3157 getOutputIndex(actionPositiveY));
3167 template <enumeration Action>
3171 getOutputIndex(actionNegativeX),
3172 getOutputIndex(actionPositiveX),
3173 getOutputIndex(actionNegativeY),
3174 getOutputIndex(actionPositiveY));
3184 template <enumeration Action>
3188 getOutputIndex(actionNegativeX),
3189 getOutputIndex(actionPositiveX),
3190 getOutputIndex(actionNegativeY),
3191 getOutputIndex(actionPositiveY),
3192 getOutputIndex(actionNegativeZ),
3193 getOutputIndex(actionPositiveZ));
3203 template <enumeration Action>
3207 getOutputIndex(actionNegativeX),
3208 getOutputIndex(actionPositiveX),
3209 getOutputIndex(actionNegativeY),
3210 getOutputIndex(actionPositiveY),
3211 getOutputIndex(actionNegativeZ),
3212 getOutputIndex(actionPositiveZ));
3222 template <enumeration Action>
3226 getOutputIndex(actionNegativeX),
3227 getOutputIndex(actionPositiveX),
3228 getOutputIndex(actionNegativeY),
3229 getOutputIndex(actionPositiveY),
3230 getOutputIndex(actionNegativeZ),
3231 getOutputIndex(actionPositiveZ));
3235 struct AbsoluteState {
3238 Optional<float> touchPressure{};
3239 Optional<i16vec2> controllerLeftStickPosition{};
3240 Optional<i16vec2> controllerRightStickPosition{};
3241 Optional<int16_t> controllerLeftTriggerPosition{};
3242 Optional<int16_t> controllerRightTriggerPosition{};
3247 struct RelativeState {
3248 vec2 transientMouseWheelScroll{};
3249 float transientPinchScale = 1.0f;
3252 BitArray<INPUT_COUNT> transientInputPresses{};
3253 BitArray<INPUT_COUNT> transientInputReleases{};
3254 BitArray<OUTPUT_COUNT> transientOutputPresses{};
3255 BitArray<OUTPUT_COUNT> transientOutputReleases{};
3258 template <
typename Action>
3260 return static_cast<OutputIndex>(
static_cast<std::underlying_type_t<Action>
>(action));
3264 void setCursorPosition(
TimePoint timestamp, Optional<vec2>& position,
vec2 newPosition,
vec2 relativeMotion,
vec2 sensitivity,
Input inputLeft,
Input inputRight,
Input inputUp,
3267 void setMousePosition(
TimePoint timestamp,
vec2 newPosition,
vec2 relativeMotion) {
3268 setCursorPosition(timestamp, currentState.mousePosition, newPosition, relativeMotion, options.preferences.mouseSensitivity,
Input::MOUSE_MOTION_LEFT,
3272 GREM_API(events)
void scrollMouseWheel(
TimePoint timestamp,
vec2 scrollAmount);
3274 void setTouchPosition(
TimePoint timestamp,
vec2 newPosition,
vec2 relativeMotion) {
3275 setCursorPosition(timestamp, currentState.touchPosition, newPosition, relativeMotion, options.preferences.touchMotionSensitivity,
Input::TOUCH_FINGER_MOTION_LEFT,
3279 GREM_API(events)
void setTouchPressure(
TimePoint timestamp,
float newPressure);
3281 GREM_API(events)
void pinchFingers(
TimePoint timestamp,
float scale);
3284 void setControllerStickPosition(
TimePoint timestamp, Optional<i16vec2>& position,
i16vec2 newPosition,
vec2 sensitivity,
float curveExponent,
float innerDeadzone,
3287 void setControllerLeftStickPosition(
TimePoint timestamp,
i16vec2 newPosition) {
3288 setControllerStickPosition(timestamp, currentState.controllerLeftStickPosition, newPosition, options.preferences.controllerLeftStickSensitivity,
3289 options.preferences.controllerLeftStickCurveExponent, options.preferences.controllerLeftStickInnerDeadzone, options.preferences.controllerLeftStickOuterDeadzone,
3293 void setControllerRightStickPosition(
TimePoint timestamp,
i16vec2 newPosition) {
3294 setControllerStickPosition(timestamp, currentState.controllerRightStickPosition, newPosition, options.preferences.controllerRightStickSensitivity,
3295 options.preferences.controllerRightStickCurveExponent, options.preferences.controllerRightStickInnerDeadzone, options.preferences.controllerRightStickOuterDeadzone,
3300 void setControllerTriggerPosition(
TimePoint timestamp, Optional<int16_t>& position, int16_t newPosition,
float lowerDeadzone,
float upperDeadzone,
Input inputAxis);
3302 void setControllerLeftTriggerPosition(
TimePoint timestamp, int16_t newPosition) {
3303 setControllerTriggerPosition(timestamp, currentState.controllerLeftTriggerPosition, newPosition, options.preferences.controllerLeftTriggerLowerDeadzone,
3307 void setControllerRightTriggerPosition(
TimePoint timestamp, int16_t newPosition) {
3308 setControllerTriggerPosition(timestamp, currentState.controllerRightTriggerPosition, newPosition, options.preferences.controllerRightTriggerLowerDeadzone,
3312 InputManagerPreferences initialPreferences;
3313 InputManagerOptions options;
3314 HashMap<Input, ArrayList<OutputIndex>> bindings{};
3315 HashMap<OutputIndex, ArrayList<Input>> boundInputs{};
3316 AbsoluteState currentState{};
3317 AbsoluteState previousState{};
3318 RelativeState relativeState{};
3319 ArrayList<OutputEvent> polledOutputEvents{};
3320 ArrayList<OutputEvent> pendingOutputEvents{};
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
Definition Allocation.hpp:25
constexpr iterator begin() noexcept
Definition Allocation.hpp:433
Definition ArrayList.hpp:27
Base interface for a filesystem.
Definition Filesystem.hpp:25
String readInputFileString(CStringView filepath) const
Open an input file in the virtual filesystem for reading and read its full contents into a string of ...
Definition Filesystem.hpp:382
virtual void createParentOutputDirectories(CStringView filepath)=0
Create any missing parent directories above a host directory relative to the current output directory...
virtual OutputFileHandle openEmptyOutputFile(CStringView filepath)=0
Create a file relative to the current output directory and open it for writing, overwriting any exist...
Definition FunctionView.hpp:29
Definition HashMap.hpp:30
constexpr iterator end() noexcept
Definition HashMap.hpp:712
constexpr iterator find(const Key &key)
Definition HashMap.hpp:626
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
GREM_ALWAYS_INLINE void write(Span< const byte > data)
Write data to the end of an open file.
Definition File.hpp:809
Definition Reader.hpp:658
constexpr size_type size() const noexcept
Definition Span.hpp:207
friend class Variant
Definition Variant.hpp:1402
JSON value type.
Definition json.hpp:538
Definition concepts.hpp:94
Definition concepts.hpp:35
constexpr String convertToLowercaseString(StringView string)
Convert each character in a string to its corresponding lowercase ASCII letter (a-z) if it has one.
Definition ascii.hpp:210
constexpr String convertToUppercaseString(StringView string)
Convert each character in a string to its corresponding uppercase ASCII letter (A-Z) if it has one.
Definition ascii.hpp:250
Definition controller.hpp:11
constexpr size_t getInputIndex(Input input) noexcept
Get the 0-based index of an Input.
Definition Input.hpp:207
Input
General identifier for a specific control on a physical input device, such as a certain keyboard key,...
Definition Input.hpp:182
@ CONTROLLER_AXIS_RIGHT_TRIGGER
Controller right trigger (R Trigger/R2).
Definition Input.hpp:184
@ TOUCH_FINGER_PRESSURE
Finger pressure.
Definition Input.hpp:184
@ TOUCH_FINGER_MOTION_RIGHT
Finger right movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_LEFT_STICK_RIGHT
Controller left analog stick rightward movement.
Definition Input.hpp:184
@ MOUSE_MOTION_LEFT
Mouse left movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_RIGHT_STICK_RIGHT
Controller right analog stick rightward movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_LEFT_STICK_UP
Controller left analog stick upward movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_RIGHT_STICK_UP
Controller right analog stick upward movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_LEFT_STICK_DOWN
Controller left analog stick downward movement.
Definition Input.hpp:184
@ MOUSE_SCROLL_LEFT
Mouse scroll wheel leftward movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_LEFT_TRIGGER
Controller left trigger (L Trigger/L2).
Definition Input.hpp:184
@ MOUSE_SCROLL_DOWN
Mouse scroll wheel downward movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_LEFT_STICK_LEFT
Controller left analog stick leftward movement.
Definition Input.hpp:184
@ MOUSE_MOTION_RIGHT
Mouse right movement.
Definition Input.hpp:184
@ TOUCH_FINGER_MOTION_UP
Finger up movement.
Definition Input.hpp:184
@ TOUCH_FINGER_MOTION_DOWN
Finger down movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_RIGHT_STICK_DOWN
Controller right analog stick downward movement.
Definition Input.hpp:184
@ MOUSE_SCROLL_RIGHT
Mouse scroll wheel rightward movement.
Definition Input.hpp:184
@ PINCH_FINGERS_MOTION_TOGETHER
Finger pinch "zoom out" movement.
Definition Input.hpp:184
@ MOUSE_SCROLL_UP
Mouse scroll wheel upward movement.
Definition Input.hpp:184
@ TOUCH_FINGER_MOTION_LEFT
Finger left movement.
Definition Input.hpp:184
@ MOUSE_MOTION_UP
Mouse up movement.
Definition Input.hpp:184
@ MOUSE_MOTION_DOWN
Mouse down movement.
Definition Input.hpp:184
@ CONTROLLER_AXIS_RIGHT_STICK_LEFT
Controller right analog stick leftward movement.
Definition Input.hpp:184
constexpr size_t INPUT_COUNT
Total number of inputs that exist in the Input enumeration.
Definition Input.hpp:197
Definition aliases.hpp:15
json::VariantBase< std::allocator > Variant
JSON value type using the default allocator.
Definition json.hpp:1094
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
vec< 2, float > vec2
Definition math.hpp:438
Clock::time_point TimePoint
Definition Clock.hpp:26
vec< 3, int32_t > i32vec3
Definition math.hpp:415
vec< 3, float > vec3
Definition math.hpp:439
StringBase< char, std::char_traits< char >, std::allocator< char > > String
Definition String.hpp:17
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
vec< 2, int32_t > i32vec2
Definition math.hpp:414
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertDegreesToRadians(T angleInDegrees)
Definition math.hpp:3390
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL scale(vec< 2, T > scale)
Create a scale matrix describing a 2D scale.
Definition math.hpp:2665
vec< 2, int16_t > i16vec2
Definition math.hpp:406
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635
Data structure containing information about an event.
Definition Event.hpp:370
Stateful wrapper object of an input stream for JSON deserialization.
Definition json.hpp:5525