InspectableArray.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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; // TODO - This will unnecessarily hold references to the object
  13. private int numArrayElements;
  14. private GUILabel guiLabel;
  15. private GUILayout guiChildLayout;
  16. private GUILayoutY guiContentLayout;
  17. private bool isInitialized;
  18. public InspectableArray(string title, InspectableFieldLayout layout, SerializableProperty property)
  19. : base(title, layout, property)
  20. {
  21. }
  22. private void Initialize(int layoutIndex)
  23. {
  24. if (property.Type != SerializableProperty.FieldType.Array)
  25. return;
  26. guiLabel = new GUILabel(title); // TODO - Add foldout and hook up its callbacks
  27. layout.AddElement(layoutIndex, guiLabel);
  28. guiChildLayout = layout.AddLayoutX(layoutIndex);
  29. guiChildLayout.AddSpace(IndentAmount);
  30. guiContentLayout = guiChildLayout.AddLayoutY();
  31. isInitialized = true;
  32. }
  33. protected override bool IsModified()
  34. {
  35. if (!isInitialized)
  36. return true;
  37. object newPropertyValue = property.GetValue<object>();
  38. if (oldPropertyValue != newPropertyValue)
  39. {
  40. oldPropertyValue = newPropertyValue;
  41. return true;
  42. }
  43. SerializableArray array = property.GetArray();
  44. if (array.GetLength() != numArrayElements)
  45. return true;
  46. return base.IsModified();
  47. }
  48. protected override void Update(int layoutIndex)
  49. {
  50. base.Update(layoutIndex);
  51. if (!isInitialized)
  52. Initialize(layoutIndex);
  53. SerializableArray array = property.GetArray();
  54. int childLayoutIndex = 0;
  55. numArrayElements = array.GetLength();
  56. for (int i = 0; i < numArrayElements; i++)
  57. {
  58. InspectableObjectBase childObj = CreateDefaultInspectable(i + ".", new InspectableFieldLayout(guiContentLayout), array.GetProperty(i));
  59. AddChild(childObj);
  60. childObj.Refresh(childLayoutIndex);
  61. childLayoutIndex += childObj.GetNumLayoutElements();
  62. }
  63. }
  64. }
  65. }