// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEditor.CustomEditors;
using FlaxEditor.Windows;
using FlaxEngine.GUI;
using DockState = FlaxEditor.GUI.Docking.DockState;
namespace FlaxEditor
{
///
/// Base class for custom editor window that can create custom GUI layout and expose various functionalities to the user.
///
///
public abstract class CustomEditorWindow : CustomEditor
{
private class Win : EditorWindow
{
private readonly CustomEditorPresenter _presenter;
private CustomEditorWindow _customEditor;
public Win(CustomEditorWindow customEditor)
: base(Editor.Instance, false, ScrollBars.Vertical)
{
Title = customEditor.GetType().Name;
_customEditor = customEditor;
_presenter = new CustomEditorPresenter(null);
_presenter.Panel.Parent = this;
Set(customEditor);
}
private void Set(CustomEditorWindow value)
{
_customEditor = value;
_presenter.Select(value);
_presenter.OverrideEditor = value;
}
///
protected override void OnShow()
{
base.OnShow();
_presenter.BuildLayout();
}
///
protected override void OnClose()
{
Set(null);
base.OnClose();
}
}
private readonly Win _win;
///
/// Gets the editor window.
///
public EditorWindow Window => _win;
///
/// Initializes a new instance of the class.
///
protected CustomEditorWindow()
{
_win = new Win(this);
ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
}
///
/// Finalizes an instance of the class.
///
~CustomEditorWindow()
{
ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;
}
private void OnScriptsReloadBegin()
{
// Skip if window type is not from game script assembly (eg. plugin code)
var type = GetType();
if (!FlaxEngine.Scripting.IsTypeFromGameScripts(type))
return;
if (!Window.IsHidden)
{
Editor.Instance.Windows.AddToRestore(this);
}
Window.Close();
Window.Dispose();
}
///
/// Shows the window.
///
/// Initial window state.
public void Show(DockState state = DockState.Float)
{
_win.Show(state);
}
}
}