From 071a73c6d98a9a187be03fcfa7c9b623fa6977a2 Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Tue, 26 Sep 2023 10:03:34 -0500 Subject: [PATCH] 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. ///