using System; using System.Collections.Generic; using NitroxClient.Debuggers.Drawer.Nitrox; using NitroxClient.Debuggers.Drawer.Subnautica; using NitroxClient.Debuggers.Drawer.Unity; using NitroxClient.Debuggers.Drawer.UnityUI; using NitroxClient.MonoBehaviours; using NitroxModel.DataStructures; using NitroxModel.DataStructures.Unity; using NitroxModel.Helper; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; namespace NitroxClient.Debuggers.Drawer; /// /// Registers known drawers into lookup dictionaries that are searched when and /// are called. /// public class DrawerManager { private readonly Dictionary> drawers = new(); private readonly Dictionary> editorDrawers = new(); public DrawerManager(SceneDebugger sceneDebugger) { Validate.NotNull(sceneDebugger); ColorDrawer colorDrawer = new(); UnityEventDrawer unityEventDrawer = new(); SelectableDrawer selectableDrawer = new(sceneDebugger, colorDrawer); VectorDrawer vectorDrawer = new(); RectDrawer rectDrawer = new(); LayoutGroupDrawer layoutGroupDrawer = new(rectDrawer); MaterialDrawer materialDrawer = new(); ImageDrawer imageDrawer = new(colorDrawer, materialDrawer, rectDrawer); NitroxEntityDrawer nitroxEntityDrawer = new(); AddDrawer(nitroxEntityDrawer); AddDrawer(nitroxEntityDrawer); AddDrawer(); AddDrawer>(); AddDrawer>(); AddDrawer(); AddDrawer(new(selectableDrawer, unityEventDrawer)); AddDrawer(new(sceneDebugger)); AddDrawer(); AddDrawer(); AddDrawer(new(vectorDrawer)); AddDrawer(); AddDrawer(new(sceneDebugger, selectableDrawer)); AddDrawer(new(sceneDebugger)); AddDrawer(); AddDrawer(new(vectorDrawer, rectDrawer)); AddDrawer(layoutGroupDrawer); AddDrawer(imageDrawer); AddDrawer(imageDrawer); AddDrawer(layoutGroupDrawer); AddDrawer(); AddDrawer(new(vectorDrawer)); AddDrawer(new(sceneDebugger, selectableDrawer)); AddDrawer(new(sceneDebugger)); AddDrawer(selectableDrawer); AddDrawer(new(sceneDebugger, selectableDrawer)); AddDrawer(new(colorDrawer, materialDrawer)); AddDrawer(new(sceneDebugger, selectableDrawer, unityEventDrawer)); AddDrawer(); AddDrawer(new(vectorDrawer)); AddDrawer(new(sceneDebugger, vectorDrawer)); AddDrawer(unityEventDrawer); AddDrawer>(unityEventDrawer); AddDrawer(); AddDrawer(new(vectorDrawer)); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(vectorDrawer); AddEditor(colorDrawer); AddEditor(colorDrawer); AddEditor(materialDrawer); AddEditor(rectDrawer); AddEditor(rectDrawer); } /// /// Tries to draw the item given its type. If item is null, returns false and does nothing. /// /// True if a drawer is known for the given item type. public bool TryDraw(T item) { if (item == null) { return false; } if (!drawers.TryGetValue(item.GetType(), out IDrawer drawer)) { return false; } drawer.Draw(item); return true; } /// /// Tries to draw the editor given the type of item. If item is null, returns false and does nothing. /// /// Item to draw the editor for. /// Changed result from the editor. /// True if an editor is known for the given item type. public bool TryDrawEditor(T item, out T result) { if (item == null) { result = default; return false; } if (!editorDrawers.TryGetValue(item.GetType(), out IEditorDrawer drawer)) { result = default; return false; } result = (T)drawer.Draw(item); return true; } private void AddDrawer(TDrawer drawer) where TDrawer : IDrawer { drawers.Add(typeof(TDrawable), new DrawerWrapper(drawer)); } private void AddDrawer() where TDrawer : IDrawer, new() { drawers.Add(typeof(TDrawable), new DrawerWrapper(new TDrawer())); } private void AddEditor(TDrawer drawer) where TDrawer : IEditorDrawer { editorDrawers.Add(typeof(TDrawable), new EditorDrawerWrapper(drawer)); } private class DrawerWrapper(IDrawer inner) : IDrawer { public void Draw(object target) => inner.Draw((T)target); } private class EditorDrawerWrapper(IEditorDrawer inner) : IEditorDrawer { public object Draw(object target) => inner.Draw((T)target); } }