FEditor.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.AnimatedValues;
  4. using System;
  5. using System.Collections.Generic;
  6. using Flux;
  7. namespace FluxEditor
  8. {
  9. /**
  10. * @brief Base for all the editor classes used in the sequence window.
  11. */
  12. public abstract class FEditor : ScriptableObject, ISelectableElement
  13. {
  14. /** @brief Reference to the sequence editor this editor belongs to */
  15. public abstract FSequenceEditor SequenceEditor{ get; }
  16. /** @brief Is this element selected? */
  17. [SerializeField]
  18. protected bool _isSelected;
  19. /** @brief Is this element selected? */
  20. public bool IsSelected { get { return _isSelected; } }
  21. /** @brief What's the rect used to draw this element? */
  22. // protected Rect _rect;
  23. public Rect Rect {
  24. get
  25. {
  26. return _rect;
  27. }
  28. set
  29. {
  30. _rect = value;
  31. }
  32. }
  33. private Rect _rect = new Rect();
  34. private AnimVector3 _offset = new AnimVector3();
  35. public AnimVector3 Offset { get { return _offset; } }
  36. public virtual void ClearOffset()
  37. {
  38. _offset.target = _offset.value = Vector3.zero;
  39. }
  40. private int _guiId = 0;
  41. public int GuiId { get { return _guiId; } protected set { _guiId = value; } }
  42. [SerializeField]
  43. private FObject _obj = null;
  44. public FObject Obj { get { return _obj; } set { _obj = value; } }
  45. private FEditor _owner = null;
  46. public FEditor Owner { get { return _owner; } }
  47. public virtual void ReserveGuiIds()
  48. {
  49. GuiId = EditorGUIUtility.GetControlID( FocusType.Passive );
  50. }
  51. /** @brief Called on selection. */
  52. public virtual void OnSelect()
  53. {
  54. _isSelected = true;
  55. }
  56. /** @brief Called on deselection. */
  57. public virtual void OnDeselect()
  58. {
  59. _isSelected = false;
  60. }
  61. /** @brief Called when the editor is deleted, e.g. deleting an event
  62. * from the track.
  63. * @note it is different from OnDestroy because it is called while
  64. * the object is still "proper" (e.g. event still belongs to track).
  65. */
  66. public virtual void OnDelete()
  67. {
  68. }
  69. // public virtual float ContentIndent { get { return 0; } }
  70. protected Vector2 _contentOffset = Vector2.zero;
  71. public Vector2 ContentOffset { get { return _contentOffset; } }
  72. protected virtual void OnEnable()
  73. {
  74. hideFlags = HideFlags.DontSave;
  75. }
  76. protected virtual void OnDestroy()
  77. {
  78. _owner = null;
  79. _offset.valueChanged.RemoveAllListeners();
  80. }
  81. public void RefreshRuntimeObject()
  82. {
  83. _obj = (FObject)EditorUtility.InstanceIDToObject( _obj.GetInstanceID() );
  84. }
  85. /** @brief Inits the editor object.
  86. * @param obj CObject the editor manages
  87. */
  88. public virtual void Init( FObject obj, FEditor owner )
  89. {
  90. _obj = obj;
  91. _owner = owner;
  92. _offset.valueChanged.AddListener(SequenceEditor.Repaint);
  93. }
  94. public virtual Rect GetGlobalRect()
  95. {
  96. Rect r = Rect;
  97. FEditor owner = Owner;
  98. while( owner != null )
  99. {
  100. r.x += owner.ContentOffset.x;
  101. r.y += owner.ContentOffset.y;
  102. owner = owner.Owner;
  103. }
  104. return r;
  105. }
  106. public abstract void Render( Rect rect, float headerWidth );
  107. public abstract float Height { get; }
  108. }
  109. /**
  110. * @brief Attribute to specify which editor will handle the representation
  111. * of a specific FObject class. It works in the same way as Unity's
  112. * CustomEditor, but here it is for specifying how that FObject will be
  113. * represented inside sequence window.
  114. */
  115. public class FEditorAttribute : Attribute
  116. {
  117. public Type type;
  118. public FEditorAttribute( Type type )
  119. {
  120. this.type = type;
  121. }
  122. }
  123. }