summaryrefslogtreecommitdiff
path: root/AlienSurvival/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
blob: fb9fb5311800636150d7dd15328b0445489fee38 (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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

// https://docs.unity3d.com/ScriptReference/Handles.html

// Scene视图中的自定义handles
public static class EditorHandlesHelper
{
	static int s_ValueScaleHandleHash;
	static int s_PositionArrowHandleHash;

    // limit value 数据
    static float s_StartScale;
    static float s_ValueDrag;
    static float s_ScaleDrawLength;

	static Vector3 s_LastPosition;

    // style 
    static GUIStyle s_StyleBlackLabel;
    static GUIStyle s_StyleWhiteLabel;
    static Dictionary<int/*color*/, GUIStyle> s_StyleLabels;

    static EditorHandlesHelper()
    {
        s_ValueScaleHandleHash = "ValueScaleHandle".GetHashCode();
		s_PositionArrowHandleHash = "PositionArrowHandle".GetHashCode();


		SetupStyles();
    }

    static void SetupStyles()
    {
        s_StyleBlackLabel = new GUIStyle();
        s_StyleBlackLabel.normal.textColor = Color.black;
        s_StyleWhiteLabel = new GUIStyle();
        s_StyleWhiteLabel.normal.textColor = Color.white;
        s_StyleLabels = new Dictionary<int, GUIStyle>();
    }

    #region 单一handle

    /// <summary>
	/// 无事件箭头
	/// </summary>
	/// <param name="position"></param>
	/// <param name="direction"></param>
	/// <param name="length"></param>
	/// <param name="arrowSize"></param>
	/// <param name="dotted"></param>
    public static void Arrow(Vector3 position, Vector3 direction, float length = 1f, float arrowSize = 0.1f, bool dotted = false)
    {
        direction = direction.normalized;
        if(!dotted)
            Handles.DrawLine(position, position + direction * length);
        else
            Handles.DrawDottedLine(position, position + direction * length,1f);
        Handles.ConeHandleCap(0, position + direction * length, Quaternion.LookRotation(direction, Vector3.up), arrowSize, EventType.Repaint);
    }

	/// <summary>
	/// 处理位置的可拖拽箭头
	/// </summary>
	/// <param name="currentValue"></param>
	/// <param name="position"></param>
	/// <param name="direction"></param>
	/// <param name="length"></param>
	/// <param name="arrowSize"></param>
	/// <param name="dotted"></param>
	/// <returns></returns>
	public static Vector3 PositionArrow(Vector3 position, Vector3 direction, float length, float arrowSize, bool dotted = false)
	{
		direction = direction.normalized;

		Vector3 value = Vector3.zero;
		Handles.CapFunction capFunc = Handles.ConeHandleCap;

		Quaternion capRot = Quaternion.LookRotation(direction, Vector3.up);
		float size = arrowSize;
		Vector3 arrowPos = position + direction * length;

		int id = GUIUtility.GetControlID(s_PositionArrowHandleHash, FocusType.Keyboard);

		Event current = Event.current;
		switch (current.GetTypeForControl(id))
		{
			case EventType.MouseDown:
				if ((HandleUtility.nearestControl == id && current.button == 0) || (GUIUtility.keyboardControl == id && current.button == 2))
				{
					s_LastPosition = Camera.current.ScreenToWorldPoint(Event.current.mousePosition);

					GUIUtility.keyboardControl = id;
					GUIUtility.hotControl = id;
					current.Use();
					EditorGUIUtility.SetWantsMouseJumping(1);
				}
				break;
			case EventType.MouseUp:
				if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2))
				{
					GUIUtility.keyboardControl = 0;
					GUIUtility.hotControl = 0;
					current.Use();
					EditorGUIUtility.SetWantsMouseJumping(0);
				}
				break;
			case EventType.MouseDrag:
				if (GUIUtility.hotControl == id)
				{
					Vector3 worldpos = Camera.current.ScreenToWorldPoint(Event.current.mousePosition);
					value = worldpos - s_LastPosition;

					s_LastPosition = worldpos;

					GUI.changed = true;
					current.Use();
				}
				break;
			//case EventType.KeyDown:
			//	if (GUIUtility.hotControl == id && current.keyCode == KeyCode.Escape)
			//	{
			//		GUIUtility.hotControl = 0;
			//		GUI.changed = true;
			//		current.Use();
			//	}
			//	break;
			case EventType.Repaint:
				{
					Color color = Color.white;
					if (id == GUIUtility.keyboardControl)
					{
						color = Handles.color;
						Handles.color = Handles.selectedColor;
					}
					capFunc(id, arrowPos, capRot, size * 0.15f, EventType.Repaint);
					DottedLine(position, arrowPos, 1f, dotted);
					if (id == GUIUtility.keyboardControl)
					{
						Handles.color = color;
					}
					break;
				}
			case EventType.Layout:
				HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(arrowPos, size * 0.15f));
				break;
		}

		return value;
	}

	public static void DottedLine(Vector3 p1, Vector3 p2, float length, bool dotted = true)
	{
		if (!dotted)
			Handles.DrawLine(p1, p2);
		else
			Handles.DrawDottedLine(p1, p2, 1f);
	}

	// 控制数值的箭头
	public static float ScaleValue(float value, Vector3 position, Vector3 direction, float length, Handles.CapFunction capFunc, Quaternion capRot, float size, float snap = 0)
    {
        Handles.DrawLine(position, position + direction * length);
        value = ScaleValue(value, position + direction, capRot, size, capFunc, snap);
        return value;
    }

    // 控制数值的handle
    public static float ScaleValue(float value, Vector3 position, Quaternion rotation, float size, Handles.CapFunction capFunc, float snap)
    {
        int controlID = GUIUtility.GetControlID(s_ValueScaleHandleHash, FocusType.Keyboard);
        int id = controlID;
        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.keyboardControl = id;
                    GUIUtility.hotControl = id;
                    s_StartScale = value;
                    s_ValueDrag = 0f;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(1);
                }
                break;
            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && (current.button == 0 || current.button == 2))
                {
                    GUIUtility.hotControl = 0;
                    s_ScaleDrawLength = 1f;
                    current.Use();
                    EditorGUIUtility.SetWantsMouseJumping(0);
                }
                break;
            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    s_ValueDrag += HandleUtility.niceMouseDelta * 0.01f;
					//value = (Handles.SnapValue(s_ValueDrag, snap) + 1f) * s_StartScale;
					value = Camera.current.ScreenToWorldPoint(Event.current.mousePosition).x - 1;
                    s_ScaleDrawLength = value / s_StartScale;
                    GUI.changed = true;
                    current.Use();
                }
                break;
            case EventType.KeyDown:
                if (GUIUtility.hotControl == id && current.keyCode == KeyCode.Escape)
                {
                    value = s_StartScale;
                    s_ScaleDrawLength = 1f;
                    GUIUtility.hotControl = 0;
                    GUI.changed = true;
                    current.Use();
                }
                break;
            case EventType.Repaint:
                {
                    Color color = Color.white;
                    if (id == GUIUtility.keyboardControl)
                    {
                        color = Handles.color;
                        Handles.color = Handles.selectedColor;
                    }
                    capFunc(id, position, rotation, size * 0.15f, EventType.Repaint);
                    if (id == GUIUtility.keyboardControl)
                    {
                        Handles.color = color;
                    }
                    break;
                }
            case EventType.Layout:
                HandleUtility.AddControl(id, HandleUtility.DistanceToCircle(position, size * 0.15f));
                break;
        }
        return value;
    }

    // 带背景的label
    public static void Label(Vector3 position, string label, Color textColor)
    {
        GUIStyle textStyle = TryGetLabelStyleOrCreate(textColor);
        Handles.Label(position, label, textStyle);
    }

    public static void WireCube(Vector3 position, Vector3 size, Color col)
    {
        Color c = Handles.color;
        Handles.color = col;
        Handles.DrawWireCube(position, size);
        Handles.color = c;
    }

    #endregion

    #region 复合handle

    #endregion

    #region 内部方法

    static GUIStyle TryGetLabelStyleOrCreate(Color color)
    {
        GUIStyle style;
        if (!s_StyleLabels.TryGetValue(color.GetHashCode(), out style))
        {
            style = new GUIStyle();
            style.normal.textColor = color;
            s_StyleLabels.Add(color.GetHashCode(), style);
        }
        return style;
    }

    #endregion 

}