2
0

InspectableObject.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System.Collections.Generic;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Displays GUI for a serializable property containing a generic object. Inspectable object fields are displayed
  7. /// in separate rows.
  8. /// </summary>
  9. public class InspectableObject : InspectableField
  10. {
  11. private const int IndentAmount = 5;
  12. private object propertyValue;
  13. private List<InspectableField> children = new List<InspectableField>();
  14. private GUILayoutX guiChildLayout;
  15. private GUILayoutX guiTitleLayout;
  16. private bool isExpanded;
  17. private bool forceUpdate = true;
  18. /// <summary>
  19. /// Creates a new inspectable array GUI for the specified property.
  20. /// </summary>
  21. /// <param name="title">Name of the property, or some other value to set as the title.</param>
  22. /// <param name="depth">Determines how deep within the inspector nesting hierarchy is this field.Some fields may
  23. /// contain other fields, in which case you should increase this value by one.</param>
  24. /// <param name="layout">Parent layout that all the field elements will be added to.</param>
  25. /// <param name="property">Serializable property referencing the array whose contents to display.</param>
  26. public InspectableObject(string title, int depth, InspectableFieldLayout layout, SerializableProperty property)
  27. : base(title, depth, layout, property)
  28. {
  29. }
  30. /// <inheritdoc/>
  31. public override GUILayoutX GetTitleLayout()
  32. {
  33. return guiTitleLayout;
  34. }
  35. /// <inheritdoc/>
  36. public override bool IsModified()
  37. {
  38. if (forceUpdate)
  39. return true;
  40. object newPropertyValue = property.GetValue<object>();
  41. if (propertyValue == null)
  42. return newPropertyValue != null;
  43. if (newPropertyValue == null)
  44. return propertyValue != null;
  45. return base.IsModified();
  46. }
  47. /// <inheritdoc/>
  48. public override bool Refresh(int layoutIndex)
  49. {
  50. bool anythingModified = base.Refresh(layoutIndex);
  51. int currentIndex = 0;
  52. for (int i = 0; i < children.Count; i++)
  53. {
  54. anythingModified |= children[i].Refresh(currentIndex);
  55. currentIndex += children[i].GetNumLayoutElements();
  56. }
  57. return anythingModified;
  58. }
  59. /// <inheritdoc/>
  60. public override bool ShouldRebuildOnModify()
  61. {
  62. return true;
  63. }
  64. /// <inheritdoc/>
  65. protected internal override void BuildGUI(int index)
  66. {
  67. guiTitleLayout = null;
  68. guiChildLayout = null;
  69. layout.DestroyElements();
  70. if (property.Type != SerializableProperty.FieldType.Object)
  71. return;
  72. if (propertyValue == null)
  73. {
  74. guiChildLayout = null;
  75. guiTitleLayout = layout.AddLayoutX(index);
  76. guiTitleLayout.AddElement(new GUILabel(title));
  77. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  78. if (!property.IsValueType)
  79. {
  80. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  81. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  82. createBtn.OnClick += OnCreateButtonClicked;
  83. guiTitleLayout.AddElement(createBtn);
  84. }
  85. }
  86. else
  87. {
  88. guiTitleLayout = layout.AddLayoutX(index);
  89. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  90. guiFoldout.Value = isExpanded;
  91. guiFoldout.OnToggled += OnFoldoutToggled;
  92. guiTitleLayout.AddElement(guiFoldout);
  93. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  94. GUIButton clearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(20));
  95. clearBtn.OnClick += OnClearButtonClicked;
  96. guiTitleLayout.AddElement(clearBtn);
  97. if (isExpanded)
  98. {
  99. SerializableObject serializableObject = property.GetObject();
  100. SerializableField[] fields = serializableObject.Fields;
  101. if (fields.Length > 0)
  102. {
  103. guiChildLayout = layout.AddLayoutX(index);
  104. guiChildLayout.AddSpace(IndentAmount);
  105. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  106. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  107. guiIndentLayoutX.AddSpace(IndentAmount);
  108. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  109. guiIndentLayoutY.AddSpace(IndentAmount);
  110. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  111. guiIndentLayoutY.AddSpace(IndentAmount);
  112. guiIndentLayoutX.AddSpace(IndentAmount);
  113. guiChildLayout.AddSpace(IndentAmount);
  114. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  115. string bgPanelStyle = depth % 2 == 0 ? EditorStyles.InspectorContentBgAlternate : EditorStyles.InspectorContentBg;
  116. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  117. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  118. backgroundPanel.AddElement(inspectorContentBg);
  119. int currentIndex = 0;
  120. foreach (var field in fields)
  121. {
  122. if (!field.Inspectable)
  123. continue;
  124. InspectableField inspectable = CreateInspectable(field.Name, currentIndex, depth + 1,
  125. new InspectableFieldLayout(guiContentLayout), field.GetProperty());
  126. children.Add(inspectable);
  127. currentIndex += inspectable.GetNumLayoutElements();
  128. }
  129. }
  130. }
  131. else
  132. guiChildLayout = null;
  133. }
  134. }
  135. /// <inheritdoc/>
  136. protected internal override void Update(int layoutIndex)
  137. {
  138. foreach (var child in children)
  139. child.Destroy();
  140. children.Clear();
  141. propertyValue = property.GetValue<object>();
  142. BuildGUI(layoutIndex);
  143. forceUpdate = false;
  144. }
  145. /// <summary>
  146. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  147. /// </summary>
  148. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  149. private void OnFoldoutToggled(bool expanded)
  150. {
  151. isExpanded = expanded;
  152. forceUpdate = true;
  153. }
  154. /// <summary>
  155. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new object with default
  156. /// values in the place of the current array.
  157. /// </summary>
  158. private void OnCreateButtonClicked()
  159. {
  160. property.SetValue(property.CreateObjectInstance<object>());
  161. }
  162. /// <summary>
  163. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current object and sets
  164. /// the reference to the object in the parent object to null. This is only relevant for objects of reference types.
  165. /// </summary>
  166. private void OnClearButtonClicked()
  167. {
  168. property.SetValue<object>(null);
  169. }
  170. }
  171. }