summaryrefslogtreecommitdiff
path: root/UnityCollection/Assets/Tools/EditorGUIHelper/Editor/EditorHandlesHelper.cs
blob: 4bf4b8f96665bff0d69309ff08e3b7f9f7b12b14 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

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

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

    static EditorHandlesHelper()
    {
        s_ValueScaleHandleHash = "ValueScaleHandleHash".GetHashCode();
    }

    #region 单一handle

    // 控制数值的handle
    public static float ScaleValueHandle(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;
                    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;
    }

    #endregion

    #region 复合handle

    #endregion 

}