InspectableDictionary.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Displays GUI for a serializable property containing a dictionary. Dictionary contents are displayed as rows of
  9. /// entries that can be shown, hidden or manipulated.
  10. /// </summary>
  11. public class InspectableDictionary : InspectableField
  12. {
  13. private object propertyValue; // TODO - This will unnecessarily hold references to the object
  14. private int numElements;
  15. private InspectableDictionaryGUI dictionaryGUIField;
  16. /// <summary>
  17. /// Creates a new inspectable dictionary GUI for the specified property.
  18. /// </summary>
  19. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  20. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  21. /// contain other fields, in which case you should increase this value by one.</param>
  22. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  23. /// <param name="property">Serializable property referencing the dictionary whose contents to display.</param>
  24. public InspectableDictionary(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  25. : base(title, depth, layout, property)
  26. {
  27. }
  28. /// <inheritdoc/>
  29. public override GUILayoutX GetTitleLayout()
  30. {
  31. return dictionaryGUIField.GetTitleLayout();
  32. }
  33. /// <inheritdoc/>
  34. public override bool IsModified()
  35. {
  36. object newPropertyValue = property.GetValue<object>();
  37. if (propertyValue == null)
  38. return newPropertyValue != null;
  39. if (newPropertyValue == null)
  40. return propertyValue != null;
  41. SerializableDictionary dictionary = property.GetDictionary();
  42. if (dictionary.GetLength() != numElements)
  43. return true;
  44. return base.IsModified();
  45. }
  46. /// <inheritdoc/>
  47. public override void Refresh(int layoutIndex)
  48. {
  49. if (IsModified())
  50. Update(layoutIndex);
  51. dictionaryGUIField.Refresh();
  52. }
  53. /// <inheritdoc/>
  54. public override bool ShouldRebuildOnModify()
  55. {
  56. return true;
  57. }
  58. /// <inheritdoc/>
  59. protected internal override void BuildGUI(int layoutIndex)
  60. {
  61. GUILayout dictionaryLayout = layout.AddLayoutY(layoutIndex);
  62. dictionaryGUIField = InspectableDictionaryGUI.Create(title, property, dictionaryLayout, depth);
  63. }
  64. /// <inheritdoc/>
  65. protected internal override void Update(int layoutIndex)
  66. {
  67. propertyValue = property.GetValue<object>();
  68. if (propertyValue != null)
  69. {
  70. SerializableDictionary dictionary = property.GetDictionary();
  71. numElements = dictionary.GetLength();
  72. }
  73. else
  74. numElements = 0;
  75. layout.DestroyElements();
  76. BuildGUI(layoutIndex);
  77. }
  78. /// <summary>
  79. /// Creates GUI elements that allow viewing and manipulation of a <see cref="SerializableDictionary"/> referenced
  80. /// by a serializable property.
  81. /// </summary>
  82. public class InspectableDictionaryGUI : GUIDictionaryFieldBase
  83. {
  84. private SerializableProperty property;
  85. private List<object> orderedKeys = new List<object>();
  86. /// <summary>
  87. /// Constructs a new dictionary GUI.
  88. /// </summary>
  89. /// <param name="title">Label to display on the list GUI title.</param>
  90. /// <param name="property">Serializable property referencing a dictionary</param>
  91. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  92. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  93. /// nested containers whose backgrounds are overlaping. Also determines background style,
  94. /// depths divisible by two will use an alternate style.</param>
  95. protected InspectableDictionaryGUI(LocString title, SerializableProperty property, GUILayout layout, int depth = 0)
  96. : base(title, layout, depth)
  97. {
  98. this.property = property;
  99. UpdateKeys();
  100. }
  101. /// <summary>
  102. /// Builds the inspectable dictionary GUI elements. Must be called at least once in order for the contents to
  103. /// be populated.
  104. /// </summary>
  105. /// <param name="title">Label to display on the list GUI title.</param>
  106. /// <param name="property">Serializable property referencing a dictionary</param>
  107. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  108. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  109. /// nested containers whose backgrounds are overlaping. Also determines background style,
  110. /// depths divisible by two will use an alternate style.</param>
  111. public static InspectableDictionaryGUI Create(LocString title, SerializableProperty property, GUILayout layout,
  112. int depth = 0)
  113. {
  114. InspectableDictionaryGUI guiDictionary = new InspectableDictionaryGUI(title, property, layout, depth);
  115. guiDictionary.BuildGUI();
  116. return guiDictionary;
  117. }
  118. /// <summary>
  119. /// Updates the ordered set of keys used for mapping sequential indexes to keys. Should be called whenever a
  120. /// dictionary key changes.
  121. /// </summary>
  122. private void UpdateKeys()
  123. {
  124. orderedKeys.Clear();
  125. IDictionary dictionary = property.GetValue<IDictionary>();
  126. if (dictionary != null)
  127. {
  128. foreach (var key in dictionary)
  129. orderedKeys.Add(key);
  130. }
  131. }
  132. /// <inheritdoc/>
  133. protected override GUIDictionaryFieldRow CreateRow()
  134. {
  135. return new InspectableDictionaryGUIRow();
  136. }
  137. /// <inheritdoc/>
  138. protected override int GetNumRows()
  139. {
  140. IDictionary dictionary = property.GetValue<IDictionary>();
  141. if (dictionary != null)
  142. return dictionary.Count;
  143. return 0;
  144. }
  145. /// <inheritdoc/>
  146. protected override bool IsNull()
  147. {
  148. IDictionary dictionary = property.GetValue<IDictionary>();
  149. return dictionary == null;
  150. }
  151. /// <inheritdoc/>
  152. protected internal override object GetKey(int rowIdx)
  153. {
  154. return orderedKeys[rowIdx];
  155. }
  156. /// <inheritdoc/>
  157. protected internal override object GetValue(object key)
  158. {
  159. SerializableDictionary dictionary = property.GetDictionary();
  160. return dictionary.GetProperty(key);
  161. }
  162. /// <inheritdoc/>
  163. protected internal override void SetValue(object key, object value)
  164. {
  165. // Setting the value should be done through the property
  166. throw new InvalidOperationException();
  167. }
  168. /// <inheritdoc/>
  169. protected internal override bool Contains(object key)
  170. {
  171. IDictionary dictionary = property.GetValue<IDictionary>();
  172. return dictionary.Contains(key); ;
  173. }
  174. /// <inheritdoc/>
  175. protected internal override void EditEntry(object oldKey, object newKey, object value)
  176. {
  177. IDictionary dictionary = property.GetValue<IDictionary>();
  178. dictionary.Remove(oldKey);
  179. dictionary.Add(newKey, value);
  180. UpdateKeys();
  181. }
  182. /// <inheritdoc/>
  183. protected internal override void AddEntry(object key, object value)
  184. {
  185. IDictionary dictionary = property.GetValue<IDictionary>();
  186. dictionary.Add(key, value);
  187. UpdateKeys();
  188. }
  189. /// <inheritdoc/>
  190. protected internal override void RemoveEntry(object key)
  191. {
  192. IDictionary dictionary = property.GetValue<IDictionary>();
  193. dictionary.Remove(key);
  194. UpdateKeys();
  195. }
  196. /// <inheritdoc/>
  197. protected internal override object CreateKey()
  198. {
  199. SerializableDictionary dictionary = property.GetDictionary();
  200. return SerializableUtility.Create(dictionary.KeyType);
  201. }
  202. /// <inheritdoc/>
  203. protected internal override object CreateValue()
  204. {
  205. SerializableDictionary dictionary = property.GetDictionary();
  206. return SerializableUtility.Create(dictionary.ValueType);
  207. }
  208. /// <inheritdoc/>
  209. protected override void CreateDictionary()
  210. {
  211. property.SetValue(property.CreateDictionaryInstance());
  212. UpdateKeys();
  213. }
  214. /// <inheritdoc/>
  215. protected override void DeleteDictionary()
  216. {
  217. property.SetValue<object>(null);
  218. UpdateKeys();
  219. }
  220. }
  221. /// <summary>
  222. /// Contains GUI elements for a single key/value pair in the dictionary.
  223. /// </summary>
  224. private class InspectableDictionaryGUIRow : GUIDictionaryFieldRow
  225. {
  226. private InspectableField fieldKey;
  227. private InspectableField fieldValue;
  228. /// <inheritdoc/>
  229. protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
  230. {
  231. if (fieldKey == null)
  232. {
  233. SerializableProperty property = GetKey<SerializableProperty>();
  234. fieldKey = CreateInspectable("Key", 0, depth + 1,
  235. new InspectableFieldLayout(layout), property);
  236. }
  237. return fieldKey.GetTitleLayout();
  238. }
  239. /// <inheritdoc/>
  240. protected override void CreateValueGUI(GUILayoutY layout)
  241. {
  242. if (fieldValue == null)
  243. {
  244. SerializableProperty property = GetValue<SerializableProperty>();
  245. fieldValue = CreateInspectable("Value", 0, depth + 1,
  246. new InspectableFieldLayout(layout), property);
  247. }
  248. }
  249. /// <inheritdoc/>
  250. protected internal override bool Refresh()
  251. {
  252. bool rebuild = false;
  253. if (fieldKey.IsModified())
  254. rebuild = fieldKey.ShouldRebuildOnModify();
  255. fieldKey.Refresh(0);
  256. fieldValue.Refresh(0);
  257. return rebuild;
  258. }
  259. }
  260. }
  261. }