InspectableObject.cs 3.9 KB

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