A game about forced loneliness, made by TACStudios
at master 90 lines 4.2 kB view raw
1#if UNITY_EDITOR && UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS 2using System; 3using System.Collections.Generic; 4using System.Linq; 5using UnityEditor; 6using UnityEngine.UIElements; 7 8namespace UnityEngine.InputSystem.Editor 9{ 10 internal class CompositePartBindingPropertiesView : ViewBase<CompositePartBindingPropertiesView.ViewState> 11 { 12 private readonly DropdownField m_CompositePartField; 13 private readonly IMGUIContainer m_PathEditorContainer; 14 15 private const string UxmlName = InputActionsEditorConstants.PackagePath + 16 InputActionsEditorConstants.ResourcesPath + 17 InputActionsEditorConstants.CompositePartBindingPropertiesViewUxml; 18 19 public CompositePartBindingPropertiesView(VisualElement root, StateContainer stateContainer) 20 : base(root, stateContainer) 21 { 22 var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(UxmlName); 23 var container = visualTreeAsset.CloneTree(); 24 rootElement.Add(container); 25 26 m_PathEditorContainer = container.Q<IMGUIContainer>("path-editor-container"); 27 m_CompositePartField = container.Q<DropdownField>("composite-part-dropdown"); 28 29 CreateSelector(Selectors.GetSelectedBinding, 30 (b, s) => b.HasValue && b.Value.isPartOfComposite ? Selectors.GetCompositePartBindingViewState(b.Value, s) : null); 31 } 32 33 public override void RedrawUI(ViewState viewState) 34 { 35 if (viewState == null) 36 return; 37 // TODO: Persist control picker state 38 var controlPathEditor = new InputControlPathEditor(viewState.selectedBindingPath, new InputControlPickerState(), 39 () => { Dispatch(Commands.ApplyModifiedProperties()); }); 40 41 controlPathEditor.SetControlPathsToMatch(viewState.currentControlScheme.deviceRequirements.Select(x => x.controlPath)); 42 controlPathEditor.SetExpectedControlLayout(viewState.expectedControlLayoutName); 43 44 m_PathEditorContainer.onGUIHandler = controlPathEditor.OnGUI; 45 46 m_CompositePartField.choices.Clear(); 47 m_CompositePartField.choices.AddRange(viewState.compositePartNames); 48 m_CompositePartField.SetValueWithoutNotify(viewState.selectedCompositePartName); 49 50 m_CompositePartField.RegisterValueChangedCallback(evt => 51 { 52 Dispatch(Commands.SetCompositeBindingPartName(viewState.selectedBinding, evt.newValue)); 53 }); 54 } 55 56 internal class ViewState 57 { 58 public SerializedProperty selectedBindingPath; 59 public SerializedInputBinding selectedBinding; 60 public IEnumerable<string> compositePartNames; 61 public InputControlScheme currentControlScheme; 62 public string expectedControlLayoutName; 63 public string selectedCompositePartName; 64 } 65 } 66 67 internal static partial class Selectors 68 { 69 public static CompositePartBindingPropertiesView.ViewState GetCompositePartBindingViewState(SerializedInputBinding binding, 70 InputActionsEditorState state) 71 { 72 var compositeParts = GetCompositePartOptions(binding.name, binding.compositePath).ToList(); 73 var selectedCompositePartName = ObjectNames.NicifyVariableName( 74 compositeParts.First(str => string.Equals(str, binding.name, StringComparison.OrdinalIgnoreCase))); 75 76 var compositePartBindingViewState = new CompositePartBindingPropertiesView.ViewState 77 { 78 selectedBinding = binding, 79 selectedBindingPath = GetSelectedBindingPath(state), 80 selectedCompositePartName = selectedCompositePartName, 81 currentControlScheme = state.selectedControlScheme, 82 compositePartNames = compositeParts.Select(ObjectNames.NicifyVariableName).ToList(), 83 expectedControlLayoutName = InputBindingComposite.GetExpectedControlLayoutName(binding.compositePath, binding.name) ?? "" 84 }; 85 return compositePartBindingViewState; 86 } 87 } 88} 89 90#endif