InspectableObject.cs 6.9 KB

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