summaryrefslogtreecommitdiff
path: root/Assets/ThirdParty/BezierCurves/Editor/BezierCurveEditor.cs
blob: 6a6c04f62a156fe72bc58f80b11f14785b62fda5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(BezierCurve))]
public class BezierCurveEditor : Editor 
{	
	BezierCurve curve;
	SerializedProperty resolutionProp;
	SerializedProperty closeProp;
	SerializedProperty pointsProp;
	SerializedProperty colorProp;
	
	private static bool showPoints = true;
	
	void OnEnable()
	{
		curve = (BezierCurve)target;
		
		resolutionProp = serializedObject.FindProperty("resolution");
		closeProp = serializedObject.FindProperty("_close");
		pointsProp = serializedObject.FindProperty("points");
		colorProp = serializedObject.FindProperty("drawColor");
	}
	
	public override void OnInspectorGUI()
	{
		serializedObject.Update();
		
		EditorGUILayout.PropertyField(resolutionProp);
		EditorGUILayout.PropertyField(closeProp);
		EditorGUILayout.PropertyField(colorProp);
		
		showPoints = EditorGUILayout.Foldout(showPoints, "Points");
		
		if(showPoints)
		{
			int pointCount = pointsProp.arraySize;
			
			for(int i = 0; i < pointCount; i++)
			{
				DrawPointInspector(curve[i], i);
			}
			
			if(GUILayout.Button("Add Point"))
			{
				Undo.RegisterSceneUndo("Add Point");
				
				GameObject pointObject = new GameObject("Point "+pointsProp.arraySize);
				pointObject.transform.parent = curve.transform;
				pointObject.transform.localPosition = Vector3.zero;
				BezierPoint newPoint = pointObject.AddComponent<BezierPoint>();
				
				newPoint.curve = curve;
				newPoint.handle1 = Vector3.right*0.1f;
				newPoint.handle2 = -Vector3.right*0.1f;
				
				pointsProp.InsertArrayElementAtIndex(pointsProp.arraySize);
				pointsProp.GetArrayElementAtIndex(pointsProp.arraySize - 1).objectReferenceValue = newPoint;
			}
		}
		
		if(GUI.changed)
		{
			serializedObject.ApplyModifiedProperties();
			EditorUtility.SetDirty(target);
		}
	}
	
	void OnSceneGUI()
	{
		for(int i = 0; i < curve.pointCount; i++)
		{
			DrawPointSceneGUI(curve[i]);
		}
	}
	
	void DrawPointInspector(BezierPoint point, int index)
	{
		SerializedObject serObj = new SerializedObject(point);
		
		SerializedProperty handleStyleProp = serObj.FindProperty("handleStyle");
		SerializedProperty handle1Prop = serObj.FindProperty("_handle1");
		SerializedProperty handle2Prop = serObj.FindProperty("_handle2");
		
		EditorGUILayout.BeginHorizontal();
		
		if(GUILayout.Button("X", GUILayout.Width(20)))
		{
			Undo.RegisterSceneUndo("Remove Point");
			pointsProp.MoveArrayElement(curve.GetPointIndex(point), curve.pointCount - 1);
			pointsProp.arraySize--;
			DestroyImmediate(point.gameObject);
			return;
		}

		EditorGUILayout.ObjectField(point.gameObject, typeof(GameObject), true);
		
		if(index != 0 && GUILayout.Button(@"/\", GUILayout.Width(25)))
		{
			UnityEngine.Object other = pointsProp.GetArrayElementAtIndex(index - 1).objectReferenceValue;
			pointsProp.GetArrayElementAtIndex(index - 1).objectReferenceValue = point;
			pointsProp.GetArrayElementAtIndex(index).objectReferenceValue = other;
		}
		
		if(index != pointsProp.arraySize - 1 && GUILayout.Button(@"\/", GUILayout.Width(25)))
		{
			UnityEngine.Object other = pointsProp.GetArrayElementAtIndex(index + 1).objectReferenceValue;
			pointsProp.GetArrayElementAtIndex(index + 1).objectReferenceValue = point;
			pointsProp.GetArrayElementAtIndex(index).objectReferenceValue = other;
		}
		
		EditorGUILayout.EndHorizontal();
		
		EditorGUI.indentLevel++;
		EditorGUI.indentLevel++;
		
		int newType = (int)((object)EditorGUILayout.EnumPopup("Handle Type", (BezierPoint.HandleStyle)handleStyleProp.enumValueIndex));
		
		if(newType != handleStyleProp.enumValueIndex)
		{
			handleStyleProp.enumValueIndex = newType;
			if(newType == 0)
			{
				if(handle1Prop.vector3Value != Vector3.zero) handle2Prop.vector3Value = -handle1Prop.vector3Value;
				else if(handle2Prop.vector3Value != Vector3.zero) handle1Prop.vector3Value = -handle2Prop.vector3Value;
				else
				{
					handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);	
					handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);	
				}
			}
			
			else if(newType == 1)
			{
				if(handle1Prop.vector3Value == Vector3.zero && handle2Prop.vector3Value == Vector3.zero)
				{
					handle1Prop.vector3Value = new Vector3(0.1f, 0, 0);	
					handle2Prop.vector3Value = new Vector3(-0.1f, 0, 0);
				}
			}
			
			else if(newType == 2)
			{
				handle1Prop.vector3Value = Vector3.zero;	
				handle2Prop.vector3Value = Vector3.zero;	
			}
		}
		
		Vector3 newPointPos = EditorGUILayout.Vector3Field("Position : ", point.transform.localPosition);
		if(newPointPos != point.transform.localPosition)
		{
			Undo.RegisterUndo(point.transform, "Move Bezier Point");
			point.transform.localPosition = newPointPos;
		}
		
		if(handleStyleProp.enumValueIndex == 0)
		{			
			Vector3 newPosition;
			
			newPosition = EditorGUILayout.Vector3Field("Handle 1", handle1Prop.vector3Value);
			if(newPosition != handle1Prop.vector3Value)
			{
				handle1Prop.vector3Value = newPosition;
				handle2Prop.vector3Value = -newPosition;
			}
			
			newPosition = EditorGUILayout.Vector3Field("Handle 2", handle2Prop.vector3Value);
			if(newPosition != handle2Prop.vector3Value)
			{
				handle1Prop.vector3Value = -newPosition;
				handle2Prop.vector3Value = newPosition;				
			}
		}
		
		else if(handleStyleProp.enumValueIndex == 1)
		{
			EditorGUILayout.PropertyField(handle1Prop);
			EditorGUILayout.PropertyField(handle2Prop);			
		}
		
		EditorGUI.indentLevel--;
		EditorGUI.indentLevel--;	
		
		if(GUI.changed)
		{
			serObj.ApplyModifiedProperties();
			EditorUtility.SetDirty(serObj.targetObject);
		}
	}
	
