miepzerino
2025-03-29 ad79d9ca49274cc660fc2030a071b24314f0f210
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
 
namespace Flexalon.Editor
{
    internal static class FlexalonGUI
    {
        private static Dictionary<string, List<Texture2D>> _bgTextures = new Dictionary<string, List<Texture2D>>();
 
        internal static void Vertical(Action action)
        {
            EditorGUILayout.BeginVertical();
            action();
            EditorGUILayout.EndVertical();
        }
 
        internal static void Vertical(float width, Action action)
        {
            EditorGUILayout.BeginVertical(GUILayout.Width(width));
            action();
            EditorGUILayout.EndVertical();
        }
 
        internal static void Vertical(GUIStyle style, Action action)
        {
            EditorGUILayout.BeginVertical(style, GUILayout.ExpandWidth(false));
            action();
            EditorGUILayout.EndVertical();
        }
 
        internal static void Vertical(GUIStyle style, float width, Action action)
        {
            EditorGUILayout.BeginVertical(style, GUILayout.Width(width));
            action();
            EditorGUILayout.EndVertical();
        }
 
        internal static void VerticalExpanded(GUIStyle style, Action action)
        {
            EditorGUILayout.BeginVertical(style, GUILayout.ExpandHeight(true));
            action();
            EditorGUILayout.EndVertical();
        }
 
        internal static void Horizontal(Action action)
        {
            EditorGUILayout.BeginHorizontal();
            action();
            EditorGUILayout.EndHorizontal();
        }
 
        internal static void Horizontal(GUIStyle style, Action action)
        {
            EditorGUILayout.BeginHorizontal(style);
            action();
            EditorGUILayout.EndHorizontal();
        }
 
        internal static void HorizontalExpanded(Action action)
        {
            EditorGUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
            action();
            EditorGUILayout.EndHorizontal();
        }
 
        internal static void HorizontalCentered(Action action)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            action();
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }
 
        internal static void HorizontalCentered(GUIStyle style, Action action)
        {
            EditorGUILayout.BeginHorizontal(style);
            GUILayout.FlexibleSpace();
            action();
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();
        }
 
