A game about forced loneliness, made by TACStudios
1#if PACKAGE_INPUT_SYSTEM_EXISTS
2using System.Linq;
3using JetBrains.Annotations;
4using Unity.VisualScripting.FullSerializer;
5using Unity.VisualScripting.InputSystem;
6using UnityEditor;
7using UnityEngine;
8using UnityEngine.InputSystem;
9
10namespace Unity.VisualScripting
11{
12 [Inspector(typeof(InputAction))]
13 public class InputActionInspector : Inspector
14 {
15 private readonly GraphReference m_Reference;
16 private readonly OnInputSystemEvent m_InputSystemUnit;
17
18 public InputActionInspector(Metadata metadata, GraphReference reference, OnInputSystemEvent inputSystemUnit) : base(metadata)
19 {
20 m_Reference = reference;
21 m_InputSystemUnit = inputSystemUnit;
22 }
23
24 // Called by reflection from TypeUtility.Instantiate
25 [UsedImplicitly]
26 public InputActionInspector(Metadata metadata) : base(metadata)
27 {
28 }
29
30 protected override float GetHeight(float width, GUIContent label) => EditorGUIUtility.singleLineHeight;
31 public override float GetAdaptiveWidth()
32 {
33 return Mathf.Max(100, metadata.value is InputAction action && action.name != null
34 ? (EditorStyles.popup.CalcSize(new GUIContent(action.name)).x + 1)
35 : 0);
36 }
37
38 protected override void OnGUI(Rect position, GUIContent label)
39 {
40 position = BeginLabeledBlock(metadata, position, label);
41
42 var togglePosition = position.VerticalSection(ref y, EditorGUIUtility.singleLineHeight);
43
44 if (m_InputSystemUnit == null)
45 {
46 EndBlock(metadata);
47 return;
48 }
49
50 var inputActionAsset = Flow.Predict<PlayerInput>(m_InputSystemUnit.Target, m_Reference)?.actions;
51 if (!inputActionAsset)
52 EditorGUI.LabelField(togglePosition, "No Actions found");
53 else
54 {
55 var value = metadata.value is InputAction ? (InputAction)metadata.value : default;
56
57 int currentIndex = -1;
58 if (value != null && value.id != default)
59 {
60 int i = 0;
61 foreach (var playerInputAction in inputActionAsset)
62 {
63 if (playerInputAction.id == value.id)
64 {
65 currentIndex = i;
66 break;
67 }
68 i++;
69 }
70 }
71
72 var displayedOptions = Enumerable.Repeat(new GUIContent("<None>"), 1).Concat(inputActionAsset.Select(a => new GUIContent(a.name))).ToArray();
73 var newIndex = EditorGUI.Popup(togglePosition, currentIndex + 1, displayedOptions);
74 if (EndBlock(metadata) || ActionTypeHasChanged(currentIndex, value))
75 {
76 metadata.RecordUndo();
77 if (newIndex == 0)
78 metadata.value = default;
79 else
80 {
81 var inputAction = inputActionAsset.ElementAt(newIndex - 1);
82
83 metadata.value =
84 InputAction_DirectConverter.MakeInputActionWithId(inputAction.id.ToString(),
85 inputAction.name, inputAction.expectedControlType, inputAction.type);
86 m_InputSystemUnit.Analyser(m_Reference).isDirty = true;
87 }
88 }
89 return;
90 }
91
92 EndBlock(metadata);
93
94 bool ActionTypeHasChanged(int currentIndex, InputAction value)
95 {
96 try
97 {
98 // getting the total action count would be expensive, as it would need to be computed everytime
99 return value?.type != inputActionAsset.ElementAt(currentIndex)?.type;
100 }
101 catch
102 {
103 return true;
104 }
105 }
106 }
107 }
108}
109#endif