GUIDictionaryField.cs 34 KB

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