module-navigation-tool/Editor/Toolbar/ToolbarDrawer.cs

79 lines
2.3 KiB
C#

using Game.NavigationTool.Editor.Toolbar;
using UnityEditor;
using UnityEngine;
namespace Game.NavigationTool.Editor
{
[InitializeOnLoad]
internal static class ToolbarDrawer
{
private static bool IS_INITIALIZED;
private static IToolbarDrawer[] DRAWERS;
private static Styles styles;
static ToolbarDrawer()
{
EditorApplication.update -= OnEditorUpdate;
EditorApplication.update += OnEditorUpdate;
}
private static void OnEditorUpdate()
{
if (!IS_INITIALIZED)
{
styles = new Styles();
DRAWERS = ToolbarUtility.GetAllDrawers();
ToolbarUtility.AddGuiListener(OnGUI);
IS_INITIALIZED = true;
}
for (var i = 0; i < DRAWERS.Length; i++)
{
DRAWERS[i].Update();
}
}
private static void OnGUI()
{
const float Y = 5.0f;
const float SPACING = 4.0f;
const float HEIGHT = 22.0f;
const float PLAY_BUTTON_EXTENT = 75.0f;
const float PLAY_BUTTON_OFFSET = -23.0f;
if (DRAWERS == null)
return;
styles.Initialize(GUI.skin);
float xLeft = EditorGUIUtility.currentViewWidth * 0.5f + PLAY_BUTTON_OFFSET - PLAY_BUTTON_EXTENT;
float xRight = EditorGUIUtility.currentViewWidth * 0.5f + PLAY_BUTTON_OFFSET + PLAY_BUTTON_EXTENT;
for (var i = 0; i < DRAWERS.Length; i++)
{
IToolbarDrawer drawer = DRAWERS[i];
if (!drawer.Visible)
continue;
GUI.enabled = drawer.Enabled;
float width = drawer.CalculateWidth();
Rect rect;
if (drawer.Placement == EToolbarPlacement.Left)
{
rect = new Rect(xLeft - width, Y, width, HEIGHT);
xLeft -= width + SPACING;
}
else
{
rect = new Rect(xRight, Y, width, HEIGHT);
xRight += width + SPACING;
}
drawer.Draw(rect, styles);
}
GUI.enabled = true;
}
}
}