You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.5 KiB

4 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /**
  5. * Internal Representation of a Sequence<br>
  6. * <br>
  7. * &nbsp;&nbsp;<h4>Example:</h4>
  8. * var seq = LeanTween.sequence();<br>
  9. * seq.append(1f); <span style="color:gray">// delay everything one second</span><br>
  10. * seq.append( () => { <span style="color:gray">// fire an event before start</span><br>
  11. * &nbsp;Debug.Log("I have started");<br>
  12. * });<br>
  13. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); <span style="color:gray">// do a tween</span><br>
  14. * seq.append( (object obj) => { <span style="color:gray">// fire event after tween</span><br>
  15. * &nbsp;var dict = obj as Dictionary<string,string>;<br>
  16. * &nbsp;Debug.Log("We are done now obj value:"+dict["hi"]);<br>
  17. * }, new Dictionary<string,string>(){ {"hi","sup"} } );<br>
  18. * @class LTSeq
  19. * @constructor
  20. */
  21. public class LTSeq {
  22. public LTSeq previous;
  23. public LTSeq current;
  24. public LTDescr tween;
  25. public float totalDelay;
  26. public float timeScale;
  27. private int debugIter;
  28. public uint counter;
  29. public bool toggle = false;
  30. private uint _id;
  31. public int id{
  32. get{
  33. uint toId = _id | counter << 16;
  34. /*uint backId = toId & 0xFFFF;
  35. uint backCounter = toId >> 16;
  36. if(_id!=backId || backCounter!=counter){
  37. Debug.LogError("BAD CONVERSION toId:"+_id);
  38. }*/
  39. return (int)toId;
  40. }
  41. }
  42. public void reset(){
  43. previous = null;
  44. tween = null;
  45. totalDelay = 0f;
  46. }
  47. public void init(uint id, uint global_counter){
  48. reset();
  49. _id = id;
  50. counter = global_counter;
  51. this.current = this;
  52. }
  53. private LTSeq addOn(){
  54. this.current.toggle = true;
  55. LTSeq lastCurrent = this.current;
  56. this.current = LeanTween.sequence(true);
  57. Debug.Log("this.current:" + this.current.id + " lastCurrent:" + lastCurrent.id);
  58. this.current.previous = lastCurrent;
  59. lastCurrent.toggle = false;
  60. this.current.totalDelay = lastCurrent.totalDelay;
  61. this.current.debugIter = lastCurrent.debugIter + 1;
  62. return current;
  63. }
  64. private float addPreviousDelays(){
  65. // Debug.Log("delay:"+delay+" count:"+this.current.count+" this.current.totalDelay:"+this.current.totalDelay);
  66. LTSeq prev = this.current.previous;
  67. if (prev != null && prev.tween!=null) {
  68. return this.current.totalDelay + prev.tween.time;
  69. }
  70. return this.current.totalDelay;
  71. }
  72. /**
  73. * Add a time delay to the sequence
  74. * @method append (delay)
  75. * @param {float} delay:float amount of time to add to the sequence
  76. * @return {LTSeq} LTDescr an object that distinguishes the tween
  77. * var seq = LeanTween.sequence();<br>
  78. * seq.append(1f); // delay everything one second<br>
  79. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  80. */
  81. public LTSeq append( float delay ){
  82. this.current.totalDelay += delay;
  83. return this.current;
  84. }
  85. /**
  86. * Add a time delay to the sequence
  87. * @method append (method)
  88. * @param {System.Action} callback:System.Action method you want to be called
  89. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  90. * @example
  91. * var seq = LeanTween.sequence();<br>
  92. * seq.append( () => { // fire an event before start<br>
  93. * &nbsp;Debug.Log("I have started");<br>
  94. * });<br>
  95. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  96. * seq.append( () => { // fire event after tween<br>
  97. * &nbsp;Debug.Log("We are done now");<br>
  98. * });;<br>
  99. */
  100. public LTSeq append( System.Action callback ){
  101. LTDescr newTween = LeanTween.delayedCall(0f, callback);
  102. // Debug.Log("newTween:" + newTween);
  103. append(newTween);
  104. return addOn();
  105. }
  106. /**
  107. * Add a time delay to the sequence
  108. * @method add (method(object))
  109. * @param {System.Action} callback:System.Action method you want to be called
  110. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  111. * @example
  112. * var seq = LeanTween.sequence();<br>
  113. * seq.append( () => { // fire an event before start<br>
  114. * &nbsp;Debug.Log("I have started");<br>
  115. * });<br>
  116. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a tween<br>
  117. * seq.append((object obj) => { // fire event after tween
  118. * &nbsp;var dict = obj as Dictionary<string,string>;
  119. * &nbsp;Debug.Log("We are done now obj value:"+dict["hi"]);
  120. * &nbsp;}, new Dictionary<string,string>(){ {"hi","sup"} } );
  121. */
  122. public LTSeq append( System.Action<object> callback, object obj ){
  123. append(LeanTween.delayedCall(0f, callback).setOnCompleteParam(obj));
  124. return addOn();
  125. }
  126. public LTSeq append( GameObject gameObject, System.Action callback ){
  127. append(LeanTween.delayedCall(gameObject, 0f, callback));
  128. return addOn();
  129. }
  130. public LTSeq append( GameObject gameObject, System.Action<object> callback, object obj ){
  131. append(LeanTween.delayedCall(gameObject, 0f, callback).setOnCompleteParam(obj));
  132. return addOn();
  133. }
  134. /**
  135. * Retrieve a sequencer object where you can easily chain together tweens and methods one after another
  136. *
  137. * @method add (tween)
  138. * @return {LTSeq} LTSeq an object that you can add tweens, methods and time on to
  139. * @example
  140. * var seq = LeanTween.sequence();<br>
  141. * seq.append( LeanTween.move(cube1, Vector3.one * 10f, 1f) ); // do a move tween<br>
  142. * seq.append( LeanTween.rotateAround( avatar1, Vector3.forward, 360f, 1f ) ); // then do a rotate tween<br>
  143. */
  144. public LTSeq append( LTDescr tween ){
  145. this.current.tween = tween;
  146. // Debug.Log("tween:" + tween + " delay:" + this.current.totalDelay);
  147. this.current.totalDelay = addPreviousDelays();
  148. tween.setDelay( this.current.totalDelay );
  149. return addOn();
  150. }
  151. public LTSeq insert( LTDescr tween ){
  152. this.current.tween = tween;
  153. tween.setDelay( addPreviousDelays() );
  154. return addOn();
  155. }
  156. public LTSeq setScale( float timeScale ){
  157. // Debug.Log("this.current:" + this.current.previous.debugIter+" tween:"+this.current.previous.tween);
  158. setScaleRecursive(this.current, timeScale, 500);
  159. return addOn();
  160. }
  161. private void setScaleRecursive( LTSeq seq, float timeScale, int count ){
  162. if (count > 0) {
  163. this.timeScale = timeScale;
  164. // Debug.Log("seq.count:" + count + " seq.tween:" + seq.tween);
  165. seq.totalDelay *= timeScale;
  166. if (seq.tween != null) {
  167. // Debug.Log("seq.tween.time * timeScale:" + seq.tween.time * timeScale + " seq.totalDelay:"+seq.totalDelay +" time:"+seq.tween.time+" seq.tween.delay:"+seq.tween.delay);
  168. if (seq.tween.time != 0f)
  169. seq.tween.setTime(seq.tween.time * timeScale);
  170. seq.tween.setDelay(seq.tween.delay * timeScale);
  171. }
  172. if (seq.previous != null)
  173. setScaleRecursive(seq.previous, timeScale, count - 1);
  174. }
  175. }
  176. public LTSeq reverse(){
  177. return addOn();
  178. }
  179. }