using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityObject = UnityEngine.Object;
using UnityEngine.UIElements;
namespace Unity.Tutorials.Core.Editor
{
///
/// Proxy class for accessing UnityEditor.GUIView.
///
///
/// Not a pure proxy class, contains some custom functionality.
///
internal class GUIViewProxy
{
public static List FindAllGuiViewVisualElements()
{
return Resources.FindObjectsOfTypeAll().Select(UIElementsHelper.GetVisualTree).ToList();
}
///
/// Raised then the position of the underlying GUIView changes.
///
public static event Action PositionChanged;
///
/// Type of internal class GUIView.
///
public static Type GuiViewType => typeof(GUIView);
///
/// Type of internal class TooltipView.
///
public static Type TooltipViewType => typeof(TooltipView);
///
/// Type of internal class Toolbar.
///
public static Type ToolbarType => typeof(Toolbar);
///
/// Type of internal class GameView.
///
public static Type GameViewType => typeof(GameView);
///
/// Type of internal class InspectorWindow.
///
public static Type InspectorWindowType => typeof(InspectorWindow);
static GUIViewProxy()
{
GUIView.positionChanged += (guiView) => PositionChanged?.Invoke(guiView);
}
///
/// Returns if typeof(GUIView).IsAssignableFrom(type).
///
///
///
public static bool IsAssignableFrom(Type type) => GuiViewType.IsAssignableFrom(type);
internal GUIView GuiView { get; }
///
/// Does this GUIViewProxy have a GUIView.
///
public bool IsValid => GuiView != null;
///
/// Are the underlying GUIView's window and the window's root view valid.
///
public bool IsWindowAndRootViewValid => GuiView.window != null && GuiView.window.rootView != null;
///
/// Position of the GUIView.
///
public Rect Position => GuiView.position;
internal GUIViewProxy(GUIView guiView)
{
GuiView = guiView;
}
///
/// Calls GUIView.Repaint().
///
public void Repaint() => GuiView.Repaint();
///
/// Calls GUIView.RepaintImmediately().
///
public void RepaintImmediately() => GuiView.RepaintImmediately();
///
/// Is the type instance of underlying HostView's actual view.
///
///
///
public bool IsActualViewAssignableTo(Type editorWindowType)
{
var hostView = GuiView as HostView;
return hostView != null && hostView.actualView != null && editorWindowType.IsInstanceOfType(hostView.actualView);
}
///
/// Is the GUIView docked to the Editor.
///
///
public bool IsDockedToEditor()
{
var hostView = GuiView as HostView;
return hostView == null || hostView.window == null || hostView.window.showMode == ShowMode.MainWindow;
}
///
/// Is the type instance of the underlying GUIView.
///
///
///
public bool IsGUIViewAssignableTo(Type targetViewType) => targetViewType.IsInstanceOfType(GuiView);
}
internal class GUIViewProxyComparer : IEqualityComparer
{
public bool Equals(GUIViewProxy x, GUIViewProxy y) => x.GuiView == y.GuiView;
public int GetHashCode(GUIViewProxy obj) => obj.GuiView.GetHashCode();
}
internal static class EditorWindowExtension
{
public static GUIViewProxy GetParent(this EditorWindow window) => new GUIViewProxy(window.m_Parent);
// EditorWindow.docked not public until 2020.1.
public static bool IsDocked(this EditorWindow window) => window.docked;
}
}