//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2019 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System.Collections.Generic;
namespace bs.Editor
{
///
/// Contains per-scene data relevant to the editor.
///
[SerializeObject]
internal class EditorSceneData
{
///
/// Creates new editor scene data.
///
/// Root object of the scene to create editor scene data for.
/// New editor scene data object, initialized with the hierarchy of the provided scene.
public static EditorSceneData FromScene(SceneObject root)
{
EditorSceneData output = new EditorSceneData();
output.Root = EditorSceneObject.FromSceneObject(root);
return output;
}
///
/// Updates the editor scene object hierarchy from the current scene hierarchy.
///
/// Root of the hierarchy to update from.
public void UpdateFromScene(SceneObject root)
{
Dictionary lookup = GetLookup();
Root = EditorSceneObject.FromSceneObject(root);
void UpdateFromOldData(EditorSceneObject so)
{
if (lookup.TryGetValue(so.UUID, out EditorSceneObject data))
so.CopyData(data);
foreach(var entry in so.Children)
UpdateFromOldData(entry);
}
UpdateFromOldData(Root);
}
///
/// Builds a lookup table of scene object UUID -> editor scene object data, for all scene objects.
///
/// Lookup table.
public Dictionary GetLookup()
{
Dictionary lookup = new Dictionary();
void AddToLookup(EditorSceneObject so)
{
lookup[so.UUID] = so;
foreach(var entry in so.Children)
AddToLookup(entry);
}
if(Root != null)
AddToLookup(Root);
return lookup;
}
///
/// Root object of the scene hierarchy.
///
public EditorSceneObject Root;
}
///
/// Contains editor-relevant data about a scene object.
///
[SerializeObject]
internal class EditorSceneObject
{
///
/// Unique identifier of the scene object.
///
public UUID UUID;
///
/// True if the object is expanded in the hierarchy view.
///
public bool IsExpanded;
///
/// Child scene objects, if any.
///
public EditorSceneObject[] Children;
///
/// Initializes the object from a corresponding scene object.
///
/// Scene object that this object contains additional data for.
/// New editor scene object.
public static EditorSceneObject FromSceneObject(SceneObject so)
{
EditorSceneObject output = new EditorSceneObject();
output.UUID = so.UUID;
int numChildren = so.GetNumChildren();
output.Children = new EditorSceneObject[numChildren];
for (int i = 0; i < numChildren; i++)
output.Children[i] = FromSceneObject(so.GetChild(i));
return output;
}
///
/// Copies the stored data from one object instance to another. Does not copy object UUID or child list.
///
/// Object from which to copy the data.
public void CopyData(EditorSceneObject other)
{
IsExpanded = other.IsExpanded;
}
}
}