// Copyright (c) Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.Content;
using FlaxEditor.Options;
using FlaxEngine;
using FlaxEngine.GUI;
using DockWindow = FlaxEditor.GUI.Docking.DockWindow;
namespace FlaxEditor.Windows
{
///
/// Base class for all windows in Editor.
///
///
public abstract class EditorWindow : DockWindow
{
///
/// Gets the editor object.
///
public readonly Editor Editor;
///
/// Gets a value indicating whether this window can open content finder popup.
///
protected virtual bool CanOpenContentFinder => true;
///
/// Gets a value indicating whether this window can use UI navigation (tab/enter).
///
protected virtual bool CanUseNavigation => true;
///
/// Initializes a new instance of the class.
///
/// The editor.
/// True if hide window on closing, otherwise it will be destroyed.
/// The scroll bars.
protected EditorWindow(Editor editor, bool hideOnClose, ScrollBars scrollBars)
: base(editor.UI.MasterPanel, hideOnClose, scrollBars)
{
AutoFocus = true;
Editor = editor;
InputActions.Add(options => options.ContentFinder, () =>
{
if (CanOpenContentFinder)
{
Editor.ContentFinding.ShowFinder(RootWindow);
}
});
// Set up editor window shortcuts
InputActions.Add(options => options.ContentWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.ContentWin.FocusOrShow();
});
InputActions.Add(options => options.SceneWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.SceneWin.FocusOrShow();
});
InputActions.Add(options => options.ToolboxWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.ToolboxWin.FocusOrShow();
});
InputActions.Add(options => options.PropertiesWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.PropertiesWin.FocusOrShow();
});
InputActions.Add(options => options.GameWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.GameWin.FocusOrShow();
});
InputActions.Add(options => options.EditorWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.EditWin.FocusOrShow();
});
InputActions.Add(options => options.DebugLogWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.DebugLogWin.FocusOrShow();
});
InputActions.Add(options => options.OutputLogWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.OutputLogWin.FocusOrShow();
});
InputActions.Add(options => options.GraphicsQualityWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.GraphicsQualityWin.FocusOrShow();
});
InputActions.Add(options => options.GameCookerWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.GameCookerWin.FocusOrShow();
});
InputActions.Add(options => options.ProfilerWindow, () =>
{
if (InputOptions.ProfilerShortcutAvaliable)
Editor.Windows.ProfilerWin.FocusOrShow();
});
InputActions.Add(options => options.ContentFinder, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.ContentFinding.ShowSearch();
});
InputActions.Add(options => options.VisualScriptDebuggerWindow, () =>
{
if (InputOptions.WindowShortcutsAvaliable)
Editor.Windows.VisualScriptDebuggerWin.FocusOrShow();
});
// Register
Editor.Windows.OnWindowAdd(this);
}
///
/// Determines whether this window is holding reference to the specified item.
///
/// The item.
/// true if window is editing the specified item; otherwise, false.
public virtual bool IsEditingItem(ContentItem item)
{
return false;
}
#region Window Events
///
/// Fired when scene starts saving
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneSaving(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene gets saved
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneSaved(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene gets saving error
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneSaveError(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene starts loading
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneLoading(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene gets loaded
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneLoaded(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene cannot be loaded
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneLoadError(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene gets unloading
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneUnloading(Scene scene, Guid sceneId)
{
}
///
/// Fired when scene gets unloaded
///
/// The scene object. It may be null!
/// The scene ID.
public virtual void OnSceneUnloaded(Scene scene, Guid sceneId)
{
}
///
/// Called before Editor will enter play mode.
///
public virtual void OnPlayBeginning()
{
}
///
/// Called when Editor is entering play mode.
///
public virtual void OnPlayBegin()
{
}
///
/// Called when Editor leaves the play mode.
///
public virtual void OnPlayEnd()
{
}
///
/// Called when window should be initialized.
/// At this point, main window, content database, default editor windows are ready.
///
public virtual void OnInit()
{
}
///
/// Called when every engine update.
/// Note: may be called at the lower frequency than the engine updates.
///
public virtual void OnUpdate()
{
}
///
/// Called when editor is being closed and window should perform release data operations.
///
public virtual void OnExit()
{
}
///
/// Called when Editor state gets changed.
///
public virtual void OnEditorStateChanged()
{
}
#endregion
///
public override bool OnKeyDown(KeyboardKeys key)
{
// Prevent closing the editor window when using RMB + Ctrl + W to slow down the camera flight
if (Editor.Options.Options.Input.CloseTab.Process(this, key))
{
if (Root.GetMouseButton(MouseButton.Right))
return true;
}
if (base.OnKeyDown(key))
return true;
switch (key)
{
case KeyboardKeys.Return:
if (CanUseNavigation && Root?.FocusedControl != null)
{
Root.SubmitFocused();
return true;
}
break;
case KeyboardKeys.Tab:
if (CanUseNavigation && Root != null)
{
bool shiftDown = Root.GetKey(KeyboardKeys.Shift);
Root.Navigate(shiftDown ? NavDirection.Previous : NavDirection.Next);
return true;
}
break;
}
return false;
}
///
public override void OnDestroy()
{
if (IsDisposing)
return;
OnExit();
// Unregister
Editor.Windows.OnWindowRemove(this);
base.OnDestroy();
}
}
}