AnchorOverrideEditor.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace TheraBytes.BetterUi.Editor
  7. {
  8. [CustomEditor(typeof(AnchorOverride)), CanEditMultipleObjects]
  9. public class AnchorOverrideEditor : UnityEditor.Editor
  10. {
  11. static readonly GUIContent IsAnimatedContent = new GUIContent("Animate",
  12. "If enabled the anchors are not applied instantly but with an animation.");
  13. static readonly GUIContent AccelerationContent = new GUIContent("Acceleration",
  14. "Defines how fast the movement reaches maximum speed.");
  15. static readonly GUIContent MaxMoveSpeedContent = new GUIContent("Max Move Speed",
  16. "The maximum velocity when moving.");
  17. static readonly GUIContent SnapThresholdContent = new GUIContent("Snap Threshold",
  18. "The distance of the farest anchor / axis, at which the animation stops and the target anchors are applied.");
  19. static readonly string[] HorizontalOptions = { "Center", "Pivot", "Left", "Right" };
  20. static readonly string[] VerticalOptions = { "Center", "Pivot", "Bottom", "Top" };
  21. SerializedProperty anchorsFallback, anchorsConfigs,
  22. mode, isAnimated, acceleration, maxMoveSpeed, snapThreshold;
  23. Dictionary<string, UnityEditorInternal.ReorderableList> lists = new Dictionary<string, UnityEditorInternal.ReorderableList>();
  24. private void OnEnable()
  25. {
  26. //AnchorOverride ao = target as AnchorOverride;
  27. anchorsFallback = serializedObject.FindProperty("anchorsFallback");
  28. anchorsConfigs = serializedObject.FindProperty("anchorsConfigs");
  29. mode = serializedObject.FindProperty("mode");
  30. isAnimated = serializedObject.FindProperty("isAnimated");
  31. acceleration = serializedObject.FindProperty("acceleration");
  32. maxMoveSpeed = serializedObject.FindProperty("maxMoveSpeed");
  33. snapThreshold = serializedObject.FindProperty("snapThreshold");
  34. IntroduceList(ResolutionMonitor.Instance.FallbackName + " (Fallback)", anchorsFallback.FindPropertyRelative("elements"));
  35. SerializedProperty items = anchorsConfigs.FindPropertyRelative("items");
  36. for (int i = 0; i < items.arraySize; i++)
  37. {
  38. SerializedProperty prop = items.GetArrayElementAtIndex(i);
  39. SerializedProperty elements = prop.FindPropertyRelative("elements");
  40. SerializedProperty configNameProp = prop.FindPropertyRelative("screenConfigName");
  41. string configName = configNameProp.stringValue;
  42. IntroduceList(configName, elements);
  43. }
  44. }
  45. public override void OnInspectorGUI()
  46. {
  47. EditorGUILayout.Space();
  48. DrawModeSelection();
  49. DrawAnimationSettings();
  50. EditorGUILayout.Space();
  51. ScreenConfigConnectionHelper.DrawGui("Anchors", anchorsConfigs, ref anchorsFallback, DrawAnchorSettings);
  52. }
  53. private void DrawModeSelection()
  54. {
  55. if (isAnimated.boolValue)
  56. {
  57. EditorGUILayout.PropertyField(mode);
  58. }
  59. else
  60. {
  61. string[] options = { "Auto Update", /* enum index 1 is skipped, */ "Manual Update" };
  62. int prevIndex = (mode.enumValueIndex == 2) ? 1 : 0;
  63. int newIndex = EditorGUILayout.Popup("Mode", prevIndex, options);
  64. if (newIndex != prevIndex)
  65. {
  66. mode.enumValueIndex = (newIndex == 1) ? 2 : 0;
  67. serializedObject.ApplyModifiedProperties();
  68. }
  69. }
  70. }
  71. private void DrawAnimationSettings()
  72. {
  73. EditorGUILayout.PropertyField(isAnimated, IsAnimatedContent);
  74. if (isAnimated.boolValue)
  75. {
  76. EditorGUI.indentLevel++;
  77. EditorGUILayout.PropertyField(acceleration, AccelerationContent);
  78. EditorGUILayout.PropertyField(maxMoveSpeed, MaxMoveSpeedContent);
  79. EditorGUILayout.PropertyField(snapThreshold, SnapThresholdContent);
  80. EditorGUI.indentLevel--;
  81. }
  82. }
  83. void DrawAnchorSettings(string configName, SerializedProperty prop)
  84. {
  85. SerializedProperty elements = prop.FindPropertyRelative("elements");
  86. IntroduceList(configName, elements);
  87. lists[configName].DoLayoutList();
  88. }
  89. void IntroduceList(string configName, SerializedProperty elements)
  90. {
  91. if (lists.ContainsKey(configName))
  92. return;
  93. const float SPACE = 5;
  94. var list = new UnityEditorInternal.ReorderableList(serializedObject, elements);
  95. list.elementHeight = 5 * EditorGUIUtility.singleLineHeight + 3 * SPACE;
  96. list.drawElementCallback += (Rect rect, int index, bool isActive, bool isFocused) =>
  97. {
  98. Rect r = new Rect(rect.x, rect.y + SPACE, rect.width, EditorGUIUtility.singleLineHeight);
  99. var prop = elements.GetArrayElementAtIndex(index);
  100. var reference = prop.FindPropertyRelative("reference");
  101. EditorGUI.PropertyField(r, reference);
  102. r.y += EditorGUIUtility.singleLineHeight + SPACE;
  103. var minX = prop.FindPropertyRelative("minX");
  104. DrawAnchorEdgeSetting("Min X", minX, r, AnchorOverride.AnchorReference.ReferenceLocation.LowerLeft, HorizontalOptions);
  105. r.y += EditorGUIUtility.singleLineHeight;
  106. var maxX = prop.FindPropertyRelative("maxX");
  107. DrawAnchorEdgeSetting("Max X", maxX, r, AnchorOverride.AnchorReference.ReferenceLocation.UpperRight, HorizontalOptions);
  108. r.y += EditorGUIUtility.singleLineHeight;
  109. var minY = prop.FindPropertyRelative("minY");
  110. DrawAnchorEdgeSetting("Min Y", minY, r, AnchorOverride.AnchorReference.ReferenceLocation.LowerLeft, VerticalOptions);
  111. r.y += EditorGUIUtility.singleLineHeight;
  112. var maxY = prop.FindPropertyRelative("maxY");
  113. DrawAnchorEdgeSetting("Max Y", maxY, r, AnchorOverride.AnchorReference.ReferenceLocation.UpperRight, VerticalOptions);
  114. };
  115. list.drawHeaderCallback += (Rect rect) =>
  116. {
  117. EditorGUI.LabelField(rect, "Anchor Overrides");
  118. };
  119. lists.Add(configName, list);
  120. }
  121. private void DrawAnchorEdgeSetting(string label, SerializedProperty edge, Rect r, AnchorOverride.AnchorReference.ReferenceLocation defaultLocation, string[] displayOptions)
  122. {
  123. bool somethingChanged = false;
  124. Rect checkRect = new Rect(r.x, r.y, 60, r.height);
  125. AnchorOverride.AnchorReference.ReferenceLocation location = (AnchorOverride.AnchorReference.ReferenceLocation)edge.enumValueIndex;
  126. bool prevChecked = location != AnchorOverride.AnchorReference.ReferenceLocation.Disabled;
  127. if (EditorGUI.ToggleLeft(checkRect, label, location != AnchorOverride.AnchorReference.ReferenceLocation.Disabled))
  128. {
  129. if (!prevChecked)
  130. {
  131. edge.enumValueIndex = (int)defaultLocation;
  132. somethingChanged = true;
  133. }
  134. Rect popupRect = new Rect(r.x + checkRect.width, r.y, r.width - checkRect.width, r.height);
  135. int prevIndex = edge.enumValueIndex - 1;
  136. int index = EditorGUI.Popup(popupRect, edge.enumValueIndex - 1, displayOptions);
  137. if (index != prevIndex)
  138. {
  139. edge.enumValueIndex = index + 1;
  140. somethingChanged = true;
  141. }
  142. }
  143. else if (prevChecked)
  144. {
  145. edge.enumValueIndex = (int)AnchorOverride.AnchorReference.ReferenceLocation.Disabled;
  146. somethingChanged = true;
  147. }
  148. if (somethingChanged)
  149. {
  150. serializedObject.ApplyModifiedProperties();
  151. }
  152. }
  153. }
  154. }