InspectableObject.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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));
  50. guiTitleLayout.AddElement(new GUILabel("Empty"));
  51. if (!property.IsValueType)
  52. {
  53. GUIButton createBtn = new GUIButton("Create");
  54. createBtn.OnClick += OnCreateButtonClicked;
  55. guiTitleLayout.AddElement(createBtn);
  56. }
  57. }
  58. else
  59. {
  60. guiTitleLayout = layout.AddLayoutX(index);
  61. GUIFoldout guiFoldout = new GUIFoldout(title);
  62. guiFoldout.SetExpanded(isExpanded);
  63. guiFoldout.OnToggled += OnFoldoutToggled;
  64. guiTitleLayout.AddElement(guiFoldout);
  65. GUIButton clearBtn = new GUIButton("Clear");
  66. clearBtn.OnClick += OnClearButtonClicked;
  67. guiTitleLayout.AddElement(clearBtn);
  68. if (isExpanded)
  69. {
  70. guiChildLayout = layout.AddLayoutX(index);
  71. guiChildLayout.AddSpace(IndentAmount);
  72. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  73. SerializableObject serializableObject = property.GetObject();
  74. foreach (var field in serializableObject.fields)
  75. {
  76. if (!field.Inspectable)
  77. continue;
  78. if (field.HasCustomInspector)
  79. AddChild(CreateCustomInspectable(field.CustomInspectorType, field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  80. else
  81. AddChild(CreateDefaultInspectable(field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  82. }
  83. }
  84. else
  85. guiChildLayout = null;
  86. }
  87. }
  88. private void OnFoldoutToggled(bool expanded)
  89. {
  90. isExpanded = expanded;
  91. forceUpdate = true;
  92. }
  93. private void OnCreateButtonClicked()
  94. {
  95. property.SetValue(property.CreateObjectInstance<object>());
  96. }
  97. private void OnClearButtonClicked()
  98. {
  99. property.SetValue<object>(null);
  100. }
  101. }
  102. }