Add custom editor for buttons that allow listening for them inside the editor.

This commit is contained in:
MineBill
2024-01-02 19:36:27 +02:00
parent b275ffc146
commit a5d0916f93
4 changed files with 220 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
using System;
using FlaxEngine;
namespace FlaxEditor.CustomEditors.Editors
{
/// <summary>
/// Custom editor for <see cref="GamepadButton"/>.
/// Allows capturing gamepad buttons and assigning them
/// to the edited value.
/// </summary>
[CustomEditor(typeof(GamepadButton))]
public class GamepadButtonEditor : BindableButtonEditor
{
/// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout)
{
base.Initialize(layout);
FlaxEngine.Scripting.Update += ScriptingOnUpdate;
}
/// <inheritdoc />
protected override void Deinitialize()
{
FlaxEngine.Scripting.Update -= ScriptingOnUpdate;
base.Deinitialize();
}
private void ScriptingOnUpdate()
{
if (!IsListeningForInput)
return;
// Since there is no way to get an event about
// which gamepad pressed what button, we have
// to poll all gamepads and buttons manually.
for (var i = 0; i < Input.GamepadsCount; i++)
{
var pad = Input.Gamepads[i];
foreach (var btn in Enum.GetValues<GamepadButton>())
{
if (pad.GetButtonUp(btn))
{
IsListeningForInput = false;
SetValue(btn);
return;
}
}
}
}
}
}