From ea1da0481fee1fc2ef2b69de8dd75abe21e305e9 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 08:35:10 -0500 Subject: [PATCH 1/9] Change input event to trigger different input states. --- Source/Engine/Engine/InputEvent.cs | 37 +++++++++++++++++++++++++----- Source/Engine/Input/Input.cpp | 6 ++--- Source/Engine/Input/Input.h | 2 +- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Source/Engine/Engine/InputEvent.cs b/Source/Engine/Engine/InputEvent.cs index 176d21fd0..c6c46c43d 100644 --- a/Source/Engine/Engine/InputEvent.cs +++ b/Source/Engine/Engine/InputEvent.cs @@ -16,14 +16,24 @@ namespace FlaxEngine public string Name; /// - /// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use to catch events without active waiting. + /// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use , , to catch events without active waiting. /// public bool Active => Input.GetAction(Name); /// - /// Occurs when event is triggered (e.g. user pressed a key). Called before scripts update. + /// Occurs when event is pressed (e.g. user pressed a key). Called before scripts update. /// - public event Action Triggered; + public event Action Pressed; + + /// + /// Occurs when event is being pressing (e.g. user pressing a key). Called before scripts update. + /// + public event Action Pressing; + + /// + /// Occurs when event is released (e.g. user releases a key). Called before scripts update. + /// + public event Action Released; /// /// Initializes a new instance of the class. @@ -51,10 +61,25 @@ namespace FlaxEngine Input.ActionTriggered -= Handler; } - private void Handler(string name) + private void Handler(string name, InputActionState state) { - if (string.Equals(name, Name, StringComparison.OrdinalIgnoreCase)) - Triggered?.Invoke(); + if (!string.Equals(name, Name, StringComparison.OrdinalIgnoreCase)) + return; + switch (state) + { + case InputActionState.None: break; + case InputActionState.Waiting: break; + case InputActionState.Pressing: + Pressing?.Invoke(); + break; + case InputActionState.Press: + Pressed?.Invoke(); + break; + case InputActionState.Release: + Released?.Invoke(); + break; + default: break; + } } /// diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 81411ed76..af5600823 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -97,7 +97,7 @@ Action Input::MouseLeave; Delegate Input::TouchDown; Delegate Input::TouchMove; Delegate Input::TouchUp; -Delegate Input::ActionTriggered; +Delegate Input::ActionTriggered; Array Input::ActionMappings; Array Input::AxisMappings; @@ -1022,9 +1022,9 @@ void InputService::Update() { for (auto i = Actions.Begin(); i.IsNotEnd(); ++i) { - if (i->Value.Active) + if (i->Value.State != InputActionState::Waiting) { - Input::ActionTriggered(i->Key); + Input::ActionTriggered(i->Key, i->Value.State); } } } diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index 84d312fa3..5ff02eda9 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -293,7 +293,7 @@ public: /// Event fired when virtual input action is triggered. Called before scripts update. See to edit configuration. /// /// - API_EVENT() static Delegate ActionTriggered; + API_EVENT() static Delegate ActionTriggered; /// /// Gets the value of the virtual action identified by name. Use to get the current config. From eeab9774a91d2425048c11b3f1eb6270e133c38d Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 09:31:23 -0500 Subject: [PATCH 2/9] Add easily getting action state --- Source/Engine/Engine/InputEvent.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Engine/InputEvent.cs b/Source/Engine/Engine/InputEvent.cs index c6c46c43d..b8b3dbfa9 100644 --- a/Source/Engine/Engine/InputEvent.cs +++ b/Source/Engine/Engine/InputEvent.cs @@ -16,10 +16,15 @@ namespace FlaxEngine public string Name; /// - /// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use , , to catch events without active waiting. + /// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use to catch events without active waiting. /// public bool Active => Input.GetAction(Name); + /// + /// Returns the event state. Use Use , , to catch events without active waiting. + /// + public InputActionState State => Input.GetActionState(Name); + /// /// Occurs when event is pressed (e.g. user pressed a key). Called before scripts update. /// From ed2dd7211ae786add63b0b5908ae01215f6a97fa Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 09:40:30 -0500 Subject: [PATCH 3/9] Add back depriciated Triggered event. --- Source/Engine/Engine/InputEvent.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Source/Engine/Engine/InputEvent.cs b/Source/Engine/Engine/InputEvent.cs index b8b3dbfa9..0cfc47896 100644 --- a/Source/Engine/Engine/InputEvent.cs +++ b/Source/Engine/Engine/InputEvent.cs @@ -25,6 +25,12 @@ namespace FlaxEngine /// public InputActionState State => Input.GetActionState(Name); + /// + /// Occurs when event is triggered (e.g. user pressed a key). Called before scripts update. + /// + [System.Obsolete("Depreciated in 1.7, use Pressed.")] + public event Action Triggered; + /// /// Occurs when event is pressed (e.g. user pressed a key). Called before scripts update. /// @@ -75,6 +81,7 @@ namespace FlaxEngine case InputActionState.None: break; case InputActionState.Waiting: break; case InputActionState.Pressing: + Triggered?.Invoke(); Pressing?.Invoke(); break; case InputActionState.Press: From 3a9dd3f8f85b0c53d6818c1ce36f61cb11e2fdb4 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 09:41:41 -0500 Subject: [PATCH 4/9] Small wording change --- Source/Engine/Engine/InputEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Engine/InputEvent.cs b/Source/Engine/Engine/InputEvent.cs index 0cfc47896..8c84a8a7f 100644 --- a/Source/Engine/Engine/InputEvent.cs +++ b/Source/Engine/Engine/InputEvent.cs @@ -28,7 +28,7 @@ namespace FlaxEngine /// /// Occurs when event is triggered (e.g. user pressed a key). Called before scripts update. /// - [System.Obsolete("Depreciated in 1.7, use Pressed.")] + [System.Obsolete("Depreciated in 1.7, use Pressed Action.")] public event Action Triggered; /// From 071a73c6d98a9a187be03fcfa7c9b623fa6977a2 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 10:03:34 -0500 Subject: [PATCH 5/9] Add AxisChanged event to InputAxis. --- Source/Engine/Engine/InputAxis.cs | 32 +++++++++++++++++++++++++++++++ Source/Engine/Input/Input.cpp | 9 +++++++++ Source/Engine/Input/Input.h | 6 ++++++ 3 files changed, 47 insertions(+) diff --git a/Source/Engine/Engine/InputAxis.cs b/Source/Engine/Engine/InputAxis.cs index f8ba7e72b..7b4da3191 100644 --- a/Source/Engine/Engine/InputAxis.cs +++ b/Source/Engine/Engine/InputAxis.cs @@ -1,5 +1,7 @@ // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. +using System; + namespace FlaxEngine { /// @@ -23,11 +25,17 @@ namespace FlaxEngine /// public float ValueRaw => Input.GetAxisRaw(Name); + /// + /// Occurs when axis is changed. Called before scripts update. + /// + public event Action ValueChanged; + /// /// Initializes a new instance of the class. /// public InputAxis() { + Input.AxisChanged += Handler; } /// @@ -36,7 +44,31 @@ namespace FlaxEngine /// The axis name. public InputAxis(string name) { + Input.AxisChanged += Handler; Name = name; } + + private void Handler(string name) + { + if (string.Equals(Name, name, StringComparison.OrdinalIgnoreCase)) + ValueChanged?.Invoke(); + } + + /// + /// Finalizes an instance of the class. + /// + ~InputAxis() + { + Input.AxisChanged -= Handler; + } + + /// + /// Releases this object. + /// + public void Dispose() + { + Input.AxisChanged -= Handler; + GC.SuppressFinalize(this); + } } } diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index af5600823..a54b6d631 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -98,6 +98,7 @@ Delegate Input::TouchDown; Delegate Input::TouchMove; Delegate Input::TouchUp; Delegate Input::ActionTriggered; +Delegate Input::AxisChanged; Array Input::ActionMappings; Array Input::AxisMappings; @@ -1020,6 +1021,14 @@ void InputService::Update() // Send events for the active actions (send events only in play mode) if (!Time::GetGamePaused()) { + for (auto i = Axes.Begin(); i.IsNotEnd(); ++i) + { + if (Math::NotNearEqual(i->Value.Value, i->Value.PrevKeyValue)) + { + Input::AxisChanged(i->Key); + } + } + for (auto i = Actions.Begin(); i.IsNotEnd(); ++i) { if (i->Value.State != InputActionState::Waiting) diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index 5ff02eda9..ed66a6cd9 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -295,6 +295,12 @@ public: /// API_EVENT() static Delegate ActionTriggered; + /// + /// Event fired when virtual input axis is changed. Called before scripts update. See to edit configuration. + /// + /// + API_EVENT() static Delegate AxisChanged; + /// /// Gets the value of the virtual action identified by name. Use to get the current config. /// From 9b48d1feb85a52f867788ac6d4594d2e095fbe8a Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 10:04:08 -0500 Subject: [PATCH 6/9] Update comments --- Source/Engine/Input/Input.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index a54b6d631..2a098088e 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -1018,7 +1018,7 @@ void InputService::Update() Input::SetMousePosition(Screen::GetSize() * 0.5f); } - // Send events for the active actions (send events only in play mode) + // Send events for the active actions and axes (send events only in play mode) if (!Time::GetGamePaused()) { for (auto i = Axes.Begin(); i.IsNotEnd(); ++i) From d14dc56b5c8f9d62bf6ec5ab265d22952d8caf54 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 10:26:06 -0500 Subject: [PATCH 7/9] Change name to AxisValueChanged. --- Source/Engine/Engine/InputAxis.cs | 8 ++++---- Source/Engine/Input/Input.cpp | 4 ++-- Source/Engine/Input/Input.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Source/Engine/Engine/InputAxis.cs b/Source/Engine/Engine/InputAxis.cs index 7b4da3191..c70bada74 100644 --- a/Source/Engine/Engine/InputAxis.cs +++ b/Source/Engine/Engine/InputAxis.cs @@ -35,7 +35,7 @@ namespace FlaxEngine /// public InputAxis() { - Input.AxisChanged += Handler; + Input.AxisValueChanged += Handler; } /// @@ -44,7 +44,7 @@ namespace FlaxEngine /// The axis name. public InputAxis(string name) { - Input.AxisChanged += Handler; + Input.AxisValueChanged += Handler; Name = name; } @@ -59,7 +59,7 @@ namespace FlaxEngine /// ~InputAxis() { - Input.AxisChanged -= Handler; + Input.AxisValueChanged -= Handler; } /// @@ -67,7 +67,7 @@ namespace FlaxEngine /// public void Dispose() { - Input.AxisChanged -= Handler; + Input.AxisValueChanged -= Handler; GC.SuppressFinalize(this); } } diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 2a098088e..d3c09c130 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -98,7 +98,7 @@ Delegate Input::TouchDown; Delegate Input::TouchMove; Delegate Input::TouchUp; Delegate Input::ActionTriggered; -Delegate Input::AxisChanged; +Delegate Input::AxisValueChanged; Array Input::ActionMappings; Array Input::AxisMappings; @@ -1025,7 +1025,7 @@ void InputService::Update() { if (Math::NotNearEqual(i->Value.Value, i->Value.PrevKeyValue)) { - Input::AxisChanged(i->Key); + Input::AxisValueChanged(i->Key); } } diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index ed66a6cd9..eb1299334 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -299,7 +299,7 @@ public: /// Event fired when virtual input axis is changed. Called before scripts update. See to edit configuration. /// /// - API_EVENT() static Delegate AxisChanged; + API_EVENT() static Delegate AxisValueChanged; /// /// Gets the value of the virtual action identified by name. Use to get the current config. From 9694522b4ea367fc7efc232ae0d74734fe02d372 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Thu, 28 Sep 2023 13:25:56 -0500 Subject: [PATCH 8/9] Change to use enum value. --- Source/Engine/Input/Input.cpp | 2 +- Source/Engine/Input/Input.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index d3c09c130..dbcdf66bd 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -97,7 +97,7 @@ Action Input::MouseLeave; Delegate Input::TouchDown; Delegate Input::TouchMove; Delegate Input::TouchUp; -Delegate Input::ActionTriggered; +Delegate Input::ActionTriggered; Delegate Input::AxisValueChanged; Array Input::ActionMappings; Array Input::AxisMappings; diff --git a/Source/Engine/Input/Input.h b/Source/Engine/Input/Input.h index eb1299334..c11026eff 100644 --- a/Source/Engine/Input/Input.h +++ b/Source/Engine/Input/Input.h @@ -293,7 +293,7 @@ public: /// Event fired when virtual input action is triggered. Called before scripts update. See to edit configuration. /// /// - API_EVENT() static Delegate ActionTriggered; + API_EVENT() static Delegate ActionTriggered; /// /// Event fired when virtual input axis is changed. Called before scripts update. See to edit configuration. From 4e42f84873d86711b0c5d40883be5ad15911efe0 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Mon, 2 Oct 2023 08:39:13 -0500 Subject: [PATCH 9/9] Fix bug in depreciated triggered action placement. --- Source/Engine/Engine/InputEvent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Engine/InputEvent.cs b/Source/Engine/Engine/InputEvent.cs index 8c84a8a7f..0267aed96 100644 --- a/Source/Engine/Engine/InputEvent.cs +++ b/Source/Engine/Engine/InputEvent.cs @@ -81,10 +81,10 @@ namespace FlaxEngine case InputActionState.None: break; case InputActionState.Waiting: break; case InputActionState.Pressing: - Triggered?.Invoke(); Pressing?.Invoke(); break; case InputActionState.Press: + Triggered?.Invoke(); Pressed?.Invoke(); break; case InputActionState.Release: