DragonECS-Unity/src/Debug/Editor/EcsEditor.cs

141 lines
4.5 KiB
C#
Raw Normal View History

2023-03-27 20:31:58 +08:00
#if UNITY_EDITOR
2023-05-07 00:50:44 +08:00
using System;
using System.Runtime.InteropServices;
2023-05-27 09:14:36 +08:00
using UnityEditor;
2023-03-27 20:31:58 +08:00
using UnityEngine;
2023-03-29 19:58:58 +08:00
namespace DCFApixels.DragonECS.Editors
2023-03-27 20:31:58 +08:00
{
2023-06-29 14:26:01 +08:00
public static class EcsGUI
{
private static GUIStyle _greenStyle;
private static GUIStyle _redStyle;
private static bool _isInit = false;
private static void Init()
{
if (_isInit)
return;
_greenStyle = EcsEditor.GetStyle(new Color32(75, 255, 0, 100));
_redStyle = EcsEditor.GetStyle(new Color32(255, 0, 75, 100));
_isInit = true;
}
public static void DrawConnectStatus(Rect position, bool status)
{
Init();
if (status)
GUI.Box(position, "Connected", _greenStyle);
else
GUI.Box(position, "Not connected", _redStyle);
}
public static class Layout
{
public static void DrawConnectStatus(bool status, params GUILayoutOption[] options)
{
Init();
if (status)
GUILayout.Box("Connected", _greenStyle, GUILayout.ExpandWidth(true));
else
GUILayout.Box("Not connected", _redStyle, GUILayout.ExpandWidth(true));
}
}
}
2023-05-27 09:14:36 +08:00
[InitializeOnLoad]
2023-03-27 20:31:58 +08:00
public static class EcsEditor
{
2023-06-22 14:40:26 +08:00
static EcsEditor()
2023-05-27 09:14:36 +08:00
{
colorBoxeStyles = new SparseArray<GUIStyle>();
}
2023-05-07 00:50:44 +08:00
private static SparseArray<GUIStyle> colorBoxeStyles = new SparseArray<GUIStyle>();
public static GUIStyle GetStyle(Color color, float alphaMultiplier)
2023-03-27 20:31:58 +08:00
{
2023-05-07 00:50:44 +08:00
color.a *= alphaMultiplier;
return GetStyle(color);
}
public static GUIStyle GetStyle(Color32 color32)
{
int colorCode = new Color32Union(color32).colorCode;
if (colorBoxeStyles.TryGetValue(colorCode, out GUIStyle style))
{
2023-05-27 09:14:36 +08:00
if (style == null || style.normal.background == null)
{
2023-05-07 00:50:44 +08:00
style = CreateStyle(color32, colorCode);
2023-05-27 09:14:36 +08:00
colorBoxeStyles[colorCode] = style;
}
2023-05-07 00:50:44 +08:00
return style;
}
2023-03-27 20:31:58 +08:00
2023-05-07 00:50:44 +08:00
style = CreateStyle(color32, colorCode);
colorBoxeStyles.Add(colorCode, style);
2023-03-27 20:31:58 +08:00
return style;
}
2023-05-07 00:50:44 +08:00
private static GUIStyle CreateStyle(Color32 color32, int colorCode)
{
GUIStyle result = new GUIStyle(GUI.skin.box);
Color componentColor = color32;
2023-06-29 14:26:01 +08:00
Texture2D texture2D = CreateTexture(2, 2, componentColor);
result.hover.background = texture2D;
result.focused.background = texture2D;
result.active.background = texture2D;
result.normal.background = texture2D;
2023-05-07 00:50:44 +08:00
return result;
}
2023-03-27 20:31:58 +08:00
private static Texture2D CreateTexture(int width, int height, Color color)
{
var pixels = new Color[width * height];
for (var i = 0; i < pixels.Length; ++i)
pixels[i] = color;
var result = new Texture2D(width, height);
result.SetPixels(pixels);
result.Apply();
return result;
}
2023-05-07 00:50:44 +08:00
2023-05-23 01:48:54 +08:00
public static string GetGenericName(Type type) => EcsDebugUtility.GetGenericTypeName(type);
2023-05-07 00:50:44 +08:00
public static string GetName<T>() => GetName(typeof(T));
2023-05-23 01:48:54 +08:00
public static string GetName(Type type) => EcsDebugUtility.GetName(type);
2023-05-07 00:50:44 +08:00
public static string GetDescription<T>() => GetDescription(typeof(T));
2023-05-23 01:48:54 +08:00
public static string GetDescription(Type type) => EcsDebugUtility.GetDescription(type);
2023-05-07 00:50:44 +08:00
#region Utils
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 4)]
private readonly ref struct Color32Union
{
[FieldOffset(0)]
public readonly int colorCode;
[FieldOffset(0)]
public readonly byte r;
[FieldOffset(1)]
public readonly byte g;
[FieldOffset(2)]
public readonly byte b;
[FieldOffset(3)]
public readonly byte a;
public Color32Union(byte r, byte g, byte b, byte a) : this()
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public Color32Union(Color32 color) : this()
{
r = color.r;
g = color.g;
b = color.b;
a = color.a;
}
}
#endregion
2023-03-27 20:31:58 +08:00
}
}
#endif