InspectableArray.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 InspectableArray : InspectableObjectBase
  10. {
  11. private const int IndentAmount = 15;
  12. private object oldPropertyValue;
  13. private List<InspectableObjectBase> arrayElements = new List<InspectableObjectBase>();
  14. private GUILabel guiLabel;
  15. private GUILayout guiChildLayout;
  16. private GUILayout guiContentLayout;
  17. private bool isInitialized;
  18. public InspectableArray(string title, SerializableProperty property)
  19. : base(title, property)
  20. {
  21. }
  22. protected void Initialize(GUILayout layout)
  23. {
  24. if (property.Type != SerializableProperty.FieldType.Array)
  25. return;
  26. guiLabel = new GUILabel(title);
  27. layout.AddElement(guiLabel); // TODO - Add foldout and hook up its callbacks
  28. guiChildLayout = layout.AddLayoutX();
  29. guiChildLayout.AddSpace(IndentAmount);
  30. guiContentLayout = guiChildLayout.AddLayoutY();
  31. isInitialized = true;
  32. }
  33. protected override bool IsModified()
  34. {
  35. object newPropertyValue = property.GetValue<object>();
  36. if (oldPropertyValue != newPropertyValue)
  37. {
  38. oldPropertyValue = newPropertyValue;
  39. return true;
  40. }
  41. SerializableArray array = property.GetArray();
  42. if (array.GetLength() != arrayElements.Count)
  43. return true;
  44. return base.IsModified();
  45. }
  46. protected override void Update(GUILayout layout)
  47. {
  48. base.Update(layout);
  49. if (!isInitialized)
  50. Initialize(layout);
  51. // TODO - Update base GUI elements
  52. SerializableArray array = property.GetArray();
  53. for (int i = arrayElements.Count; i < array.GetLength(); i++)
  54. {
  55. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", array.GetProperty(i));
  56. AddChild(childObj);
  57. arrayElements.Add(childObj);
  58. childObj.Refresh(layout);
  59. }
  60. for (int i = array.GetLength(); i < arrayElements.Count; i++)
  61. {
  62. arrayElements[i].Destroy();
  63. }
  64. arrayElements.RemoveRange(array.GetLength(), arrayElements.Count - array.GetLength());
  65. }
  66. public override void Destroy()
  67. {
  68. if (guiLabel != null)
  69. guiLabel.Destroy();
  70. if (guiContentLayout != null)
  71. guiContentLayout.Destroy();
  72. if (guiChildLayout != null)
  73. guiChildLayout.Destroy();
  74. base.Destroy();
  75. }
  76. }
  77. }