暫無描述
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.

PropertiesViewBase.cs 6.0KB

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