//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System.Collections.Generic;
using bs;
namespace bs.Editor
{
/** @addtogroup Inspector
* @{
*/
///
/// Displays GUI elements for all the inspectable fields in an object.
///
public abstract class Inspector
{
public const short START_BACKGROUND_DEPTH = 50;
///
/// Returns the main GUI layout for the inspector.
///
protected GUILayoutY Layout
{
get { return layout; }
}
///
/// Returns the main GUI panel for the inspector. is a child of this panel.
///
protected GUIPanel GUI
{
get { return mainPanel; }
}
///
/// Returns the secondary GUI panel. Located at the bottom of the inspector window and unlike has
/// no padding or styling applied. Only available when inspecting resources.
///
protected GUIPanel PreviewGUI
{
get { return previewPanel; }
}
///
/// Returns the object the inspector is currently displaying. If the current object is a resource use
/// instead;
///
protected object InspectedObject
{
get { return inspectedObject; }
}
///
/// Returns the path to the resource the inspector is currently displaying.
///
protected string InspectedResourcePath
{
get { return inspectedResourcePath; }
}
///
/// A set of properties that the inspector can read/write. They will be persisted even after the inspector is closed
/// and restored when it is re-opened.
///
protected internal SerializableProperties Persistent
{
get { return persistent; }
}
private GUIPanel rootGUI;
private GUIPanel mainPanel;
private GUIPanel previewPanel;
private GUILayoutY layout;
private object inspectedObject;
private string inspectedResourcePath;
private SerializableProperties persistent;
///
/// Common code called by both Initialize() overloads.
///
/// Primary GUI panel to add the GUI elements to.
/// Secondary GUI panel located at the bottom of the inspector window, aimed primarily for
/// resource previews, but can be used for any purpose.
/// A set of properties that the inspector can read/write. They will be persisted even
/// after the inspector is closed and restored when it is re-opened.
private void InitializeBase(GUIPanel mainGui, GUIPanel previewGui, SerializableProperties persistent)
{
rootGUI = mainGui;
this.persistent = persistent;
GUILayout contentLayoutX = mainGui.AddLayoutX();
contentLayoutX.AddSpace(5);
GUILayout contentLayoutY = contentLayoutX.AddLayoutY();
contentLayoutY.AddSpace(5);
GUIPanel contentPanel = contentLayoutY.AddPanel();
contentLayoutY.AddSpace(5);
contentLayoutX.AddSpace(5);
GUIPanel backgroundPanel = mainGui.AddPanel(START_BACKGROUND_DEPTH);
GUITexture inspectorContentBg = new GUITexture(null, EditorStylesInternal.InspectorContentBg);
backgroundPanel.AddElement(inspectorContentBg);
mainPanel = contentPanel;
previewPanel = previewGui;
layout = GUI.AddLayoutY();
}
///
/// Initializes the inspector using an object instance. Must be called after construction.
///
/// GUI panel to add the GUI elements to.
/// Instance of the object whose fields to display GUI for.
/// A set of properties that the inspector can read/write. They will be persisted even
/// after the inspector is closed and restored when it is re-opened.
internal virtual void Initialize(GUIPanel gui, object instance, SerializableProperties persistent)
{
InitializeBase(gui, null, persistent);
inspectedObject = instance;
Initialize();
Refresh();
}
///
/// Initializes the inspector using a resource path. Must be called after construction.
///
/// Primary GUI panel to add the GUI elements to.
/// Secondary GUI panel located at the bottom of the inspector window, aimed primarily for
/// resource previews, but can be used for any purpose.
/// Path to the resource for which to display GUI for.
/// A set of properties that the inspector can read/write. They will be persisted even
/// after the inspector is closed and restored when it is re-opened.
internal virtual void Initialize(GUIPanel mainGui, GUIPanel previewGui, string path,
SerializableProperties persistent)
{
InitializeBase(mainGui, previewGui, persistent);
inspectedResourcePath = path;
Initialize();
Refresh();
}
///
/// Changes keyboard focus to the provided field.
///
/// Path to the field on the object being inspected.
internal virtual void FocusOnField(string path) { }
///
/// Loads the currently inspected resource into the field. By default resources
/// are not loaded and you can only retrieve their path through .
///
protected void LoadResource()
{
if(!string.IsNullOrEmpty(inspectedResourcePath))
inspectedObject = ProjectLibrary.Load(inspectedResourcePath);
}
///
/// Hides or shows the inspector GUI elements.
///
/// True to make the GUI elements visible.
internal virtual void SetVisible(bool visible)
{
rootGUI.Active = visible;
}
///
/// Destroys all inspector GUI elements.
///
internal void Destroy()
{
Layout.Destroy();
GUI.Destroy();
}
///
/// Called when the inspector is first created.
///
protected internal abstract void Initialize();
///
/// Checks if contents of the inspector have been modified, and updates them if needed.
///
/// State representing was anything modified between two last calls to .
protected internal abstract InspectableState Refresh();
}
/** @} */
}