InspectableObjectBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public virtual GUILayoutX GetTitleLayout()
  57. {
  58. return null;
  59. }
  60. protected virtual bool IsModified()
  61. {
  62. return false;
  63. }
  64. protected virtual void Update(int layoutIndex)
  65. {
  66. // Destroy all children as we expect update to rebuild them
  67. InspectableObjectBase[] childrenCopy = children.ToArray();
  68. for (int i = 0; i < childrenCopy.Length; i++)
  69. {
  70. childrenCopy[i].Destroy();
  71. }
  72. children.Clear();
  73. }
  74. protected InspectableObjectBase GetChild(int index)
  75. {
  76. return children[index];
  77. }
  78. protected int GetChildCount()
  79. {
  80. return children.Count;
  81. }
  82. public virtual void Destroy()
  83. {
  84. layout.DestroyElements();
  85. InspectableObjectBase[] childrenCopy = children.ToArray();
  86. for (int i = 0; i < childrenCopy.Length; i++)
  87. childrenCopy[i].Destroy();
  88. children.Clear();
  89. if (parent != null)
  90. parent.RemoveChild(this);
  91. }
  92. public static InspectableObjectBase CreateDefaultInspectable(string title, InspectableFieldLayout layout, SerializableProperty property)
  93. {
  94. switch (property.Type)
  95. {
  96. case SerializableProperty.FieldType.Int:
  97. return new InspectableInt(title, layout, property);
  98. case SerializableProperty.FieldType.Float:
  99. return new InspectableFloat(title, layout, property);
  100. case SerializableProperty.FieldType.Bool:
  101. return new InspectableBool(title, layout, property);
  102. case SerializableProperty.FieldType.Color:
  103. return new InspectableColor(title, layout, property);
  104. case SerializableProperty.FieldType.String:
  105. return new InspectableString(title, layout, property);
  106. case SerializableProperty.FieldType.Vector2:
  107. return new InspectableVector2(title, layout, property);
  108. case SerializableProperty.FieldType.Vector3:
  109. return new InspectableVector3(title, layout, property);
  110. case SerializableProperty.FieldType.Vector4:
  111. return new InspectableVector4(title, layout, property);
  112. case SerializableProperty.FieldType.ResourceRef:
  113. return new InspectableResourceRef(title, layout, property);
  114. case SerializableProperty.FieldType.GameObjectRef:
  115. return new InspectableGameObjectRef(title, layout, property);
  116. case SerializableProperty.FieldType.Object:
  117. return new InspectableObject(title, layout, property);
  118. case SerializableProperty.FieldType.Array:
  119. return new InspectableArray(title, layout, property);
  120. case SerializableProperty.FieldType.List:
  121. return new InspectableList(title, layout, property);
  122. }
  123. throw new Exception("No inspector exists for the provided field type.");
  124. }
  125. public static InspectableObjectBase CreateCustomInspectable(Type inspectableType, string title, InspectableFieldLayout layout, SerializableProperty property)
  126. {
  127. if (!inspectableType.IsSubclassOf(typeof (InspectableObjectBase)))
  128. throw new Exception("Invalid inspector type.");
  129. return (InspectableObjectBase)Activator.CreateInstance(inspectableType, title, property);
  130. }
  131. }
  132. }