InspectableObjectBase.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 InspectableFieldLayout layout;
  14. protected SerializableProperty property;
  15. protected string title;
  16. public InspectableObjectBase(string title, InspectableFieldLayout layout, SerializableProperty property)
  17. {
  18. this.layout = layout;
  19. this.title = title;
  20. this.property = property;
  21. }
  22. protected void AddChild(InspectableObjectBase child)
  23. {
  24. if (child.parent == this)
  25. return;
  26. if (child.parent != null)
  27. child.parent.RemoveChild(child);
  28. children.Add(child);
  29. child.parent = this;
  30. }
  31. protected void RemoveChild(InspectableObjectBase child)
  32. {
  33. children.Remove(child);
  34. child.parent = null;
  35. }
  36. public virtual bool Refresh(int layoutIndex)
  37. {
  38. bool anythingModified = false;
  39. if (IsModified())
  40. {
  41. Update(layoutIndex);
  42. anythingModified = true;
  43. }
  44. int currentIndex = 0;
  45. for (int i = 0; i < children.Count; i++)
  46. {
  47. anythingModified |= children[i].Refresh(currentIndex);
  48. currentIndex += children[i].GetNumLayoutElements();
  49. }
  50. return anythingModified;
  51. }
  52. public int GetNumLayoutElements()
  53. {
  54. return layout.GetNumElements();
  55. }
  56. protected virtual bool IsModified()
  57. {
  58. return false;
  59. }
  60. protected virtual void Update(int layoutIndex)
  61. {
  62. // Destroy all children as we expect update to rebuild them
  63. InspectableObjectBase[] childrenCopy = children.ToArray();
  64. for (int i = 0; i < childrenCopy.Length; i++)
  65. {
  66. childrenCopy[i].Destroy();
  67. }
  68. children.Clear();
  69. }
  70. public virtual void Destroy()
  71. {
  72. layout.DestroyElements();
  73. InspectableObjectBase[] childrenCopy = children.ToArray();
  74. for (int i = 0; i < childrenCopy.Length; i++)
  75. childrenCopy[i].Destroy();
  76. children.Clear();
  77. if (parent != null)
  78. parent.RemoveChild(this);
  79. }
  80. public static InspectableObjectBase CreateDefaultInspectable(string title, InspectableFieldLayout layout, SerializableProperty property)
  81. {
  82. switch (property.Type)
  83. {
  84. case SerializableProperty.FieldType.Int:
  85. return new InspectableInt(title, layout, property);
  86. case SerializableProperty.FieldType.Float:
  87. return new InspectableFloat(title, layout, property);
  88. case SerializableProperty.FieldType.Bool:
  89. return new InspectableBool(title, layout, property);
  90. case SerializableProperty.FieldType.Color:
  91. return new InspectableColor(title, layout, property);
  92. case SerializableProperty.FieldType.String:
  93. return new InspectableString(title, layout, property);
  94. case SerializableProperty.FieldType.Vector2:
  95. return new InspectableVector2(title, layout, property);
  96. case SerializableProperty.FieldType.Vector3:
  97. return new InspectableVector3(title, layout, property);
  98. case SerializableProperty.FieldType.Vector4:
  99. return new InspectableVector4(title, layout, property);
  100. case SerializableProperty.FieldType.ResourceRef:
  101. return new InspectableResourceRef(title, layout, property);
  102. case SerializableProperty.FieldType.GameObjectRef:
  103. return new InspectableGameObjectRef(title, layout, property);
  104. case SerializableProperty.FieldType.Object:
  105. return new InspectableObject(title, layout, property);
  106. case SerializableProperty.FieldType.Array:
  107. return new InspectableArray(title, layout, property);
  108. case SerializableProperty.FieldType.List:
  109. return new InspectableList(title, layout, property);
  110. }
  111. throw new Exception("No inspector exists for the provided field type.");
  112. }
  113. public static InspectableObjectBase CreateCustomInspectable(Type inspectableType, string title, InspectableFieldLayout layout, SerializableProperty property)
  114. {
  115. if (!inspectableType.IsSubclassOf(typeof (InspectableObjectBase)))
  116. throw new Exception("Invalid inspector type.");
  117. return (InspectableObjectBase)Activator.CreateInstance(inspectableType, title, property);
  118. }
  119. }
  120. }