-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathGUIHelper.cs
226 lines (198 loc) · 10.6 KB
/
GUIHelper.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace ModKit.Utility {
public static class GUIHelper {
public const string onMark = $"<color=green><b>{Glyphs.CheckOn()}!</b></color>";
public const string offMark = $"<color=#A0A0A0E0>{Glyphs.CheckOff()}!</color>";
public static string FormatOn = Glyphs.DisclosureOn().color(RGBA.white).Bold() + " {0}";
public static string FormatOff = Glyphs.DisclosureOff().color(RGBA.lime).Bold() + " {0}";
public static string FormatNone = $" {Glyphs.DisclosureEmpty()}!".color(RGBA.white) + " {0}";
public static string GetToggleText(ToggleState toggleState, string text) {
return toggleState switch {
ToggleState.Off => string.Format(FormatOff, text),
ToggleState.On => string.Format(FormatOn, text),
ToggleState.None => string.Format(FormatNone, text),
_ => string.Format(FormatNone),
};
}
public static int AdjusterButton(int value, string text, int min = int.MinValue, int max = int.MaxValue) {
AdjusterButton(ref value, text, min, max);
return value;
}
public static bool AdjusterButton(ref int value, string text, int min = int.MinValue, int max = int.MaxValue) {
var oldValue = value;
GUILayout.Label(text, GUILayout.ExpandWidth(false));
if (GUILayout.Button("-", GUILayout.ExpandWidth(false)) && value > min)
value--;
GUILayout.Label(value.ToString(), GUILayout.ExpandWidth(false));
if (GUILayout.Button("+", GUILayout.ExpandWidth(false)) && value < max)
value++;
return value != oldValue;
}
public static void Hyperlink(string url, Color normalColor, Color hoverColor, GUIStyle style) => Hyperlink(url, url, normalColor, hoverColor, style);
public static void Hyperlink(string text, string url, Color normalColor, Color hoverColor, GUIStyle style) {
var color = GUI.color;
GUI.color = Color.clear;
GUILayout.Label(text, style, GUILayout.ExpandWidth(false));
var lastRect = GUILayoutUtility.GetLastRect();
GUI.color = lastRect.Contains(Event.current.mousePosition) ? hoverColor : normalColor;
if (GUI.Button(lastRect, text, style))
Application.OpenURL(url);
lastRect.y += lastRect.height - 2;
lastRect.height = 1;
GUI.DrawTexture(lastRect, Texture2D.whiteTexture, ScaleMode.StretchToFill);
GUI.color = color;
}
public static void TextField(ref string value, GUIStyle style = null, params GUILayoutOption[] options) => value = GUILayout.TextField(value, style ?? GUI.skin.textField, options);
public static void TextField(ref string value, Action onChanged, GUIStyle style = null, params GUILayoutOption[] options) => TextField(ref value, null, onChanged, style, options);
public static void TextField(ref string value, Action onClear, Action onChanged, GUIStyle style = null, params GUILayoutOption[] options) {
var old = value;
TextField(ref value, style, options);
if (value != old) {
if (onClear != null && string.IsNullOrEmpty(value))
onClear();
else
onChanged();
}
}
#if false
static bool CheckboxPrivate(
ref bool value,
string title,
GUIStyle style = null,
params GUILayoutOption[] options
) {
bool changed = false;
title = value ? title.Bold() : title.color(RGBA.lightgrey);
if (UI.Toggle(title, ref value, 0, options)) changed = true;
//if (GUILayout.Button("" + (value ? onMark : offMark) + " " + title, style, options)) { value = !value; }
return changed;
}
public static bool Checkbox(
ref bool value,
String title,
GUIStyle style = null,
params GUILayoutOption[] options) {
return CheckboxPrivate(ref value, title, style, options);
}
#endif
public static ToggleState ToggleButton(ToggleState toggle, string text, GUIStyle style = null, params GUILayoutOption[] options) {
UI.ToggleButton(ref toggle, text, style, options);
return toggle;
}
public static ToggleState ToggleButton(ToggleState toggle, string text, Action on, Action off, GUIStyle style = null, params GUILayoutOption[] options) {
ToggleButton(ref toggle, text, on, off, style, options);
return toggle;
}
public static void ToggleButton(ref ToggleState toggle, string text, Action on, Action off, GUIStyle style = null, params GUILayoutOption[] options) {
var old = toggle;
UI.ToggleButton(ref toggle, text, style, options);
if (toggle != old) {
if (toggle.IsOn())
on?.Invoke();
else
off?.Invoke();
}
}
public static void ToggleButton(ref ToggleState toggle, string text, ref float minWidth, GUIStyle style = null, params GUILayoutOption[] options) {
GUIContent content = new(GetToggleText(toggle, text));
style ??= GUI.skin.button;
minWidth = Math.Max(minWidth, style.CalcSize(content).x);
if (GUILayout.Button(content, style, options?.Concat(new[] { GUILayout.Width(minWidth) }).ToArray() ?? new[] { GUILayout.Width(minWidth) }))
toggle = toggle.Flip();
}
public static void ToggleButton(ref ToggleState toggle, string text, ref float minWidth, Action on, Action off, GUIStyle style = null, params GUILayoutOption[] options) {
var old = toggle;
ToggleButton(ref toggle, text, ref minWidth, style, options);
if (toggle != old) {
if (toggle.IsOn())
on?.Invoke();
else
off?.Invoke();
}
}
public static ToggleState ToggleTypeList(ToggleState toggle, string text, HashSet<string> selectedTypes, HashSet<Type> allTypes, GUIStyle style = null, params GUILayoutOption[] options) {
GUILayout.BeginHorizontal();
UI.ToggleButton(ref toggle, text, style, options);
if (toggle.IsOn()) {
using (new GUILayout.VerticalScope()) {
using (new GUILayout.HorizontalScope()) {
if (GUILayout.Button("Select All")) {
foreach (var type in allTypes) {
selectedTypes.Add(type.FullName);
}
}
if (GUILayout.Button("Deselect All")) {
selectedTypes.Clear();
}
}
foreach (var type in allTypes) {
ToggleButton(selectedTypes.Contains(type.FullName) ? ToggleState.On : ToggleState.Off, type.Name.ToSentence(),
() => selectedTypes.Add(type.FullName),
() => selectedTypes.Remove(type.FullName),
style, options);
}
}
}
GUILayout.EndHorizontal();
return toggle;
}
public static void Toolbar(ref int selected, string[] texts, GUIStyle style = null, params GUILayoutOption[] options) => selected = GUILayout.Toolbar(selected, texts, style ?? GUI.skin.button, options);
public static void SelectionGrid(ref int selected, string[] texts, int xCount, GUIStyle style = null, params GUILayoutOption[] options) => selected = GUILayout.SelectionGrid(selected, texts, xCount, style ?? GUI.skin.button, options);
public static void SelectionGrid(ref int selected, string[] texts, int xCount, Action onChanged, GUIStyle style = null, params GUILayoutOption[] options) {
var old = selected;
SelectionGrid(ref selected, texts, xCount, style, options);
if (selected != old) {
onChanged?.Invoke();
}
}
public static float RoundedHorizontalSlider(float value, int digits, float leftValue, float rightValue, params GUILayoutOption[] options) {
if (digits < 0) {
var num = (float)Math.Pow(10d, -digits);
return (float)Math.Round(GUILayout.HorizontalSlider(value, leftValue, rightValue, options) / num, 0) * num;
}
else {
return (float)Math.Round(GUILayout.HorizontalSlider(value, leftValue, rightValue, options), digits);
}
}
private static Texture2D fillTexture = null;
private static GUIStyle fillStyle = null;
private static Color fillColor = new(1f, 1f, 1f, 0.65f);
private static readonly Color color = new(1f, 1f, 1f, 0.35f);
private static Color fillColor2 = color;
public static Color FillColor2 { get => fillColor2; set => fillColor2 = value; }
public static GUIStyle FillStyle(Color color) {
if (fillTexture == null)
fillTexture = new Texture2D(1, 1);
if (fillStyle == null)
fillStyle = new GUIStyle();
fillTexture.SetPixel(0, 0, color);
fillTexture.Apply();
fillStyle.normal.background = fillTexture;
return fillStyle;
}
public static void GUIDrawRect(Rect position, Color color) => GUI.Box(position, GUIContent.none, FillStyle(color));
//private static GUIStyle divStyle;
public static void Div(Color color, float indent = 0, float height = 0, float width = 0) {
if (fillTexture == null)
fillTexture = new Texture2D(1, 1);
var divStyle = new GUIStyle {
fixedHeight = 1
};
fillTexture.SetPixel(0, 0, color);
fillTexture.Apply();
divStyle.normal.background = fillTexture;
divStyle.margin = new RectOffset((int)indent, 0, 4, 4);
if (width > 0)
divStyle.fixedWidth = width;
else
divStyle.fixedWidth = 0;
GUILayout.Space((1f * height) / 2f);
GUILayout.Box(GUIContent.none, divStyle);
GUILayout.Space(height / 2f);
}
public static void Div(float indent = 0, float height = 25, float width = 0) => Div(fillColor, indent, height, width);
}
}