GUIDictionaryField.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. /// <summary>
  187. /// Triggered when user clicks the edit or apply (depending on state) button next to the dictionary entry. Starts
  188. /// edit mode for the element, if not already in edit mode. Applies edit mode changes if already in edit mode.
  189. /// </summary>
  190. /// <param name="key">Key of the element to edit.</param>
  191. protected internal virtual void OnEditButtonClicked(object key)
  192. {
  193. // TODO
  194. }
  195. }
  196. /// <summary>
  197. /// Creates GUI elements that allow viewing and manipulation of a <see cref="Dictionary{TKey,TValue}"/>. When constructing the
  198. /// object user can provide a custom type that manages GUI for individual dictionary elements.
  199. /// </summary>
  200. /// <typeparam name="Key">Type of key used by the dictionary.</typeparam>
  201. /// <typeparam name="Value">Type of value stored in the dictionary.</typeparam>
  202. public class GUIDictionaryField<Key, Value> : GUIDictionaryFieldBase
  203. {
  204. /// <summary>
  205. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  206. /// internal elements.
  207. /// </summary>
  208. public Action<Dictionary<Key, Value>> OnChanged;
  209. /// <summary>
  210. /// Triggered when an element in the list has been changed.
  211. /// </summary>
  212. public Action<Key> OnValueChanged;
  213. /// <summary>
  214. /// Array object whose contents are displayed.
  215. /// </summary>
  216. public Dictionary<Key, Value> Dictionary { get { return dictionary; } }
  217. protected Dictionary<Key, Value> dictionary;
  218. /// <summary>
  219. /// Constructs a new empty dictionary GUI.
  220. /// </summary>
  221. public GUIDictionaryField()
  222. { }
  223. /// <summary>
  224. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  225. /// </summary>
  226. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  227. /// <param name="title">Label to display on the list GUI title.</param>
  228. /// <param name="dictionary">Object containing the data. Can be null.</param>
  229. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  230. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  231. /// nested containers whose backgrounds are overlaping. Also determines background style,
  232. /// depths divisible by two will use an alternate style.</param>
  233. public void Update<RowType>(LocString title, Dictionary<Key, Value> dictionary,
  234. GUILayout layout, int depth = 0)
  235. where RowType : GUIDictionaryFieldRow, new()
  236. {
  237. this.dictionary = dictionary;
  238. if (dictionary != null)
  239. base.Update<RowType>(title, false, dictionary.Count, layout, depth);
  240. else
  241. base.Update<RowType>(title, true, 0, layout, depth);
  242. }
  243. /// <inheritdoc/>
  244. protected internal override object GetValue(object key)
  245. {
  246. return dictionary[(Key)key];
  247. }
  248. /// <inheritdoc/>
  249. protected internal override void SetValue(object key, object value)
  250. {
  251. dictionary[(Key)key] = (Value)value;
  252. if (OnValueChanged != null)
  253. OnValueChanged((Key)key);
  254. }
  255. /// <inheritdoc/>
  256. protected internal override bool Contains(object key)
  257. {
  258. return dictionary.ContainsKey((Key)key); ;
  259. }
  260. /// <inheritdoc/>
  261. protected override void OnCreateButtonClicked()
  262. {
  263. dictionary = new Dictionary<Key, Value>();
  264. if (OnChanged != null)
  265. OnChanged(dictionary);
  266. }
  267. /// <inheritdoc/>
  268. protected override void OnClearButtonClicked()
  269. {
  270. dictionary = null;
  271. if (OnChanged != null)
  272. OnChanged(dictionary);
  273. }
  274. /// <inheritdoc/>
  275. protected internal override void OnDeleteButtonClicked(object key)
  276. {
  277. dictionary.Remove((Key)key);
  278. if (OnValueChanged != null)
  279. OnValueChanged((Key)key);
  280. }
  281. /// <inheritdoc/>
  282. protected internal override void OnCloneButtonClicked(object key)
  283. {
  284. // TODO - Not implemented
  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 GUILayoutY rowLayout;
  324. private GUILayoutX keyRowLayout;
  325. private GUILayoutY keyLayout;
  326. private GUILayoutY valueLayout;
  327. private GUILayoutX titleLayout;
  328. private bool localTitleLayout;
  329. private GUIDictionaryFieldBase parent;
  330. protected object key;
  331. protected int depth;
  332. /// <summary>
  333. /// Constructs a new dictionary row object.
  334. /// </summary>
  335. protected GUIDictionaryFieldRow()
  336. {
  337. }
  338. /// <summary>
  339. /// (Re)creates all row GUI elements.
  340. /// </summary>
  341. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  342. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  343. /// <param name="key">Key of the element to create GUI for.</param>
  344. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  345. public void BuildGUI(GUIDictionaryFieldBase parent, GUILayout parentLayout, object key, int depth)
  346. {
  347. this.parent = parent;
  348. this.key = key;
  349. this.depth = depth;
  350. if (rowLayout == null)
  351. rowLayout = parentLayout.AddLayoutY();
  352. if (keyRowLayout == null)
  353. keyRowLayout = rowLayout.AddLayoutX();
  354. if (keyLayout == null)
  355. keyLayout = keyRowLayout.AddLayoutY();
  356. if (valueLayout == null)
  357. valueLayout = rowLayout.AddLayoutY();
  358. GUILayoutX externalTitleLayout = CreateKeyGUI(keyLayout);
  359. CreateValueGUI(valueLayout);
  360. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  361. return;
  362. if (externalTitleLayout != null)
  363. {
  364. localTitleLayout = false;
  365. titleLayout = externalTitleLayout;
  366. }
  367. else
  368. {
  369. GUILayoutY buttonCenter = keyRowLayout.AddLayoutY();
  370. buttonCenter.AddFlexibleSpace();
  371. titleLayout = buttonCenter.AddLayoutX();
  372. buttonCenter.AddFlexibleSpace();
  373. localTitleLayout = true;
  374. }
  375. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  376. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  377. GUIContent editIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Edit));
  378. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  379. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  380. GUIButton editBtn = new GUIButton(editIcon, GUIOption.FixedWidth(30));
  381. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(key);
  382. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(key);
  383. editBtn.OnClick += () => parent.OnEditButtonClicked(key);
  384. titleLayout.AddElement(cloneBtn);
  385. titleLayout.AddElement(deleteBtn);
  386. titleLayout.AddSpace(10);
  387. titleLayout.AddElement(editBtn);
  388. }
  389. /// <summary>
  390. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  391. /// </summary>
  392. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  393. /// <returns>An optional title bar layout that the standard dictionary buttons will be appended to.</returns>
  394. protected abstract GUILayoutX CreateKeyGUI(GUILayoutY layout);
  395. /// <summary>
  396. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  397. /// </summary>
  398. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  399. protected abstract void CreateValueGUI(GUILayoutY layout);
  400. /// <summary>
  401. /// Refreshes the GUI for the dictionary row and checks if anything was modified.
  402. /// </summary>
  403. /// <param name="rebuildGUI">Determines should the field's GUI elements be updated due to modifications.</param>
  404. /// <returns>True if any modifications were made, false otherwise.</returns>
  405. internal protected virtual bool Refresh(out bool rebuildGUI)
  406. {
  407. rebuildGUI = false;
  408. return false;
  409. }
  410. /// <summary>
  411. /// Gets the value contained in this dictionary's row.
  412. /// </summary>
  413. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  414. /// <returns>Value in this dictionary's row.</returns>
  415. protected T GetValue<T>()
  416. {
  417. return (T)parent.GetValue(key);
  418. }
  419. /// <summary>
  420. /// Sets the value contained in this dictionary's row.
  421. /// </summary>
  422. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  423. /// <param name="value">Value to set.</param>
  424. protected void SetValue<T>(T value)
  425. {
  426. parent.SetValue(key, value);
  427. }
  428. /// <summary>
  429. /// Destroys all row GUI elements.
  430. /// </summary>
  431. public void Destroy()
  432. {
  433. rowLayout.Destroy();
  434. rowLayout = null;
  435. }
  436. }
  437. }