InspectableObject.cs 3.7 KB

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