InspectableArray.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. guiChildLayout = layout.AddLayoutX(layoutIndex);
  189. guiChildLayout.AddSpace(IndentAmount);
  190. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  191. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  192. guiIndentLayoutX.AddSpace(IndentAmount);
  193. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  194. guiIndentLayoutY.AddSpace(IndentAmount);
  195. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  196. guiIndentLayoutY.AddSpace(IndentAmount);
  197. guiIndentLayoutX.AddSpace(IndentAmount);
  198. guiChildLayout.AddSpace(IndentAmount);
  199. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  200. string bgPanelStyle = depth % 2 == 0 ? EditorStyles.InspectorContentBgAlternate : EditorStyles.InspectorContentBg;
  201. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  202. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  203. backgroundPanel.AddElement(inspectorContentBg);
  204. for (int i = 0; i < numArrayElements; i++)
  205. {
  206. EntryRow newRow = new EntryRow(guiContentLayout);
  207. rows.Add(newRow);
  208. InspectableField childObj = CreateInspectable(i + ".", depth + 1, new InspectableFieldLayout(newRow.contentLayout), array.GetProperty(i));
  209. AddChild(childObj);
  210. childObj.Refresh(0);
  211. rows[i].Refresh(childObj, i, this);
  212. }
  213. }
  214. else
  215. guiChildLayout = null;
  216. }
  217. }
  218. /// <summary>
  219. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  220. /// </summary>
  221. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  222. private void OnFoldoutToggled(bool expanded)
  223. {
  224. isExpanded = expanded;
  225. forceUpdate = true;
  226. }
  227. /// <summary>
  228. /// Triggered when the user clicks on the resize button on the title bar. Changes the size of the array while
  229. /// preserving existing contents.
  230. /// </summary>
  231. private void OnResizeButtonClicked()
  232. {
  233. int size = guiSizeField.Value; // TODO - Support multi-rank arrays
  234. Array newArray = property.CreateArrayInstance(new int[] {size});
  235. Array array = property.GetValue<Array>();
  236. int maxSize = MathEx.Min(size, array.Length);
  237. for (int i = 0; i < maxSize; i++)
  238. newArray.SetValue(array.GetValue(i), i);
  239. property.SetValue(newArray);
  240. }
  241. /// <summary>
  242. /// Triggered when the user clicks on the delete button next to the array entry. Deletes an element in the array.
  243. /// </summary>
  244. /// <param name="index">Sequential index of the element in the array to remove.</param>
  245. private void OnDeleteButtonClicked(int index)
  246. {
  247. Array array = property.GetValue<Array>();
  248. int size = MathEx.Max(0, array.Length - 1);
  249. Array newArray = property.CreateArrayInstance(new int[] { size });
  250. int destIdx = 0;
  251. for (int i = 0; i < array.Length; i++)
  252. {
  253. if (i == index)
  254. continue;
  255. newArray.SetValue(array.GetValue(i), destIdx);
  256. destIdx++;
  257. }
  258. property.SetValue(newArray);
  259. }
  260. /// <summary>
  261. /// Triggered when the user clicks on the clone button next to the array entry. Clones an element in the array and
  262. /// adds the clone to the back of the array.
  263. /// </summary>
  264. /// <param name="index">Sequential index of the element in the array to clone.</param>
  265. private void OnCloneButtonClicked(int index)
  266. {
  267. SerializableArray array = property.GetArray();
  268. int size = array.GetLength() + 1;
  269. Array newArray = property.CreateArrayInstance(new int[] { size });
  270. object clonedEntry = null;
  271. for (int i = 0; i < array.GetLength(); i++)
  272. {
  273. object value = array.GetProperty(i).GetValue<object>();
  274. newArray.SetValue(value, i);
  275. if (i == index)
  276. {
  277. clonedEntry = array.GetProperty(i).GetValueCopy<object>();
  278. }
  279. }
  280. newArray.SetValue(clonedEntry, size - 1);
  281. property.SetValue(newArray);
  282. }
  283. /// <summary>
  284. /// Triggered when the user clicks on the move up button next to the array entry. Moves an element from the current
  285. /// array index to the one right before it, if not at zero.
  286. /// </summary>
  287. /// <param name="index">Sequential index of the element in the array to move.</param>
  288. private void OnMoveUpButtonClicked(int index)
  289. {
  290. Array array = property.GetValue<Array>();
  291. if ((index - 1) >= 0)
  292. {
  293. object previousEntry = array.GetValue(index - 1);
  294. array.SetValue(array.GetValue(index), index - 1);
  295. array.SetValue(previousEntry, index);
  296. }
  297. }
  298. /// <summary>
  299. /// Triggered when the user clicks on the move down button next to the array entry. Moves an element from the current
  300. /// array index to the one right after it, if the element isn't already the last element.
  301. /// </summary>
  302. /// <param name="index">Sequential index of the element in the array to move.</param>
  303. private void OnMoveDownButtonClicked(int index)
  304. {
  305. Array array = property.GetValue<Array>();
  306. if ((index + 1) < array.Length)
  307. {
  308. object nextEntry = array.GetValue(index + 1);
  309. array.SetValue(array.GetValue(index), index + 1);
  310. array.SetValue(nextEntry, index);
  311. }
  312. }
  313. /// <summary>
  314. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new array with zero
  315. /// elements in the place of the current array.
  316. /// </summary>
  317. private void OnCreateButtonClicked()
  318. {
  319. property.SetValue(property.CreateArrayInstance(new int[1] { 0 }));
  320. }
  321. /// <summary>
  322. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current array and sets
  323. /// the reference to the array in the parent object to null.
  324. /// </summary>
  325. private void OnClearButtonClicked()
  326. {
  327. property.SetValue<object>(null);
  328. }
  329. }
  330. }