InspectableObject.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. public class InspectableObject : InspectableObjectBase
  10. {
  11. private const int IndentAmount = 15;
  12. private object propertyValue;
  13. private GUILayoutX guiChildLayout;
  14. private GUILayoutX guiTitleLayout;
  15. private bool isExpanded;
  16. private bool forceUpdate = true;
  17. public InspectableObject(string title, InspectableFieldLayout layout, SerializableProperty property)
  18. : base(title, layout, property)
  19. {
  20. }
  21. public override GUILayoutX GetTitleLayout()
  22. {
  23. return guiTitleLayout;
  24. }
  25. protected override bool IsModified()
  26. {
  27. if (forceUpdate)
  28. return true;
  29. object newPropertyValue = property.GetValue<object>();
  30. if (propertyValue == null)
  31. return newPropertyValue != null;
  32. if (newPropertyValue == null)
  33. return propertyValue != null;
  34. return base.IsModified();
  35. }
  36. protected override void Update(int index)
  37. {
  38. base.Update(index);
  39. forceUpdate = false;
  40. guiTitleLayout = null;
  41. if (property.Type != SerializableProperty.FieldType.Object)
  42. return;
  43. layout.DestroyElements();
  44. propertyValue = property.GetValue<object>();
  45. if (propertyValue == null)
  46. {
  47. guiChildLayout = null;
  48. guiTitleLayout = layout.AddLayoutX(index);
  49. guiTitleLayout.AddElement(new GUILabel(title, GUIOption.FixedWidth(100)));
  50. guiTitleLayout.AddElement(new GUILabel("Empty"));
  51. if (!property.IsValueType)
  52. {
  53. GUIButton createBtn = new GUIButton("Cr", GUIOption.FixedWidth(20));
  54. createBtn.OnClick += OnCreateButtonClicked;
  55. guiTitleLayout.AddFlexibleSpace();
  56. guiTitleLayout.AddElement(createBtn);
  57. }
  58. }
  59. else
  60. {
  61. guiTitleLayout = layout.AddLayoutX(index);
  62. GUIFoldout guiFoldout = new GUIFoldout(title, GUIOption.FixedWidth(100));
  63. guiFoldout.SetExpanded(isExpanded);
  64. guiFoldout.OnToggled += OnFoldoutToggled;
  65. guiTitleLayout.AddElement(guiFoldout);
  66. GUIButton clearBtn = new GUIButton("Cl", GUIOption.FixedWidth(20));
  67. clearBtn.OnClick += OnClearButtonClicked;
  68. guiTitleLayout.AddFlexibleSpace();
  69. guiTitleLayout.AddElement(clearBtn);
  70. if (isExpanded)
  71. {
  72. guiChildLayout = layout.AddLayoutX(index);
  73. guiChildLayout.AddSpace(IndentAmount);
  74. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  75. SerializableObject serializableObject = property.GetObject();
  76. foreach (var field in serializableObject.fields)
  77. {
  78. if (!field.Inspectable)
  79. continue;
  80. if (field.HasCustomInspector)
  81. AddChild(CreateCustomInspectable(field.CustomInspectorType, field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  82. else
  83. AddChild(CreateDefaultInspectable(field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  84. }
  85. }
  86. else
  87. guiChildLayout = null;
  88. }
  89. }
  90. private void OnFoldoutToggled(bool expanded)
  91. {
  92. isExpanded = expanded;
  93. forceUpdate = true;
  94. }
  95. private void OnCreateButtonClicked()
  96. {
  97. property.SetValue(property.CreateObjectInstance<object>());
  98. }
  99. private void OnClearButtonClicked()
  100. {
  101. property.SetValue<object>(null);
  102. }
  103. }
  104. }