Add custom editor for buttons that allow listening for them inside the editor.
This commit is contained in:
51
Source/Editor/CustomEditors/Editors/GamepadButtonEditor.cs
Normal file
51
Source/Editor/CustomEditors/Editors/GamepadButtonEditor.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user