// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System.Collections.Generic; using FlaxEditor.CustomEditors.GUI; using FlaxEditor.GUI; using FlaxEditor.GUI.ContextMenu; using FlaxEngine; namespace FlaxEditor.CustomEditors.Editors { /// /// Default implementation of the inspector used to edit input event properties. /// [CustomEditor(typeof(InputEvent)), DefaultEditor] public class InputEventEditor : CustomEditor { private ComboBox _comboBox; /// public override DisplayStyle Style => DisplayStyle.Inline; /// public override void Initialize(LayoutElementsContainer layout) { LinkedLabel.SetupContextMenu += OnSetupContextMenu; var comboBoxElement = layout.ComboBox(); _comboBox = comboBoxElement.ComboBox; var names = new List(); foreach (var mapping in Input.ActionMappings) { if (!names.Contains(mapping.Name)) names.Add(mapping.Name); } _comboBox.Items = names; if (Values[0] is InputEvent inputEvent && names.Contains(inputEvent.Name)) _comboBox.SelectedItem = inputEvent.Name; _comboBox.SelectedIndexChanged += OnSelectedIndexChanged; } private void OnSetupContextMenu(PropertyNameLabel label, ContextMenu menu, CustomEditor linkededitor) { var button = menu.AddButton("Set to null"); button.Clicked += () => _comboBox.SelectedItem = null; } private void OnSelectedIndexChanged(ComboBox comboBox) { SetValue(comboBox.SelectedItem == null ? null : new InputEvent(comboBox.SelectedItem)); } /// public override void Refresh() { base.Refresh(); if (HasDifferentValues) { } else { if (Values[0] is InputEvent inputEvent && _comboBox.Items.Contains(inputEvent.Name)) _comboBox.SelectedItem = inputEvent.Name; else _comboBox.SelectedItem = null; } } /// protected override void Deinitialize() { if (LinkedLabel != null) LinkedLabel.SetupContextMenu -= OnSetupContextMenu; if (_comboBox != null) _comboBox.SelectedIndexChanged -= OnSelectedIndexChanged; _comboBox = null; } } /// /// Default implementation of the inspector used to edit input axis properties. /// [CustomEditor(typeof(InputAxis)), DefaultEditor] public class InputAxisEditor : CustomEditor { private ComboBox _comboBox; /// public override DisplayStyle Style => DisplayStyle.Inline; /// public override void Initialize(LayoutElementsContainer layout) { LinkedLabel.SetupContextMenu += OnSetupContextMenu; var comboBoxElement = layout.ComboBox(); _comboBox = comboBoxElement.ComboBox; var names = new List(); foreach (var mapping in Input.AxisMappings) { if (!names.Contains(mapping.Name)) names.Add(mapping.Name); } _comboBox.Items = names; if (Values[0] is InputAxis inputAxis && names.Contains(inputAxis.Name)) _comboBox.SelectedItem = inputAxis.Name; _comboBox.SelectedIndexChanged += OnSelectedIndexChanged; } private void OnSetupContextMenu(PropertyNameLabel label, ContextMenu menu, CustomEditor linkededitor) { var button = menu.AddButton("Set to null"); button.Clicked += () => _comboBox.SelectedItem = null; } private void OnSelectedIndexChanged(ComboBox comboBox) { SetValue(comboBox.SelectedItem == null ? null : new InputAxis(comboBox.SelectedItem)); } /// public override void Refresh() { base.Refresh(); if (HasDifferentValues) { } else { if (Values[0] is InputAxis inputAxis && _comboBox.Items.Contains(inputAxis.Name)) _comboBox.SelectedItem = inputAxis.Name; else _comboBox.SelectedItem = null; } } /// protected override void Deinitialize() { if (LinkedLabel != null) LinkedLabel.SetupContextMenu -= OnSetupContextMenu; if (_comboBox != null) _comboBox.SelectedIndexChanged -= OnSelectedIndexChanged; _comboBox = null; } } }