From 70912e1d564d91ab742ba29b9c1e0a61483f3630 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 9 Jul 2024 11:57:59 +0200 Subject: [PATCH] Add support for using InputAction editor on string variable --- .../CustomEditors/Editors/InputEditor.cs | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Source/Editor/CustomEditors/Editors/InputEditor.cs b/Source/Editor/CustomEditors/Editors/InputEditor.cs index 5662aea0c..f73fe19f8 100644 --- a/Source/Editor/CustomEditors/Editors/InputEditor.cs +++ b/Source/Editor/CustomEditors/Editors/InputEditor.cs @@ -33,11 +33,25 @@ namespace FlaxEditor.CustomEditors.Editors names.Add(mapping.Name); } _comboBox.Items = names; - if (Values[0] is InputEvent inputEvent && names.Contains(inputEvent.Name)) + var prev = GetValue(); + if (prev is InputEvent inputEvent && names.Contains(inputEvent.Name)) _comboBox.SelectedItem = inputEvent.Name; + else if (prev is string name && names.Contains(name)) + _comboBox.SelectedItem = name; _comboBox.SelectedIndexChanged += OnSelectedIndexChanged; } + private object GetValue() + { + if (Values[0] is InputEvent inputEvent) + return inputEvent; + if (Values[0] is string str) + return str; + if (Values.Type.Type == typeof(string)) + return string.Empty; + return null; + } + private void OnSetupContextMenu(PropertyNameLabel label, ContextMenu menu, CustomEditor linkedEditor) { var button = menu.AddButton("Set to null"); @@ -46,7 +60,16 @@ namespace FlaxEditor.CustomEditors.Editors private void OnSelectedIndexChanged(ComboBox comboBox) { - SetValue(comboBox.SelectedItem == null ? null : new InputEvent(comboBox.SelectedItem)); + object value = null; + if (comboBox.SelectedItem != null) + { + var prev = GetValue(); + if (prev is InputEvent) + value = new InputEvent(comboBox.SelectedItem); + else if (prev is string) + value = comboBox.SelectedItem; + } + SetValue(value); } /// @@ -59,8 +82,11 @@ namespace FlaxEditor.CustomEditors.Editors } else { - if (Values[0] is InputEvent inputEvent && _comboBox.Items.Contains(inputEvent.Name)) + var prev = GetValue(); + if (prev is InputEvent inputEvent && _comboBox.Items.Contains(inputEvent.Name)) _comboBox.SelectedItem = inputEvent.Name; + else if (prev is string name && _comboBox.Items.Contains(name)) + _comboBox.SelectedItem = name; else _comboBox.SelectedItem = null; }