InspectorPersistentData.cs 7.6 KB

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