InspectableObject.cs 3.2 KB

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