StreamFont.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. public struct IntPair
  8. {
  9. public int Key;
  10. public int Value;
  11. public IntPair(int key, int value)
  12. {
  13. Key = key;
  14. Value = value;
  15. }
  16. }
  17. public class StreamFont : Tween
  18. {
  19. #region 变量
  20. public override bool InOrigin
  21. {
  22. get
  23. {
  24. if (Consecutive)
  25. {
  26. if (Target.fontSize.Equal(DestList[0]))
  27. {
  28. _InOrigin = true;
  29. }
  30. else
  31. {
  32. _InOrigin = false;
  33. }
  34. }
  35. else
  36. {
  37. if (Target.fontSize.Equal(DestKvList[0].Key))
  38. {
  39. _InOrigin = true;
  40. }
  41. else
  42. {
  43. _InOrigin = false;
  44. }
  45. }
  46. return _InOrigin;
  47. }
  48. set
  49. {
  50. _InOrigin = value;
  51. if (_InOrigin)
  52. {
  53. if (Consecutive)
  54. {
  55. Target.fontSize = DestList[0];
  56. }
  57. else
  58. {
  59. Target.fontSize = DestKvList[0].Key;
  60. }
  61. BackwardFinish();
  62. }
  63. }
  64. }
  65. public override bool InDestination
  66. {
  67. get
  68. {
  69. if (Consecutive)
  70. {
  71. if (Target.fontSize.Equal(DestList.Last(0)))
  72. {
  73. _InDestination = true;
  74. }
  75. else
  76. {
  77. _InDestination = false;
  78. }
  79. }
  80. else
  81. {
  82. if (Target.fontSize.Equal(DestKvList.Last(0).Value))
  83. {
  84. _InDestination = true;
  85. }
  86. else
  87. {
  88. _InDestination = false;
  89. }
  90. }
  91. return _InDestination;
  92. }
  93. set
  94. {
  95. _InDestination = value;
  96. if (_InDestination)
  97. {
  98. if (Consecutive)
  99. {
  100. Target.fontSize = DestList.Last(0);
  101. }
  102. else
  103. {
  104. Target.fontSize = DestKvList.Last(0).Value;
  105. }
  106. ForwardFinish();
  107. }
  108. }
  109. }
  110. public int Phase;
  111. public int TotPhase;
  112. public int TempDest;
  113. public int TempDelta;
  114. public int TempOrigin;
  115. public bool Consecutive;
  116. public float TempDelay;
  117. public float TempDuration;
  118. public List<int> DestList = new List<int>();
  119. public List<float> DelayList = new List<float>();
  120. public List<float> DurationList = new List<float>();
  121. public List<IntPair> DestKvList = new List<IntPair>();
  122. public List<UnityAction> ActionList = new List<UnityAction>();
  123. public Text Target;
  124. public CurveFunctionF Func;
  125. #endregion
  126. public StreamFont(Text target, List<int> destList, List<float> delayList, List<float> durationList, bool originActive, bool destActive, Curve curve, bool cg = false, List<UnityAction> actionList = null) : base(cg, curve, target)
  127. {
  128. Func = ManaAnim.CurveFuncDicF[curve];
  129. Target = target;
  130. TotPhase = destList.Count - 1;
  131. Consecutive = true;
  132. DestList = destList;
  133. DelayList = delayList;
  134. ActionList = actionList;
  135. DurationList = durationList;
  136. if (ActionList.Valid())
  137. {
  138. if ((DestList.Count - 1) - ActionList.Count != -1)
  139. {
  140. throw new Exception();
  141. }
  142. }
  143. if (DestList.Count - DelayList.Count != 2)
  144. {
  145. throw new Exception();
  146. }
  147. if (DestList.Count - DurationList.Count != 1)
  148. {
  149. throw new Exception();
  150. }
  151. InForward = false;
  152. InBackward = false;
  153. DestActive = destActive;
  154. OriginActive = originActive;
  155. }
  156. public StreamFont(Text target, List<IntPair> destKvList, List<float> delayList, List<float> durationList, bool originActive, bool destActive, Curve curve, bool cg = false, List<UnityAction> actionList = null) : base(cg, curve, target)
  157. {
  158. Func = ManaAnim.CurveFuncDicF[curve];
  159. Target = target;
  160. TotPhase = destKvList.Count - 1;
  161. Consecutive = false;
  162. DelayList = delayList;
  163. ActionList = actionList;
  164. DestKvList = destKvList;
  165. DurationList = durationList;
  166. if (ActionList.Valid())
  167. {
  168. if (DestKvList.Count - ActionList.Count != -1)
  169. {
  170. throw new Exception();
  171. }
  172. }
  173. if (DestKvList.Count - DelayList.Count != 1)
  174. {
  175. throw new Exception();
  176. }
  177. if (DestKvList.Count - DurationList.Count != 0)
  178. {
  179. throw new Exception();
  180. }
  181. InForward = false;
  182. InBackward = false;
  183. DestActive = destActive;
  184. OriginActive = originActive;
  185. }
  186. public override void StartForward()
  187. {
  188. base.StartForward();
  189. if (Consecutive)
  190. {
  191. Phase = 1;
  192. TempDest = DestList[1];
  193. TempDelay = DelayList[0];
  194. TempOrigin = DestList[0];
  195. TempDuration = DurationList[0];
  196. }
  197. else
  198. {
  199. Phase = 0;
  200. TempDest = DestKvList[0].Value;
  201. TempDelay = DelayList[0];
  202. TempOrigin = DestKvList[0].Key;
  203. TempDuration = DurationList[0];
  204. }
  205. TempDelta = TempDest - TempOrigin;
  206. if (ActionList.Valid())
  207. {
  208. ActionList[0].SafeInvoke();
  209. }
  210. //if (InBackward)
  211. //{
  212. // Timer = ManaAnim.GetTimerFloat(Target.fontSize, Duration, TempOrigin, TempDelta, Curve);
  213. //}
  214. }
  215. public override void StartBackward()
  216. {
  217. base.StartBackward();
  218. throw new Exception();
  219. }
  220. public override bool DoForward()
  221. {
  222. Timer += Time.fixedDeltaTime;
  223. if (Phase == TotPhase)
  224. {
  225. if (Timer > TempDuration)
  226. {
  227. Timer = 0;
  228. InForward = false;
  229. InDestination = true;
  230. if (OnForwardFinish != null)
  231. {
  232. OnForwardFinish.Invoke();
  233. }
  234. if (ActionList.Valid())
  235. {
  236. ActionList.Last(0).SafeInvoke();
  237. }
  238. return true;
  239. }
  240. else
  241. {
  242. Target.fontSize = (int)Func(Timer, TempDuration, TempOrigin, TempDelta);
  243. return false;
  244. }
  245. }
  246. else
  247. {
  248. if (Timer > TempDuration + TempDelay)
  249. {
  250. Timer = 0;
  251. Phase++;
  252. if (Consecutive)
  253. {
  254. TempDest = DestList[Phase];
  255. TempOrigin = DestList[Phase - 1];
  256. TempDuration = DurationList[Phase - 1];
  257. TempDelta = TempDest - TempOrigin;
  258. if (Phase != DestList.Count - 1)
  259. {
  260. TempDelay = DelayList[Phase - 1];
  261. }
  262. if (ActionList.Valid())
  263. {
  264. ActionList[Phase - 1].SafeInvoke();
  265. }
  266. }
  267. else
  268. {
  269. TempDest = DestKvList[Phase].Value;
  270. TempOrigin = DestKvList[Phase].Key;
  271. TempDuration = DurationList[Phase];
  272. TempDelta = TempDest - TempOrigin;
  273. if (Phase != DestKvList.Count - 1)
  274. {
  275. TempDelay = DelayList[Phase];
  276. }
  277. if (ActionList.Valid())
  278. {
  279. ActionList[Phase].SafeInvoke();
  280. }
  281. }
  282. return false;
  283. }
  284. else
  285. {
  286. Target.fontSize = (int)Func(Timer, TempDuration, TempOrigin, TempDelta);
  287. return false;
  288. }
  289. }
  290. }
  291. public override bool DoBackward()
  292. {
  293. throw new Exception();
  294. }
  295. }