Add AxisChanged event to InputAxis.

This commit is contained in:
Chandler Cox
2023-09-26 10:03:34 -05:00
parent 3a9dd3f8f8
commit 071a73c6d9
3 changed files with 47 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
namespace FlaxEngine
{
/// <summary>
@@ -23,11 +25,17 @@ namespace FlaxEngine
/// </summary>
public float ValueRaw => Input.GetAxisRaw(Name);
/// <summary>
/// Occurs when axis is changed. Called before scripts update.
/// </summary>
public event Action ValueChanged;
/// <summary>
/// Initializes a new instance of the <see cref="InputAxis"/> class.
/// </summary>
public InputAxis()
{
Input.AxisChanged += Handler;
}
/// <summary>
@@ -36,7 +44,31 @@ namespace FlaxEngine
/// <param name="name">The axis name.</param>
public InputAxis(string name)
{
Input.AxisChanged += Handler;
Name = name;
}
private void Handler(string name)
{
if (string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
ValueChanged?.Invoke();
}
/// <summary>
/// Finalizes an instance of the <see cref="InputAxis"/> class.
/// </summary>
~InputAxis()
{
Input.AxisChanged -= Handler;
}
/// <summary>
/// Releases this object.
/// </summary>
public void Dispose()
{
Input.AxisChanged -= Handler;
GC.SuppressFinalize(this);
}
}
}

View File

@@ -98,6 +98,7 @@ Delegate<const Float2&, int32> Input::TouchDown;
Delegate<const Float2&, int32> Input::TouchMove;
Delegate<const Float2&, int32> Input::TouchUp;
Delegate<StringView, const InputActionState&> Input::ActionTriggered;
Delegate<StringView> Input::AxisChanged;
Array<ActionConfig> Input::ActionMappings;
Array<AxisConfig> 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)

View File

@@ -295,6 +295,12 @@ public:
/// <seealso cref="InputEvent"/>
API_EVENT() static Delegate<StringView, const InputActionState&> ActionTriggered;
/// <summary>
/// Event fired when virtual input axis is changed. Called before scripts update. See <see cref="AxisMappings"/> to edit configuration.
/// </summary>
/// <seealso cref="InputAxis"/>
API_EVENT() static Delegate<StringView> AxisChanged;
/// <summary>
/// Gets the value of the virtual action identified by name. Use <see cref="ActionMappings"/> to get the current config.
/// </summary>