InspectableDictionary.cs 17 KB

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