InspectableObject.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 void Refresh(int layoutIndex)
  49. {
  50. base.Refresh(layoutIndex);
  51. int currentIndex = 0;
  52. for (int i = 0; i < children.Count; i++)
  53. {
  54. children[i].Refresh(currentIndex);
  55. currentIndex += children[i].GetNumLayoutElements();
  56. }
  57. }
  58. /// <inheritdoc/>
  59. public override bool ShouldRebuildOnModify()
  60. {
  61. return true;
  62. }
  63. /// <inheritdoc/>
  64. protected internal override void BuildGUI(int index)
  65. {
  66. guiTitleLayout = null;
  67. guiChildLayout = null;
  68. layout.DestroyElements();
  69. if (property.Type != SerializableProperty.FieldType.Object)
  70. return;
  71. if (propertyValue == null)
  72. {
  73. guiChildLayout = null;
  74. guiTitleLayout = layout.AddLayoutX(index);
  75. guiTitleLayout.AddElement(new GUILabel(title));
  76. guiTitleLayout.AddElement(new GUILabel("Empty", GUIOption.FixedWidth(100)));
  77. if (!property.IsValueType)
  78. {
  79. GUIContent createIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Create));
  80. GUIButton createBtn = new GUIButton(createIcon, GUIOption.FixedWidth(30));
  81. createBtn.OnClick += OnCreateButtonClicked;
  82. guiTitleLayout.AddElement(createBtn);
  83. }
  84. }
  85. else
  86. {
  87. guiTitleLayout = layout.AddLayoutX(index);
  88. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  89. guiFoldout.Value = isExpanded;
  90. guiFoldout.OnToggled += OnFoldoutToggled;
  91. guiTitleLayout.AddElement(guiFoldout);
  92. GUIContent clearIcon = new GUIContent(EditorBuiltin.GetInspectorWindowIcon(InspectorWindowIcon.Clear));
  93. GUIButton clearBtn = new GUIButton(clearIcon, GUIOption.FixedWidth(20));
  94. clearBtn.OnClick += OnClearButtonClicked;
  95. guiTitleLayout.AddElement(clearBtn);
  96. if (isExpanded)
  97. {
  98. SerializableObject serializableObject = property.GetObject();
  99. SerializableField[] fields = serializableObject.Fields;
  100. if (fields.Length > 0)
  101. {
  102. guiChildLayout = layout.AddLayoutX(index);
  103. guiChildLayout.AddSpace(IndentAmount);
  104. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  105. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  106. guiIndentLayoutX.AddSpace(IndentAmount);
  107. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  108. guiIndentLayoutY.AddSpace(IndentAmount);
  109. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  110. guiIndentLayoutY.AddSpace(IndentAmount);
  111. guiIndentLayoutX.AddSpace(IndentAmount);
  112. guiChildLayout.AddSpace(IndentAmount);
  113. short backgroundDepth = (short)(Inspector.START_BACKGROUND_DEPTH - depth - 1);
  114. string bgPanelStyle = depth % 2 == 0 ? EditorStyles.InspectorContentBgAlternate : EditorStyles.InspectorContentBg;
  115. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  116. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  117. backgroundPanel.AddElement(inspectorContentBg);
  118. int currentIndex = 0;
  119. foreach (var field in fields)
  120. {
  121. if (!field.Inspectable)
  122. continue;
  123. InspectableField inspectable = CreateInspectable(field.Name, currentIndex, depth + 1,
  124. new InspectableFieldLayout(guiContentLayout), field.GetProperty());
  125. children.Add(inspectable);
  126. currentIndex += inspectable.GetNumLayoutElements();
  127. }
  128. }
  129. }
  130. else
  131. guiChildLayout = null;
  132. }
  133. }
  134. /// <inheritdoc/>
  135. protected internal override void Update(int layoutIndex)
  136. {
  137. foreach (var child in children)
  138. child.Destroy();
  139. children.Clear();
  140. propertyValue = property.GetValue<object>();
  141. BuildGUI(layoutIndex);
  142. forceUpdate = false;
  143. }
  144. /// <summary>
  145. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  146. /// </summary>
  147. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  148. private void OnFoldoutToggled(bool expanded)
  149. {
  150. isExpanded = expanded;
  151. forceUpdate = true;
  152. }
  153. /// <summary>
  154. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new object with default
  155. /// values in the place of the current array.
  156. /// </summary>
  157. private void OnCreateButtonClicked()
  158. {
  159. property.SetValue(property.CreateObjectInstance<object>());
  160. }
  161. /// <summary>
  162. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current object and sets
  163. /// the reference to the object in the parent object to null. This is only relevant for objects of reference types.
  164. /// </summary>
  165. private void OnClearButtonClicked()
  166. {
  167. property.SetValue<object>(null);
  168. }
  169. }
  170. }