1.1.0: Added README and ToolbarSettings, so visibility of tools can be set in Preferences -> Module -> Toolbar
This commit is contained in:
parent
e1d2deb4c5
commit
9802f8aaa3
23 changed files with 297 additions and 43 deletions
|
|
@ -1,18 +1,22 @@
|
|||
using Module.NavigationTool.Editor.Toolbar;
|
||||
using UnityEditor;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_2021_1_OR_NEWER
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UIElements;
|
||||
#endif
|
||||
|
||||
namespace Module.NavigationTool.Editor
|
||||
namespace Module.NavigationTool.Editor.Toolbar
|
||||
{
|
||||
[InitializeOnLoad]
|
||||
internal static class ToolbarDrawer
|
||||
{
|
||||
private static bool IS_INITIALIZED;
|
||||
private static AbstractToolbarDrawer[] DRAWERS;
|
||||
|
||||
#if UNITY_2021_1_OR_NEWER
|
||||
private static readonly Dictionary<AbstractToolbarDrawer, IMGUIContainer> DICT_MAPPING = new Dictionary<AbstractToolbarDrawer, IMGUIContainer>();
|
||||
#endif
|
||||
|
||||
static ToolbarDrawer()
|
||||
{
|
||||
|
|
@ -26,40 +30,93 @@ namespace Module.NavigationTool.Editor
|
|||
{
|
||||
DRAWERS = ToolbarUtility.GetAllDrawers();
|
||||
|
||||
#if UNITY_2021_1_OR_NEWER
|
||||
ToolbarUtility.OnUpdate(OnCreateElements);
|
||||
#else
|
||||
#if !UNITY_2021_1_OR_NEWER
|
||||
ToolbarUtility.AddGuiListener(OnGUI);
|
||||
#endif
|
||||
|
||||
IS_INITIALIZED = true;
|
||||
}
|
||||
|
||||
#if UNITY_2021_1_OR_NEWER
|
||||
ToolbarUtility.OnUpdate(OnUpdateElements);
|
||||
#endif
|
||||
|
||||
for (var i = 0; i < DRAWERS.Length; i++)
|
||||
{
|
||||
DRAWERS[i].Update();
|
||||
if (DRAWERS[i].Visible)
|
||||
DRAWERS[i].Update();
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_2021_1_OR_NEWER
|
||||
private static void OnCreateElements(VisualElement leftAlign, VisualElement rightAlign)
|
||||
private static void OnUpdateElements(VisualElement leftAlign, VisualElement rightAlign)
|
||||
{
|
||||
const float HEIGHT = 22.0f;
|
||||
var added = false;
|
||||
|
||||
for (int i = DRAWERS.Length - 1; i >= 0; i--)
|
||||
{
|
||||
AbstractToolbarDrawer drawer = DRAWERS[i];
|
||||
var rect = new Rect(0.0f, 0.0f, drawer.CalculateWidth(), HEIGHT);
|
||||
drawer.Setup(rect);
|
||||
|
||||
var container = new IMGUIContainer(drawer.OnGUI);
|
||||
container.style.width = rect.width;
|
||||
bool valid = CheckValidityOfContainer(drawer, leftAlign, rightAlign);
|
||||
|
||||
if (drawer.Placement == EToolbarPlacement.Left)
|
||||
leftAlign.Add(container);
|
||||
else
|
||||
rightAlign.Add(container);
|
||||
if (drawer.Visible && !valid)
|
||||
{
|
||||
var rect = new Rect(0.0f, 0.0f, drawer.CalculateWidth(), HEIGHT);
|
||||
drawer.Setup(rect);
|
||||
|
||||
var container = new ToolbarIMGUIContainer(drawer.OnGUI, drawer.Priority);
|
||||
container.style.width = rect.width;
|
||||
DICT_MAPPING.Add(drawer, container);
|
||||
added = true;
|
||||
|
||||
if (drawer.Placement == EToolbarPlacement.Left)
|
||||
leftAlign.Add(container);
|
||||
else
|
||||
rightAlign.Add(container);
|
||||
}
|
||||
else if (!drawer.Visible && valid)
|
||||
{
|
||||
IMGUIContainer container = DICT_MAPPING[drawer];
|
||||
DICT_MAPPING.Remove(drawer);
|
||||
container.RemoveFromHierarchy();
|
||||
}
|
||||
}
|
||||
|
||||
if (added)
|
||||
{
|
||||
leftAlign.Sort(SortVisualElements);
|
||||
rightAlign.Sort(SortVisualElements);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CheckValidityOfContainer(AbstractToolbarDrawer drawer, VisualElement leftAlign, VisualElement rightAlign)
|
||||
{
|
||||
if (!DICT_MAPPING.TryGetValue(drawer, out IMGUIContainer container))
|
||||
return false;
|
||||
|
||||
bool valid = drawer.Placement == EToolbarPlacement.Left
|
||||
? leftAlign.Contains(container)
|
||||
: rightAlign.Contains(container);
|
||||
|
||||
if (!valid)
|
||||
DICT_MAPPING.Remove(drawer);
|
||||
|
||||
return valid;
|
||||
}
|
||||
|
||||
private static int SortVisualElements(VisualElement ve0, VisualElement ve1)
|
||||
{
|
||||
var c0 = ve0 as ToolbarIMGUIContainer;
|
||||
var c1 = ve1 as ToolbarIMGUIContainer;
|
||||
|
||||
if (c0 == null && c1 == null)
|
||||
return 0;
|
||||
if (c0 == null)
|
||||
return -1;
|
||||
if (c1 == null)
|
||||
return 1;
|
||||
|
||||
return c0.Priority.CompareTo(c1.Priority);
|
||||
}
|
||||
#else
|
||||
private static void OnGUI()
|
||||
|
|
@ -80,6 +137,9 @@ namespace Module.NavigationTool.Editor
|
|||
{
|
||||
AbstractToolbarDrawer drawer = DRAWERS[i];
|
||||
|
||||
if (!drawer.Visible)
|
||||
continue;
|
||||
|
||||
GUI.enabled = drawer.Enabled;
|
||||
float width = drawer.CalculateWidth();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue