//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Collections;
using System.Collections.Generic;
using BansheeEngine;
namespace BansheeEditor
{
/** @addtogroup Inspector
* @{
*/
///
/// Displays GUI for a serializable property containing a dictionary. Dictionary contents are displayed as rows of
/// entries that can be shown, hidden or manipulated.
///
public class InspectableDictionary : InspectableField
{
private InspectableDictionaryGUI dictionaryGUIField;
///
/// Creates a new inspectable dictionary GUI for the specified property.
///
/// Parent Inspector this field belongs to.
/// Name of the property, or some other value to set as the title.
/// Full path to this property (includes name of this property and all parent properties).
/// Determines how deep within the inspector nesting hierarchy is this field. Some fields may
/// contain other fields, in which case you should increase this value by one.
/// Parent layout that all the field elements will be added to.
/// Serializable property referencing the array whose contents to display.
public InspectableDictionary(Inspector parent, string title, string path, int depth, InspectableFieldLayout layout,
SerializableProperty property)
: base(parent, title, path, SerializableProperty.FieldType.Dictionary, depth, layout, property)
{
}
///
public override GUILayoutX GetTitleLayout()
{
return dictionaryGUIField.GetTitleLayout();
}
///
public override InspectableState Refresh(int layoutIndex)
{
return dictionaryGUIField.Refresh();
}
///
protected internal override void Initialize(int layoutIndex)
{
GUILayout dictionaryLayout = layout.AddLayoutY(layoutIndex);
dictionaryGUIField = InspectableDictionaryGUI.Create(parent, title, path, property, dictionaryLayout, depth);
dictionaryGUIField.IsExpanded = parent.Persistent.GetBool(path + "_Expanded");
dictionaryGUIField.OnExpand += x => parent.Persistent.SetBool(path + "_Expanded", x);
}
///
/// Creates GUI elements that allow viewing and manipulation of a referenced
/// by a serializable property.
///
public class InspectableDictionaryGUI : GUIDictionaryFieldBase
{
private SerializableProperty property;
private IDictionary dictionary;
private int numElements;
private Inspector parent;
private string path;
private List orderedKeys = new List();
///
/// Returns the parent inspector the array GUI belongs to.
///
public Inspector Inspector
{
get { return parent; }
}
///
/// Returns a property path to the array field (name of the array field and all parent object fields).
///
public string Path
{
get { return path; }
}
///
/// Constructs a new dictionary GUI.
///
/// Parent Inspector this field belongs to.
/// Label to display on the list GUI title.
/// Full path to this property (includes name of this property and all parent properties).
///
/// Serializable property referencing a dictionary
/// Layout to which to append the list GUI elements to.
/// Determines at which depth to render the background. Useful when you have multiple
/// nested containers whose backgrounds are overlaping. Also determines background style,
/// depths divisible by two will use an alternate style.
protected InspectableDictionaryGUI(Inspector parent, LocString title, string path, SerializableProperty property,
GUILayout layout, int depth = 0)
: base(title, layout, depth)
{
this.property = property;
this.parent = parent;
this.path = path;
dictionary = property.GetValue();
if (dictionary != null)
numElements = dictionary.Count;
UpdateKeys();
}
///
/// Builds the inspectable dictionary GUI elements. Must be called at least once in order for the contents to
/// be populated.
///
/// Parent Inspector this field belongs to.
/// Label to display on the list GUI title.
/// Full path to this property (includes name of this property and all parent properties).
///
/// Serializable property referencing a dictionary
/// Layout to which to append the list GUI elements to.
/// Determines at which depth to render the background. Useful when you have multiple
/// nested containers whose backgrounds are overlaping. Also determines background style,
/// depths divisible by two will use an alternate style.
public static InspectableDictionaryGUI Create(Inspector parent, LocString title, string path,
SerializableProperty property, GUILayout layout, int depth = 0)
{
InspectableDictionaryGUI guiDictionary = new InspectableDictionaryGUI(parent, title, path, property,
layout, depth);
guiDictionary.BuildGUI();
return guiDictionary;
}
///
public override InspectableState Refresh()
{
// Check if any modifications to the array were made outside the inspector
IDictionary newDict = property.GetValue();
if (dictionary == null && newDict != null)
{
dictionary = newDict;
numElements = dictionary.Count;
BuildGUI();
}
else if (newDict == null && dictionary != null)
{
dictionary = null;
numElements = 0;
BuildGUI();
}
else
{
if (dictionary != null)
{
if (numElements != dictionary.Count)
{
numElements = dictionary.Count;
BuildGUI();
}
}
}
return base.Refresh();
}
///
/// Updates the ordered set of keys used for mapping sequential indexes to keys. Should be called whenever a
/// dictionary key changes.
///
private void UpdateKeys()
{
orderedKeys.Clear();
if (dictionary != null)
{
SerializableDictionary dict = property.GetDictionary();
foreach (var key in dictionary.Keys)
orderedKeys.Add(dict.GetProperty(key).Key);
}
}
///
protected override GUIDictionaryFieldRow CreateRow()
{
return new InspectableDictionaryGUIRow();
}
///
protected override int GetNumRows()
{
if (dictionary != null)
return dictionary.Count;
return 0;
}
///
protected override bool IsNull()
{
return dictionary == null;
}
///
protected internal override object GetKey(int rowIdx)
{
return orderedKeys[rowIdx];
}
///
protected internal override object GetValue(object key)
{
SerializableProperty keyProperty = (SerializableProperty)key;
SerializableDictionary dictionary = property.GetDictionary();
return dictionary.GetProperty(keyProperty.GetValue