GUIDictionaryField.cs 32 KB

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