| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using BansheeEngine;
-
- namespace BansheeEditor
- {
- public class InspectableObjectBase
- {
- private List<InspectableObjectBase> children = new List<InspectableObjectBase>();
- private InspectableObjectBase parent;
-
- protected InspectableFieldLayout layout;
- protected SerializableProperty property;
- protected string title;
- public InspectableObjectBase(string title, InspectableFieldLayout layout, SerializableProperty property)
- {
- this.layout = layout;
- this.title = title;
- this.property = property;
- }
- protected void AddChild(InspectableObjectBase child)
- {
- if (child.parent == this)
- return;
- if (child.parent != null)
- child.parent.RemoveChild(child);
- children.Add(child);
- child.parent = this;
- }
- protected void RemoveChild(InspectableObjectBase child)
- {
- children.Remove(child);
- child.parent = null;
- }
- public virtual bool Refresh(int layoutIndex)
- {
- bool anythingModified = false;
- if (IsModified())
- {
- Update(layoutIndex);
- anythingModified = true;
- }
- int currentIndex = 0;
- for (int i = 0; i < children.Count; i++)
- {
- anythingModified |= children[i].Refresh(currentIndex);
- currentIndex += children[i].GetNumLayoutElements();
- }
- return anythingModified;
- }
- public int GetNumLayoutElements()
- {
- return layout.GetNumElements();
- }
- public virtual GUILayoutX GetTitleLayout()
- {
- return null;
- }
- protected virtual bool IsModified()
- {
- return false;
- }
- protected virtual void Update(int layoutIndex)
- {
- // Destroy all children as we expect update to rebuild them
- InspectableObjectBase[] childrenCopy = children.ToArray();
- for (int i = 0; i < childrenCopy.Length; i++)
- {
- childrenCopy[i].Destroy();
- }
- children.Clear();
- }
- protected InspectableObjectBase GetChild(int index)
- {
- return children[index];
- }
- protected int GetChildCount()
- {
- return children.Count;
- }
- public virtual void Destroy()
- {
- layout.DestroyElements();
- InspectableObjectBase[] childrenCopy = children.ToArray();
- for (int i = 0; i < childrenCopy.Length; i++)
- childrenCopy[i].Destroy();
- children.Clear();
- if (parent != null)
- parent.RemoveChild(this);
- }
- public static InspectableObjectBase CreateDefaultInspectable(string title, InspectableFieldLayout layout, SerializableProperty property)
- {
- switch (property.Type)
- {
- case SerializableProperty.FieldType.Int:
- return new InspectableInt(title, layout, property);
- case SerializableProperty.FieldType.Float:
- return new InspectableFloat(title, layout, property);
- case SerializableProperty.FieldType.Bool:
- return new InspectableBool(title, layout, property);
- case SerializableProperty.FieldType.Color:
- return new InspectableColor(title, layout, property);
- case SerializableProperty.FieldType.String:
- return new InspectableString(title, layout, property);
- case SerializableProperty.FieldType.Vector2:
- return new InspectableVector2(title, layout, property);
- case SerializableProperty.FieldType.Vector3:
- return new InspectableVector3(title, layout, property);
- case SerializableProperty.FieldType.Vector4:
- return new InspectableVector4(title, layout, property);
- case SerializableProperty.FieldType.ResourceRef:
- return new InspectableResourceRef(title, layout, property);
- case SerializableProperty.FieldType.GameObjectRef:
- return new InspectableGameObjectRef(title, layout, property);
- case SerializableProperty.FieldType.Object:
- return new InspectableObject(title, layout, property);
- case SerializableProperty.FieldType.Array:
- return new InspectableArray(title, layout, property);
- case SerializableProperty.FieldType.List:
- return new InspectableList(title, layout, property);
- }
- throw new Exception("No inspector exists for the provided field type.");
- }
- public static InspectableObjectBase CreateCustomInspectable(Type inspectableType, string title, InspectableFieldLayout layout, SerializableProperty property)
- {
- if (!inspectableType.IsSubclassOf(typeof (InspectableObjectBase)))
- throw new Exception("Invalid inspector type.");
- return (InspectableObjectBase)Activator.CreateInstance(inspectableType, title, property);
- }
- }
- }
|