//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using bs;
using System.Collections.Generic;
namespace bs.Editor
{
/** @addtogroup Inspector
* @{
*/
///
/// Contains Inspector specific data that should persist assembly refresh.
///
internal class InspectorPersistentData : ManagedComponent
{
[SerializeField]
private Dictionary componentProperties =
new Dictionary();
[SerializeField]
private Dictionary resourceProperties =
new Dictionary();
///
/// Returns existing, or creates new properties for a component with the specified id.
///
/// Internal ID of the component to retrieve properties for.
/// A set of key value pairs representing persistent properties of an inspectable component.
public SerializableProperties GetProperties(ulong componentId)
{
SerializableProperties output;
if (!componentProperties.TryGetValue(componentId, out output))
{
output = new SerializableProperties();
componentProperties[componentId] = output;
}
return output;
}
///
/// Returns existing, or creates new properties for a resource with the specified UUID.
///
/// Unique identifier of the resource to retrieve properties for.
/// A set of key value pairs representing persistent properties of an inspectable resource.
public SerializableProperties GetProperties(string uuid)
{
SerializableProperties output;
if (!resourceProperties.TryGetValue(uuid, out output))
{
output = new SerializableProperties();
resourceProperties[uuid] = output;
}
return output;
}
}
///
/// Stores a serializable set of key-value pairs of various types.
///
[SerializeObject]
public class SerializableProperties
{
[SerializeField]
private Dictionary booleans = new Dictionary();
[SerializeField]
private Dictionary floats = new Dictionary();
[SerializeField]
private Dictionary ints = new Dictionary();
[SerializeField]
private Dictionary strings = new Dictionary();
///
/// Sets a floating point value to a property with the specified name.
///
/// Name to record the property under.
/// Value of the property.
protected internal void SetFloat(string name, float value)
{
floats[name] = value;
}
///
/// Sets a integer value to a property with the specified name.
///
/// Name to record the property under.
/// Value of the property.
protected internal void SetInt(string name, int value)
{
ints[name] = value;
}
///
/// Sets a boolean value to a property with the specified name.
///
/// Name to record the property under.
/// Value of the property.
protected internal void SetBool(string name, bool value)
{
booleans[name] = value;
}
///
/// Sets a string value to a property with the specified name.
///
/// Name to record the property under.
/// Value of the property.
protected internal void SetString(string name, string value)
{
strings[name] = value;
}
///
/// Retrieves a value of a floating point property.
///
/// Name of the property to retrieve.
/// Default value to return if property cannot be found.
/// Value of the property if it exists, otherwise the default value.
protected internal float GetFloat(string name, float defaultValue = 0.0f)
{
float value;
if (floats.TryGetValue(name, out value))
return value;
return defaultValue;
}
///
/// Retrieves a value of an integer property.
///
/// Name of the property to retrieve.
/// Default value to return if property cannot be found.
/// Value of the property if it exists, otherwise the default value.
protected internal int GetInt(string name, int defaultValue = 0)
{
int value;
if (ints.TryGetValue(name, out value))
return value;
return defaultValue;
}
///
/// Retrieves a value of a boolean property.
///
/// Name of the property to retrieve.
/// Default value to return if property cannot be found.
/// Value of the property if it exists, otherwise the default value.
protected internal bool GetBool(string name, bool defaultValue = false)
{
bool value;
if (booleans.TryGetValue(name, out value))
return value;
return defaultValue;
}
///
/// Retrieves a value of a string property.
///
/// Name of the property to retrieve.
/// Default value to return if property cannot be found.
/// Value of the property if it exists, otherwise the default value.
protected internal string GetString(string name, string defaultValue = "")
{
string value;
if (strings.TryGetValue(name, out value))
return value;
return defaultValue;
}
///
/// Checks does a persistent property with the specified name exists.
///
/// Name of the property to check.
/// True if the property exists, false otherwise.
protected internal bool HasKey(string name)
{
return floats.ContainsKey(name) || ints.ContainsKey(name) || booleans.ContainsKey(name) ||
strings.ContainsKey(name);
}
///
/// Deletes a persistent property with the specified name.
///
/// Name of the property to delete.
protected internal void DeleteKey(string name)
{
floats.Remove(name);
ints.Remove(name);
booleans.Remove(name);
strings.Remove(name);
}
}
/** @} */
}