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

This commit is contained in:
Wojtek Figat
2023-05-12 14:13:18 +02:00
3 changed files with 66 additions and 1 deletions

View File

@@ -263,6 +263,37 @@ API_ENUM() enum class InputActionMode
Release = 2,
};
/// <summary>
/// The input action event phases.
/// </summary>
API_ENUM() enum class InputActionState
{
/// <summary>
/// The key/button is not assigned.
/// </summary>
None = 0,
/// <summary>
/// The key/button is waiting for input.
/// </summary>
Waiting = 1,
/// <summary>
/// User is pressing the key/button.
/// </summary>
Pressing = 2,
/// <summary>
/// User pressed the key/button (but wasn't pressing it in the previous frame).
/// </summary>
Press = 3,
/// <summary>
/// User released the key/button (was pressing it in the previous frame).
/// </summary>
Release = 4,
};
/// <summary>
/// The input gamepad index.
/// </summary>

View File

@@ -25,15 +25,17 @@ struct AxisEvaluation
bool Used;
};
struct ActionData
struct ActionData
{
bool Active;
uint64 FrameIndex;
InputActionState State;
ActionData()
{
Active = false;
FrameIndex = 0;
State = InputActionState::Waiting;
}
};
@@ -597,6 +599,16 @@ bool Input::GetAction(const StringView& name)
return e ? e->Active : false;
}
InputActionState Input::GetActionState(const StringView& name)
{
const auto e = Actions.TryGet(name);
if (e != nullptr)
{
return e->State;
}
return InputActionState::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.State = InputActionState::Waiting;
// Mark as updated in this frame
data.FrameIndex = frame;
@@ -830,6 +843,19 @@ 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.State = InputActionState::Press;
}
else if (Input::GetKey(config.Key) || Input::GetMouseButton(config.MouseButton) || Input::GetGamepadButton(config.Gamepad, config.GamepadButton))
{
data.State = InputActionState::Pressing;
}
else if (Input::GetKeyUp(config.Key) || Input::GetMouseButtonUp(config.MouseButton) || Input::GetGamepadButtonUp(config.Gamepad, config.GamepadButton))
{
data.State = InputActionState::Release;
}
data.Active |= isActive;
}

View File

@@ -309,6 +309,14 @@ public:
/// <seealso cref="ActionMappings"/>
API_FUNCTION() static bool GetAction(const StringView& name);
/// <summary>
/// Gets the value of the virtual action identified by name. Use <see cref="ActionMappings"/> to get the current config.
/// </summary>
/// <param name="name">The action name.</param>
/// <returns>A InputActionPhase determining the current phase of the Action (e.g If it was just pressed, is being held or just released).</returns>
/// <seealso cref="ActionMappings"/>
API_FUNCTION() static InputActionState GetActionState(const StringView& name);
/// <summary>
/// Gets the value of the virtual axis identified by name. Use <see cref="AxisMappings"/> to get the current config.
/// </summary>