GUIDictionaryField.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 Dictionary<int, GUIDictionaryFieldRow> rows = new Dictionary<int, GUIDictionaryFieldRow>();
  15. protected GUIDictionaryFieldRow editRow;
  16. protected GUILayoutX guiChildLayout;
  17. protected GUILayoutX guiTitleLayout;
  18. protected GUILayoutY guiContentLayout;
  19. protected bool isExpanded;
  20. protected int depth;
  21. private int editRowIdx = -1;
  22. private object editKey;
  23. private object editValue;
  24. private object editOriginalKey;
  25. /// <summary>
  26. /// Constructs a new GUI dictionary.
  27. /// </summary>
  28. protected GUIDictionaryFieldBase()
  29. { }
  30. /// <summary>
  31. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  32. /// </summary>
  33. /// <typeparam name="T">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  34. /// <param name="title">Label to display on the dictionary GUI title.</param>
  35. /// <param name="empty">Should the created field represent a null object.</param>
  36. /// <param name="numRows">Number of rows to create GUI for. Only matters for a non-null dictionary.</param>
  37. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  38. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  39. /// nested containers whose backgrounds are overlaping. Also determines background style,
  40. /// depths divisible by two will use an alternate style.</param>
  41. protected void Update<T>(LocString title, bool empty, int numRows, GUILayout layout,
  42. int depth = 0) where T : GUIDictionaryFieldRow, new()
  43. {
  44. Destroy();
  45. this.depth = depth;
  46. if (empty)
  47. {
  48. guiChildLayout = null;
  49. guiContentLayout = null;
  50. guiTitleLayout = layout.AddLayoutX();
  51. guiTitleLayout.AddElement(new GUILabel(title));
  52. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  53. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  54. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  55. createBtn.OnClick += OnCreateButtonClicked;
  56. guiTitleLayout.AddElement(createBtn);
  57. }
  58. else
  59. {
  60. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  61. guiFoldout.Value = isExpanded;
  62. guiFoldout.OnToggled += OnFoldoutToggled;
  63. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  64. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  65. guiClearBtn.OnClick += OnClearButtonClicked;
  66. GUIContent addIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Add));
  67. GUIButton guiAddBtn = new GUIButton(addIcon, GUIOption.FixedWidth(30));
  68. guiAddBtn.OnClick += OnAddButtonClicked;
  69. guiTitleLayout = layout.AddLayoutX();
  70. guiTitleLayout.AddElement(guiFoldout);
  71. guiTitleLayout.AddElement(guiAddBtn);
  72. guiTitleLayout.AddElement(guiClearBtn);
  73. if (numRows > 0)
  74. {
  75. guiChildLayout = layout.AddLayoutX();
  76. guiChildLayout.AddSpace(IndentAmount);
  77. guiChildLayout.Enabled = isExpanded;
  78. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  79. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  80. guiIndentLayoutX.AddSpace(IndentAmount);
  81. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  82. guiIndentLayoutY.AddSpace(IndentAmount);
  83. guiContentLayout = guiIndentLayoutY.AddLayoutY();
  84. guiIndentLayoutY.AddSpace(IndentAmount);
  85. guiIndentLayoutX.AddSpace(IndentAmount);
  86. guiChildLayout.AddSpace(IndentAmount);
  87. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  88. string bgPanelStyle = depth % 2 == 0
  89. ? EditorStyles.InspectorContentBgAlternate
  90. : EditorStyles.InspectorContentBg;
  91. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  92. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  93. backgroundPanel.AddElement(inspectorContentBg);
  94. for (int i = 0; i < numRows; i++)
  95. {
  96. GUIDictionaryFieldRow newRow = new T();
  97. newRow.BuildGUI(this, guiContentLayout, i, depth);
  98. rows.Add(i, newRow);
  99. }
  100. editRow = new T();
  101. editRow.BuildGUI(this, guiContentLayout, numRows, depth);
  102. editRow.Enabled = false;
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// Returns the layout that is used for positioning the elements in the title bar.
  108. /// </summary>
  109. /// <returns>Horizontal layout for positioning the title bar elements.</returns>
  110. public GUILayoutX GetTitleLayout()
  111. {
  112. return guiTitleLayout;
  113. }
  114. /// <summary>
  115. /// Refreshes contents of all dictionary rows and checks if anything was modified.
  116. /// </summary>
  117. /// <returns>True if any entry in the list was modified, false otherwise.</returns>
  118. public bool Refresh()
  119. {
  120. bool anythingModified = false;
  121. for (int i = 0; i < rows.Count; i++)
  122. {
  123. bool updateGUI;
  124. anythingModified |= rows[i].Refresh(out updateGUI);
  125. if (updateGUI)
  126. rows[i].BuildGUI(this, guiContentLayout, i, depth);
  127. }
  128. if (editRow.Enabled)
  129. {
  130. bool updateGUI;
  131. anythingModified |= editRow.Refresh(out updateGUI);
  132. if (updateGUI)
  133. editRow.BuildGUI(this, guiContentLayout, rows.Count, depth);
  134. }
  135. return anythingModified;
  136. }
  137. /// <summary>
  138. /// Destroys the GUI elements.
  139. /// </summary>
  140. public void Destroy()
  141. {
  142. if (guiTitleLayout != null)
  143. {
  144. guiTitleLayout.Destroy();
  145. guiTitleLayout = null;
  146. }
  147. if (guiChildLayout != null)
  148. {
  149. guiChildLayout.Destroy();
  150. guiChildLayout = null;
  151. }
  152. for (int i = 0; i < rows.Count; i++)
  153. rows[i].Destroy();
  154. rows.Clear();
  155. editRow.Destroy();
  156. }
  157. /// <summary>
  158. /// Checks is the specified row index the temporary edit row.
  159. /// </summary>
  160. /// <param name="rowIdx">Sequential index of the row to check.</param>
  161. /// <returns>True if the index is of an edit row.</returns>
  162. private bool IsTemporaryRow(int rowIdx)
  163. {
  164. return rowIdx == rows.Count;
  165. }
  166. /// <summary>
  167. /// Gets a value of an element at the specified index in the list. Also handles temporary edit fields.
  168. /// </summary>
  169. /// <param name="rowIdx">Sequential index of the row to set the value for.</param>
  170. /// <returns>Value of the list element at the specified key.</returns>
  171. protected internal virtual object GetValueInternal(int rowIdx)
  172. {
  173. if (rowIdx == editRowIdx)
  174. return editValue;
  175. else
  176. return GetValue(GetKey(rowIdx));
  177. }
  178. /// <summary>
  179. /// Sets a value of an element at the specified index in the list. Also handles temporary edit fields.
  180. /// </summary>
  181. /// <param name="rowIdx">Sequential index of the row to set the value for.</param>
  182. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  183. protected internal virtual void SetValueInternal(int rowIdx, object value)
  184. {
  185. if (rowIdx == editRowIdx)
  186. editValue = value;
  187. else
  188. SetValue(GetKey(rowIdx), value);
  189. }
  190. /// <summary>
  191. /// Changes the value of the key of the specified row.
  192. /// </summary>
  193. /// <param name="rowIdx">Sequential index of the row to set the key for.</param>
  194. /// <param name="key">Key to assign to the specified row.</param>
  195. protected internal void SetKey(int rowIdx, object key)
  196. {
  197. if (editRowIdx != rowIdx)
  198. {
  199. Debug.LogError("Cannot change a dictionary row that is not in edit state.");
  200. return;
  201. }
  202. editKey = key;
  203. }
  204. /// <summary>
  205. /// Gets a key for a row at the specified index.
  206. /// </summary>
  207. /// <param name="rowIdx">Sequential index of the row for which to retrieve the key.</param>
  208. /// <returns>Key for a row at the specified index..</returns>
  209. protected internal abstract object GetKey(int rowIdx);
  210. /// <summary>
  211. /// Gets a value of an element at the specified index in the list.
  212. /// </summary>
  213. /// <param name="key">Key of the element whose value to retrieve.</param>
  214. /// <returns>Value of the dictionary entry for the specified key.</returns>
  215. protected internal abstract object GetValue(object key);
  216. /// <summary>
  217. /// Sets a value of an element at the specified index in the list.
  218. /// </summary>
  219. /// <param name="key">Key of the element whose value to set.</param>
  220. /// <param name="value">Value to assign to the element. Caller must ensure it is of valid type.</param>
  221. protected internal abstract void SetValue(object key, object value);
  222. /// <summary>
  223. /// Adds a new entry to the dictionary.
  224. /// </summary>
  225. /// <param name="key">Key of the entry to add.</param>
  226. /// <param name="value">Value of the entry to add.</param>
  227. protected internal abstract void AddEntry(object key, object value);
  228. /// <summary>
  229. /// Removes the specified entry from the dictionary.
  230. /// </summary>
  231. /// <param name="key">Key of the entry to remove.</param>
  232. protected internal abstract void RemoveEntry(object key);
  233. /// <summary>
  234. /// Creates a new empty key object of a valid type that can be used in the dictionary.
  235. /// </summary>
  236. /// <returns>New empty key object.</returns>
  237. protected internal abstract object CreateKey();
  238. /// <summary>
  239. /// Creates a new empty value object of a valid type that can be used in the dictionary.
  240. /// </summary>
  241. /// <returns>New empty value object.</returns>
  242. protected internal abstract object CreateValue();
  243. /// <summary>
  244. /// Checks does the element with the specified key exist in the dictionary.
  245. /// </summary>
  246. /// <param name="key">Key of the element to check for existence.</param>
  247. /// <returns>True if the key exists in the dictionary, false otherwise.</returns>
  248. protected internal abstract bool Contains(object key);
  249. /// <summary>
  250. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  251. /// </summary>
  252. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  253. private void OnFoldoutToggled(bool expanded)
  254. {
  255. isExpanded = expanded;
  256. if (guiChildLayout != null)
  257. guiChildLayout.Enabled = isExpanded;
  258. }
  259. /// <summary>
  260. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new dictionary with zero
  261. /// elements in the place of the current dictionary.
  262. /// </summary>
  263. protected abstract void OnCreateButtonClicked();
  264. /// <summary>
  265. /// Triggered when the user clicks on the add button on the title bar. Adds a new empty element to the dictionary.
  266. /// </summary>
  267. protected virtual void OnAddButtonClicked()
  268. {
  269. if (editRowIdx != -1)
  270. {
  271. DialogBox.Open(
  272. new LocEdString("Edit in progress."),
  273. new LocEdString("You are editing the entry with key \"" + editKey + "\". You cannot add a row " +
  274. "before applying or discarding those changes. Do you wish to apply those changes first?"),
  275. DialogBox.Type.YesNoCancel,
  276. x =>
  277. {
  278. switch (x)
  279. {
  280. case DialogBox.ResultType.Yes:
  281. if (ApplyChanges())
  282. StartAdd();
  283. break;
  284. case DialogBox.ResultType.No:
  285. DiscardChanges();
  286. StartAdd();
  287. break;
  288. }
  289. });
  290. }
  291. else
  292. StartAdd();
  293. }
  294. /// <summary>
  295. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current dictionary object.
  296. /// </summary>
  297. protected abstract void OnClearButtonClicked();
  298. /// <summary>
  299. /// Triggered when the user clicks on the delete button next to a dictionary entry. Deletes an element in the
  300. /// dictionary.
  301. /// </summary>
  302. /// <param name="rowIdx">Sequential row index of the entry that was clicked.</param>
  303. protected internal virtual void OnDeleteButtonClicked(int rowIdx)
  304. {
  305. if (editRowIdx != -1)
  306. DiscardChanges();
  307. else
  308. RemoveEntry(GetKey(rowIdx));
  309. }
  310. /// <summary>
  311. /// Triggered when the user clicks on the clone button next to a dictionary entry. Clones an element and
  312. /// adds the clone to the dictionary.
  313. /// </summary>
  314. /// <param name="rowIdx">Sequential row index of the entry that was clicked.</param>
  315. protected internal virtual void OnCloneButtonClicked(int rowIdx)
  316. {
  317. if (editRowIdx != -1)
  318. {
  319. DialogBox.Open(
  320. new LocEdString("Edit in progress."),
  321. new LocEdString("You are editing the entry with key \"" + editKey + "\". You cannot clone a row " +
  322. "before applying or discarding those changes. Do you wish to apply those changes first?"),
  323. DialogBox.Type.YesNoCancel,
  324. x =>
  325. {
  326. switch (x)
  327. {
  328. case DialogBox.ResultType.Yes:
  329. if (ApplyChanges())
  330. StartClone(rowIdx);
  331. break;
  332. case DialogBox.ResultType.No:
  333. DiscardChanges();
  334. StartClone(rowIdx);
  335. break;
  336. }
  337. });
  338. }
  339. else
  340. StartClone(rowIdx);
  341. }
  342. /// <summary>
  343. /// Triggered when user clicks the edit or apply (depending on state) button next to the dictionary entry. Starts
  344. /// edit mode for the element, if not already in edit mode. Applies edit mode changes if already in edit mode.
  345. /// </summary>
  346. /// <param name="rowIdx">Sequential row index of the entry that was clicked.</param>
  347. protected internal virtual void OnEditButtonClicked(int rowIdx)
  348. {
  349. if (editRowIdx == rowIdx)
  350. ApplyChanges();
  351. else
  352. {
  353. if (editRowIdx != -1)
  354. {
  355. DialogBox.Open(
  356. new LocEdString("Edit in progress."),
  357. new LocEdString("You are already editing the entry with key \"" + editKey + "\". You cannot edit " +
  358. "another row before applying or discarding those changes. Do you wish to apply those changes first?"),
  359. DialogBox.Type.YesNoCancel,
  360. x =>
  361. {
  362. switch (x)
  363. {
  364. case DialogBox.ResultType.Yes:
  365. if (ApplyChanges())
  366. StartEdit(rowIdx);
  367. break;
  368. case DialogBox.ResultType.No:
  369. DiscardChanges();
  370. StartEdit(rowIdx);
  371. break;
  372. }
  373. });
  374. }
  375. else
  376. StartEdit(rowIdx);
  377. }
  378. }
  379. /// <summary>
  380. /// Starts an edit operation on a row with the specified key. Allows the user to change the key of the specified row.
  381. /// Caller must ensure no edit operation is already in progress.
  382. /// </summary>
  383. /// <param name="rowIdx">Sequential row index of the entry to edit.</param>
  384. private void StartEdit(int rowIdx)
  385. {
  386. object key = GetKey(rowIdx);
  387. editKey = SerializableUtility.Clone(key);
  388. editValue = SerializableUtility.Clone(GetValue(key));
  389. editOriginalKey = key;
  390. editRowIdx = rowIdx;
  391. rows[rowIdx].EditMode = true;
  392. }
  393. /// <summary>
  394. /// Starts an add operation. Adds a new key/value pair and allows the user to set them up in a temporary row
  395. /// before inserting them into the dictionary. Caller must ensure no edit operation is already in progress.
  396. /// </summary>
  397. private void StartAdd()
  398. {
  399. editKey = CreateKey();
  400. editValue = CreateValue();
  401. editOriginalKey = null;
  402. editRowIdx = rows.Count;
  403. editRow.Enabled = true;
  404. editRow.EditMode = true;
  405. }
  406. /// <summary>
  407. /// Starts a clone operation. Adds a new key/value pair by cloning an existing one. Allows the user to modify the
  408. /// new pair in a temporary row before inserting them into the dictionary. Caller must ensure no edit operation is
  409. /// already in progress.
  410. /// </summary>
  411. /// <param name="key">Key of the row to clone.</param>
  412. private void StartClone(object key)
  413. {
  414. editKey = SerializableUtility.Clone(key);
  415. editValue = SerializableUtility.Clone(GetValue(key));
  416. editOriginalKey = null;
  417. editRowIdx = rows.Count;
  418. editRow.Enabled = true;
  419. editRow.EditMode = true;
  420. }
  421. /// <summary>
  422. /// Attempts to apply any changes made to the currently edited row.
  423. /// </summary>
  424. /// <returns>True if the changes were successfully applied, false if the new key already exists in the dictionary.
  425. /// </returns>
  426. private bool ApplyChanges()
  427. {
  428. if (editRowIdx == -1)
  429. return true;
  430. if (Contains(editKey))
  431. {
  432. DialogBox.Open(
  433. new LocEdString("Key already exists."),
  434. new LocEdString("Cannot add a key \"" + editKey + "\" to dictionary. Key already exists"),
  435. DialogBox.Type.OK);
  436. return false;
  437. }
  438. else
  439. {
  440. if (editOriginalKey != null)
  441. RemoveEntry(editOriginalKey);
  442. if (IsTemporaryRow(editRowIdx))
  443. {
  444. editRow.EditMode = false;
  445. editRow.Enabled = false;
  446. }
  447. else
  448. {
  449. rows[editRowIdx].EditMode = false;
  450. }
  451. AddEntry(editKey, editValue);
  452. editKey = null;
  453. editValue = null;
  454. editOriginalKey = null;
  455. editRowIdx = -1;
  456. return true;
  457. }
  458. }
  459. /// <summary>
  460. /// Cancels any changes made on the currently edited row.
  461. /// </summary>
  462. private void DiscardChanges()
  463. {
  464. if (editRowIdx != -1)
  465. {
  466. editKey = null;
  467. editValue = null;
  468. editOriginalKey = null;
  469. editRow.Enabled = false;
  470. editRowIdx = -1;
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// Creates GUI elements that allow viewing and manipulation of a <see cref="Dictionary{TKey,TValue}"/>. When constructing the
  476. /// object user can provide a custom type that manages GUI for individual dictionary elements.
  477. /// </summary>
  478. /// <typeparam name="Key">Type of key used by the dictionary.</typeparam>
  479. /// <typeparam name="Value">Type of value stored in the dictionary.</typeparam>
  480. public class GUIDictionaryField<Key, Value> : GUIDictionaryFieldBase
  481. {
  482. public delegate int SortDictionaryDelegate(Key a, Key b);
  483. /// <summary>
  484. /// Triggered when the reference array has been changed. This does not include changes that only happen to its
  485. /// internal elements.
  486. /// </summary>
  487. public Action<Dictionary<Key, Value>> OnChanged;
  488. /// <summary>
  489. /// Triggered when an element in the list has been changed.
  490. /// </summary>
  491. public Action<Key> OnValueChanged;
  492. /// <summary>
  493. /// Optional method that will be used for sorting the elements in the dictionary.
  494. /// </summary>
  495. public SortDictionaryDelegate SortMethod;
  496. /// <summary>
  497. /// Array object whose contents are displayed.
  498. /// </summary>
  499. public Dictionary<Key, Value> Dictionary { get { return dictionary; } }
  500. protected Dictionary<Key, Value> dictionary;
  501. private List<Key> orderedKeys = new List<Key>();
  502. /// <summary>
  503. /// Constructs a new empty dictionary GUI.
  504. /// </summary>
  505. public GUIDictionaryField()
  506. { }
  507. /// <summary>
  508. /// Updates the GUI dictionary contents. Must be called at least once in order for the contents to be populated.
  509. /// </summary>
  510. /// <typeparam name="RowType">Type of rows that are used to handle GUI for individual dictionary elements.</typeparam>
  511. /// <param name="title">Label to display on the list GUI title.</param>
  512. /// <param name="dictionary">Object containing the data. Can be null.</param>
  513. /// <param name="layout">Layout to which to append the list GUI elements to.</param>
  514. /// <param name="depth">Determines at which depth to render the background. Useful when you have multiple
  515. /// nested containers whose backgrounds are overlaping. Also determines background style,
  516. /// depths divisible by two will use an alternate style.</param>
  517. public void Update<RowType>(LocString title, Dictionary<Key, Value> dictionary,
  518. GUILayout layout, int depth = 0)
  519. where RowType : GUIDictionaryFieldRow, new()
  520. {
  521. this.dictionary = dictionary;
  522. if (dictionary != null)
  523. base.Update<RowType>(title, false, dictionary.Count, layout, depth);
  524. else
  525. base.Update<RowType>(title, true, 0, layout, depth);
  526. UpdateKeys();
  527. }
  528. /// <summary>
  529. /// Updates the ordered set of keys used for mapping sequential indexes to keys. Should be called whenever a
  530. /// dictionary key changes.
  531. /// </summary>
  532. private void UpdateKeys()
  533. {
  534. orderedKeys.Clear();
  535. if (dictionary != null)
  536. {
  537. foreach (var KVP in dictionary)
  538. orderedKeys.Add(KVP.Key);
  539. if (SortMethod != null)
  540. orderedKeys.Sort((x,y) => SortMethod(x, y));
  541. else
  542. orderedKeys.Sort();
  543. }
  544. }
  545. /// <inheritdoc/>
  546. protected internal override object GetKey(int rowIdx)
  547. {
  548. return orderedKeys[rowIdx];
  549. }
  550. /// <inheritdoc/>
  551. protected internal override object GetValue(object key)
  552. {
  553. return dictionary[(Key)key];
  554. }
  555. /// <inheritdoc/>
  556. protected internal override void SetValue(object key, object value)
  557. {
  558. dictionary[(Key)key] = (Value)value;
  559. if (OnValueChanged != null)
  560. OnValueChanged((Key)key);
  561. }
  562. /// <inheritdoc/>
  563. protected internal override bool Contains(object key)
  564. {
  565. return dictionary.ContainsKey((Key)key); ;
  566. }
  567. /// <inheritdoc/>
  568. protected internal override void AddEntry(object key, object value)
  569. {
  570. dictionary[(Key)key] = (Value)value;
  571. if (OnChanged != null)
  572. OnChanged(dictionary);
  573. UpdateKeys();
  574. }
  575. /// <inheritdoc/>
  576. protected internal override void RemoveEntry(object key)
  577. {
  578. dictionary.Remove((Key) key);
  579. if (OnChanged != null)
  580. OnChanged(dictionary);
  581. UpdateKeys();
  582. }
  583. /// <inheritdoc/>
  584. protected internal override object CreateKey()
  585. {
  586. return default(Key);
  587. }
  588. /// <inheritdoc/>
  589. protected internal override object CreateValue()
  590. {
  591. return default(Value);
  592. }
  593. /// <inheritdoc/>
  594. protected override void OnCreateButtonClicked()
  595. {
  596. dictionary = new Dictionary<Key, Value>();
  597. if (OnChanged != null)
  598. OnChanged(dictionary);
  599. UpdateKeys();
  600. }
  601. /// <inheritdoc/>
  602. protected override void OnClearButtonClicked()
  603. {
  604. dictionary = null;
  605. if (OnChanged != null)
  606. OnChanged(dictionary);
  607. UpdateKeys();
  608. }
  609. }
  610. /// <summary>
  611. /// Contains GUI elements for a single entry in a dictionary.
  612. /// </summary>
  613. public abstract class GUIDictionaryFieldRow
  614. {
  615. private GUILayoutY rowLayout;
  616. private GUILayoutX keyRowLayout;
  617. private GUILayoutY keyLayout;
  618. private GUILayoutY valueLayout;
  619. private GUILayoutX titleLayout;
  620. private GUIButton deleteBtn;
  621. private GUIButton editBtn;
  622. private bool localTitleLayout;
  623. private bool enabled = true;
  624. private GUIDictionaryFieldBase parent;
  625. protected int rowIdx;
  626. protected int depth;
  627. /// <summary>
  628. /// Determines is the row currently being displayed.
  629. /// </summary>
  630. internal bool Enabled
  631. {
  632. get { return enabled; }
  633. set { enabled = value; rowLayout.Enabled = value; }
  634. }
  635. /// <summary>
  636. /// Enables or disables the row's edit mode. This determines what type of buttons are shown on the row title bar.
  637. /// </summary>
  638. internal bool EditMode
  639. {
  640. set
  641. {
  642. if (value)
  643. {
  644. GUIContent cancelIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Cancel));
  645. GUIContent applyIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Apply));
  646. deleteBtn.SetContent(cancelIcon);
  647. editBtn.SetContent(applyIcon);
  648. }
  649. else
  650. {
  651. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  652. GUIContent editIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Edit));
  653. deleteBtn.SetContent(deleteIcon);
  654. editBtn.SetContent(editIcon);
  655. }
  656. }
  657. }
  658. /// <summary>
  659. /// Constructs a new dictionary row object.
  660. /// </summary>
  661. protected GUIDictionaryFieldRow()
  662. {
  663. }
  664. /// <summary>
  665. /// (Re)creates all row GUI elements.
  666. /// </summary>
  667. /// <param name="parent">Parent array GUI object that the entry is contained in.</param>
  668. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  669. /// <param name="rowIdx">Sequential index of the row.</param>
  670. /// <param name="depth">Determines the depth at which the element is rendered.</param>
  671. internal void BuildGUI(GUIDictionaryFieldBase parent, GUILayout parentLayout, int rowIdx, int depth)
  672. {
  673. this.parent = parent;
  674. this.rowIdx = rowIdx;
  675. this.depth = depth;
  676. if (rowLayout == null)
  677. rowLayout = parentLayout.AddLayoutY();
  678. if (keyRowLayout == null)
  679. keyRowLayout = rowLayout.AddLayoutX();
  680. if (keyLayout == null)
  681. keyLayout = keyRowLayout.AddLayoutY();
  682. if (valueLayout == null)
  683. valueLayout = rowLayout.AddLayoutY();
  684. GUILayoutX externalTitleLayout = CreateKeyGUI(keyLayout);
  685. CreateValueGUI(valueLayout);
  686. if (localTitleLayout || (titleLayout != null && titleLayout == externalTitleLayout))
  687. return;
  688. if (externalTitleLayout != null)
  689. {
  690. localTitleLayout = false;
  691. titleLayout = externalTitleLayout;
  692. }
  693. else
  694. {
  695. GUILayoutY buttonCenter = keyRowLayout.AddLayoutY();
  696. buttonCenter.AddFlexibleSpace();
  697. titleLayout = buttonCenter.AddLayoutX();
  698. buttonCenter.AddFlexibleSpace();
  699. localTitleLayout = true;
  700. }
  701. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  702. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  703. GUIContent editIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Edit));
  704. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  705. deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  706. editBtn = new GUIButton(editIcon, GUIOption.FixedWidth(30));
  707. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(rowIdx);
  708. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(rowIdx);
  709. editBtn.OnClick += () => parent.OnEditButtonClicked(rowIdx);
  710. titleLayout.AddElement(cloneBtn);
  711. titleLayout.AddElement(deleteBtn);
  712. titleLayout.AddSpace(10);
  713. titleLayout.AddElement(editBtn);
  714. }
  715. /// <summary>
  716. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  717. /// </summary>
  718. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  719. /// <returns>An optional title bar layout that the standard dictionary buttons will be appended to.</returns>
  720. protected abstract GUILayoutX CreateKeyGUI(GUILayoutY layout);
  721. /// <summary>
  722. /// Creates GUI elements specific to type in the key portion of a dictionary entry.
  723. /// </summary>
  724. /// <param name="layout">Layout to insert the row GUI elements to.</param>
  725. protected abstract void CreateValueGUI(GUILayoutY layout);
  726. /// <summary>
  727. /// Refreshes the GUI for the dictionary row and checks if anything was modified.
  728. /// </summary>
  729. /// <param name="rebuildGUI">Determines should the field's GUI elements be updated due to modifications.</param>
  730. /// <returns>True if any modifications were made, false otherwise.</returns>
  731. internal protected virtual bool Refresh(out bool rebuildGUI)
  732. {
  733. rebuildGUI = false;
  734. return false;
  735. }
  736. /// <summary>
  737. /// Gets the key contained in this dictionary's row.
  738. /// </summary>
  739. /// <typeparam name="T">Type of the key. Must match the dictionary's element type.</typeparam>
  740. /// <returns>Key in this dictionary's row.</returns>
  741. protected T GetKey<T>()
  742. {
  743. return (T)parent.GetKey(rowIdx);
  744. }
  745. /// <summary>
  746. /// Sets the key for in this dictionary's row.
  747. /// </summary>
  748. /// <typeparam name="T">Type of the key. Must match the dictionary's element type.</typeparam>
  749. /// <param name="key">Key to assign to this row.</param>
  750. protected void SetKey<T>(T key)
  751. {
  752. parent.SetKey(rowIdx, key);
  753. }
  754. /// <summary>
  755. /// Gets the value contained in this dictionary's row.
  756. /// </summary>
  757. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  758. /// <returns>Value in this dictionary's row.</returns>
  759. protected T GetValue<T>()
  760. {
  761. return (T)parent.GetValueInternal(rowIdx);
  762. }
  763. /// <summary>
  764. /// Sets the value contained in this dictionary's row.
  765. /// </summary>
  766. /// <typeparam name="T">Type of the value. Must match the dictionary's element type.</typeparam>
  767. /// <param name="value">Value to set.</param>
  768. protected void SetValue<T>(T value)
  769. {
  770. parent.SetValueInternal(rowIdx, value);
  771. }
  772. /// <summary>
  773. /// Destroys all row GUI elements.
  774. /// </summary>
  775. public void Destroy()
  776. {
  777. rowLayout.Destroy();
  778. rowLayout = null;
  779. }
  780. }
  781. }