InspectorPersistentData.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. using System.Collections.Generic;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Contains Inspector specific data that should persist assembly refresh.
  9. /// </summary>
  10. internal class InspectorPersistentData : Component
  11. {
  12. [SerializeField]
  13. private Dictionary<ulong, SerializableProperties> componentProperties =
  14. new Dictionary<ulong, SerializableProperties>();
  15. [SerializeField]
  16. private Dictionary<string, SerializableProperties> resourceProperties =
  17. new Dictionary<string, SerializableProperties>();
  18. /// <summary>
  19. /// Returns existing, or creates new properties for a component with the specified id.
  20. /// </summary>
  21. /// <param name="componentId">Internal ID of the component to retrieve properties for.</param>
  22. /// <returns>A set of key value pairs representing persistent properties of an inspectable component.</returns>
  23. public SerializableProperties GetProperties(ulong componentId)
  24. {
  25. SerializableProperties output;
  26. if (!componentProperties.TryGetValue(componentId, out output))
  27. {
  28. output = new SerializableProperties();
  29. componentProperties[componentId] = output;
  30. }
  31. return output;
  32. }
  33. /// <summary>
  34. /// Returns existing, or creates new properties for a resource with the specified UUID.
  35. /// </summary>
  36. /// <param name="uuid">Unique identifier of the resource to retrieve properties for.</param>
  37. /// <returns>A set of key value pairs representing persistent properties of an inspectable resource.</returns>
  38. public SerializableProperties GetProperties(string uuid)
  39. {
  40. SerializableProperties output;
  41. if (!resourceProperties.TryGetValue(uuid, out output))
  42. {
  43. output = new SerializableProperties(); ;
  44. resourceProperties[uuid] = output;
  45. }
  46. return output;
  47. }
  48. }
  49. /// <summary>
  50. /// Stores a serializable set of key-value pairs of various types.
  51. /// </summary>
  52. [SerializeObject]
  53. public class SerializableProperties
  54. {
  55. [SerializeField]
  56. private Dictionary<string, bool> booleans = new Dictionary<string, bool>();
  57. [SerializeField]
  58. private Dictionary<string, float> floats = new Dictionary<string, float>();
  59. [SerializeField]
  60. private Dictionary<string, int> ints = new Dictionary<string, int>();
  61. [SerializeField]
  62. private Dictionary<string, string> strings = new Dictionary<string, string>();
  63. /// <summary>
  64. /// Sets a floating point value to a property with the specified name.
  65. /// </summary>
  66. /// <param name="name">Name to record the property under.</param>
  67. /// <param name="value">Value of the property.</param>
  68. protected internal void SetFloat(string name, float value)
  69. {
  70. floats[name] = value;
  71. }
  72. /// <summary>
  73. /// Sets a integer value to a property with the specified name.
  74. /// </summary>
  75. /// <param name="name">Name to record the property under.</param>
  76. /// <param name="value">Value of the property.</param>
  77. protected internal void SetInt(string name, int value)
  78. {
  79. ints[name] = value;
  80. }
  81. /// <summary>
  82. /// Sets a boolean value to a property with the specified name.
  83. /// </summary>
  84. /// <param name="name">Name to record the property under.</param>
  85. /// <param name="value">Value of the property.</param>
  86. protected internal void SetBool(string name, bool value)
  87. {
  88. booleans[name] = value;
  89. }
  90. /// <summary>
  91. /// Sets a string value to a property with the specified name.
  92. /// </summary>
  93. /// <param name="name">Name to record the property under.</param>
  94. /// <param name="value">Value of the property.</param>
  95. protected internal void SetString(string name, string value)
  96. {
  97. strings[name] = value;
  98. }
  99. /// <summary>
  100. /// Retrieves a value of a floating point property.
  101. /// </summary>
  102. /// <param name="name">Name of the property to retrieve.</param>
  103. /// <param name="defaultValue">Default value to return if property cannot be found.</param>
  104. /// <returns>Value of the property if it exists, otherwise the default value.</returns>
  105. protected internal float GetFloat(string name, float defaultValue = 0.0f)
  106. {
  107. float value;
  108. if (floats.TryGetValue(name, out value))
  109. return value;
  110. return defaultValue;
  111. }
  112. /// <summary>
  113. /// Retrieves a value of an integer property.
  114. /// </summary>
  115. /// <param name="name">Name of the property to retrieve.</param>
  116. /// <param name="defaultValue">Default value to return if property cannot be found.</param>
  117. /// <returns>Value of the property if it exists, otherwise the default value.</returns>
  118. protected internal int GetInt(string name, int defaultValue = 0)
  119. {
  120. int value;
  121. if (ints.TryGetValue(name, out value))
  122. return value;
  123. return defaultValue;
  124. }
  125. /// <summary>
  126. /// Retrieves a value of a boolean property.
  127. /// </summary>
  128. /// <param name="name">Name of the property to retrieve.</param>
  129. /// <param name="defaultValue">Default value to return if property cannot be found.</param>
  130. /// <returns>Value of the property if it exists, otherwise the default value.</returns>
  131. protected internal bool GetBool(string name, bool defaultValue = false)
  132. {
  133. bool value;
  134. if (booleans.TryGetValue(name, out value))
  135. return value;
  136. return defaultValue;
  137. }
  138. /// <summary>
  139. /// Retrieves a value of a string property.
  140. /// </summary>
  141. /// <param name="name">Name of the property to retrieve.</param>
  142. /// <param name="defaultValue">Default value to return if property cannot be found.</param>
  143. /// <returns>Value of the property if it exists, otherwise the default value.</returns>
  144. protected internal string GetString(string name, string defaultValue = "")
  145. {
  146. string value;
  147. if (strings.TryGetValue(name, out value))
  148. return value;
  149. return defaultValue;
  150. }
  151. /// <summary>
  152. /// Checks does a persistent property with the specified name exists.
  153. /// </summary>
  154. /// <param name="name">Name of the property to check.</param>
  155. /// <returns>True if the property exists, false otherwise.</returns>
  156. protected internal bool HasKey(string name)
  157. {
  158. return floats.ContainsKey(name) || ints.ContainsKey(name) || booleans.ContainsKey(name) ||
  159. strings.ContainsKey(name);
  160. }
  161. /// <summary>
  162. /// Deletes a persistent property with the specified name.
  163. /// </summary>
  164. /// <param name="name">Name of the property to delete.</param>
  165. protected internal void DeleteKey(string name)
  166. {
  167. floats.Remove(name);
  168. ints.Remove(name);
  169. booleans.Remove(name);
  170. strings.Remove(name);
  171. }
  172. }
  173. }