InspectableDictionary.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 InspectableDictionaryGUI dictionaryGUIField;
  14. /// <summary>
  15. /// Creates a new inspectable dictionary GUI for the specified property.
  16. /// </summary>
  17. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  18. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field. Some fields may
  19. /// contain other fields, in which case you should increase this value by one.</param>
  20. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  21. /// <param name="property">Serializable property referencing the dictionary whose contents to display.</param>
  22. public InspectableDictionary(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  23. : base(title, SerializableProperty.FieldType.Dictionary, depth, layout, property)
  24. {
  25. }
  26. /// <inheritdoc/>
  27. public override GUILayoutX GetTitleLayout()
  28. {
  29. return dictionaryGUIField.GetTitleLayout();
  30. }
  31. /// <inheritdoc/>
  32. public override InspectableState Refresh(int layoutIndex)
  33. {
  34. return dictionaryGUIField.Refresh();
  35. }
  36. /// <inheritdoc/>
  37. protected internal override void Initialize(int layoutIndex)
  38. {
  39. GUILayout dictionaryLayout = layout.AddLayoutY(layoutIndex);
  40. dictionaryGUIField = InspectableDictionaryGUI.Create(title, property, dictionaryLayout, depth);
  41. }
  42. /// <summary>
  43. /// Creates GUI elements that allow viewing and manipulation of a <see cref="SerializableDictionary"/> referenced
  44. /// by a serializable property.
  45. /// </summary>
  46. public class InspectableDictionaryGUI : GUIDictionaryFieldBase
  47. {
  48. private SerializableProperty property;
  49. private IDictionary dictionary;
  50. private int numElements;
  51. private List<SerializableProperty> orderedKeys = new List<SerializableProperty>();
  52. /// <summary>
  53. /// Constructs a new dictionary GUI.
  54. /// </summary>
  55. /// <param name="title">Label to display on the list GUI title.</param>
  56. /// <param name="property">Serializable property referencing a dictionary</param>
  57. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  58. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  59. /// nested containers whose backgrounds are overlaping. Also determines background style,
  60. /// depths divisible by two will use an alternate style.</param>
  61. protected InspectableDictionaryGUI(LocString title, SerializableProperty property, GUILayout layout, int depth = 0)
  62. : base(title, layout, depth)
  63. {
  64. this.property = property;
  65. dictionary = property.GetValue<IDictionary>();
  66. if (dictionary != null)
  67. numElements = dictionary.Count;
  68. UpdateKeys();
  69. }
  70. /// <summary>
  71. /// Builds the inspectable dictionary GUI elements. Must be called at least once in order for the contents to
  72. /// be populated.
  73. /// </summary>
  74. /// <param name="title">Label to display on the list GUI title.</param>
  75. /// <param name="property">Serializable property referencing a dictionary</param>
  76. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  77. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  78. /// nested containers whose backgrounds are overlaping. Also determines background style,
  79. /// depths divisible by two will use an alternate style.</param>
  80. public static InspectableDictionaryGUI Create(LocString title, SerializableProperty property, GUILayout layout,
  81. int depth = 0)
  82. {
  83. InspectableDictionaryGUI guiDictionary = new InspectableDictionaryGUI(title, property, layout, depth);
  84. guiDictionary.BuildGUI();
  85. return guiDictionary;
  86. }
  87. /// <inheritdoc/>
  88. public override InspectableState Refresh()
  89. {
  90. // Check if any modifications to the array were made outside the inspector
  91. IDictionary newDict = property.GetValue<IDictionary>();
  92. if (dictionary == null && newDict != null)
  93. {
  94. dictionary = newDict;
  95. numElements = dictionary.Count;
  96. BuildGUI();
  97. }
  98. else if (newDict == null && dictionary != null)
  99. {
  100. dictionary = null;
  101. numElements = 0;
  102. BuildGUI();
  103. }
  104. else
  105. {
  106. if (dictionary != null)
  107. {
  108. if (numElements != dictionary.Count)
  109. {
  110. numElements = dictionary.Count;
  111. BuildGUI();
  112. }
  113. }
  114. }
  115. return base.Refresh();
  116. }
  117. /// <summary>
  118. /// Updates the ordered set of keys used for mapping sequential indexes to keys. Should be called whenever a
  119. /// dictionary key changes.
  120. /// </summary>
  121. private void UpdateKeys()
  122. {
  123. orderedKeys.Clear();
  124. if (dictionary != null)
  125. {
  126. SerializableDictionary dict = property.GetDictionary();
  127. foreach (var key in dictionary.Keys)
  128. orderedKeys.Add(dict.GetProperty(key).Key);
  129. }
  130. }
  131. /// <inheritdoc/>
  132. protected override GUIDictionaryFieldRow CreateRow()
  133. {
  134. return new InspectableDictionaryGUIRow();
  135. }
  136. /// <inheritdoc/>
  137. protected override int GetNumRows()
  138. {
  139. if (dictionary != null)
  140. return dictionary.Count;
  141. return 0;
  142. }
  143. /// <inheritdoc/>
  144. protected override bool IsNull()
  145. {
  146. return dictionary == null;
  147. }
  148. /// <inheritdoc/>
  149. protected internal override object GetKey(int rowIdx)
  150. {
  151. return orderedKeys[rowIdx];
  152. }
  153. /// <inheritdoc/>
  154. protected internal override object GetValue(object key)
  155. {
  156. SerializableProperty keyProperty = (SerializableProperty)key;
  157. SerializableDictionary dictionary = property.GetDictionary();
  158. return dictionary.GetProperty(keyProperty.GetValue<object>()).Value;
  159. }
  160. /// <inheritdoc/>
  161. protected internal override void SetValue(object key, object value)
  162. {
  163. // Setting the value should be done through the property
  164. throw new InvalidOperationException();
  165. }
  166. /// <inheritdoc/>
  167. protected internal override bool Contains(object key)
  168. {
  169. SerializableProperty keyProperty = (SerializableProperty)key;
  170. return dictionary.Contains(keyProperty.GetValue<object>()); ;
  171. }
  172. /// <inheritdoc/>
  173. protected internal override void EditEntry(object oldKey, object newKey, object value)
  174. {
  175. SerializableProperty oldKeyProperty = (SerializableProperty)oldKey;
  176. SerializableProperty newKeyProperty = (SerializableProperty)newKey;
  177. SerializableProperty valueProperty = (SerializableProperty)value;
  178. dictionary.Remove(oldKeyProperty.GetValue<object>());
  179. dictionary.Add(newKeyProperty.GetValue<object>(), valueProperty.GetValue<object>());
  180. numElements = dictionary.Count;
  181. UpdateKeys();
  182. }
  183. /// <inheritdoc/>
  184. protected internal override void AddEntry(object key, object value)
  185. {
  186. SerializableProperty keyProperty = (SerializableProperty)key;
  187. SerializableProperty valueProperty = (SerializableProperty)value;
  188. dictionary.Add(keyProperty.GetValue<object>(), valueProperty.GetValue<object>());
  189. numElements = dictionary.Count;
  190. UpdateKeys();
  191. }
  192. /// <inheritdoc/>
  193. protected internal override void RemoveEntry(object key)
  194. {
  195. SerializableProperty keyProperty = (SerializableProperty)key;
  196. dictionary.Remove(keyProperty.GetValue<object>());
  197. numElements = dictionary.Count;
  198. UpdateKeys();
  199. }
  200. /// <inheritdoc/>
  201. protected internal override object CreateKey()
  202. {
  203. SerializableDictionary dictionary = property.GetDictionary();
  204. DictionaryDataWrapper data = new DictionaryDataWrapper();
  205. data.value = SerializableUtility.Create(dictionary.KeyType);
  206. SerializableProperty keyProperty = new SerializableProperty(dictionary.KeyPropertyType,
  207. dictionary.KeyType,
  208. () => data.value, (x) => data.value = x);
  209. return keyProperty;
  210. }
  211. /// <inheritdoc/>
  212. protected internal override object CreateValue()
  213. {
  214. SerializableDictionary dictionary = property.GetDictionary();
  215. DictionaryDataWrapper data = new DictionaryDataWrapper();
  216. data.value = SerializableUtility.Create(dictionary.ValueType);
  217. SerializableProperty valueProperty = new SerializableProperty(dictionary.ValuePropertyType,
  218. dictionary.ValueType,
  219. () => data.value, (x) => data.value = x);
  220. return valueProperty;
  221. }
  222. /// <inheritdoc/>
  223. protected internal override KeyValuePair<object, object> CloneElement(int index)
  224. {
  225. SerializableProperty keyProperty = (SerializableProperty)GetKey(index);
  226. SerializableProperty valueProperty = (SerializableProperty)GetValue(keyProperty);
  227. SerializableDictionary dictionary = property.GetDictionary();
  228. DictionaryDataWrapper keyData = new DictionaryDataWrapper();
  229. keyData.value = SerializableUtility.Clone(keyProperty.GetValue<object>());
  230. SerializableProperty clonedKeyProperty = new SerializableProperty(dictionary.KeyPropertyType,
  231. dictionary.KeyType,
  232. () => keyData.value, (x) => keyData.value = x);
  233. DictionaryDataWrapper valueData = new DictionaryDataWrapper();
  234. valueData.value = SerializableUtility.Clone(valueProperty.GetValue<object>());
  235. SerializableProperty clonedValueProperty = new SerializableProperty(dictionary.ValuePropertyType,
  236. dictionary.ValueType,
  237. () => valueData.value, (x) => valueData.value = x);
  238. return new KeyValuePair<object,object>(clonedKeyProperty, clonedValueProperty);
  239. }
  240. /// <inheritdoc/>
  241. protected override void CreateDictionary()
  242. {
  243. dictionary = property.CreateDictionaryInstance();
  244. numElements = dictionary.Count;
  245. property.SetValue(dictionary);
  246. UpdateKeys();
  247. }
  248. /// <inheritdoc/>
  249. protected override void DeleteDictionary()
  250. {
  251. dictionary = null;
  252. numElements = 0;
  253. property.SetValue<object>(null);
  254. UpdateKeys();
  255. }
  256. /// <summary>
  257. /// Wraps a dictionary key or a value.
  258. /// </summary>
  259. class DictionaryDataWrapper
  260. {
  261. public object value;
  262. }
  263. }
  264. /// <summary>
  265. /// Contains GUI elements for a single key/value pair in the dictionary.
  266. /// </summary>
  267. private class InspectableDictionaryGUIRow : GUIDictionaryFieldRow
  268. {
  269. private GUILayoutY keyLayout;
  270. private InspectableField fieldKey;
  271. private InspectableField fieldValue;
  272. /// <inheritdoc/>
  273. protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
  274. {
  275. keyLayout = layout;
  276. SerializableProperty property = GetKey<SerializableProperty>();
  277. fieldKey = CreateInspectable("Key", 0, Depth + 1,
  278. new InspectableFieldLayout(layout), property);
  279. return fieldKey.GetTitleLayout();
  280. }
  281. /// <inheritdoc/>
  282. protected override void CreateValueGUI(GUILayoutY layout)
  283. {
  284. SerializableProperty property = GetValue<SerializableProperty>();
  285. fieldValue = CreateInspectable("Value", 0, Depth + 1,
  286. new InspectableFieldLayout(layout), property);
  287. }
  288. /// <inheritdoc/>
  289. protected override void OnEditModeChanged(bool editMode)
  290. {
  291. keyLayout.Disabled = !editMode;
  292. }
  293. /// <inheritdoc/>
  294. protected internal override InspectableState Refresh()
  295. {
  296. fieldKey.Property = GetKey<SerializableProperty>();
  297. fieldValue.Property = GetValue<SerializableProperty>();
  298. return fieldValue.Refresh(0);
  299. }
  300. }
  301. }
  302. }