GUIDictionaryField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. /// <summary>
  210. /// Constructs a new empty dictionary GUI.
  211. /// </summary>
  212. public GUIDictionaryField()
  213. { }
  214. /// <summary>
  215. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  216. /// </summary>
  217. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  218. /// <param name="title">Label to display on the list GUI title.</param>
  219. /// <param name="dictionary">Object containing the data. Can be null.</param>
  220. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  221. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  222. /// nested containers whose backgrounds are overlaping. Also determines background style,
  223. /// depths divisible by two will use an alternate style.</param>
  224. public void Update<RowType>(LocString title, Dictionary<Key, Value> dictionary,
  225. GUILayout layout, int depth = 0)
  226. where RowType : GUIDictionaryFieldRow, new()
  227. {
  228. this.dictionary = dictionary;
  229. if (dictionary != null)
  230. base.Update<RowType>(title, false, dictionary.Count, layout, depth);
  231. else
  232. base.Update<RowType>(title, true, 0, layout, depth);
  233. }
  234. /// <inheritdoc/>
  235. protected internal override object GetValue(object key)
  236. {
  237. return dictionary[key];
  238. }
  239. /// <inheritdoc/>
  240. protected internal override void SetValue(object key, object value)
  241. {
  242. dictionary[key] = value;
  243. if (OnValueChanged != null)
  244. OnValueChanged();
  245. }
  246. /// <inheritdoc/>
  247. protected internal override bool Contains(object key)
  248. {
  249. return dictionary.Contains(key);;
  250. }
  251. /// <inheritdoc/>
  252. protected override void OnCreateButtonClicked()
  253. {
  254. dictionary = new Dictionary<Key, Value>();
  255. if (OnChanged != null)
  256. OnChanged(dictionary);
  257. }
  258. /// <inheritdoc/>
  259. protected override void OnClearButtonClicked()
  260. {
  261. dictionary = null;
  262. if (OnChanged != null)
  263. OnChanged(dictionary);
  264. }
  265. /// <inheritdoc/>
  266. protected internal override void OnDeleteButtonClicked(object key)
  267. {
  268. dictionary.Remove(key);
  269. if (OnValueChanged != null)
  270. OnValueChanged();
  271. }
  272. /// <summary>
  273. /// Triggered when the user clicks on the clone button next to a dictionary entry. Clones the element and adds the
  274. /// clone to the dictionary. Non-value types must implement the <see cref="ICloneable"/> interface in order to be
  275. /// cloned. If it doesn't the clone will point to a null reference.
  276. /// </summary>
  277. /// <param name="key">Key of the element to clone.</param>
  278. protected internal override void OnCloneButtonClicked(object key)
  279. {
  280. // TODO - Not supported
  281. //int size = array.GetLength(0) + 1;
  282. //Array newArray = Array.CreateInstance(arrayType.GetElementType(), size);
  283. //object clonedEntry = null;
  284. //for (int i = 0; i < array.GetLength(0); i++)
  285. //{
  286. // object value = array.GetValue(i);
  287. // newArray.SetValue(value, i);
  288. // if (i == index)
  289. // {
  290. // if (value == null)
  291. // clonedEntry = null;
  292. // else
  293. // {
  294. // ValueType valueType = value as ValueType;
  295. // if (valueType != null)
  296. // clonedEntry = valueType;
  297. // else
  298. // {
  299. // ICloneable cloneable = value as ICloneable;
  300. // if (cloneable != null)
  301. // clonedEntry = cloneable.Clone();
  302. // else
  303. // clonedEntry = null;
  304. // }
  305. // }
  306. // }
  307. //}
  308. //newArray.SetValue(clonedEntry, size - 1);
  309. //array = newArray;
  310. //if (OnChanged != null)
  311. // OnChanged(array);
  312. }
  313. }
  314. /// <summary>
  315. /// Contains GUI elements for a single entry in a dictionary.
  316. /// </summary>
  317. public abstract class GUIDictionaryFieldRow
  318. {
  319. private GUILayoutX rowLayout;
  320. private GUILayoutY keyLayout;
  321. private GUILayoutY valueLayout;
  322. private GUILayoutX titleLayout;
  323. private bool localTitleLayout;
  324. private GUIDictionaryFieldBase parent;
  325. protected object key;
  326. protected int depth;
  327. /// <summary>
  328. /// Constructs a new dictionary row object.
  329. /// </summary>
  330. protected GUIDictionaryFieldRow()
  331. {
  332. }
  333. /// <summary>
  334. /// (Re)creates all row GUI elements.
  335. /// </summary>
  336. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  337. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  338. /// <param name="key">Key of the element to create GUI for.</param>
  339. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  340. public void BuildGUI(GUIDictionaryFieldBase parent, GUILayout parentLayout, object key, int depth)
  341. {
  342. this.parent = parent;
  343. this.key = key;
  344. this.depth = depth;
  345. if (rowLayout == null)
  346. rowLayout = parentLayout.AddLayoutX();
  347. if (keyLayout == null)
  348. keyLayout = rowLayout.AddLayoutY();
  349. if (valueLayout == null)
  350. valueLayout = rowLayout.AddLayoutY();
  351. CreateKeyGUI(keyLayout);
  352. GUILayoutX externalTitleLayout = CreateValueGUI(valueLayout);
  353. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  354. return;
  355. if (externalTitleLayout != null)
  356. {
  357. localTitleLayout = false;
  358. titleLayout = externalTitleLayout;
  359. }
  360. else
  361. {
  362. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  363. buttonCenter.AddFlexibleSpace();
  364. titleLayout = buttonCenter.AddLayoutX();
  365. buttonCenter.AddFlexibleSpace();
  366. localTitleLayout = true;
  367. }
  368. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  369. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  370. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  371. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  372. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(key);
  373. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(key);
  374. titleLayout.AddElement(cloneBtn);
  375. titleLayout.AddElement(deleteBtn);
  376. }
  377. /// <summary>
  378. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  379. /// </summary>
  380. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  381. protected abstract void CreateKeyGUI(GUILayoutY layout);
  382. /// <summary>
  383. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  384. /// </summary>
  385. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  386. /// <returns>An optional title bar layout that the standard dictionary buttons will be appended to.</returns>
  387. protected abstract GUILayoutX CreateValueGUI(GUILayoutY layout);
  388. /// <summary>
  389. /// Refreshes the GUI for the dictionary row and checks if anything was modified.
  390. /// </summary>
  391. /// <param name="rebuildGUI">Determines should the field's GUI elements be updated due to modifications.</param>
  392. /// <returns>True if any modifications were made, false otherwise.</returns>
  393. internal protected virtual bool Refresh(out bool rebuildGUI)
  394. {
  395. rebuildGUI = false;
  396. return false;
  397. }
  398. /// <summary>
  399. /// Gets the value contained in this dictionary's row.
  400. /// </summary>
  401. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  402. /// <returns>Value in this dictionary's row.</returns>
  403. protected T GetValue<T>()
  404. {
  405. return (T)parent.GetValue(key);
  406. }
  407. /// <summary>
  408. /// Sets the value contained in this dictionary's row.
  409. /// </summary>
  410. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  411. /// <param name="value">Value to set.</param>
  412. protected void SetValue<T>(T value)
  413. {
  414. parent.SetValue(key, value);
  415. }
  416. /// <summary>
  417. /// Destroys all row GUI elements.
  418. /// </summary>
  419. public void Destroy()
  420. {
  421. rowLayout.Destroy();
  422. rowLayout = null;
  423. }
  424. }
  425. }