InspectableObject.cs 3.6 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. guiChildLayout = layout.AddLayoutX(index);
  61. guiChildLayout.SetVisible(isExpanded);
  62. guiChildLayout.AddSpace(IndentAmount);
  63. GUILayoutY guiContentLayout = guiChildLayout.AddLayoutY();
  64. SerializableObject serializableObject = property.GetObject();
  65. foreach (var field in serializableObject.fields)
  66. {
  67. if (!field.Inspectable)
  68. continue;
  69. if (field.HasCustomInspector)
  70. AddChild(CreateCustomInspectable(field.CustomInspectorType, field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  71. else
  72. AddChild(CreateDefaultInspectable(field.Name, new InspectableFieldLayout(guiContentLayout), field.GetProperty()));
  73. }
  74. }
  75. }
  76. private void OnFoldoutToggled(bool expanded)
  77. {
  78. if (guiChildLayout != null)
  79. guiChildLayout.SetVisible(expanded);
  80. isExpanded = expanded;
  81. forceUpdate = true;
  82. }
  83. private void OnCreateButtonClicked()
  84. {
  85. property.SetValue(property.CreateObjectInstance<object>());
  86. }
  87. private void OnClearButtonClicked()
  88. {
  89. property.SetValue<object>(null);
  90. }
  91. }
  92. }