SpatialButtonInspector.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using EZXR.Glass.Inputs;
  6. using System;
  7. namespace EZXR.Glass.UI
  8. {
  9. [CustomEditor(typeof(SpatialButton))]
  10. [CanEditMultipleObjects]
  11. public class SpatialButtonInspector : SpatialSelectableInspector
  12. {
  13. private SpatialButton _target_SpatialButtonInspector;
  14. SerializedProperty preTriggerZoneThickness;
  15. SerializedProperty rearTriggerZoneThickness;
  16. SerializedProperty useEnterKey;
  17. SerializedProperty m_DelegatesProperty;
  18. GUIContent m_IconToolbarMinus;
  19. GUIContent m_EventIDName;
  20. GUIContent[] m_EventTypes;
  21. GUIContent m_AddButonContent;
  22. protected void Awake()
  23. {
  24. }
  25. protected override void OnEnable()
  26. {
  27. base.OnEnable();
  28. _target_SpatialButtonInspector = target as SpatialButton;
  29. preTriggerZoneThickness = serializedObject.FindProperty("preTriggerZoneThickness");
  30. rearTriggerZoneThickness = serializedObject.FindProperty("rearTriggerZoneThickness");
  31. useEnterKey = serializedObject.FindProperty("useEnterKey");
  32. m_DelegatesProperty = serializedObject.FindProperty("m_Delegates");
  33. m_AddButonContent = EditorGUIUtility.TrTextContent("Add New Event Type");
  34. m_EventIDName = new GUIContent("");
  35. // Have to create a copy since otherwise the tooltip will be overwritten.
  36. m_IconToolbarMinus = new GUIContent(EditorGUIUtility.IconContent("Toolbar Minus"));
  37. m_IconToolbarMinus.tooltip = "Remove all events in this list.";
  38. string[] eventNames = Enum.GetNames(typeof(SpatialButton.SpatialButtonEventType));
  39. m_EventTypes = new GUIContent[eventNames.Length];
  40. for (int i = 0; i < eventNames.Length; ++i)
  41. {
  42. m_EventTypes[i] = new GUIContent(eventNames[i]);
  43. }
  44. if (m_DelegatesProperty.arraySize == 0)
  45. {
  46. OnAddNewSelected(6);
  47. }
  48. }
  49. public override void OnInspectorGUI()
  50. {
  51. base.OnInspectorGUI();
  52. serializedObject.Update();
  53. if (_target_SpatialButtonInspector.size.z <= _target_SpatialButtonInspector.heightMin)
  54. {
  55. _target_SpatialButtonInspector.size = new Vector3(_target_SpatialButtonInspector.size.x, _target_SpatialButtonInspector.size.y, _target_SpatialButtonInspector.heightMin * 2);
  56. }
  57. EditorGUILayout.PropertyField(preTriggerZoneThickness, new GUIContent("前置触发区厚度(单位:m)"));
  58. EditorGUILayout.PropertyField(rearTriggerZoneThickness, new GUIContent("后置触发区厚度(单位:m)"));
  59. EditorGUILayout.PropertyField(useEnterKey, new GUIContent("响应虚拟键盘的Enter键"));
  60. #region Event
  61. EditorGUILayout.Space();
  62. EditorGUILayout.BeginVertical(EditorStyles.helpBox);
  63. {
  64. int toBeRemovedEntry = -1;
  65. Vector2 removeButtonSize = GUIStyle.none.CalcSize(m_IconToolbarMinus);
  66. for (int i = 0; i < m_DelegatesProperty.arraySize; ++i)
  67. {
  68. //EditorGUILayout.Space();
  69. SerializedProperty delegateProperty = m_DelegatesProperty.GetArrayElementAtIndex(i);
  70. SerializedProperty eventProperty = delegateProperty.FindPropertyRelative("eventID");
  71. SerializedProperty callbacksProperty = delegateProperty.FindPropertyRelative("callback");
  72. m_EventIDName.text = eventProperty.enumDisplayNames[eventProperty.enumValueIndex];
  73. EditorGUILayout.PropertyField(callbacksProperty, m_EventIDName);
  74. Rect callbackRect = GUILayoutUtility.GetLastRect();
  75. Rect removeButtonPos = new Rect(callbackRect.xMax - removeButtonSize.x - 8, callbackRect.y + 1, removeButtonSize.x, removeButtonSize.y);
  76. if (GUI.Button(removeButtonPos, m_IconToolbarMinus, GUIStyle.none))
  77. {
  78. toBeRemovedEntry = i;
  79. }
  80. }
  81. if (toBeRemovedEntry > -1)
  82. {
  83. RemoveEntry(toBeRemovedEntry);
  84. }
  85. Rect btPosition = GUILayoutUtility.GetRect(m_AddButonContent, GUI.skin.button);
  86. const float addButonWidth = 200f;
  87. btPosition.x = btPosition.x + (btPosition.width - addButonWidth) / 2;
  88. btPosition.width = addButonWidth;
  89. if (GUI.Button(btPosition, m_AddButonContent))
  90. {
  91. ShowAddTriggermenu();
  92. }
  93. }
  94. EditorGUILayout.EndVertical();
  95. EditorGUILayout.Space();
  96. #endregion
  97. serializedObject.ApplyModifiedProperties();
  98. SceneView.RepaintAll();
  99. }
  100. private void RemoveEntry(int toBeRemovedEntry)
  101. {
  102. m_DelegatesProperty.DeleteArrayElementAtIndex(toBeRemovedEntry);
  103. }
  104. void ShowAddTriggermenu()
  105. {
  106. // Now create the menu, add items and show it
  107. GenericMenu menu = new GenericMenu();
  108. for (int i = 0; i < m_EventTypes.Length; ++i)
  109. {
  110. bool active = true;
  111. // Check if we already have a Entry for the current eventType, if so, disable it
  112. for (int p = 0; p < m_DelegatesProperty.arraySize; ++p)
  113. {
  114. SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(p);
  115. SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
  116. if (eventProperty.enumValueIndex == i)
  117. {
  118. active = false;
  119. }
  120. }
  121. if (active)
  122. menu.AddItem(m_EventTypes[i], false, OnAddNewSelected, i);
  123. else
  124. menu.AddDisabledItem(m_EventTypes[i]);
  125. }
  126. menu.ShowAsContext();
  127. Event.current.Use();
  128. }
  129. private void OnAddNewSelected(object index)
  130. {
  131. int selected = (int)index;
  132. m_DelegatesProperty.arraySize += 1;
  133. SerializedProperty delegateEntry = m_DelegatesProperty.GetArrayElementAtIndex(m_DelegatesProperty.arraySize - 1);
  134. SerializedProperty eventProperty = delegateEntry.FindPropertyRelative("eventID");
  135. eventProperty.enumValueIndex = selected;
  136. serializedObject.ApplyModifiedProperties();
  137. }
  138. }
  139. }