forked from keenanwoodall/Deform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBendDeformerEditor.cs
133 lines (109 loc) · 4.48 KB
/
BendDeformerEditor.cs
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
using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using Beans.Unity.Editor;
using Deform;
namespace DeformEditor
{
[CustomEditor (typeof (BendDeformer)), CanEditMultipleObjects]
public class BendDeformerEditor : DeformerEditor
{
private class Content
{
public static readonly GUIContent Angle = new GUIContent (text: "Angle", tooltip: "How many degrees the mesh should be bent by the time it reaches the top bounds.");
public static readonly GUIContent Factor = DeformEditorGUIUtility.DefaultContent.Factor;
public static readonly GUIContent Mode = new GUIContent (text: "Mode", tooltip: "Unlimited: Entire mesh is bent.\nLimited: Mesh is only bent between bounds.");
public static readonly GUIContent Top = new GUIContent (text: "Top", tooltip: "Any vertices above this will have been fully bent.");
public static readonly GUIContent Bottom = new GUIContent (text: "Bottom", tooltip: "Any vertices below this will be fully unbent.");
public static readonly GUIContent Axis = DeformEditorGUIUtility.DefaultContent.Axis;
}
private class Properties
{
public SerializedProperty Angle;
public SerializedProperty Factor;
public SerializedProperty Mode;
public SerializedProperty Top;
public SerializedProperty Bottom;
public SerializedProperty Axis;
public Properties (SerializedObject obj)
{
Angle = obj.FindProperty ("angle");
Factor = obj.FindProperty ("factor");
Mode = obj.FindProperty ("mode");
Top = obj.FindProperty ("top");
Bottom = obj.FindProperty ("bottom");
Axis = obj.FindProperty ("axis");
}
}
private Properties properties;
private ArcHandle angleHandle = new ArcHandle ();
private VerticalBoundsHandle boundsHandle = new VerticalBoundsHandle ();
protected override void OnEnable ()
{
base.OnEnable ();
properties = new Properties (serializedObject);
boundsHandle.handleCapFunction = DeformHandles.HandleCapFunction;
boundsHandle.drawGuidelineCallback = (a, b) => DeformHandles.Line (a, b, DeformHandles.LineMode.LightDotted);
}
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
serializedObject.UpdateIfRequiredOrScript ();
EditorGUILayout.PropertyField (properties.Angle, Content.Angle);
EditorGUILayout.PropertyField (properties.Factor, Content.Factor);
EditorGUILayout.PropertyField (properties.Mode, Content.Mode);
using (new EditorGUI.IndentLevelScope ())
{
EditorGUILayoutx.MinField (properties.Top, properties.Bottom.floatValue, Content.Top);
EditorGUILayoutx.MaxField (properties.Bottom, properties.Top.floatValue, Content.Bottom);
}
EditorGUILayout.PropertyField (properties.Axis, Content.Axis);
serializedObject.ApplyModifiedProperties ();
EditorApplication.QueuePlayerLoopUpdate ();
}
public override void OnSceneGUI ()
{
base.OnSceneGUI ();
var bend = target as BendDeformer;
DrawAngleHandle (bend);
boundsHandle.handleColor = DeformEditorSettings.SolidHandleColor;
boundsHandle.screenspaceHandleSize = DeformEditorSettings.ScreenspaceSliderHandleCapSize;
if (boundsHandle.DrawHandle (bend.Top, bend.Bottom, bend.Axis, Vector3.up))
{
Undo.RecordObject (bend, "Changed Bounds");
bend.Top = boundsHandle.top;
bend.Bottom = boundsHandle.bottom;
}
EditorApplication.QueuePlayerLoopUpdate ();
}
private void DrawAngleHandle (BendDeformer bend)
{
var radiusDistanceOffset = HandleUtility.GetHandleSize (bend.Axis.position + bend.Axis.up * bend.Top) * DeformEditorSettings.ScreenspaceSliderHandleCapSize * 2f;
angleHandle.angle = bend.Angle;
angleHandle.radius = (bend.Top - bend.Bottom) + radiusDistanceOffset;
angleHandle.fillColor = Color.clear;
var handleRotation = bend.Axis.rotation * Quaternion.Euler (-90, 0f, 0f);
// There's some weird issue where if you pass the normal lossyScale, the handle's scale on the y axis is changed when the transform's z axis is changed.
// My simple solution is to swap the y and z.
var handleScale = new Vector3
(
x: bend.Axis.lossyScale.x,
y: bend.Axis.lossyScale.z,
z: bend.Axis.lossyScale.y
);
var matrix = Matrix4x4.TRS (bend.Axis.position, handleRotation, handleScale);
using (new Handles.DrawingScope (DeformEditorSettings.SolidHandleColor, matrix))
{
using (var check = new EditorGUI.ChangeCheckScope ())
{
angleHandle.DrawHandle ();
if (check.changed)
{
Undo.RecordObject (bend, "Changed Angle");
bend.Angle = angleHandle.angle;
}
}
}
}
}
}