From f80ded20573a6f1d85e79fd42bee1696d47567a0 Mon Sep 17 00:00:00 2001 From: Olly Rybak Date: Thu, 11 May 2023 17:30:20 +1000 Subject: [PATCH 1/5] Initial setup --- Source/Engine/Input/Enums.h | 25 +++++++++++++++++++++++++ Source/Engine/Input/Input.cpp | 20 ++++++++++++++++++++ Source/Engine/Input/Input.h | 8 ++++++++ Source/Engine/Input/VirtualInput.h | 3 +++ 4 files changed, 56 insertions(+) diff --git a/Source/Engine/Input/Enums.h b/Source/Engine/Input/Enums.h index 80e10786f..272231e41 100644 --- a/Source/Engine/Input/Enums.h +++ b/Source/Engine/Input/Enums.h @@ -263,6 +263,31 @@ API_ENUM() enum class InputActionMode Release = 2, }; +/// +/// The input action event trigger modes. +/// +API_ENUM() enum class InputActionPhase +{ + /// + /// User is pressing the key/button. + /// + Pressing = 0, + + /// + /// User pressed the key/button (but wasn't pressing it in the previous frame). + /// + Press = 1, + + /// + /// User released the key/button (was pressing it in the previous frame). + /// + Release = 2, + + Waiting = 3, + + None = 4, +}; + /// /// The input gamepad index. /// diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 7a4547c62..32a672635 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -29,11 +29,13 @@ struct ActionData { bool Active; uint64 FrameIndex; + InputActionPhase Phase; ActionData() { Active = false; FrameIndex = 0; + Phase = InputActionPhase::None; } }; @@ -597,6 +599,16 @@ bool Input::GetAction(const StringView& name) return e ? e->Active : false; } +InputActionPhase Input::GetActionPhase(const StringView& name) +{ + const auto e = Actions.TryGet(name); + if (e != nullptr) + { + return e->Phase; + } + return InputActionPhase::None; +} + float Input::GetAxis(const StringView& name) { const auto e = Axes.TryGet(name); @@ -806,6 +818,7 @@ void InputService::Update() ActionData& data = Actions[name]; data.Active = false; + data.Phase = InputActionPhase::Waiting; // Mark as updated in this frame data.FrameIndex = frame; @@ -830,6 +843,13 @@ void InputService::Update() isActive = Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton); } + if (Input::GetKeyDown(config.Key) || Input::GetMouseButtonDown(config.MouseButton) || Input::GetGamepadButtonDown(config.Gamepad, config.GamepadButton)) + data.Phase = InputActionPhase::Press; + else if (Input::GetKey(config.Key) || Input::GetMouseButton(config.MouseButton) || Input::GetGamepadButton(config.Gamepad, config.GamepadButton)) + data.Phase = InputActionPhase::Pressing; + else if (Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton)) + data.Phase = InputActionPhase::Release; + data.Active |= isActive; } diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index d9f86076b..dd40c485a 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -309,6 +309,14 @@ public: /// API_FUNCTION() static bool GetAction(const StringView& name); + /// + /// Gets the value of the virtual action identified by name. Use to get the current config. + /// + /// The action name. + /// True if action has been triggered in the current frame (e.g. button pressed), otherwise false. + /// + API_FUNCTION() static InputActionPhase GetActionPhase(const StringView& name); + /// /// Gets the value of the virtual axis identified by name. Use to get the current config. /// diff --git a/Source/Engine/Input/VirtualInput.h b/Source/Engine/Input/VirtualInput.h index 8fd4efeb4..e8f669632 100644 --- a/Source/Engine/Input/VirtualInput.h +++ b/Source/Engine/Input/VirtualInput.h @@ -48,6 +48,9 @@ API_STRUCT() struct ActionConfig /// API_FIELD(Attributes="EditorOrder(40)") InputGamepadIndex Gamepad; + + API_FIELD(Attributes = "EditorOrder(50)") + InputActionMode Phase; }; /// From 395a72b7d45fc8d50be120cef62a277aff7efa13 Mon Sep 17 00:00:00 2001 From: Olly Rybak Date: Thu, 11 May 2023 21:10:40 +1000 Subject: [PATCH 2/5] Added comments / reformat --- Source/Engine/Input/Enums.h | 22 ++++++++++++++-------- Source/Engine/Input/Input.cpp | 10 ++++++++-- Source/Engine/Input/Input.h | 2 +- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Source/Engine/Input/Enums.h b/Source/Engine/Input/Enums.h index 272231e41..4c96f715f 100644 --- a/Source/Engine/Input/Enums.h +++ b/Source/Engine/Input/Enums.h @@ -264,28 +264,34 @@ API_ENUM() enum class InputActionMode }; /// -/// The input action event trigger modes. +/// The input action event phases. /// API_ENUM() enum class InputActionPhase { + /// + /// The key/button is not assigned. + /// + None = 0, + + /// + /// The key/button is waiting for input. + /// + Waiting = 1, + /// /// User is pressing the key/button. /// - Pressing = 0, + Pressing = 2, /// /// User pressed the key/button (but wasn't pressing it in the previous frame). /// - Press = 1, + Press = 3, /// /// User released the key/button (was pressing it in the previous frame). /// - Release = 2, - - Waiting = 3, - - None = 4, + Release = 4, }; /// diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 32a672635..2f45a4a16 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -25,7 +25,7 @@ struct AxisEvaluation bool Used; }; -struct ActionData +struct ActionData { bool Active; uint64 FrameIndex; @@ -35,7 +35,7 @@ struct ActionData { Active = false; FrameIndex = 0; - Phase = InputActionPhase::None; + Phase = InputActionPhase::Waiting; } }; @@ -844,11 +844,17 @@ void InputService::Update() } if (Input::GetKeyDown(config.Key) || Input::GetMouseButtonDown(config.MouseButton) || Input::GetGamepadButtonDown(config.Gamepad, config.GamepadButton)) + { data.Phase = InputActionPhase::Press; + } else if (Input::GetKey(config.Key) || Input::GetMouseButton(config.MouseButton) || Input::GetGamepadButton(config.Gamepad, config.GamepadButton)) + { data.Phase = InputActionPhase::Pressing; + } else if (Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton)) + { data.Phase = InputActionPhase::Release; + } data.Active |= isActive; } diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index dd40c485a..35f3dac32 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -313,7 +313,7 @@ public: /// Gets the value of the virtual action identified by name. Use to get the current config. /// /// The action name. - /// True if action has been triggered in the current frame (e.g. button pressed), otherwise false. + /// A InputActionPhase determining the current phase of the Action (e.g If it was just pressed, is being held or just released). /// API_FUNCTION() static InputActionPhase GetActionPhase(const StringView& name); From d5237715a5b14cec7321b2cc8006c5d73481a540 Mon Sep 17 00:00:00 2001 From: Olly Rybak Date: Thu, 11 May 2023 21:12:03 +1000 Subject: [PATCH 3/5] Removed unused field --- Source/Engine/Input/VirtualInput.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/Engine/Input/VirtualInput.h b/Source/Engine/Input/VirtualInput.h index e8f669632..8fd4efeb4 100644 --- a/Source/Engine/Input/VirtualInput.h +++ b/Source/Engine/Input/VirtualInput.h @@ -48,9 +48,6 @@ API_STRUCT() struct ActionConfig /// API_FIELD(Attributes="EditorOrder(40)") InputGamepadIndex Gamepad; - - API_FIELD(Attributes = "EditorOrder(50)") - InputActionMode Phase; }; /// From 4b4bf833a93fb6d2d28994f1f700b54ed16a9bae Mon Sep 17 00:00:00 2001 From: Olly Rybak Date: Fri, 12 May 2023 00:25:03 +1000 Subject: [PATCH 4/5] Rename Phase > State --- Source/Engine/Input/Enums.h | 2 +- Source/Engine/Input/Input.cpp | 16 ++++++++-------- Source/Engine/Input/Input.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Source/Engine/Input/Enums.h b/Source/Engine/Input/Enums.h index 4c96f715f..82498f500 100644 --- a/Source/Engine/Input/Enums.h +++ b/Source/Engine/Input/Enums.h @@ -266,7 +266,7 @@ API_ENUM() enum class InputActionMode /// /// The input action event phases. /// -API_ENUM() enum class InputActionPhase +API_ENUM() enum class InputActionState { /// /// The key/button is not assigned. diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 2f45a4a16..b22efa4fc 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -29,13 +29,13 @@ struct ActionData { bool Active; uint64 FrameIndex; - InputActionPhase Phase; + InputActionState Phase; ActionData() { Active = false; FrameIndex = 0; - Phase = InputActionPhase::Waiting; + Phase = InputActionState::Waiting; } }; @@ -599,14 +599,14 @@ bool Input::GetAction(const StringView& name) return e ? e->Active : false; } -InputActionPhase Input::GetActionPhase(const StringView& name) +InputActionState Input::GetActionState(const StringView& name) { const auto e = Actions.TryGet(name); if (e != nullptr) { return e->Phase; } - return InputActionPhase::None; + return InputActionState::None; } float Input::GetAxis(const StringView& name) @@ -818,7 +818,7 @@ void InputService::Update() ActionData& data = Actions[name]; data.Active = false; - data.Phase = InputActionPhase::Waiting; + data.Phase = InputActionState::Waiting; // Mark as updated in this frame data.FrameIndex = frame; @@ -845,15 +845,15 @@ void InputService::Update() if (Input::GetKeyDown(config.Key) || Input::GetMouseButtonDown(config.MouseButton) || Input::GetGamepadButtonDown(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionPhase::Press; + data.Phase = InputActionState::Press; } else if (Input::GetKey(config.Key) || Input::GetMouseButton(config.MouseButton) || Input::GetGamepadButton(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionPhase::Pressing; + data.Phase = InputActionState::Pressing; } else if (Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionPhase::Release; + data.Phase = InputActionState::Release; } data.Active |= isActive; diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index 35f3dac32..1fcb352ea 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -315,7 +315,7 @@ public: /// The action name. /// A InputActionPhase determining the current phase of the Action (e.g If it was just pressed, is being held or just released). /// - API_FUNCTION() static InputActionPhase GetActionPhase(const StringView& name); + API_FUNCTION() static InputActionState GetActionState(const StringView& name); /// /// Gets the value of the virtual axis identified by name. Use to get the current config. From b8ed115f63097210b667fbb67ba1e43216deec5e Mon Sep 17 00:00:00 2001 From: Olly Rybak Date: Fri, 12 May 2023 09:30:35 +1000 Subject: [PATCH 5/5] More renames --- Source/Engine/Input/Input.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index b22efa4fc..8658b984c 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -29,13 +29,13 @@ struct ActionData { bool Active; uint64 FrameIndex; - InputActionState Phase; + InputActionState State; ActionData() { Active = false; FrameIndex = 0; - Phase = InputActionState::Waiting; + State = InputActionState::Waiting; } }; @@ -604,7 +604,7 @@ InputActionState Input::GetActionState(const StringView& name) const auto e = Actions.TryGet(name); if (e != nullptr) { - return e->Phase; + return e->State; } return InputActionState::None; } @@ -818,7 +818,7 @@ void InputService::Update() ActionData& data = Actions[name]; data.Active = false; - data.Phase = InputActionState::Waiting; + data.State = InputActionState::Waiting; // Mark as updated in this frame data.FrameIndex = frame; @@ -845,15 +845,15 @@ void InputService::Update() if (Input::GetKeyDown(config.Key) || Input::GetMouseButtonDown(config.MouseButton) || Input::GetGamepadButtonDown(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionState::Press; + data.State = InputActionState::Press; } else if (Input::GetKey(config.Key) || Input::GetMouseButton(config.MouseButton) || Input::GetGamepadButton(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionState::Pressing; + data.State = InputActionState::Pressing; } else if (Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton)) { - data.Phase = InputActionState::Release; + data.State = InputActionState::Release; } data.Active |= isActive;