InspectableDictionary.cs 17 KB

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