        internal static Vector2 Scroll(Vector2 scrollPosition, Action action)
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.ExpandWidth(true));
            action();
            EditorGUILayout.EndScrollView();
            return scrollPosition;
        }
 
        internal static Vector2 Scroll(Vector2 scrollPosition, float height, Action action)
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Height(height), GUILayout.ExpandWidth(true));
            action();
            EditorGUILayout.EndScrollView();
            return scrollPosition;
        }
 
        internal static Vector2 Scroll(Vector2 scrollPosition, GUIStyle style, Action action)
        {
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, style, GUILayout.ExpandWidth(true));
            action();
            EditorGUILayout.EndScrollView();
            return scrollPosition;
        }
 
        internal static void DisableGroup(bool disable, Action action)
        {
            EditorGUI.BeginDisabledGroup(disable);
            action();
            EditorGUI.EndDisabledGroup();
        }
 
        private static Dictionary<string, Texture2D> _textures = new Dictionary<string, Texture2D>();
 
        internal static bool ImageButton(string guid, int width, int height)
        {
            if (!_textures.TryGetValue(guid, out var texture))
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
                _textures[guid] = texture;
            }
 
            return GUILayout.Button(texture, GUILayout.Width(width), GUILayout.Height(height));
        }
 
        public static void Image(string guid, int width, int height)
        {
            if (!_textures.TryGetValue(guid, out var texture))
            {
                var path = AssetDatabase.GUIDToAssetPath(guid);
                texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
                _textures[guid] = texture;
            }
 
            GUILayout.Label(texture, GUILayout.Width(width), GUILayout.Height(height));
        }
 
        public static bool Checkbox(bool value, Action action)
        {
            Horizontal(() =>
            {
                GUILayout.Space(10);
                value = EditorGUILayout.Toggle(value, GUILayout.Width(20));
                action();
            });
 
            return value;
        }
 
        public static bool Checkbox(bool value, string label, GUIStyle labelStyle)
        {
            return Checkbox(value, () => GUILayout.Label(label, labelStyle));
        }
 
        public static bool Button(string label, GUIStyle style, int width, int height)
        {
            var labelContent = new GUIContent(label);
            var position = GUILayoutUtility.GetRect(width, height, style);
            EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
            return GUI.Button(position, labelContent, style);
        }
 
        public static Rect GetLinkRect(GUIContent labelContent, GUIStyle style, Vector2 position)
        {
            var size = style.CalcSize(labelContent);
            return new Rect(position.x - size.x * 0.5f, position.y - size.y * 0.5f, size.x, size.y);
        }
 
        public static bool Link(string label, GUIStyle style, float lineThickness = 0.5f)
        {
            var labelContent = new GUIContent(label);
            var rect = GUILayoutUtility.GetRect(labelContent, style, GUILayout.ExpandWidth(false));
            return Link(labelContent, style, rect, lineThickness);
        }
 
        public static bool Link(GUIContent labelContent, GUIStyle style, Rect rect, float lineThickness = 0.5f)
        {
            // Draw a box for the underline
            var lineRect = new Rect(rect.xMin, rect.yMax - lineThickness / 2, rect.width, lineThickness);
            EditorGUI.DrawRect(lineRect, style.normal.textColor);
            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);
            return GUI.Button(rect, labelContent, style);
        }
 
        public static void HorizontalLine()
        {
            HorizontalLine(Color.white);
        }
 
        public static void HorizontalLine(Color color, float thickness = 0.5f)
        {
            var position = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.ExpandWidth(true));
            HorizontalLine(position.xMin, position.xMax, position.y, color, thickness);
        }
 
        public static void HorizontalLine(float startX, float endX, float y)
        {
            HorizontalLine(startX, endX, y, Color.white);
        }
 
        public static void HorizontalLine(float startX, float endX, float y, Color color, float thickness = 0.5f)
        {
            EditorGUI.DrawRect(new Rect(startX, y - thickness / 2, endX - startX, thickness), color);
        }
 
        public static void VerticalLine()
        {
            VerticalLine(Color.white);
        }
 
        public static void VerticalLine(Color color, float thickness = 0.5f)
        {
            var position = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.ExpandHeight(true));
            VerticalLine(position.x, position.yMin, position.yMax, color, thickness);
        }
 
        public static void VerticalLine(float x, float startY, float endY)
        {
            VerticalLine(x, startY, endY, Color.white);
        }
 
        public static void VerticalLine(float x, float startY, float endY, Color color, float thickness = 0.5f)
        {
            EditorGUI.DrawRect(new Rect(x - thickness / 2, startY, thickness, endY - startY), color);
        }
 
        public static int StyleFontSize;
        public static string StyleTag;
        public static Font StyleFont = null;
 
        public static GUIStyle CreateStyle()
        {
            var style = new GUIStyle();
            style.wordWrap = true;
            style.richText = true;
            style.fontSize = StyleFontSize;
            style.font = StyleFont;
            return style;
        }
 
        public static GUIStyle CreateStyle(string textColor)
        {
            return CreateStyle(HexColor(textColor));
        }
 
        public static GUIStyle CreateStyle(Color textColor)
        {
            var style = CreateStyle();
            SetTextColor(style, textColor);
            return style;
        }
 
        public static GUIStyle CreateStyle(string textColor, string backgroundColor)
        {
            return CreateStyle(HexColor(textColor), HexColor(backgroundColor));
        }
 
        public static GUIStyle CreateStyle(Color textColor, Color backgroundColor)
        {
            var style = CreateStyle(textColor);
            SetBackgroundColor(style, backgroundColor);
            return style;
        }
 
        public static void SetBackgroundColor(GUIStyle style, Color color)
        {
            var bgTex = new Texture2D(1, 1);
            bgTex.SetPixel(0, 0, color);
            bgTex.Apply();
 
            style.normal.background =
                style.active.background =
                style.focused.background =
                style.hover.background = bgTex;
 
            bgTex.hideFlags = HideFlags.DontSave;
 
            if (!_bgTextures.TryGetValue(StyleTag, out var textures))
            {
                textures = new List<Texture2D>();
                _bgTextures[StyleTag] = textures;
            }
 
            textures.Add(bgTex);
        }
 
        public static void SetBackgroundImage(GUIStyle style, Texture2D image)
        {
            style.normal.background =
                style.active.background =
                style.focused.background =
                style.hover.background = image;
        }
 
        public static void CleanupBackgroundTextures(string styleTag)
        {
            if (_bgTextures.TryGetValue(styleTag, out var textures))
            {
                foreach (var texture in textures)
                {
                    UnityEngine.Object.DestroyImmediate(texture);
                }
 
                _bgTextures.Remove(styleTag);
            }
        }
 
        public static void SetTextColor(GUIStyle style, Color color)
        {
            style.normal.textColor =
                style.active.textColor =
                style.focused.textColor =
                style.hover.textColor = color;
        }
 
        public static Color Gray(int gray)
        {
            return new Color(gray / 255f, gray / 255f, gray / 255f);
        }
 
        public static Color HexColor(string hex)
        {
            ColorUtility.TryParseHtmlString(hex, out var color);
            return color;
        }
 
        public static bool HelpBoxLinkButton(string message, MessageType type)
        {
            Texture icon = null;
 
            switch (type)
            {
                case MessageType.Info:
                    icon = EditorGUIUtility.IconContent("console.infoicon").image;
                    break;
                case MessageType.Warning:
                    icon = EditorGUIUtility.IconContent("console.warnicon").image;
                    break;
                case MessageType.Error:
                    icon = EditorGUIUtility.IconContent("console.erroricon").image;
                    break;
            }
 
            var style = new GUIStyle(EditorStyles.helpBox)
            {
                richText = true,
                fontSize = EditorStyles.helpBox.fontSize
            };
            EditorGUILayout.LabelField(GUIContent.none, new GUIContent(message, icon), style);
            var position = GUILayoutUtility.GetLastRect();
            EditorGUIUtility.AddCursorRect(position, MouseCursor.Link);
            return GUI.Button(position, GUIContent.none, EditorStyles.linkLabel);
        }
 
        public static void Box(int width, int height, Color backgroundColor, Color borderColor, int borderThickness)
        {
            var rect = GUILayoutUtility.GetRect(width, height);
            Rect outer = new Rect(rect);
            Rect inner = new Rect(rect.x + borderThickness,
                rect.y + borderThickness,
                rect.width - borderThickness * 2,
                rect.height - borderThickness * 2);
 
            EditorGUI.DrawRect(outer, borderColor);
            EditorGUI.DrawRect(inner, backgroundColor);
        }
 
        public static Rect GetRect(int width, int height)
        {
            return GUILayoutUtility.GetRect(width, height, GUILayout.ExpandWidth(false), GUILayout.ExpandHeight(false));
        }
 
        public static bool BoxButton(GUIContent content, Rect rect, GUIStyle style, Color backgroundColor, Color borderColor, int borderThickness)
        {
            Rect inner = new Rect(
                rect.x + borderThickness,
                rect.y + borderThickness,
                rect.width - borderThickness * 2,
                rect.height - borderThickness * 2);
 
            EditorGUI.DrawRect(rect, borderColor);
            EditorGUI.DrawRect(inner, backgroundColor);
 
            EditorGUIUtility.AddCursorRect(rect, MouseCursor.Link);
            return GUI.Button(rect, content, style);
        }
    }
}