InspectableDictionary.cs 17 KB

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