InspectorPersistentData.cs 7.5 KB

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