InspectableObject.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if (!propertyValue.Equals(newPropertyValue))
  35. return true;
  36. return base.IsModified();
  37. }
  38. protected override void Update(int index)
  39. {
  40. base.Update(index);
  41. forceUpdate = false;
  42. guiTitleLayout = null;
  43. if (property.Type != SerializableProperty.FieldType.Object)
  44. return;
  45. layout.DestroyElements();
  46. propertyValue = property.GetValue<object>();
  47. if (propertyValue == null)
  48. {
  49. guiChildLayout = null;
  50. guiTitleLayout = layout.AddLayoutX(index);
  51. guiTitleLayout.AddElement(new GUILabel(title));
  52. guiTitleLayout.AddElement(new GUILabel("Empty"));
  53. if (!property.IsValueType)
  54. {
  55. GUIButton createBtn = new GUIButton("Create");
  56. createBtn.OnClick += OnCreateButtonClicked;
  57. guiTitleLayout.AddElement(createBtn);
  58. }
  59. }
  60. else
  61. {
  62. guiTitleLayout = layout.AddLayoutX(index);
  63. GUIFoldout guiFoldout = new GUIFoldout(title);
  64. guiFoldout.SetExpanded(isExpanded);
  65. guiFoldout.OnToggled += OnFoldoutToggled;
  66. guiTitleLayout.AddElement(guiFoldout);
  67. GUIButton clearBtn = new GUIButton("Clear");
  68. clearBtn.OnClick += OnClearButtonClicked;
  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. }