Merge branch 'input' of https://github.com/Tryibion/FlaxEngine into Tryibion-input

This commit is contained in:
Wojtek Figat
2023-10-02 21:30:31 +02:00
4 changed files with 92 additions and 8 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.AxisValueChanged += Handler;
}
/// <summary>
@@ -36,7 +44,31 @@ namespace FlaxEngine
/// <param name="name">The axis name.</param>
public InputAxis(string name)
{
Input.AxisValueChanged += 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.AxisValueChanged -= Handler;
}
/// <summary>
/// Releases this object.
/// </summary>
public void Dispose()
{
Input.AxisValueChanged -= Handler;
GC.SuppressFinalize(this);
}
}
}

View File

@@ -16,15 +16,36 @@ namespace FlaxEngine
public string Name;
/// <summary>
/// Returns true if the event has been triggered during the current frame (e.g. user pressed a key). Use <see cref="Triggered"/> 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 <see cref="Pressed"/> to catch events without active waiting.
/// </summary>
public bool Active => Input.GetAction(Name);
/// <summary>
/// Returns the event state. Use Use <see cref="Pressed"/>, <see cref="Pressing"/>, <see cref="Released"/> to catch events without active waiting.
/// </summary>
public InputActionState State => Input.GetActionState(Name);
/// <summary>
/// Occurs when event is triggered (e.g. user pressed a key). Called before scripts update.
/// </summary>
[System.Obsolete("Depreciated in 1.7, use Pressed Action.")]
public event Action Triggered;
/// <summary>
/// Occurs when event is pressed (e.g. user pressed a key). Called before scripts update.
/// </summary>
public event Action Pressed;
/// <summary>
/// Occurs when event is being pressing (e.g. user pressing a key). Called before scripts update.
/// </summary>
public event Action Pressing;
/// <summary>
/// Occurs when event is released (e.g. user releases a key). Called before scripts update.
/// </summary>
public event Action Released;
/// <summary>
/// Initializes a new instance of the <see cref="InputEvent"/> class.
/// </summary>
@@ -51,10 +72,26 @@ namespace FlaxEngine
Input.ActionTriggered -= Handler;
}
private void Handler(string name)
private void Handler(string name, InputActionState state)
{
if (string.Equals(name, Name, StringComparison.OrdinalIgnoreCase))
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:
Triggered?.Invoke();
Pressed?.Invoke();
break;
case InputActionState.Release:
Released?.Invoke();
break;
default: break;
}
}
/// <summary>

View File

@@ -97,7 +97,8 @@ Action Input::MouseLeave;
Delegate<const Float2&, int32> Input::TouchDown;
Delegate<const Float2&, int32> Input::TouchMove;
Delegate<const Float2&, int32> Input::TouchUp;
Delegate<StringView> Input::ActionTriggered;
Delegate<StringView, InputActionState> Input::ActionTriggered;
Delegate<StringView> Input::AxisValueChanged;
Array<ActionConfig> Input::ActionMappings;
Array<AxisConfig> Input::AxisMappings;
@@ -1017,14 +1018,22 @@ 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)
{
if (Math::NotNearEqual(i->Value.Value, i->Value.PrevKeyValue))
{
Input::AxisValueChanged(i->Key);
}
}
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);
}
}
}

View File

@@ -293,7 +293,13 @@ public:
/// Event fired when virtual input action is triggered. Called before scripts update. See <see cref="ActionMappings"/> to edit configuration.
/// </summary>
/// <seealso cref="InputEvent"/>
API_EVENT() static Delegate<StringView> ActionTriggered;
API_EVENT() static Delegate<StringView, 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> AxisValueChanged;
/// <summary>
/// Gets the value of the virtual action identified by name. Use <see cref="ActionMappings"/> to get the current config.