| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using UnityEditor;
- using UnityEngine;
- namespace TheraBytes.BetterUi.Editor
- {
- [CustomEditor(typeof(BetterLocator)), CanEditMultipleObjects]
- public class BetterLocatorEditor : UnityEditor.Editor
- {
- SerializedProperty transformFallback, transformConfigs;
- BetterLocator locator;
- Dictionary<RectTransformData, bool> anchorExpands = new Dictionary<RectTransformData, bool>();
- static bool autoPullFromTransform = true;
- static bool autoPushToTransform = false;
- bool autoPull, autoPush;
- bool hasOffsetter;
- bool pauseAutoPushOnce = false; // recursive infinite loop protection
- protected virtual void OnEnable()
- {
- this.locator = target as BetterLocator;
- transformFallback = serializedObject.FindProperty("transformFallback");
- transformConfigs = serializedObject.FindProperty("transformConfigs");
- hasOffsetter = locator.gameObject.GetComponent<BetterOffsetter>() != null;
- autoPull = autoPullFromTransform && !hasOffsetter;
- autoPush = autoPushToTransform && !hasOffsetter;
- this.locator.OnValidate();
- }
- public override void OnInspectorGUI()
- {
- string currentScreen = ResolutionMonitor.CurrentScreenConfiguration?.Name;
- bool isCurrentScreenConfig = currentScreen == null // fallback is always present
- || locator.CurrentTransformData.ScreenConfigName == currentScreen;
- if (isCurrentScreenConfig)
- {
- EditorGUILayout.PrefixLabel("Live Update");
- EditorGUILayout.BeginHorizontal();
- EditorGUI.BeginDisabledGroup(!locator.isActiveAndEnabled);
- bool newPull = GUILayout.Toggle(autoPull, "↓ Auto-Pull", "ButtonLeft", GUILayout.MinHeight(30));
- bool newPush = GUILayout.Toggle(autoPush, "↑ Auto-Push", "ButtonRight", GUILayout.MinHeight(30));
- EditorGUI.EndDisabledGroup();
- EditorGUILayout.EndHorizontal();
- if(newPull != autoPull)
- {
- autoPull = newPull;
- autoPullFromTransform = newPull;
- }
- if (newPush != autoPush)
- {
- autoPush = newPush;
- autoPushToTransform = newPush;
- }
- }
- else
- {
- EditorGUILayout.BeginVertical(GUILayout.MinHeight(52));
- GUILayout.FlexibleSpace();
- EditorGUILayout.HelpBox($"To prevent accidentally overriding the wrong config, live update is disabled. It is enabled only if you have a config for the current screen configuration ('{currentScreen}') present.",
- MessageType.Info);
- EditorGUILayout.EndHorizontal();
- }
- if (autoPull && locator.isActiveAndEnabled && isCurrentScreenConfig)
- {
- locator.CurrentTransformData.PullFromTransform(locator.transform as RectTransform);
- }
- ScreenConfigConnectionHelper.DrawGui("Rect Transform Override", transformConfigs, ref transformFallback, DrawTransformData);
- if (autoPush && !(pauseAutoPushOnce) && isCurrentScreenConfig)
- {
- locator.CurrentTransformData.PushToTransform(locator.transform as RectTransform);
- }
- pauseAutoPushOnce = false;
- }
- void DrawTransformData(string configName, SerializedProperty prop)
- {
- RectTransformData data = prop.GetValue<RectTransformData>();
- bool isCurrent = locator.CurrentTransformData == data;
-
- if (!(anchorExpands.ContainsKey(data)))
- {
- anchorExpands.Add(data, true);
- }
- bool anchorExpand = anchorExpands[data];
- float height = (anchorExpand)
- ? RectTransformDataDrawer.HeightWithAnchorsExpanded
- : RectTransformDataDrawer.HeightWithoutAnchorsExpanded;
- EditorGUILayout.BeginVertical("box");
- Rect bounds = EditorGUILayout.GetControlRect(false, height);
-
- bool canEdit = !(isCurrent) || !(autoPull) || autoPush;
- // Pull
- EditorGUI.BeginDisabledGroup(isCurrent && autoPull);
- if (GUI.Button(new Rect(bounds.position + new Vector2(5, 5), new Vector2(40, 40)), "Pull\n↓"))
- {
- Undo.RecordObject(locator, "Pull From Rect Transform");
- data.PullFromTransform(locator.transform as RectTransform);
- }
- EditorGUI.EndDisabledGroup();
- // Push
- EditorGUI.BeginDisabledGroup(!(canEdit) || (isCurrent && autoPush));
- if (GUI.Button(new Rect(bounds.position + new Vector2(50, 25), new Vector2(40, 40)), "↑\nPush"))
- {
- Undo.RecordObject(locator.transform, "Push To Rect Transform");
- data.PushToTransform(locator.transform as RectTransform);
- pauseAutoPushOnce = true;
- }
- EditorGUI.EndDisabledGroup();
- // Fields
- if(!canEdit)
- {
- EditorGUI.BeginDisabledGroup(true);
- }
- RectTransformDataDrawer.DrawFields(prop, data, bounds, ref anchorExpand);
- anchorExpands[data] = anchorExpand;
- if (!canEdit)
- {
- EditorGUI.EndDisabledGroup();
- }
- EditorGUILayout.EndVertical();
- }
-
- [MenuItem("CONTEXT/RectTransform/♠ Add Better Locator", false)]
- public static void AddBetterLocator(MenuCommand command)
- {
- var ctx = command.context as RectTransform;
- var locator = ctx.gameObject.AddComponent<BetterLocator>();
- while(UnityEditorInternal.ComponentUtility.MoveComponentUp(locator))
- { }
- }
- [MenuItem("CONTEXT/RectTransform/♠ Add Better Locator", true)]
- public static bool CheckBetterLocator(MenuCommand command)
- {
- var ctx = command.context as RectTransform;
- return ctx.gameObject.GetComponent<BetterLocator>() == null;
- }
- }
- }
|