InspectableObjectBase.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 InspectableObjectBase
  10. {
  11. private List<InspectableObjectBase> children = new List<InspectableObjectBase>();
  12. private InspectableObjectBase parent;
  13. protected SerializableProperty property;
  14. protected string title;
  15. public InspectableObjectBase(string title, SerializableProperty property)
  16. {
  17. this.title = title;
  18. this.property = property;
  19. }
  20. protected void AddChild(InspectableObjectBase child)
  21. {
  22. if (child.parent == this)
  23. return;
  24. if (child.parent != null)
  25. child.parent.RemoveChild(child);
  26. children.Add(child);
  27. child.parent = this;
  28. }
  29. protected void RemoveChild(InspectableObjectBase child)
  30. {
  31. children.Remove(child);
  32. child.parent = null;
  33. }
  34. public virtual void Refresh(GUILayout layout)
  35. {
  36. if (IsModified())
  37. Update(layout);
  38. for (int i = 0; i < children.Count; i++)
  39. children[i].Refresh(layout);
  40. }
  41. protected virtual bool IsModified()
  42. {
  43. return false;
  44. }
  45. protected virtual void Update(GUILayout layout)
  46. {
  47. // Do nothing
  48. }
  49. public virtual void Destroy()
  50. {
  51. InspectableObjectBase[] childrenCopy = children.ToArray();
  52. for (int i = 0; i < childrenCopy.Length; i++)
  53. children[i].Destroy();
  54. children.Clear();
  55. if (parent != null)
  56. parent.RemoveChild(this);
  57. }
  58. public static InspectableObjectBase CreateDefaultInspectable(string title, SerializableProperty property)
  59. {
  60. switch (property.Type)
  61. {
  62. case SerializableProperty.FieldType.Int:
  63. return new InspectableInt(title, property);
  64. case SerializableProperty.FieldType.Object:
  65. return new InspectableObject(title, property);
  66. case SerializableProperty.FieldType.Array:
  67. return new InspectableArray(title, property);
  68. // TODO - Add all other types
  69. }
  70. throw new Exception("No inspector exists for the provided field type.");
  71. }
  72. public static InspectableObjectBase CreateCustomInspectable(Type inspectableType, string title, SerializableProperty property)
  73. {
  74. if (!inspectableType.IsSubclassOf(typeof (InspectableObjectBase)))
  75. throw new Exception("Invalid inspector type.");
  76. return (InspectableObjectBase)Activator.CreateInstance(inspectableType, title, property);
  77. }
  78. }
  79. }