Improve input editors to use combo box.

This commit is contained in:
Chandler Cox
2023-09-20 13:19:03 -05:00
parent 92733d1a69
commit dfb9f1985f

View File

@@ -1,8 +1,10 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. // Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System.Collections.Generic; using System.Collections.Generic;
using FlaxEditor.CustomEditors.GUI;
using FlaxEditor.GUI;
using FlaxEditor.GUI.ContextMenu;
using FlaxEngine; using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.CustomEditors.Editors namespace FlaxEditor.CustomEditors.Editors
{ {
@@ -12,7 +14,7 @@ namespace FlaxEditor.CustomEditors.Editors
[CustomEditor(typeof(InputEvent)), DefaultEditor] [CustomEditor(typeof(InputEvent)), DefaultEditor]
public class InputEventEditor : CustomEditor public class InputEventEditor : CustomEditor
{ {
private Dropdown _dropdown; private ComboBox _comboBox;
/// <inheritdoc /> /// <inheritdoc />
public override DisplayStyle Style => DisplayStyle.Inline; public override DisplayStyle Style => DisplayStyle.Inline;
@@ -20,23 +22,30 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout) public override void Initialize(LayoutElementsContainer layout)
{ {
var dropdownElement = layout.Custom<Dropdown>(); LinkedLabel.SetupContextMenu += OnSetupContextMenu;
_dropdown = dropdownElement.CustomControl; var comboBoxElement = layout.ComboBox();
var names = new List<LocalizedString>(); _comboBox = comboBoxElement.ComboBox;
var names = new List<string>();
foreach (var mapping in Input.ActionMappings) foreach (var mapping in Input.ActionMappings)
{ {
if (!names.Contains(mapping.Name)) if (!names.Contains(mapping.Name))
names.Add(mapping.Name); names.Add(mapping.Name);
} }
_dropdown.Items = names; _comboBox.Items = names;
if (Values[0] is InputEvent inputEvent && names.Contains(inputEvent.Name)) if (Values[0] is InputEvent inputEvent && names.Contains(inputEvent.Name))
_dropdown.SelectedItem = inputEvent.Name; _comboBox.SelectedItem = inputEvent.Name;
_dropdown.SelectedIndexChanged += OnSelectedIndexChanged; _comboBox.SelectedIndexChanged += OnSelectedIndexChanged;
} }
private void OnSelectedIndexChanged(Dropdown dropdown) private void OnSetupContextMenu(PropertyNameLabel label, ContextMenu menu, CustomEditor linkededitor)
{ {
SetValue(new InputEvent(dropdown.SelectedItem)); var button = menu.AddButton("Reset to null");
button.Clicked += () => _comboBox.SelectedItem = null;
}
private void OnSelectedIndexChanged(ComboBox comboBox)
{
SetValue(comboBox.SelectedItem == null ? null : new InputEvent(comboBox.SelectedItem));
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -49,17 +58,21 @@ namespace FlaxEditor.CustomEditors.Editors
} }
else else
{ {
if (Values[0] is InputEvent inputEvent && _dropdown.Items.Contains(inputEvent.Name)) if (Values[0] is InputEvent inputEvent && _comboBox.Items.Contains(inputEvent.Name))
_dropdown.SelectedItem = inputEvent.Name; _comboBox.SelectedItem = inputEvent.Name;
else
_comboBox.SelectedItem = null;
} }
} }
/// <inheritdoc /> /// <inheritdoc />
protected override void Deinitialize() protected override void Deinitialize()
{ {
if (_dropdown != null) if (LinkedLabel != null)
_dropdown.SelectedIndexChanged -= OnSelectedIndexChanged; LinkedLabel.SetupContextMenu -= OnSetupContextMenu;
_dropdown = null; if (_comboBox != null)
_comboBox.SelectedIndexChanged -= OnSelectedIndexChanged;
_comboBox = null;
} }
} }
@@ -69,7 +82,7 @@ namespace FlaxEditor.CustomEditors.Editors
[CustomEditor(typeof(InputAxis)), DefaultEditor] [CustomEditor(typeof(InputAxis)), DefaultEditor]
public class InputAxisEditor : CustomEditor public class InputAxisEditor : CustomEditor
{ {
private Dropdown _dropdown; private ComboBox _comboBox;
/// <inheritdoc /> /// <inheritdoc />
public override DisplayStyle Style => DisplayStyle.Inline; public override DisplayStyle Style => DisplayStyle.Inline;
@@ -77,23 +90,30 @@ namespace FlaxEditor.CustomEditors.Editors
/// <inheritdoc /> /// <inheritdoc />
public override void Initialize(LayoutElementsContainer layout) public override void Initialize(LayoutElementsContainer layout)
{ {
var dropdownElement = layout.Custom<Dropdown>(); LinkedLabel.SetupContextMenu += OnSetupContextMenu;
_dropdown = dropdownElement.CustomControl; var comboBoxElement = layout.ComboBox();
var names = new List<LocalizedString>(); _comboBox = comboBoxElement.ComboBox;
var names = new List<string>();
foreach (var mapping in Input.AxisMappings) foreach (var mapping in Input.AxisMappings)
{ {
if (!names.Contains(mapping.Name)) if (!names.Contains(mapping.Name))
names.Add(mapping.Name); names.Add(mapping.Name);
} }
_dropdown.Items = names; _comboBox.Items = names;
if (Values[0] is InputAxis inputAxis && names.Contains(inputAxis.Name)) if (Values[0] is InputAxis inputAxis && names.Contains(inputAxis.Name))
_dropdown.SelectedItem = inputAxis.Name; _comboBox.SelectedItem = inputAxis.Name;
_dropdown.SelectedIndexChanged += OnSelectedIndexChanged; _comboBox.SelectedIndexChanged += OnSelectedIndexChanged;
} }
private void OnSelectedIndexChanged(Dropdown dropdown) private void OnSetupContextMenu(PropertyNameLabel label, ContextMenu menu, CustomEditor linkededitor)
{ {
SetValue(new InputAxis(dropdown.SelectedItem)); var button = menu.AddButton("Reset to null");
button.Clicked += () => _comboBox.SelectedItem = null;
}
private void OnSelectedIndexChanged(ComboBox comboBox)
{
SetValue(comboBox.SelectedItem == null ? null : new InputAxis(comboBox.SelectedItem));
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -106,17 +126,21 @@ namespace FlaxEditor.CustomEditors.Editors
} }
else else
{ {
if (Values[0] is InputAxis inputAxis && _dropdown.Items.Contains(inputAxis.Name)) if (Values[0] is InputAxis inputAxis && _comboBox.Items.Contains(inputAxis.Name))
_dropdown.SelectedItem = inputAxis.Name; _comboBox.SelectedItem = inputAxis.Name;
else
_comboBox.SelectedItem = null;
} }
} }
/// <inheritdoc /> /// <inheritdoc />
protected override void Deinitialize() protected override void Deinitialize()
{ {
if (_dropdown != null) if (LinkedLabel != null)
_dropdown.SelectedIndexChanged -= OnSelectedIndexChanged; LinkedLabel.SetupContextMenu -= OnSetupContextMenu;
_dropdown = null; if (_comboBox != null)
_comboBox.SelectedIndexChanged -= OnSelectedIndexChanged;
_comboBox = null;
} }
} }
} }