A game about forced loneliness, made by TACStudios
1using System;
2using System.Linq;
3using System.Collections;
4using System.Collections.Generic;
5using UnityEngine;
6using UnityEditor;
7using UnityEditor.EditorTools;
8
9namespace UnityEditor.U2D.Common.Path
10{
11 internal abstract class PathComponentEditor<T> : Editor where T : ScriptablePath
12 {
13 private static class Contents
14 {
15 public static readonly GUIContent snappingLabel = new GUIContent("Snapping", "Snap points using the snap settings");
16 }
17
18 private Editor m_CachedEditor = null;
19
20 // Returns true on Changed.
21 protected bool DoEditButton<U>(GUIContent icon, string label) where U : PathEditorTool<T>
22 {
23 const float kButtonWidth = 33;
24 const float kButtonHeight = 23;
25 const float k_SpaceBetweenLabelAndButton = 5;
26 var buttonStyle = new GUIStyle("EditModeSingleButton");
27
28 var rect = EditorGUILayout.GetControlRect(true, kButtonHeight, buttonStyle);
29 var buttonRect = new Rect(rect.xMin + EditorGUIUtility.labelWidth, rect.yMin, kButtonWidth, kButtonHeight);
30
31 var labelContent = new GUIContent(label);
32 var labelSize = GUI.skin.label.CalcSize(labelContent);
33
34 var labelRect = new Rect(
35 buttonRect.xMax + k_SpaceBetweenLabelAndButton,
36 rect.yMin + (rect.height - labelSize.y) * .5f,
37 labelSize.x,
38 rect.height);
39
40 bool hasChanged = false;
41 using (new EditorGUI.DisabledGroupScope(!EditorToolManager.IsAvailable<U>()))
42 {
43 using (var check = new EditorGUI.ChangeCheckScope())
44 {
45 var isActive = GUI.Toggle(buttonRect, EditorToolManager.IsActiveTool<U>(), icon, buttonStyle);
46
47 GUI.Label(labelRect, label);
48
49 if (check.changed)
50 {
51 if (isActive)
52 ToolManager.SetActiveTool<U>();
53 else
54 ToolManager.RestorePreviousTool();
55 hasChanged = true;
56 }
57 }
58 }
59 return hasChanged;
60 }
61
62 protected void DoPathInspector<U>() where U : PathEditorTool<T>
63 {
64 if (EditorToolManager.IsActiveTool<U>() && EditorToolManager.IsAvailable<U>())
65 {
66 var paths = EditorToolManager.GetEditorTool<U>().paths;
67
68 CreateCachedEditor(paths, null, ref m_CachedEditor);
69
70 if (m_CachedEditor == null) //Needed to avoid a nullref on exiting playmode
71 return;
72
73 using (var check = new EditorGUI.ChangeCheckScope())
74 {
75 m_CachedEditor.OnInspectorGUI();
76
77 if (check.changed)
78 EditorToolManager.GetEditorTool<U>().SetShapes();
79 }
80 }
81 }
82
83 protected void DoOpenEndedInspector<U>(SerializedProperty isOpenEndedProperty) where U : PathEditorTool<T>
84 {
85 serializedObject.Update();
86
87 using (var check = new EditorGUI.ChangeCheckScope())
88 {
89 EditorGUILayout.PropertyField(isOpenEndedProperty);
90
91 if (check.changed)
92 {
93 if (EditorToolManager.IsActiveTool<U>() && EditorToolManager.IsAvailable<U>())
94 {
95 var paths = EditorToolManager.GetEditorTool<U>().paths;
96
97 foreach (var path in paths)
98 {
99 path.undoObject.RegisterUndo("Set Open Ended");
100 path.isOpenEnded = isOpenEndedProperty.boolValue;
101 }
102 }
103 }
104 }
105
106 serializedObject.ApplyModifiedProperties();
107 }
108 }
109}