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
|
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(BezierPoint))]
[CanEditMultipleObjects]
public class BezierPointEditor : Editor {
BezierPoint point;
SerializedProperty handleTypeProp;
SerializedProperty handle1Prop;
SerializedProperty handle2Prop;
private delegate void HandleFunction(BezierPoint p);
private HandleFunction[] handlers = new HandleFunction[] { HandleConnected, HandleBroken, HandleAbsent };
void OnEnable(){
point = (BezierPoint)target;
handleTypeProp = serializedObject.FindProperty("handleStyle");
handle1Prop = serializedObject.FindProperty("_handle1");
handle2Prop = serializedObject.FindProperty("_handle2");
}
public override void OnInspectorGUI (){
serializedObject.Update();
BezierPoint.HandleStyle newHandleType = (BezierPoint.HandleStyle)EditorGUILayout.EnumPopup("Handle Type", (BezierPoint.HandleStyle)handleTypeProp.intValue);
if(newHandleType != (BezierPoint.HandleStyle)handleTypeProp.intValue)
{
handleTypeProp.intValue = (int)newHandleType;
if((int)newHandleType == 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((int)newHandleType == 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((int)newHandleType == 2)
{
handle1Prop.vector3Value = Vector3.zero;
handle2Prop.vector3Value = Vector3.zero;
}
}
if(handleTypeProp.intValue != 2)
{
Vector3 newHandle1 = EditorGUILayout.Vector3Field("Handle 1", handle1Prop.vector3Value);
Vector3 newHandle2 = EditorGUILayout.Vector3Field("Handle 2", handle2Prop.vector3Value);
if(handleTypeProp.intValue == 0){
if(newHandle1 != handle1Prop.vector3Value){
handle1Prop.vector3Value = newHandle1;
handle2Prop.vector3Value = -newHandle1;
}
else if(newHandle2 != handle2Prop.vector3Value){
handle1Prop.vector3Value = -newHandle2;
handle2Prop.vector3Value = newHandle2;
}
}
else{
handle1Prop.vector3Value = newHandle1;
handle2Prop.vector3Value = newHandle2;
}
}
if(GUI.changed){
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}
}
void OnSceneGUI()
{
Handles.color = Color.green;
Vector3 newPosition = Handles.FreeMoveHandle(point.position, point.transform.rotation, HandleUtility.GetHandleSize(point.position)*0.2f, Vector3.zero, Handles.CubeCap);
if(point.position != newPosition) point.position = newPosition;
handlers[(int)point.handleStyle](point);
Handles.color = Color.yellow;
Handles.DrawLine(point.position, point.globalHandle1);
Handles.DrawLine(point.position, point.globalHandle2);
BezierCurveEditor.DrawOtherPoints(point.curve, point);
}
private static void HandleConnected(BezierPoint p){
Handles.color = Color.cyan;
Vector3 newGlobal1 = Handles.FreeMoveHandle(p.globalHandle1, p.transform.rotation, HandleUtility.GetHandleSize(p.globalHandle1)*0.15f, Vector3.zero, Handles.SphereCap);
if(newGlobal1 != p.globalHandle1){
Undo.RegisterUndo(p, "Move Handle");
p.globalHandle1 = newGlobal1;
p.globalHandle2 = -(newGlobal1 - p.position) + p.position;
}
Vector3 newGlobal2 = Handles.FreeMoveHandle(p.globalHandle2, p.transform.rotation, HandleUtility.GetHandleSize(p.globalHandle2)*0.15f, Vector3.zero, Handles.SphereCap);
if(newGlobal2 != p.globalHandle2){
Undo.RegisterUndo(p, "Move Handle");
p.globalHandle1 = -(newGlobal2 - p.position) + p.position;
p.globalHandle2 = newGlobal2;
}
}
private static void HandleBroken(BezierPoint p){
Handles.color = Color.cyan;
Vector3 newGlobal1 = Handles.FreeMoveHandle(p.globalHandle1, Quaternion.identity, HandleUtility.GetHandleSize(p.globalHandle1)*0.15f, Vector3.zero, Handles.SphereCap);
Vector3 newGlobal2 = Handles.FreeMoveHandle(p.globalHandle2, Quaternion.identity, HandleUtility.GetHandleSize(p.globalHandle2)*0.15f, Vector3.zero, Handles.SphereCap);
if(newGlobal1 != p.globalHandle1)
{
Undo.RegisterUndo(p, "Move Handle");
p.globalHandle1 = newGlobal1;
}
if(newGlobal2 != p.globalHandle2)
{
Undo.RegisterUndo(p, "Move Handle");
p.globalHandle2 = newGlobal2;
}
}
private static void HandleAbsent(BezierPoint p)
{
p.handle1 = Vector3.zero;
p.handle2 = Vector3.zero;
}
}
|