A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2using System;
3using UnityEditor;
4using UnityEngine.InputSystem.Editor.Lists;
5using UnityEngine.InputSystem.Utilities;
6
7////TODO: show parameters for selected interaction or processor inline in list rather than separately underneath list
8
9namespace UnityEngine.InputSystem.Editor
10{
11 /// <summary>
12 /// Base class for views that show the properties of actions or bindings.
13 /// </summary>
14 internal abstract class PropertiesViewBase
15 {
16 protected PropertiesViewBase(string label, SerializedProperty bindingOrAction, Action<FourCC> onChange, string expectedControlLayout = null)
17 {
18 if (bindingOrAction == null)
19 throw new ArgumentNullException(nameof(bindingOrAction));
20
21 m_InteractionsProperty = bindingOrAction.FindPropertyRelative("m_Interactions");
22 m_ProcessorsProperty = bindingOrAction.FindPropertyRelative("m_Processors");
23
24 m_InteractionsList = new InteractionsListView(m_InteractionsProperty, OnInteractionsModified, expectedControlLayout);
25 UpdateProcessors(expectedControlLayout);
26
27 m_OnChange = onChange;
28 m_GeneralFoldoutLabel = EditorGUIUtility.TrTextContent(label);
29 }
30
31 protected void UpdateProcessors(string expectedControlLayout)
32 {
33 m_ProcessorsList = new ProcessorsListView(m_ProcessorsProperty, OnProcessorsModified, expectedControlLayout);
34 }
35
36 public void OnGUI()
37 {
38 EditorGUILayout.BeginVertical();
39 DrawGeneralGroup();
40 if (!m_IsPartOfComposite)
41 {
42 EditorGUILayout.Space();
43 DrawInteractionsGroup();
44 }
45 EditorGUILayout.Space();
46 DrawProcessorsGroup();
47 GUILayout.FlexibleSpace();
48 EditorGUILayout.EndVertical();
49 }
50
51 protected abstract void DrawGeneralProperties();
52
53 private void DrawGeneralGroup()
54 {
55 m_GeneralFoldout = DrawFoldout(m_GeneralFoldoutLabel, m_GeneralFoldout);
56 if (m_GeneralFoldout)
57 {
58 EditorGUI.indentLevel++;
59 DrawGeneralProperties();
60 EditorGUI.indentLevel--;
61 }
62 }
63
64 private void DrawProcessorsGroup()
65 {
66 m_ProcessorsFoldout = DrawFoldout(s_ProcessorsFoldoutLabel, m_ProcessorsFoldout, s_ProcessorsAddButton, m_ProcessorsList.OnAddDropdown);
67 if (m_ProcessorsFoldout)
68 m_ProcessorsList.OnGUI();
69 }
70
71 private void DrawInteractionsGroup()
72 {
73 m_InteractionsFoldout = DrawFoldout(s_InteractionsFoldoutLabel, m_InteractionsFoldout, s_InteractionsAddButton, m_InteractionsList.OnAddDropdown);
74 if (m_InteractionsFoldout)
75 m_InteractionsList.OnGUI();
76 }
77
78 private static bool DrawFoldout(GUIContent content, bool folded, GUIContent addButton = null, Action<Rect> addDropDown = null)
79 {
80 const int k_PopupSize = 20;
81 var bgRect = GUILayoutUtility.GetRect(content, Styles.s_FoldoutBackgroundStyle);
82 EditorGUI.LabelField(bgRect, GUIContent.none, Styles.s_FoldoutBackgroundStyle);
83 var foldoutRect = bgRect;
84 foldoutRect.xMax -= k_PopupSize;
85 var retval = EditorGUI.Foldout(foldoutRect, folded, content, true, Styles.s_FoldoutStyle);
86 if (addButton != null)
87 {
88 var popupRect = bgRect;
89 popupRect.xMin = popupRect.xMax - k_PopupSize;
90 if (GUI.Button(popupRect, addButton, EditorStyles.label))
91 addDropDown(popupRect);
92 }
93 return retval;
94 }
95
96 private void OnProcessorsModified()
97 {
98 m_ProcessorsProperty.stringValue = m_ProcessorsList.ToSerializableString();
99 m_ProcessorsProperty.serializedObject.ApplyModifiedProperties();
100 m_OnChange(k_ProcessorsChanged);
101 }
102
103 private void OnInteractionsModified()
104 {
105 m_InteractionsProperty.stringValue = m_InteractionsList.ToSerializableString();
106 m_InteractionsProperty.serializedObject.ApplyModifiedProperties();
107 m_OnChange(k_InteractionsChanged);
108 }
109
110 public Action<FourCC> onChange => m_OnChange;
111
112 private bool m_GeneralFoldout = true;
113 private bool m_InteractionsFoldout = true;
114 private bool m_ProcessorsFoldout = true;
115 protected bool m_IsPartOfComposite;
116
117 private readonly Action<FourCC> m_OnChange;
118
119 private readonly InteractionsListView m_InteractionsList;
120 private ProcessorsListView m_ProcessorsList;
121
122 private readonly SerializedProperty m_InteractionsProperty;
123 private readonly SerializedProperty m_ProcessorsProperty;
124
125 private readonly GUIContent m_GeneralFoldoutLabel;
126
127 ////TODO: tooltips
128 private static readonly GUIContent s_ProcessorsFoldoutLabel = EditorGUIUtility.TrTextContent("Processors");
129 public static readonly GUIContent s_ProcessorsAddButton = EditorGUIUtility.TrIconContent("Toolbar Plus More", "Add Processor");
130 private static readonly GUIContent s_InteractionsFoldoutLabel = EditorGUIUtility.TrTextContent("Interactions");
131 public static readonly GUIContent s_InteractionsAddButton = EditorGUIUtility.TrIconContent("Toolbar Plus More", "Add Interaction");
132
133 public static FourCC k_InteractionsChanged => new FourCC("IACT");
134 public static FourCC k_ProcessorsChanged => new FourCC("PROC");
135
136 private static class Styles
137 {
138 public static readonly GUIStyle s_FoldoutBackgroundStyle = new GUIStyle("Label")
139 .WithNormalBackground(AssetDatabase.LoadAssetAtPath<Texture2D>(InputActionTreeView.ResourcesPath + "foldoutBackground.png"))
140 .WithBorder(new RectOffset(3, 3, 3, 3))
141 .WithMargin(new RectOffset(1, 1, 3, 3));
142 public static readonly GUIStyle s_FoldoutStyle = new GUIStyle("foldout");
143 }
144 }
145}
146#endif // UNITY_EDITOR