Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputActionPropertiesView.cs 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEditor;
  6. using UnityEngine.InputSystem.Controls;
  7. using UnityEngine.InputSystem.Utilities;
  8. namespace UnityEngine.InputSystem.Editor
  9. {
  10. /// <summary>
  11. /// UI that edits the properties of an <see cref="InputAction"/>.
  12. /// </summary>
  13. /// <remarks>
  14. /// Right-most pane in <see cref="InputActionEditorWindow"/> when an action is selected.
  15. /// </remarks>
  16. internal class InputActionPropertiesView : PropertiesViewBase
  17. {
  18. public static FourCC k_PropertiesChanged => new FourCC("PROP");
  19. public InputActionPropertiesView(SerializedProperty actionProperty, Action<FourCC> onChange = null)
  20. : base("Action", actionProperty, onChange, actionProperty.FindPropertyRelative("m_ExpectedControlType").stringValue)
  21. {
  22. m_ExpectedControlTypeProperty = actionProperty.FindPropertyRelative(nameof(InputAction.m_ExpectedControlType));
  23. m_ActionTypeProperty = actionProperty.FindPropertyRelative(nameof(InputAction.m_Type));
  24. m_ActionFlagsProperty = actionProperty.FindPropertyRelative(nameof(InputAction.m_Flags));
  25. m_SelectedActionType = (InputActionType)m_ActionTypeProperty.intValue;
  26. m_WantsInitialStateCheck = (m_ActionFlagsProperty.intValue & (int)InputAction.ActionFlags.WantsInitialStateCheck) != 0;
  27. BuildControlTypeList();
  28. m_SelectedControlType = Array.IndexOf(m_ControlTypeList, m_ExpectedControlTypeProperty.stringValue);
  29. if (m_SelectedControlType == -1)
  30. m_SelectedControlType = 0;
  31. if (s_ControlTypeLabel == null)
  32. s_ControlTypeLabel = EditorGUIUtility.TrTextContent("Control Type", m_ExpectedControlTypeProperty.GetTooltip());
  33. if (s_ActionTypeLabel == null)
  34. s_ActionTypeLabel = EditorGUIUtility.TrTextContent("Action Type", m_ActionTypeProperty.GetTooltip());
  35. if (s_WantsInitialStateCheckLabel == null)
  36. s_WantsInitialStateCheckLabel = EditorGUIUtility.TrTextContent("Initial State Check",
  37. "Whether in the next input update after the action was enabled, the action should "
  38. + "immediately trigger if any of its bound controls are currently in a non-default state. "
  39. + "This check happens implicitly for Value actions but can be explicitly enabled for Button and Pass-Through actions.");
  40. }
  41. protected override void DrawGeneralProperties()
  42. {
  43. EditorGUI.BeginChangeCheck();
  44. m_SelectedActionType = EditorGUILayout.EnumPopup(s_ActionTypeLabel, m_SelectedActionType);
  45. if ((InputActionType)m_SelectedActionType != InputActionType.Button)
  46. m_SelectedControlType = EditorGUILayout.Popup(s_ControlTypeLabel, m_SelectedControlType, m_ControlTypeOptions);
  47. if ((InputActionType)m_SelectedActionType != InputActionType.Value)
  48. m_WantsInitialStateCheck = EditorGUILayout.Toggle(s_WantsInitialStateCheckLabel, m_WantsInitialStateCheck);
  49. if (EditorGUI.EndChangeCheck())
  50. {
  51. if ((InputActionType)m_SelectedActionType == InputActionType.Button)
  52. m_ExpectedControlTypeProperty.stringValue = "Button";
  53. else if (m_SelectedControlType == 0)
  54. m_ExpectedControlTypeProperty.stringValue = string.Empty;
  55. else
  56. m_ExpectedControlTypeProperty.stringValue = m_ControlTypeList[m_SelectedControlType];
  57. m_ActionTypeProperty.intValue = (int)(InputActionType)m_SelectedActionType;
  58. if (m_WantsInitialStateCheck)
  59. m_ActionFlagsProperty.intValue |= (int)InputAction.ActionFlags.WantsInitialStateCheck;
  60. else
  61. m_ActionFlagsProperty.intValue &= ~(int)InputAction.ActionFlags.WantsInitialStateCheck;
  62. m_ActionTypeProperty.serializedObject.ApplyModifiedProperties();
  63. UpdateProcessors(m_ExpectedControlTypeProperty.stringValue);
  64. onChange(k_PropertiesChanged);
  65. }
  66. }
  67. private void BuildControlTypeList()
  68. {
  69. var types = new List<string>();
  70. var allLayouts = InputSystem.s_Manager.m_Layouts;
  71. foreach (var layoutName in allLayouts.layoutTypes.Keys)
  72. {
  73. if (EditorInputControlLayoutCache.TryGetLayout(layoutName).hideInUI)
  74. continue;
  75. // If the action type is InputActionType.Value, skip button controls.
  76. var type = allLayouts.layoutTypes[layoutName];
  77. if ((InputActionType)m_SelectedActionType == InputActionType.Value &&
  78. typeof(ButtonControl).IsAssignableFrom(type))
  79. continue;
  80. ////TODO: skip aliases
  81. if (typeof(InputControl).IsAssignableFrom(type) &&
  82. !typeof(InputDevice).IsAssignableFrom(type))
  83. {
  84. types.Add(layoutName);
  85. }
  86. }
  87. // Sort alphabetically.
  88. types.Sort((a, b) => string.Compare(a, b, StringComparison.OrdinalIgnoreCase));
  89. // Make sure "Any" is always topmost entry.
  90. types.Insert(0, "Any");
  91. m_ControlTypeList = types.ToArray();
  92. m_ControlTypeOptions = m_ControlTypeList.Select(x => new GUIContent(ObjectNames.NicifyVariableName(x)))
  93. .ToArray();
  94. }
  95. private readonly SerializedProperty m_ExpectedControlTypeProperty;
  96. private readonly SerializedProperty m_ActionTypeProperty;
  97. private readonly SerializedProperty m_ActionFlagsProperty;
  98. private string m_ExpectedControlLayout;
  99. private string[] m_ControlTypeList;
  100. private GUIContent[] m_ControlTypeOptions;
  101. private int m_SelectedControlType;
  102. private Enum m_SelectedActionType;
  103. private bool m_WantsInitialStateCheck;
  104. private static GUIContent s_ActionTypeLabel;
  105. private static GUIContent s_ControlTypeLabel;
  106. private static GUIContent s_WantsInitialStateCheckLabel;
  107. }
  108. }
  109. #endif // UNITY_EDITOR