GUIDictionaryField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Base class for objects that display GUI for a modifyable dictionary of elements. Elements can be added, modified or
  9. /// removed.
  10. /// </summary>
  11. public abstract class GUIDictionaryFieldBase
  12. {
  13. private const int IndentAmount = 5;
  14. protected List<GUIDictionaryFieldRow> rows = new List<GUIDictionaryFieldRow>();
  15. protected GUILayoutX guiChildLayout;
  16. protected GUILayoutX guiTitleLayout;
  17. protected GUILayoutY guiContentLayout;
  18. protected bool isExpanded;
  19. protected int depth;
  20. /// <summary>
  21. /// Constructs a new GUI dictionary.
  22. /// </summary>
  23. protected GUIDictionaryFieldBase()
  24. { }
  25. /// <summary>
  26. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  27. /// </summary>
  28. /// <typeparam name="T">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  29. /// <param name="title">Label to display on the dictionary GUI title.</param>
  30. /// <param name="empty">Should the created field represent a null object.</param>
  31. /// <param name="numRows">Number of rows to create GUI for. Only matters for a non-null dictionary.</param>
  32. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  33. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  34. /// nested containers whose backgrounds are overlaping. Also determines background style,
  35. /// depths divisible by two will use an alternate style.</param>
  36. protected void Update<T>(LocString title, bool empty, int numRows, GUILayout layout,
  37. int depth = 0) where T : GUIDictionaryFieldRow, new()
  38. {
  39. Destroy();
  40. this.depth = depth;
  41. if (empty)
  42. {
  43. guiChildLayout = null;
  44. guiContentLayout = null;
  45. guiTitleLayout = layout.AddLayoutX();
  46. guiTitleLayout.AddElement(new GUILabel(title));
  47. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  48. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  49. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  50. createBtn.OnClick += OnCreateButtonClicked;
  51. guiTitleLayout.AddElement(createBtn);
  52. }
  53. else
  54. {
  55. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  56. guiFoldout.Value = isExpanded;
  57. guiFoldout.OnToggled += OnFoldoutToggled;
  58. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  59. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  60. guiClearBtn.OnClick += OnClearButtonClicked;
  61. guiTitleLayout = layout.AddLayoutX();
  62. guiTitleLayout.AddElement(guiFoldout);
  63. guiTitleLayout.AddElement(guiClearBtn);
  64. if (numRows > 0)
  65. {
  66. guiChildLayout = layout.AddLayoutX();
  67. guiChildLayout.AddSpace(IndentAmount);
  68. guiChildLayout.Enabled = isExpanded;
  69. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  70. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  71. guiIndentLayoutX.AddSpace(IndentAmount);
  72. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  73. guiIndentLayoutY.AddSpace(IndentAmount);
  74. guiContentLayout = guiIndentLayoutY.AddLayoutY();
  75. guiIndentLayoutY.AddSpace(IndentAmount);
  76. guiIndentLayoutX.AddSpace(IndentAmount);
  77. guiChildLayout.AddSpace(IndentAmount);
  78. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  79. string bgPanelStyle = depth % 2 == 0
  80. ? EditorStyles.InspectorContentBgAlternate
  81. : EditorStyles.InspectorContentBg;
  82. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  83. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  84. backgroundPanel.AddElement(inspectorContentBg);
  85. for (int i = 0; i < numRows; i++)
  86. {
  87. GUIDictionaryFieldRow newRow = new T();
  88. newRow.BuildGUI(this, guiContentLayout, i, depth);
  89. rows.Add(newRow);
  90. }
  91. }
  92. }
  93. }
  94. /// <summary>
  95. /// Returns the layout that is used for positioning the elements in the title bar.
  96. /// </summary>
  97. /// <returns>Horizontal layout for positioning the title bar elements.</returns>
  98. public GUILayoutX GetTitleLayout()
  99. {
  100. return guiTitleLayout;
  101. }
  102. /// <summary>
  103. /// Refreshes contents of all dictionary rows and checks if anything was modified.
  104. /// </summary>
  105. /// <returns>True if any entry in the list was modified, false otherwise.</returns>
  106. public bool Refresh()
  107. {
  108. bool anythingModified = false;
  109. for (int i = 0; i < rows.Count; i++)
  110. {
  111. bool updateGUI;
  112. anythingModified |= rows[i].Refresh(out updateGUI);
  113. if (updateGUI)
  114. rows[i].BuildGUI(this, guiContentLayout, i, depth);
  115. }
  116. return anythingModified;
  117. }
  118. /// <summary>
  119. /// Destroys the GUI elements.
  120. /// </summary>
  121. public void Destroy()
  122. {
  123. if (guiTitleLayout != null)
  124. {
  125. guiTitleLayout.Destroy();
  126. guiTitleLayout = null;
  127. }
  128. if (guiChildLayout != null)
  129. {
  130. guiChildLayout.Destroy();
  131. guiChildLayout = null;
  132. }
  133. for (int i = 0; i < rows.Count; i++)
  134. rows[i].Destroy();
  135. rows.Clear();
  136. }
  137. /// <summary>
  138. /// Gets a value of an element at the specified index in the list.
  139. /// </summary>
  140. /// <param name="key">Key of the element whose value to retrieve.</param>
  141. /// <returns>Value of the list element at the specified key.</returns>
  142. protected internal abstract object GetValue(object key);
  143. /// <summary>
  144. /// Sets a value of an element at the specified index in the list.
  145. /// </summary>
  146. /// <param name="key">Key of the element whose value to set.</param>
  147. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  148. protected internal abstract void SetValue(object key, object value);
  149. /// <summary>
  150. /// Checks does the element with the specified key exist in the dictionary.
  151. /// </summary>
  152. /// <param name="key">Key of the element to check for existence.</param>
  153. /// <returns>True if the key exists in the dictionary, false otherwise.</returns>
  154. protected internal abstract bool Contains(object key);
  155. /// <summary>
  156. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  157. /// </summary>
  158. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  159. private void OnFoldoutToggled(bool expanded)
  160. {
  161. isExpanded = expanded;
  162. if (guiChildLayout != null)
  163. guiChildLayout.Enabled = isExpanded;
  164. }
  165. /// <summary>
  166. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new dictionary with zero
  167. /// elements in the place of the current dictionary.
  168. /// </summary>
  169. protected abstract void OnCreateButtonClicked();
  170. /// <summary>
  171. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current dictionary object.
  172. /// </summary>
  173. protected abstract void OnClearButtonClicked();
  174. /// <summary>
  175. /// Triggered when the user clicks on the delete button next to a dictionary entry. Deletes an element in the
  176. /// dictionary.
  177. /// </summary>
  178. /// <param name="key">Key of the element to remove.</param>
  179. protected internal abstract void OnDeleteButtonClicked(object key);
  180. /// <summary>
  181. /// Triggered when the user clicks on the clone button next to a dictionary entry. Clones an element and
  182. /// adds the clone to the dictionary.
  183. /// </summary>
  184. /// <param name="key">Key of the element to clone.</param>
  185. protected internal abstract void OnCloneButtonClicked(object key);
  186. }
  187. /// <summary>
  188. /// Creates GUI elements that allow viewing and manipulation of a <see cref="Dictionary{TKey,TValue}"/>. When constructing the
  189. /// object user can provide a custom type that manages GUI for individual dictionary elements.
  190. /// </summary>
  191. /// <typeparam name="Key">Type of key used by the dictionary.</typeparam>
  192. /// <typeparam name="Value">Type of value stored in the dictionary.</typeparam>
  193. public class GUIDictionaryField<Key, Value> : GUIDictionaryFieldBase
  194. {
  195. /// <summary>
  196. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  197. /// internal elements.
  198. /// </summary>
  199. public Action<IDictionary> OnChanged;
  200. /// <summary>
  201. /// Triggered when an element in the list has been changed.
  202. /// </summary>
  203. public Action OnValueChanged;
  204. /// <summary>
  205. /// Array object whose contents are displayed.
  206. /// </summary>
  207. public IDictionary Dictionary { get { return dictionary; } }
  208. protected IDictionary dictionary;
  209. protected Type keyType;
  210. protected Type valueType;
  211. /// <summary>
  212. /// Constructs a new empty dictionary GUI.
  213. /// </summary>
  214. public GUIDictionaryField()
  215. { }
  216. /// <summary>
  217. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  218. /// </summary>
  219. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  220. /// <param name="title">Label to display on the list GUI title.</param>
  221. /// <param name="dictionary">Object containing the data. Can be null.</param>
  222. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  223. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  224. /// nested containers whose backgrounds are overlaping. Also determines background style,
  225. /// depths divisible by two will use an alternate style.</param>
  226. public void Update<RowType>(LocString title, Dictionary<Key, Value> dictionary,
  227. GUILayout layout, int depth = 0)
  228. where RowType : GUIDictionaryFieldRow, new()
  229. {
  230. this.keyType = typeof(Key);
  231. this.valueType = typeof(Value);
  232. this.dictionary = dictionary;
  233. if (dictionary != null)
  234. base.Update<RowType>(title, false, dictionary.Count, layout, depth);
  235. else
  236. base.Update<RowType>(title, true, 0, layout, depth);
  237. }
  238. /// <inheritdoc/>
  239. protected internal override object GetValue(object key)
  240. {
  241. return dictionary[key];
  242. }
  243. /// <inheritdoc/>
  244. protected internal override void SetValue(object key, object value)
  245. {
  246. dictionary[key] = value;
  247. if (OnValueChanged != null)
  248. OnValueChanged();
  249. }
  250. /// <inheritdoc/>
  251. protected internal override bool Contains(object key)
  252. {
  253. return dictionary.Contains(key);;
  254. }
  255. /// <inheritdoc/>
  256. protected override void OnCreateButtonClicked()
  257. {
  258. dictionary = new Dictionary<Key, Value>();
  259. if (OnChanged != null)
  260. OnChanged(dictionary);
  261. }
  262. /// <inheritdoc/>
  263. protected override void OnClearButtonClicked()
  264. {
  265. dictionary = null;
  266. if (OnChanged != null)
  267. OnChanged(dictionary);
  268. }
  269. /// <inheritdoc/>
  270. protected internal override void OnDeleteButtonClicked(object key)
  271. {
  272. dictionary.Remove(key);
  273. if (OnValueChanged != null)
  274. OnValueChanged();
  275. }
  276. /// <summary>
  277. /// Triggered when the user clicks on the clone button next to a dictionary entry. Clones the element and adds the
  278. /// clone to the dictionary. Non-value types must implement the <see cref="ICloneable"/> interface in order to be
  279. /// cloned. If it doesn't the clone will point to a null reference.
  280. /// </summary>
  281. /// <param name="key">Key of the element to clone.</param>
  282. protected internal override void OnCloneButtonClicked(object key)
  283. {
  284. // TODO - Not supported
  285. //int size = array.GetLength(0) + 1;
  286. //Array newArray = Array.CreateInstance(arrayType.GetElementType(), size);
  287. //object clonedEntry = null;
  288. //for (int i = 0; i < array.GetLength(0); i++)
  289. //{
  290. // object value = array.GetValue(i);
  291. // newArray.SetValue(value, i);
  292. // if (i == index)
  293. // {
  294. // if (value == null)
  295. // clonedEntry = null;
  296. // else
  297. // {
  298. // ValueType valueType = value as ValueType;
  299. // if (valueType != null)
  300. // clonedEntry = valueType;
  301. // else
  302. // {
  303. // ICloneable cloneable = value as ICloneable;
  304. // if (cloneable != null)
  305. // clonedEntry = cloneable.Clone();
  306. // else
  307. // clonedEntry = null;
  308. // }
  309. // }
  310. // }
  311. //}
  312. //newArray.SetValue(clonedEntry, size - 1);
  313. //array = newArray;
  314. //if (OnChanged != null)
  315. // OnChanged(array);
  316. }
  317. }
  318. /// <summary>
  319. /// Contains GUI elements for a single entry in a dictionary.
  320. /// </summary>
  321. public abstract class GUIDictionaryFieldRow
  322. {
  323. private GUILayoutX rowLayout;
  324. private GUILayoutY keyLayout;
  325. private GUILayoutY valueLayout;
  326. private GUILayoutX titleLayout;
  327. private bool localTitleLayout;
  328. private GUIDictionaryFieldBase parent;
  329. protected object key;
  330. protected int depth;
  331. /// <summary>
  332. /// Constructs a new dictionary row object.
  333. /// </summary>
  334. protected GUIDictionaryFieldRow()
  335. {
  336. }
  337. /// <summary>
  338. /// (Re)creates all row GUI elements.
  339. /// </summary>
  340. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  341. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  342. /// <param name="key">Key of the element to create GUI for.</param>
  343. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  344. public void BuildGUI(GUIDictionaryFieldBase parent, GUILayout parentLayout, object key, int depth)
  345. {
  346. this.parent = parent;
  347. this.key = key;
  348. this.depth = depth;
  349. if (rowLayout == null)
  350. rowLayout = parentLayout.AddLayoutX();
  351. if (keyLayout == null)
  352. keyLayout = rowLayout.AddLayoutY();
  353. if (valueLayout == null)
  354. valueLayout = rowLayout.AddLayoutY();
  355. CreateKeyGUI(keyLayout);
  356. GUILayoutX externalTitleLayout = CreateValueGUI(valueLayout);
  357. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  358. return;
  359. if (externalTitleLayout != null)
  360. {
  361. localTitleLayout = false;
  362. titleLayout = externalTitleLayout;
  363. }
  364. else
  365. {
  366. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  367. buttonCenter.AddFlexibleSpace();
  368. titleLayout = buttonCenter.AddLayoutX();
  369. buttonCenter.AddFlexibleSpace();
  370. localTitleLayout = true;
  371. }
  372. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  373. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  374. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  375. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  376. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(key);
  377. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(key);
  378. titleLayout.AddElement(cloneBtn);
  379. titleLayout.AddElement(deleteBtn);
  380. }
  381. /// <summary>
  382. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  383. /// </summary>
  384. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  385. protected abstract void CreateKeyGUI(GUILayoutY layout);
  386. /// <summary>
  387. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  388. /// </summary>
  389. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  390. /// <returns>An optional title bar layout that the standard dictionary buttons will be appended to.</returns>
  391. protected abstract GUILayoutX CreateValueGUI(GUILayoutY layout);
  392. /// <summary>
  393. /// Refreshes the GUI for the dictionary row and checks if anything was modified.
  394. /// </summary>
  395. /// <param name="rebuildGUI">Determines should the field's GUI elements be updated due to modifications.</param>
  396. /// <returns>True if any modifications were made, false otherwise.</returns>
  397. internal protected virtual bool Refresh(out bool rebuildGUI)
  398. {
  399. rebuildGUI = false;
  400. return false;
  401. }
  402. /// <summary>
  403. /// Gets the value contained in this dictionary's row.
  404. /// </summary>
  405. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  406. /// <returns>Value in this dictionary's row.</returns>
  407. protected T GetValue<T>()
  408. {
  409. return (T)parent.GetValue(key);
  410. }
  411. /// <summary>
  412. /// Sets the value contained in this dictionary's row.
  413. /// </summary>
  414. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  415. /// <param name="value">Value to set.</param>
  416. protected void SetValue<T>(T value)
  417. {
  418. parent.SetValue(key, value);
  419. }
  420. /// <summary>
  421. /// Destroys all row GUI elements.
  422. /// </summary>
  423. public void Destroy()
  424. {
  425. rowLayout.Destroy();
  426. rowLayout = null;
  427. }
  428. }
  429. }