FContainer.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace Flux
  4. {
  5. public class FContainer : FObject {
  6. public static readonly Color DEFAULT_COLOR = new Color(0.14f, 0.14f, 0.14f, 0.7f);
  7. [SerializeField]
  8. private FSequence _sequence = null;
  9. [SerializeField]
  10. private Color _color;
  11. public Color Color { get { return _color; } set { _color = value; } }
  12. [SerializeField]
  13. private List<FTimeline> _timelines = new List<FTimeline>();
  14. public List<FTimeline> Timelines { get { return _timelines; } }
  15. public override FSequence Sequence { get { return _sequence; } }
  16. public override Transform Owner { get { return null; } }
  17. public static FContainer Create( Color color )
  18. {
  19. GameObject go = new GameObject("Default");
  20. FContainer container = go.AddComponent<FContainer>();
  21. container.Color = color;
  22. return container;
  23. }
  24. internal void SetSequence( FSequence sequence )
  25. {
  26. _sequence = sequence;
  27. if( _sequence )
  28. transform.parent = _sequence.Content;
  29. else
  30. transform.parent = null;
  31. }
  32. public override void Init()
  33. {
  34. foreach( FTimeline timeline in _timelines )
  35. {
  36. timeline.Init();
  37. }
  38. }
  39. public override void Stop()
  40. {
  41. foreach( FTimeline timeline in _timelines )
  42. {
  43. timeline.Stop();
  44. }
  45. }
  46. public void Resume()
  47. {
  48. foreach( FTimeline timeline in _timelines )
  49. {
  50. timeline.Resume();
  51. }
  52. }
  53. public void Pause()
  54. {
  55. foreach( FTimeline timeline in _timelines )
  56. {
  57. timeline.Pause();
  58. }
  59. }
  60. public bool IsEmpty()
  61. {
  62. foreach( FTimeline timeline in _timelines )
  63. {
  64. if( !timeline.IsEmpty() )
  65. {
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. public void UpdateTimelines( int frame, float time )
  72. {
  73. for( int i = 0; i != _timelines.Count; ++i )
  74. {
  75. if( !_timelines[i].enabled ) continue;
  76. _timelines[i].UpdateTracks( frame, time );
  77. }
  78. }
  79. public void UpdateTimelinesEditor( int frame, float time )
  80. {
  81. for( int i = 0; i != _timelines.Count; ++i )
  82. {
  83. if( !_timelines[i].enabled ) continue;
  84. _timelines[i].UpdateTracksEditor( frame, time );
  85. }
  86. }
  87. /// @brief Adds new timeline at the end of the list.
  88. /// @param timeline New timeline.
  89. public void Add( FTimeline timeline )
  90. {
  91. int id = _timelines.Count;
  92. _timelines.Add( timeline );
  93. timeline.SetId( id );
  94. // timeline.SetSequence( this );
  95. timeline.SetContainer( this );
  96. }
  97. /// @brief Removes timeline and updates their ids.
  98. /// @param timeline CTimeline to remove.
  99. /// @note After calling this function, the ids of the timelines after this
  100. /// one in the list will have an id smaller by 1.
  101. public void Remove( FTimeline timeline )
  102. {
  103. for( int i = 0; i != _timelines.Count; ++i )
  104. {
  105. if( _timelines[i] == timeline )
  106. {
  107. Remove( i );
  108. break;
  109. }
  110. }
  111. }
  112. /// @brief Removes timeline with id.
  113. /// @oaram id Id of the CTimeline to remove.
  114. /// @note After calling this function, the ids of the timelines after this
  115. /// one in the list will have an id smaller by 1.
  116. /// @warning Does not check if id is valid (i.e. between -1 & GetTimelines().Count)
  117. public void Remove( int id )
  118. {
  119. FTimeline timeline = _timelines[id];
  120. _timelines.RemoveAt( id );
  121. timeline.SetContainer( null );
  122. UpdateTimelineIds();
  123. }
  124. public void Rebuild()
  125. {
  126. _timelines.Clear();
  127. Transform t = transform;
  128. for( int i = 0; i != t.childCount; ++i )
  129. {
  130. FTimeline timeline = t.GetChild(i).GetComponent<FTimeline>();
  131. if( timeline != null )
  132. {
  133. _timelines.Add( timeline );
  134. timeline.SetContainer( this );
  135. timeline.Rebuild();
  136. }
  137. }
  138. UpdateTimelineIds();
  139. }
  140. // Updates the ids of the timelines
  141. private void UpdateTimelineIds()
  142. {
  143. for( int i = 0; i != _timelines.Count; ++i )
  144. {
  145. _timelines[i].SetId( i );
  146. }
  147. }
  148. }
  149. }