summaryrefslogtreecommitdiff
path: root/Assets/Plugins/Editor/AdvancedInspector/ExtraGUI/ExtraGUI.cs
blob: 4219fa75ae0bebac47822bf3f19d7ff2fd6b9bcc (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using UnityEngine;
using UnityEditor;

using UniToolsEditor;

namespace AdvancedInspector
{
    public static class ExtraGUI
    {
        private static Texture knob;

        private static Texture Knob
        {
            get
            {
                if (knob == null)
                {
                    knob = Helper.Load(EditorResources.Knob);
                    knob.filterMode = FilterMode.Trilinear;
                }

                return knob;
            }
        }

        private static Texture knobBack;

        private static Texture KnobBack
        {
            get
            {
                if (knobBack == null)
                    knobBack = Helper.Load(EditorResources.KnobBack);

                return knobBack;
            }
        }

        private static Vector2 mousePosition;

        public static float FloatAngle(Rect rect, float value)
        {
            return FloatAngle(rect, value, -1, -1, -1);
        }

        public static float FloatAngle(Rect rect, float value, float snap)
        {
            return FloatAngle(rect, value, snap, -1, -1);
        }

        public static float FloatAngle(Rect rect, float value, float snap, float min, float max)
        {
            int id = GUIUtility.GetControlID(FocusType.Passive, rect);

            Rect knobRect = new Rect(rect.x, rect.y, rect.height, rect.height);

            float delta;
            if (min != max)
                delta = ((max - min) / 360);
            else
                delta = 1;

            if (Event.current != null)
            {
                if (Event.current.type == EventType.MouseDown && knobRect.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = id;
                    mousePosition = Event.current.mousePosition;
                }
                else if (Event.current.type == EventType.MouseUp && GUIUtility.hotControl == id)
                    GUIUtility.hotControl = 0;
                else if (Event.current.type == EventType.MouseDrag && GUIUtility.hotControl == id)
                { 
                    Vector2 move = mousePosition - Event.current.mousePosition;
                    value += delta * (-move.x - move.y);

                    if (snap > 0)
                    {
                        float mod = value % snap;

                        if (mod < (delta * 3) || Mathf.Abs(mod - snap) < (delta * 3))
                            value = Mathf.Round(value / snap) * snap;
                    }

                    mousePosition = Event.current.mousePosition;
                    GUI.changed = true;
                }
            }

            GUI.DrawTexture(knobRect, KnobBack);
            Matrix4x4 matrix = GUI.matrix;

            if (min != max)
                GUIUtility.RotateAroundPivot(value * (360 / (max - min)), knobRect.center);
            else
                GUIUtility.RotateAroundPivot(value, knobRect.center);

            GUI.DrawTexture(knobRect, Knob);
            GUI.matrix = matrix;

            Rect label = new Rect(rect.x + rect.height + 9, rect.y + (rect.height / 2) - 9, rect.height, 18);
            value = EditorGUI.FloatField(label, value);

            if (min != max)
                value = Mathf.Clamp(value, min, max);

            return value;
        }

        public static int IntAngle(Rect rect, int value)
        {
            return IntAngle(rect, value, -1, -1, -1);
        }

        public static int IntAngle(Rect rect, int value, int snap)
        {
            return IntAngle(rect, value, snap, -1, -1);
        }

        public static int IntAngle(Rect rect, int value, int snap, int min, int max)
        {
            int id = GUIUtility.GetControlID(FocusType.Passive, rect);

            Rect knobRect = new Rect(rect.x, rect.y, rect.height, rect.height);

            int delta;
            if (min != max)
                delta = ((max - min) / 360);
            else
                delta = 1;

            if (Event.current != null)
            {
                if (Event.current.type == EventType.MouseDown && knobRect.Contains(Event.current.mousePosition))
                {
                    GUIUtility.hotControl = id;
                    mousePosition = Event.current.mousePosition;
                }
                else if (Event.current.type == EventType.MouseUp && GUIUtility.hotControl == id)
                    GUIUtility.hotControl = 0;
                else if (Event.current.type == EventType.MouseDrag && GUIUtility.hotControl == id)
                {
                    Vector2 move = mousePosition - Event.current.mousePosition;
                    value += delta * (-(int)move.x - (int)move.y);

                    if (snap > 0)
                    {
                        float mod = value % snap;

                        if (mod < (delta * 3) || Mathf.Abs(mod - snap) < (delta * 3))
                            value = (int)Mathf.Round(value / snap) * snap;
                    }

                    mousePosition = Event.current.mousePosition;
                    GUI.changed = true;
                }
            }

            GUI.DrawTexture(knobRect, KnobBack);
            Matrix4x4 matrix = GUI.matrix;

            if (min != max)
                GUIUtility.RotateAroundPivot(value * (360 / (max - min)), knobRect.center);
            else
                GUIUtility.RotateAroundPivot(value, knobRect.center);

            GUI.DrawTexture(knobRect, Knob);
            GUI.matrix = matrix;

            Rect label = new Rect(rect.x + rect.height + 9, rect.y + (rect.height / 2) - 9, rect.height, 18);
            value = EditorGUI.IntField(label, value);

            if (min != max)
                value = Mathf.Clamp(value, min, max);

            return value;
        }

        public static int CycleButton(Rect rect, int selected, GUIContent[] contents, GUIStyle style)
        {
            if (GUI.Button(rect, contents[selected], style))
            {
                selected++;
                if (selected >= contents.Length)
                    selected = 0;
            }

            return selected;
        }
    }
}