InspectableObject.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. GUIButton createBtn = new GUIButton("Cr", GUIOption.FixedWidth(20));
  69. createBtn.OnClick += OnCreateButtonClicked;
  70. guiTitleLayout.AddElement(createBtn);
  71. }
  72. }
  73. else
  74. {
  75. guiTitleLayout = layout.AddLayoutX(index);
  76. GUIToggle guiFoldout = new GUIToggle(title, EditorStyles.Foldout);
  77. guiFoldout.Value = isExpanded;
  78. guiFoldout.OnToggled += OnFoldoutToggled;
  79. guiTitleLayout.AddElement(guiFoldout);
  80. GUIButton clearBtn = new GUIButton("Cl", GUIOption.FixedWidth(20));
  81. clearBtn.OnClick += OnClearButtonClicked;
  82. guiTitleLayout.AddElement(clearBtn);
  83. if (isExpanded)
  84. {
  85. guiChildLayout = layout.AddLayoutX(index);
  86. guiChildLayout.AddSpace(IndentAmount);
  87. GUIPanel guiContentPanel = guiChildLayout.AddPanel();
  88. GUILayoutX guiIndentLayoutX = guiContentPanel.AddLayoutX();
  89. guiIndentLayoutX.AddSpace(IndentAmount);
  90. GUILayoutY guiIndentLayoutY = guiIndentLayoutX.AddLayoutY();
  91. guiIndentLayoutY.AddSpace(IndentAmount);
  92. GUILayoutY guiContentLayout = guiIndentLayoutY.AddLayoutY();
  93. guiIndentLayoutY.AddSpace(IndentAmount);
  94. guiIndentLayoutX.AddSpace(IndentAmount);
  95. guiChildLayout.AddSpace(IndentAmount);
  96. short backgroundDepth = (short) (Inspector.START_BACKGROUND_DEPTH - depth - 1);
  97. string bgPanelStyle = depth % 2 == 0 ? EditorStyles.InspectorContentBgAlternate : EditorStyles.InspectorContentBg;
  98. GUIPanel backgroundPanel = guiContentPanel.AddPanel(backgroundDepth);
  99. GUITexture inspectorContentBg = new GUITexture(null, bgPanelStyle);
  100. backgroundPanel.AddElement(inspectorContentBg);
  101. SerializableObject serializableObject = property.GetObject();
  102. foreach (var field in serializableObject.Fields)
  103. {
  104. if (!field.Inspectable)
  105. continue;
  106. AddChild(CreateInspectable(field.Name, depth + 1, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  107. }
  108. }
  109. else
  110. guiChildLayout = null;
  111. }
  112. }
  113. /// <summary>
  114. /// Triggered when the user clicks on the expand/collapse toggle in the title bar.
  115. /// </summary>
  116. /// <param name="expanded">Determines whether the contents were expanded or collapsed.</param>
  117. private void OnFoldoutToggled(bool expanded)
  118. {
  119. isExpanded = expanded;
  120. forceUpdate = true;
  121. }
  122. /// <summary>
  123. /// Triggered when the user clicks on the create button on the title bar. Creates a brand new object with default
  124. /// values in the place of the current array.
  125. /// </summary>
  126. private void OnCreateButtonClicked()
  127. {
  128. property.SetValue(property.CreateObjectInstance<object>());
  129. }
  130. /// <summary>
  131. /// Triggered when the user clicks on the clear button on the title bar. Deletes the current object and sets
  132. /// the reference to the object in the parent object to null. This is only relevant for objects of reference types.
  133. /// </summary>
  134. private void OnClearButtonClicked()
  135. {
  136. property.SetValue<object>(null);
  137. }
  138. }
  139. }