forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlider2D.cs
More file actions
144 lines (144 loc) · 5.95 KB
/
Copy pathSlider2D.cs
File metadata and controls
144 lines (144 loc) · 5.95 KB
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
using System;
using UnityEditor;
using UnityEngine;
namespace UnityEditorInternal
{
internal class Slider2D
{
private static Vector2 s_CurrentMousePosition;
private static Vector3 s_StartPosition;
private static Vector2 s_StartPlaneOffset;
public static Vector3 Do(int id, Vector3 handlePos, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, float snap, bool drawHelper)
{
return Slider2D.Do(id, handlePos, new Vector3(0f, 0f, 0f), handleDir, slideDir1, slideDir2, handleSize, drawFunc, new Vector2(snap, snap), drawHelper);
}
public static Vector3 Do(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, float snap, bool drawHelper)
{
return Slider2D.Do(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, drawFunc, new Vector2(snap, snap), drawHelper);
}
public static Vector3 Do(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper)
{
bool changed = GUI.changed;
GUI.changed = false;
Vector2 vector = Slider2D.CalcDeltaAlongDirections(id, handlePos, offset, handleDir, slideDir1, slideDir2, handleSize, drawFunc, snap, drawHelper);
if (GUI.changed)
{
handlePos = Slider2D.s_StartPosition + slideDir1 * vector.x + slideDir2 * vector.y;
}
GUI.changed |= changed;
return handlePos;
}
private static Vector2 CalcDeltaAlongDirections(int id, Vector3 handlePos, Vector3 offset, Vector3 handleDir, Vector3 slideDir1, Vector3 slideDir2, float handleSize, Handles.DrawCapFunction drawFunc, Vector2 snap, bool drawHelper)
{
Vector2 vector = new Vector2(0f, 0f);
Event current = Event.current;
switch (current.GetTypeForControl(id))
{
case EventType.MouseDown:
if (((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2)) && GUIUtility.hotControl == 0)
{
Plane plane = new Plane(Handles.matrix.MultiplyVector(handleDir), Handles.matrix.MultiplyPoint(handlePos));
Ray ray = HandleUtility.GUIPointToWorldRay(current.mousePosition);
float distance = 0f;
plane.Raycast(ray, out distance);
GUIUtility.keyboardControl = id;
GUIUtility.hotControl = id;
Slider2D.s_CurrentMousePosition = current.mousePosition;
Slider2D.s_StartPosition = handlePos;
Vector3 a = Handles.s_InverseMatrix.MultiplyPoint(ray.GetPoint(distance));
Vector3 lhs = a - handlePos;
Slider2D.s_StartPlaneOffset.x = Vector3.Dot(lhs, slideDir1);
Slider2D.s_StartPlaneOffset.y = Vector3.Dot(lhs, slideDir2);
current.Use();
EditorGUIUtility.SetWantsMouseJumping(1);
}
break;
case EventType.MouseUp:
if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2))
{
GUIUtility.hotControl = 0;
current.Use();
EditorGUIUtility.SetWantsMouseJumping(0);
}
break;
case EventType.MouseDrag:
if (GUIUtility.hotControl == id)
{
Slider2D.s_CurrentMousePosition += current.delta;
Vector3 a2 = Handles.matrix.MultiplyPoint(handlePos);
Vector3 normalized = Handles.matrix.MultiplyVector(slideDir1).normalized;
Vector3 normalized2 = Handles.matrix.MultiplyVector(slideDir2).normalized;
Ray ray2 = HandleUtility.GUIPointToWorldRay(Slider2D.s_CurrentMousePosition);
Plane plane2 = new Plane(a2, a2 + normalized, a2 + normalized2);
float distance2 = 0f;
if (plane2.Raycast(ray2, out distance2))
{
Vector3 point = Handles.s_InverseMatrix.MultiplyPoint(ray2.GetPoint(distance2));
vector.x = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir1);
vector.y = HandleUtility.PointOnLineParameter(point, Slider2D.s_StartPosition, slideDir2);
vector -= Slider2D.s_StartPlaneOffset;
if (snap.x > 0f || snap.y > 0f)
{
vector.x = Handles.SnapValue(vector.x, snap.x);
vector.y = Handles.SnapValue(vector.y, snap.y);
}
GUI.changed = true;
}
current.Use();
}
break;
case EventType.Repaint:
if (drawFunc != null)
{
Vector3 vector2 = handlePos + offset;
Quaternion rotation = Quaternion.LookRotation(handleDir, slideDir1);
Color color = Color.white;
if (id == GUIUtility.keyboardControl)
{
color = Handles.color;
Handles.color = Handles.selectedColor;
}
drawFunc(id, vector2, rotation, handleSize);
if (id == GUIUtility.keyboardControl)
{
Handles.color = color;
}
if (drawHelper && GUIUtility.hotControl == id)
{
Vector3[] array = new Vector3[4];
float d = handleSize * 10f;
array[0] = vector2 + (slideDir1 * d + slideDir2 * d);
array[1] = array[0] - slideDir1 * d * 2f;
array[2] = array[1] - slideDir2 * d * 2f;
array[3] = array[2] + slideDir1 * d * 2f;
Color color2 = Handles.color;
Handles.color = Color.white;
float num = 0.6f;
Handles.DrawSolidRectangleWithOutline(array, new Color(1f, 1f, 1f, 0.05f), new Color(num, num, num, 0.4f));
Handles.color = color2;
}
}
break;
case EventType.Layout:
if (drawFunc == new Handles.DrawCapFunction(Handles.ArrowCap))
{
HandleUtility.AddControl(id, HandleUtility.DistanceToLine(handlePos + offset, handlePos + handleDir * handleSize));
HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset + handleDir * handleSize, handleSize * 0.2f));
}
else
{
if (drawFunc == new Handles.DrawCapFunction(Handles.RectangleCap))
{
HandleUtility.AddControl(id, HandleUtility.DistanceToRectangle(handlePos + offset, Quaternion.LookRotation(handleDir, slideDir1), handleSize));
}
else
{
HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(handlePos + offset, handleSize * 0.5f));
}
}
break;
}
return vector;
}
}
}