	static void DrawPointSceneGUI(BezierPoint point)
	{	
		Handles.Label(point.position + new Vector3(0, HandleUtility.GetHandleSize(point.position) * 0.4f, 0), point.gameObject.name);
		
		Handles.color = Color.green;
		Vector3 newPosition = Handles.FreeMoveHandle(point.position, point.transform.rotation, HandleUtility.GetHandleSize(point.position)*0.1f, Vector3.zero, Handles.RectangleCap);
		
		if(newPosition != point.position)
		{
			Undo.RegisterUndo(point.transform, "Move Point");
			point.transform.position = newPosition;
		}
		
		if(point.handleStyle != BezierPoint.HandleStyle.None)
		{
			Handles.color = Color.cyan;
			Vector3 newGlobal1 = Handles.FreeMoveHandle(point.globalHandle1, point.transform.rotation, HandleUtility.GetHandleSize(point.globalHandle1)*0.075f, Vector3.zero, Handles.CircleCap);
			if(point.globalHandle1 != newGlobal1)
			{
				Undo.RegisterUndo(point, "Move Handle");
				point.globalHandle1 = newGlobal1;
				if(point.handleStyle == BezierPoint.HandleStyle.Connected) point.globalHandle2 = -(newGlobal1 - point.position) + point.position;
			}
			
			Vector3 newGlobal2 = Handles.FreeMoveHandle(point.globalHandle2, point.transform.rotation, HandleUtility.GetHandleSize(point.globalHandle2)*0.075f, Vector3.zero, Handles.CircleCap);
			if(point.globalHandle2 != newGlobal2)
			{
				Undo.RegisterUndo(point, "Move Handle");
				point.globalHandle2 = newGlobal2;
				if(point.handleStyle == BezierPoint.HandleStyle.Connected) point.globalHandle1 = -(newGlobal2 - point.position) + point.position;
			}
			
			Handles.color = Color.yellow;
			Handles.DrawLine(point.position, point.globalHandle1);
			Handles.DrawLine(point.position, point.globalHandle2);
		}
	}
	
	public static void DrawOtherPoints(BezierCurve curve, BezierPoint caller)
	{
		foreach(BezierPoint p in curve.GetAnchorPoints())
		{
			if(p != caller) DrawPointSceneGUI(p);
		}
	}
	
	[MenuItem("GameObject/Create Other/Bezier Curve")]
	public static void CreateCurve(MenuCommand command)
	{
		GameObject curveObject = new GameObject("BezierCurve");
		Undo.RegisterUndo(curveObject, "Undo Create Curve");
		BezierCurve curve = curveObject.AddComponent<BezierCurve>();
		
		BezierPoint p1 = curve.AddPointAt(Vector3.forward * 0.5f);
		p1.handleStyle = BezierPoint.HandleStyle.Connected;
		p1.handle1 = new Vector3(-0.28f, 0, 0);
		
		BezierPoint p2 = curve.AddPointAt(Vector3.right * 0.5f);
		p2.handleStyle = BezierPoint.HandleStyle.Connected;
		p2.handle1 = new Vector3(0, 0, 0.28f);
		
		BezierPoint p3 = curve.AddPointAt(-Vector3.forward * 0.5f);
		p3.handleStyle = BezierPoint.HandleStyle.Connected;
		p3.handle1 = new Vector3(0.28f, 0, 0);		
		
		BezierPoint p4 = curve.AddPointAt(-Vector3.right * 0.5f);
		p4.handleStyle = BezierPoint.HandleStyle.Connected;
		p4.handle1 = new Vector3(0, 0, -0.28f);
		
		curve.close = true;
	}
}