A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR
2using System;
3using UnityEditor;
4using UnityEditor.IMGUI.Controls;
5
6namespace UnityEngine.InputSystem.Editor
7{
8 /// <summary>
9 /// Property drawer for <see cref="InputActionMap"/>.
10 /// </summary>
11 [CustomPropertyDrawer(typeof(InputActionMap))]
12 internal class InputActionMapDrawer : InputActionDrawerBase
13 {
14 protected override TreeViewItem BuildTree(SerializedProperty property)
15 {
16 return InputActionTreeView.BuildWithJustActionsAndBindingsFromMap(property);
17 }
18
19 protected override string GetSuffixToRemoveFromPropertyDisplayName()
20 {
21 return " Action Map";
22 }
23
24 protected override bool IsPropertyAClone(SerializedProperty property)
25 {
26 // When a new item is added to a collection through the inspector, the default behaviour is
27 // to create a clone of the previous item. Here we look at all InputActionMaps that appear before
28 // the current one and compare their Ids to determine if we have a clone. We don't look past
29 // the current item because Unity will be calling this property drawer for each input action map
30 // in the collection in turn. If the user just added a new input action map, and it's a clone, as
31 // we work our way down the list, we'd end up thinking that an existing input action map was a clone
32 // of the newly added one, instead of the other way around. If we do have a clone, we need to
33 // clear out some properties of the InputActionMap (id, name, input actions, and bindings) and
34 // recreate the tree view.
35
36 if (property?.GetParentProperty() == null || property.GetParentProperty().isArray == false)
37 return false;
38
39 var array = property.GetArrayPropertyFromElement();
40 var index = property.GetIndexOfArrayElement();
41
42 for (var i = 0; i < index; i++)
43 {
44 if (property.FindPropertyRelative(nameof(InputActionMap.m_Id))?.stringValue ==
45 array.GetArrayElementAtIndex(i)?.FindPropertyRelative(nameof(InputActionMap.m_Id))?.stringValue)
46 return true;
47 }
48
49 return false;
50 }
51
52 protected override void ResetProperty(SerializedProperty property)
53 {
54 if (property == null) return;
55
56 property.SetStringValue(nameof(InputActionMap.m_Id), Guid.NewGuid().ToString());
57 property.SetStringValue(nameof(InputActionMap.m_Name), "Input Action Map");
58 property.FindPropertyRelative(nameof(InputActionMap.m_Actions))?.ClearArray();
59 property.FindPropertyRelative(nameof(InputActionMap.m_Bindings))?.ClearArray();
60 property.serializedObject?.ApplyModifiedPropertiesWithoutUndo();
61 }
62 }
63}
64#endif // UNITY_EDITOR