InspectableArray.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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()
  105. {
  106. if (forceUpdate)
  107. return true;
  108. object newPropertyValue = property.GetValue<object>();
  109. if (propertyValue == null)
  110. return newPropertyValue != null;
  111. if (newPropertyValue == null)
  112. return propertyValue != null;
  113. SerializableArray array = property.GetArray();
  114. if (array.GetLength() != numArrayElements)
  115. return true;
  116. return base.IsModified();
  117. }
  118. /// <inheritdoc/>
  119. public override bool Refresh(int layoutIndex)
  120. {
  121. bool anythingModified = false;
  122. if (IsModified())
  123. {
  124. Update(layoutIndex);
  125. anythingModified = true;
  126. }
  127. for (int i = 0; i < ChildCount; i++)
  128. {
  129. InspectableField child = GetChild(i);
  130. bool childModified = child.Refresh(0);
  131. if (childModified)
  132. rows[i].Refresh(child, i, this);
  133. anythingModified |= childModified;
  134. }
  135. return anythingModified;
  136. }
  137. /// <inheritdoc/>
  138. protected override void Update(int layoutIndex)
  139. {
  140. base.Update(layoutIndex);
  141. forceUpdate = false;
  142. guiTitleLayout = null;
  143. if (property.Type != SerializableProperty.FieldType.Array || property.InternalType.GetArrayRank() != 1) // We don't support multirank arrays
  144. return;
  145. foreach (var row in rows)
  146. row.Destroy();
  147. rows.Clear();
  148. layout.DestroyElements();
  149. propertyValue = property.GetValue<object>();
  150. if (propertyValue == null)
  151. {
  152. guiChildLayout = null;
  153. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  154. guiTitleLayout.AddElement(new GUILabel(title));
  155. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  156. if (!property.IsValueType)
  157. {
  158. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  159. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  160. createBtn.OnClick += OnCreateButtonClicked;
  161. guiTitleLayout.AddElement(createBtn);
  162. }
  163. numArrayElements = 0;
  164. }
  165. else
  166. {
  167. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  168. guiFoldout.Value = isExpanded;
  169. guiFoldout.OnToggled += OnFoldoutToggled;
  170. guiSizeField = new GUIIntField("", GUIOption.FixedWidth(50));
  171. guiSizeField.SetRange(0, int.MaxValue);
  172. GUIContent resizeIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Resize));
  173. GUIButton guiResizeBtn = new GUIButton(resizeIcon, GUIOption.FixedWidth(30));
  174. guiResizeBtn.OnClick += OnResizeButtonClicked;
  175. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  176. GUIButton guiClearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(30));
  177. guiClearBtn.OnClick += OnClearButtonClicked;
  178. guiTitleLayout = layout.AddLayoutX(layoutIndex);
  179. guiTitleLayout.AddElement(guiFoldout);
  180. guiTitleLayout.AddElement(guiSizeField);
  181. guiTitleLayout.AddElement(guiResizeBtn);
  182. guiTitleLayout.AddElement(guiClearBtn);
  183. SerializableArray array = property.GetArray();
  184. numArrayElements = array.GetLength();
  185. guiSizeField.Value = numArrayElements;
  186. if (isExpanded)
  187. {
  188. if (numArrayElements > 0)
  189. {
  190. guiChildLayout = layout.AddLayoutX(layoutIndex);
  191. guiChildLayout.AddSpace(IndentAmount);
  192. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  193. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  194. guiIndentLayoutX.AddSpace(IndentAmount);
  195. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  196. guiIndentLayoutY.AddSpace(IndentAmount);
  197. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  198. guiIndentLayoutY.AddSpace(IndentAmount);
  199. guiIndentLayoutX.AddSpace(IndentAmount);
  200. guiChildLayout.AddSpace(IndentAmount);
  201. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  202. string bgPanelStyle = depth % 2 == 0
  203. ? EditorStyles.InspectorContentBgAlternate
  204. : EditorStyles.InspectorContentBg;
  205. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  206. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  207. backgroundPanel.AddElement(inspectorContentBg);
  208. for (int i = 0; i < numArrayElements; i++)
  209. {
  210. EntryRow newRow = new EntryRow(guiContentLayout);
  211. rows.Add(newRow);
  212. InspectableField childObj = CreateInspectable(i + ".", depth + 1,
  213. new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  214. AddChild(childObj);
  215. childObj.Refresh(0);
  216. rows[i].Refresh(childObj, i, this);
  217. }
  218. }
  219. }
  220. else
  221. guiChildLayout = null;
  222. }
  223. }
  224. /// <summary>
  225. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  226. /// </summary>
  227. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  228. private void OnFoldoutToggled(bool expanded)
  229. {
  230. isExpanded = expanded;
  231. forceUpdate = true;
  232. }
  233. /// <summary>
  234. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the array while
  235. /// preserving existing contents.
  236. /// </summary>
  237. private void OnResizeButtonClicked()
  238. {
  239. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  240. Array newArray = property.CreateArrayInstance(new int[] {size});
  241. Array array = property.GetValue<Array>();
  242. int maxSize = MathEx.Min(size, array.Length);
  243. for (int i = 0; i < maxSize; i++)
  244. newArray.SetValue(array.GetValue(i), i);
  245. property.SetValue(newArray);
  246. }
  247. /// <summary>
  248. /// Triggered when the user clicks on the delete button next to the array entry. Deletes an element in the array.
  249. /// </summary>
  250. /// <param name="index">Sequential index of the element in the array to remove.</param>
  251. private void OnDeleteButtonClicked(int index)
  252. {
  253. Array array = property.GetValue<Array>();
  254. int size = MathEx.Max(0, array.Length - 1);
  255. Array newArray = property.CreateArrayInstance(new int[] { size });
  256. int destIdx = 0;
  257. for (int i = 0; i < array.Length; i++)
  258. {
  259. if (i == index)
  260. continue;
  261. newArray.SetValue(array.GetValue(i), destIdx);
  262. destIdx++;
  263. }
  264. property.SetValue(newArray);
  265. }
  266. /// <summary>
  267. /// Triggered when the user clicks on the clone button next to the array entry. Clones an element in the array and
  268. /// adds the clone to the back of the array.
  269. /// </summary>
  270. /// <param name="index">Sequential index of the element in the array to clone.</param>
  271. private void OnCloneButtonClicked(int index)
  272. {
  273. SerializableArray array = property.GetArray();
  274. int size = array.GetLength() + 1;
  275. Array newArray = property.CreateArrayInstance(new int[] { size });
  276. object clonedEntry = null;
  277. for (int i = 0; i < array.GetLength(); i++)
  278. {
  279. object value = array.GetProperty(i).GetValue<object>();
  280. newArray.SetValue(value, i);
  281. if (i == index)
  282. {
  283. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  284. }
  285. }
  286. newArray.SetValue(clonedEntry, size - 1);
  287. property.SetValue(newArray);
  288. }
  289. /// <summary>
  290. /// Triggered when the user clicks on the move up button next to the array entry. Moves an element from the current
  291. /// array index to the one right before it, if not at zero.
  292. /// </summary>
  293. /// <param name="index">Sequential index of the element in the array to move.</param>
  294. private void OnMoveUpButtonClicked(int index)
  295. {
  296. Array array = property.GetValue<Array>();
  297. if ((index - 1) >= 0)
  298. {
  299. object previousEntry = array.GetValue(index - 1);
  300. array.SetValue(array.GetValue(index), index - 1);
  301. array.SetValue(previousEntry, index);
  302. }
  303. }
  304. /// <summary>
  305. /// Triggered when the user clicks on the move down button next to the array entry. Moves an element from the current
  306. /// array index to the one right after it, if the element isn't already the last element.
  307. /// </summary>
  308. /// <param name="index">Sequential index of the element in the array to move.</param>
  309. private void OnMoveDownButtonClicked(int index)
  310. {
  311. Array array = property.GetValue<Array>();
  312. if ((index + 1) < array.Length)
  313. {
  314. object nextEntry = array.GetValue(index + 1);
  315. array.SetValue(array.GetValue(index), index + 1);
  316. array.SetValue(nextEntry, index);
  317. }
  318. }
  319. /// <summary>
  320. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new array with zero
  321. /// elements in the place of the current array.
  322. /// </summary>
  323. private void OnCreateButtonClicked()
  324. {
  325. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  326. }
  327. /// <summary>
  328. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current array and sets
  329. /// the reference to the array in the parent object to null.
  330. /// </summary>
  331. private void OnClearButtonClicked()
  332. {
  333. property.SetValue<object>(null);
  334. }
  335. }
  336. }