InspectableArray.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. /// <summary>
  10. /// Displays GUI for a serializable property containing an array. Array contents are displayed as rows of entries
  11. /// that can be shown, hidden or manipulated.
  12. /// </summary>
  13. public class InspectableArray : InspectableField
  14. {
  15. /// <summary>
  16. /// Contains GUI elements for a single entry in the array.
  17. /// </summary>
  18. private class EntryRow
  19. {
  20. public GUILayoutY contentLayout;
  21. private GUILayoutX rowLayout;
  22. private GUILayoutX titleLayout;
  23. private bool ownsTitleLayout;
  24. /// <summary>
  25. /// Constructs a new entry row object.
  26. /// </summary>
  27. /// <param name="parentLayout">Parent layout that row GUI elements will be added to.</param>
  28. public EntryRow(GUILayout parentLayout)
  29. {
  30. rowLayout = parentLayout.AddLayoutX();
  31. contentLayout = rowLayout.AddLayoutY();
  32. }
  33. /// <summary>
  34. /// Recreates all row GUI elements.
  35. /// </summary>
  36. /// <param name="child">Inspectable field of the array entry.</param>
  37. /// <param name="seqIndex">Sequential index of the array entry.</param>
  38. /// <param name="parent">Parent array object that the entry is contained in.</param>
  39. public void Refresh(InspectableField child, int seqIndex, InspectableArray parent)
  40. {
  41. if (ownsTitleLayout || (titleLayout != null && titleLayout == child.GetTitleLayout()))
  42. return;
  43. titleLayout = child.GetTitleLayout();
  44. if (titleLayout == null)
  45. {
  46. GUILayoutY buttonCenter = rowLayout.AddLayoutY();
  47. buttonCenter.AddFlexibleSpace();
  48. titleLayout = buttonCenter.AddLayoutX();
  49. buttonCenter.AddFlexibleSpace();
  50. ownsTitleLayout = true;
  51. }
  52. GUIContent cloneIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clone));
  53. GUIContent deleteIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Delete));
  54. GUIContent moveUp = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveUp));
  55. GUIContent moveDown = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.MoveDown));
  56. GUIButton cloneBtn = new GUIButton(cloneIcon, GUIOption.FixedWidth(30));
  57. GUIButton deleteBtn = new GUIButton(deleteIcon, GUIOption.FixedWidth(30));
  58. GUIButton moveUpBtn = new GUIButton(moveUp, GUIOption.FixedWidth(30));
  59. GUIButton moveDownBtn = new GUIButton(moveDown, GUIOption.FixedWidth(30));
  60. cloneBtn.OnClick += () => parent.OnCloneButtonClicked(seqIndex);
  61. deleteBtn.OnClick += () => parent.OnDeleteButtonClicked(seqIndex);
  62. moveUpBtn.OnClick += () => parent.OnMoveUpButtonClicked(seqIndex);
  63. moveDownBtn.OnClick += () => parent.OnMoveDownButtonClicked(seqIndex);
  64. titleLayout.AddElement(cloneBtn);
  65. titleLayout.AddElement(deleteBtn);
  66. titleLayout.AddElement(moveUpBtn);
  67. titleLayout.AddElement(moveDownBtn);
  68. }
  69. /// <summary>
  70. /// Destroys all row GUI elements.
  71. /// </summary>
  72. public void Destroy()
  73. {
  74. rowLayout.Destroy();
  75. }
  76. }
  77. private const int IndentAmount = 5;
  78. private object propertyValue; // TODO - This will unnecessarily hold references to the object
  79. private int numArrayElements;
  80. private List<EntryRow> rows = new List<EntryRow>();
  81. private GUIIntField guiSizeField;
  82. private GUILayoutX guiChildLayout;
  83. private GUILayoutX guiTitleLayout;
  84. private bool isExpanded;
  85. private bool forceUpdate = true;
  86. /// <summary>
  87. /// Creates a new inspectable array GUI for the specified property.
  88. /// </summary>
  89. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  90. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field.Some fields may
  91. /// contain other fields, in which case you should increase this value by one.</param>
  92. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  93. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  94. public InspectableArray(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  95. : base(title, depth, layout, property)
  96. {
  97. }
  98. /// <inheritdoc/>
  99. public override GUILayoutX GetTitleLayout()
  100. {
  101. return guiTitleLayout;
  102. }
  103. /// <inheritdoc/>
  104. protected override bool IsModified(out bool rebuildGUI)
  105. {
  106. if (forceUpdate)
  107. {
  108. rebuildGUI = true;
  109. return true;
  110. }
  111. object newPropertyValue = property.GetValue<object>();
  112. if (propertyValue == null)
  113. {
  114. rebuildGUI = newPropertyValue != null;
  115. return rebuildGUI;
  116. }
  117. if (newPropertyValue == null)
  118. {
  119. rebuildGUI = propertyValue != null;
  120. return rebuildGUI;
  121. }
  122. SerializableArray array = property.GetArray();
  123. if (array.GetLength() != numArrayElements)
  124. {
  125. rebuildGUI = true;
  126. return true;
  127. }
  128. return base.IsModified(out rebuildGUI);
  129. }
  130. /// <inheritdoc/>
  131. public override bool Refresh(int layoutIndex, out bool updateGUI)
  132. {
  133. bool anythingModified = false;
  134. if (IsModified(out updateGUI))
  135. {
  136. Update(layoutIndex, updateGUI);
  137. anythingModified = true;
  138. }
  139. for (int i = 0; i < ChildCount; i++)
  140. {
  141. bool dummy;
  142. InspectableField child = GetChild(i);
  143. bool childModified = child.Refresh(0, out dummy);
  144. if (childModified)
  145. rows[i].Refresh(child, i, this);
  146. anythingModified |= childModified;
  147. }
  148. return anythingModified;
  149. }
  150. /// <inheritdoc/>
  151. protected override void BuildGUI(int layoutIndex)
  152. {
  153. guiTitleLayout = null;
  154. guiChildLayout = null;
  155. foreach (var row in rows)
  156. row.Destroy();
  157. rows.Clear();
  158. layout.DestroyElements();
  159. if (property.Type != SerializableProperty.FieldType.Array || property.InternalType.GetArrayRank() != 1) // We don't support multirank arrays
  160. return;
  161. propertyValue = property.GetValue<object>();
  162. if (propertyValue == null)
  163. {
  164. guiChildLayout = null;
  165. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  166. guiTitleLayout.AddElement(new GUILabel(title));
  167. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  168. if (!property.IsValueType)
  169. {
  170. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  171. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  172. createBtn.OnClick += OnCreateButtonClicked;
  173. guiTitleLayout.AddElement(createBtn);
  174. }
  175. numArrayElements = 0;
  176. }
  177. else
  178. {
  179. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  180. guiFoldout.Value = isExpanded;
  181. guiFoldout.OnToggled += OnFoldoutToggled;
  182. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  183. guiSizeField.SetRange(0, int.MaxValue);
  184. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize));
  185. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  186. guiResizeBtn.OnClick += OnResizeButtonClicked;
  187. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  188. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  189. guiClearBtn.OnClick += OnClearButtonClicked;
  190. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  191. guiTitleLayout.AddElement(guiFoldout);
  192. guiTitleLayout.AddElement(guiSizeField);
  193. guiTitleLayout.AddElement(guiResizeBtn);
  194. guiTitleLayout.AddElement(guiClearBtn);
  195. SerializableArray array = property.GetArray();
  196. numArrayElements = array.GetLength();
  197. guiSizeField.Value = numArrayElements;
  198. if (isExpanded)
  199. {
  200. if (numArrayElements > 0)
  201. {
  202. guiChildLayout = layout.AddLayoutX(layoutIndex);
  203. guiChildLayout.AddSpace(IndentAmount);
  204. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  205. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  206. guiIndentLayoutX.AddSpace(IndentAmount);
  207. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  208. guiIndentLayoutY.AddSpace(IndentAmount);
  209. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  210. guiIndentLayoutY.AddSpace(IndentAmount);
  211. guiIndentLayoutX.AddSpace(IndentAmount);
  212. guiChildLayout.AddSpace(IndentAmount);
  213. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  214. string bgPanelStyle = depth % 2 == 0
  215. ? EditorStyles.InspectorContentBgAlternate
  216. : EditorStyles.InspectorContentBg;
  217. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  218. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  219. backgroundPanel.AddElement(inspectorContentBg);
  220. for (int i = 0; i < numArrayElements; i++)
  221. {
  222. EntryRow newRow = new EntryRow(guiContentLayout);
  223. rows.Add(newRow);
  224. InspectableField childObj = CreateInspectable(i + ".", 0, depth + 1,
  225. new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  226. AddChild(childObj);
  227. rows[i].Refresh(childObj, i, this);
  228. }
  229. }
  230. }
  231. else
  232. guiChildLayout = null;
  233. }
  234. }
  235. /// <inheritdoc/>
  236. protected override void Update(int layoutIndex, bool rebuildGUI)
  237. {
  238. base.Update(layoutIndex, true);
  239. BuildGUI(layoutIndex);
  240. forceUpdate = false;
  241. }
  242. /// <summary>
  243. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  244. /// </summary>
  245. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  246. private void OnFoldoutToggled(bool expanded)
  247. {
  248. isExpanded = expanded;
  249. forceUpdate = true;
  250. }
  251. /// <summary>
  252. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the array while
  253. /// preserving existing contents.
  254. /// </summary>
  255. private void OnResizeButtonClicked()
  256. {
  257. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  258. Array newArray = property.CreateArrayInstance(new int[] {size});
  259. Array array = property.GetValue<Array>();
  260. int maxSize = MathEx.Min(size, array.Length);
  261. for (int i = 0; i < maxSize; i++)
  262. newArray.SetValue(array.GetValue(i), i);
  263. property.SetValue(newArray);
  264. }
  265. /// <summary>
  266. /// Triggered when the user clicks on the delete button next to the array entry. Deletes an element in the array.
  267. /// </summary>
  268. /// <param name="index">Sequential index of the element in the array to remove.</param>
  269. private void OnDeleteButtonClicked(int index)
  270. {
  271. Array array = property.GetValue<Array>();
  272. int size = MathEx.Max(0, array.Length - 1);
  273. Array newArray = property.CreateArrayInstance(new int[] { size });
  274. int destIdx = 0;
  275. for (int i = 0; i < array.Length; i++)
  276. {
  277. if (i == index)
  278. continue;
  279. newArray.SetValue(array.GetValue(i), destIdx);
  280. destIdx++;
  281. }
  282. property.SetValue(newArray);
  283. }
  284. /// <summary>
  285. /// Triggered when the user clicks on the clone button next to the array entry. Clones an element in the array and
  286. /// adds the clone to the back of the array.
  287. /// </summary>
  288. /// <param name="index">Sequential index of the element in the array to clone.</param>
  289. private void OnCloneButtonClicked(int index)
  290. {
  291. SerializableArray array = property.GetArray();
  292. int size = array.GetLength() + 1;
  293. Array newArray = property.CreateArrayInstance(new int[] { size });
  294. object clonedEntry = null;
  295. for (int i = 0; i < array.GetLength(); i++)
  296. {
  297. object value = array.GetProperty(i).GetValue<object>();
  298. newArray.SetValue(value, i);
  299. if (i == index)
  300. {
  301. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  302. }
  303. }
  304. newArray.SetValue(clonedEntry, size - 1);
  305. property.SetValue(newArray);
  306. }
  307. /// <summary>
  308. /// Triggered when the user clicks on the move up button next to the array entry. Moves an element from the current
  309. /// array index to the one right before it, if not at zero.
  310. /// </summary>
  311. /// <param name="index">Sequential index of the element in the array to move.</param>
  312. private void OnMoveUpButtonClicked(int index)
  313. {
  314. Array array = property.GetValue<Array>();
  315. if ((index - 1) >= 0)
  316. {
  317. object previousEntry = array.GetValue(index - 1);
  318. array.SetValue(array.GetValue(index), index - 1);
  319. array.SetValue(previousEntry, index);
  320. }
  321. }
  322. /// <summary>
  323. /// Triggered when the user clicks on the move down button next to the array entry. Moves an element from the current
  324. /// array index to the one right after it, if the element isn't already the last element.
  325. /// </summary>
  326. /// <param name="index">Sequential index of the element in the array to move.</param>
  327. private void OnMoveDownButtonClicked(int index)
  328. {
  329. Array array = property.GetValue<Array>();
  330. if ((index + 1) < array.Length)
  331. {
  332. object nextEntry = array.GetValue(index + 1);
  333. array.SetValue(array.GetValue(index), index + 1);
  334. array.SetValue(nextEntry, index);
  335. }
  336. }
  337. /// <summary>
  338. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new array with zero
  339. /// elements in the place of the current array.
  340. /// </summary>
  341. private void OnCreateButtonClicked()
  342. {
  343. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  344. }
  345. /// <summary>
  346. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current array and sets
  347. /// the reference to the array in the parent object to null.
  348. /// </summary>
  349. private void OnClearButtonClicked()
  350. {
  351. property.SetValue<object>(null);
  352. }
  353. }
  354. }