123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
-
- namespace UnityEditor.U2D.Path
- {
- [CanEditMultipleObjects]
- [CustomEditor(typeof(ScriptablePath), true)]
- public class ScriptablePathInspector : Editor
- {
- private static class Contents
- {
- public static readonly GUIContent linearIcon = IconContent("TangentLinear", "TangentLinearPro", "Linear");
- public static readonly GUIContent continuousIcon = IconContent("TangentContinuous", "TangentContinuousPro", "Continuous");
- public static readonly GUIContent brokenIcon = IconContent("TangentBroken", "TangentBrokenPro", "Broken");
- public static readonly GUIContent positionLabel = new GUIContent("Position", "Position of the Control Point");
- public static readonly GUIContent enableSnapLabel = new GUIContent("Snapping", "Snap points using the snap settings");
- public static readonly GUIContent tangentModeLabel = new GUIContent("Tangent Mode");
- public static readonly GUIContent pointLabel = new GUIContent("Point");
-
-
- private static GUIContent IconContent(string name, string tooltip = null)
- {
- return new GUIContent(AssetDatabase.LoadAssetAtPath<Texture2D>("Packages/com.unity.2d.path/Editor/Handles/" + name + ".png"), tooltip);
- }
-
- private static GUIContent IconContent(string personal, string pro, string tooltip)
- {
- if (EditorGUIUtility.isProSkin)
- return IconContent(pro, tooltip);
-
- return IconContent(personal, tooltip);
- }
- }
-
- private List<ScriptablePath> m_Paths = null;
- private bool m_Dragged = false;
-
- protected List<ScriptablePath> paths
- {
- get
- {
- if (m_Paths == null)
- m_Paths = targets.Select( t => t as ScriptablePath).ToList();
-
- return m_Paths;
- }
- }
-
- public override void OnInspectorGUI()
- {
- DoTangentModeInspector();
- DoPositionInspector();
- }
-
- protected void DoTangentModeInspector()
- {
- if (!IsAnyShapeType(ShapeType.Spline))
- return;
-
- EditorGUILayout.BeginHorizontal();
- EditorGUILayout.PrefixLabel(Contents.tangentModeLabel);
-
- using (new EditorGUI.DisabledGroupScope(!IsAnyPointSelected()))
- {
- if (DoToggle(GetToggleStateFromTangentMode(TangentMode.Linear), Contents.linearIcon))
- SetMixedTangentMode(TangentMode.Linear);
-
- if (DoToggle(GetToggleStateFromTangentMode(TangentMode.Continuous), Contents.continuousIcon))
- SetMixedTangentMode(TangentMode.Continuous);
-
- if (DoToggle(GetToggleStateFromTangentMode(TangentMode.Broken), Contents.brokenIcon))
- SetMixedTangentMode(TangentMode.Broken);
- }
-
- EditorGUILayout.EndHorizontal();
- }
-
- protected void DoPositionInspector()
- {
- var showMixedValue = EditorGUI.showMixedValue;
- var wideMode = EditorGUIUtility.wideMode;
-
- var position = Vector3.zero;
- var isMixed = GetMixedPosition(out position);
-
- EditorGUI.showMixedValue = isMixed;
- EditorGUIUtility.wideMode = true;
-
- using (new EditorGUI.DisabledGroupScope(!IsAnyPointSelected()))
- {
- if (GUIUtility.hotControl == 0)
- m_Dragged = false;
-
- EditorGUI.BeginChangeCheck();
-
- var delta = EditorGUILayout.Vector2Field(Contents.positionLabel, position) - (Vector2)position;
-
- if (EditorGUI.EndChangeCheck())
- {
- if (m_Dragged == false)
- {
- foreach(var path in paths)
- path.undoObject.RegisterUndo("Point Position");
-
- m_Dragged = true;
- }
-
- SetMixedDeltaPosition(delta);
- }
- }
-
- EditorGUI.showMixedValue = showMixedValue;
- EditorGUIUtility.wideMode = wideMode;
- }
-
- private bool DoToggle(bool value, GUIContent icon)
- {
- const float kButtonWidth = 33f;
- const float kButtonHeight = 23f;
- var buttonStyle = new GUIStyle("EditModeSingleButton");
-
- var changed = false;
- using (var check = new EditorGUI.ChangeCheckScope())
- {
- value = GUILayout.Toggle(value, icon, buttonStyle, GUILayout.Width(kButtonWidth), GUILayout.Height(kButtonHeight));
- changed = check.changed;
- }
-
- return value && changed;
- }
-
- private bool GetToggleStateFromTangentMode(TangentMode mode)
- {
- foreach(var path in paths)
- {
- var selection = path.selection;
-
- foreach (var index in selection.elements)
- if (path.GetPoint(index).tangentMode != mode)
- return false;
- }
-
- return true;
- }
-
- private void SetMixedTangentMode(TangentMode tangentMode)
- {
- foreach(var path in paths)
- {
- path.undoObject.RegisterUndo("Tangent Mode");
-
- foreach (var index in path.selection.elements)
- path.SetTangentMode(index, tangentMode);
- }
-
- SceneView.RepaintAll();
- }
-
- private bool GetMixedPosition(out Vector3 position)
- {
- var first = true;
- position = Vector3.zero;
-
- var activeObject = Selection.activeObject as GameObject;
- if (Selection.count > 1 || !activeObject)
- return true;
-
- foreach(var path in paths)
- {
- MonoBehaviour behavior = path.owner as MonoBehaviour;
- if (!behavior || activeObject != behavior.gameObject)
- continue;
-
- var selection = path.selection;
-
- foreach (var index in selection.elements)
- {
- var controlPoint = path.GetPointLocal(index);
-
- if (first)
- {
- position = controlPoint.position;
- first = false;
- }
- else if (position != controlPoint.position)
- {
- return true;
- }
- }
-
- }
-
- return false;
- }
-
- private void SetMixedDeltaPosition(Vector3 delta)
- {
- foreach(var path in paths)
- {
- var selection = path.selection;
- var matrix = path.localToWorldMatrix;
-
- path.localToWorldMatrix = Matrix4x4.identity;
-
- foreach (var index in selection.elements)
- {
- var controlPoint = path.GetPoint(index);
- controlPoint.position += delta;
- path.SetPoint(index, controlPoint);
- }
-
- path.localToWorldMatrix = matrix;
- }
- }
-
- private bool IsAnyShapeType(ShapeType shapeType)
- {
- foreach(var path in paths)
- if (path.shapeType == shapeType)
- return true;
-
- return false;
- }
-
- protected bool IsAnyPointSelected()
- {
- foreach(var path in paths)
- if (path.selection.Count > 0)
- return true;
-
- return false;
- }
- }
- }
|