源战役客户端
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.

238 line
9.8 KiB

  1. TweenLite = TweenLite or BaseClass()
  2. local TweenLite = TweenLite
  3. local math_min = math.min
  4. local Status = Status
  5. local table_insert = table.insert
  6. local IsTableEmpty = IsTableEmpty
  7. local GetLocalPositionX = GetLocalPositionX
  8. local GetLocalPositionY = GetLocalPositionY
  9. local SetLocalPositionX = SetLocalPositionX
  10. local SetLocalPositionY = SetLocalPositionY
  11. TweenLite.UiAnimationType = {
  12. POS = "POS",
  13. POSX = "POSX",
  14. POSY = "POSY",
  15. ALPHA = "ALPHA", --新版的alpha 不要用旧版的
  16. ALPHA_OLD = "ALPHA_OLD",--旧版的alpha变化 后续不要再调用
  17. WIDTH = "WIDTH",
  18. HEIGHT = "HEIGHT",
  19. ROTATION = "ROTATION", --rotateion.z
  20. CANVAS_POSX="CANVAS_POSX",
  21. CANVAS_POSY="CANVAS_POSY",
  22. SCALE = "SCALE",
  23. ROTATION_Y = "ROTATION_Y",
  24. SRECT_HORIZONTAL = "SRECT_HORIZONTAL",
  25. SRECT_VERTICAL = "SRECT_VERTICAL",
  26. ANCHORED_POS = "ANCHORED_POS",
  27. ANCHORED_POSX = "ANCHORED_POSX",
  28. ANCHORED_POSY = "ANCHORED_POSY",
  29. ALPHA_ALL = "ALPHA_ALL", --动态添加CanvasGroup设置透明度,也可以自己在prefab里添加然后调用TweenLite.UiAnimationType.ALPHA
  30. FILLAMOUNT = "FILLAMOUNT",
  31. DELAY = "DALAY", --延迟
  32. }
  33. TweenLite.moving_info_list = {} --当前正在缓动的列表
  34. TweenLite.moving_info_list_delay = {} -- 保存正在更新的过程中添加进来的对象 本次更新完以后才会添加到moving_info_list里 避免更新过程中触发rehash产生影响
  35. TweenLite.curr_info_index = 0
  36. TweenLite.is_updating = false --是否正在更新
  37. --[[
  38. parent_wnd: baseclass的父容器
  39. anim_wnd:
  40. tweentype:
  41. endValue:
  42. AnimaTime:s
  43. anim_end_func:
  44. tween_type: 线
  45. ]]
  46. function TweenLite.to(parent_wnd, anim_wnd, tweentype, endValue, AnimaTime, anim_end_func, tween_type, loop)
  47. if not anim_wnd then
  48. return
  49. end
  50. local startvalue=0
  51. if tweentype == TweenLite.UiAnimationType.POS then
  52. startvalue = anim_wnd.localPosition
  53. elseif tweentype == TweenLite.UiAnimationType.POSX then
  54. startvalue = GetLocalPositionX(anim_wnd)
  55. elseif tweentype == TweenLite.UiAnimationType.POSY then
  56. startvalue = GetLocalPositionY(anim_wnd)
  57. elseif tweentype == TweenLite.UiAnimationType.ALPHA then
  58. startvalue = anim_wnd.alpha
  59. elseif tweentype == TweenLite.UiAnimationType.ALPHA_OLD then
  60. startvalue = GetAlpha(anim_wnd)
  61. elseif tweentype == TweenLite.UiAnimationType.WIDTH then
  62. startvalue = GetSizeDeltaX(anim_wnd)
  63. elseif tweentype == TweenLite.UiAnimationType.HEIGHT then
  64. startvalue = GetSizeDeltaY(anim_wnd)
  65. elseif tweentype == TweenLite.UiAnimationType.ROTATION then
  66. startvalue = anim_wnd.eulerAngles.z
  67. elseif tweentype == TweenLite.UiAnimationType.ROTATION_Y then
  68. startvalue = anim_wnd.eulerAngles.y
  69. -- elseif tweentype == TweenLite.UiAnimationType.CANVAS_POSY then
  70. -- startvalue=anim_wnd:GetVector2(ScrollViewProperty.CanvasPosition).y
  71. -- elseif tweentype == TweenLite.UiAnimationType.CANVAS_POSX then
  72. -- startvalue=anim_wnd:GetVector2(ScrollViewProperty.CanvasPosition).x
  73. elseif tweentype == TweenLite.UiAnimationType.SCALE then
  74. startvalue = anim_wnd.localScale
  75. elseif tweentype == TweenLite.UiAnimationType.SRECT_HORIZONTAL then
  76. startvalue = anim_wnd.horizontalNormalizedPosition
  77. elseif tweentype == TweenLite.UiAnimationType.SRECT_VERTICAL then
  78. startvalue = anim_wnd.verticalNormalizePosition
  79. elseif tweentype == TweenLite.UiAnimationType.ANCHORED_POS then
  80. startvalue = anim_wnd.anchoredPosition
  81. elseif tweentype == TweenLite.UiAnimationType.ANCHORED_POSX then
  82. startvalue = anim_wnd.anchoredPosition.x
  83. elseif tweentype == TweenLite.UiAnimationType.ANCHORED_POSY then
  84. startvalue = anim_wnd.anchoredPosition.y
  85. elseif tweentype == TweenLite.UiAnimationType.FILLAMOUNT then
  86. startvalue = anim_wnd:GetComponent("Image").fillAmount
  87. elseif tweentype == TweenLite.UiAnimationType.ALPHA_ALL then
  88. --添加一个CanvasGroup组件
  89. local component = anim_wnd:GetComponent("CanvasGroup")
  90. if not component then
  91. component = anim_wnd:AddComponent(typeof(UnityEngine.CanvasGroup))
  92. end
  93. startvalue = component.alpha
  94. elseif tweentype == TweenLite.UiAnimationType.DELAY then
  95. end
  96. local tid = TweenLite.AddMovingItem(parent_wnd, anim_wnd, tweentype, startvalue, endValue, AnimaTime, anim_end_func, tween_type, loop)
  97. if TweenLite.delayfun_timer == nil then
  98. local function delayfun()
  99. TweenLite.Update()
  100. end
  101. TweenLite.delayfun_timer = GlobalTimerQuest:AddPeriodQuest(delayfun, 0.02, -1)
  102. end
  103. return tid
  104. end
  105. function TweenLite.setMovingInfo(v,progress)
  106. if not v or v.parent_wnd._use_delete_method or IsNull(v.anim_wnd) then
  107. return true
  108. end
  109. local curr_value = v.startvalue + (v.endValue - v.startvalue) * progress
  110. if v.tweentype==TweenLite.UiAnimationType.POS then
  111. -- v.anim_wnd.localPosition = curr_value
  112. SetLocalPosition(v.anim_wnd, curr_value.x, curr_value.y , curr_value.z)
  113. elseif v.tweentype==TweenLite.UiAnimationType.POSX then
  114. -- local vect = Vector3(curr_value,v.anim_wnd.localPosition.y, v.anim_wnd.localPosition.z)
  115. -- v.anim_wnd.localPosition = vect
  116. SetLocalPositionX(v.anim_wnd, curr_value)
  117. elseif v.tweentype==TweenLite.UiAnimationType.POSY then
  118. -- local vect = Vector3(v.anim_wnd.localPosition.x,curr_value, v.anim_wnd.localPosition.z)
  119. -- v.anim_wnd.localPosition = vect
  120. SetLocalPositionY(v.anim_wnd, curr_value)
  121. elseif v.tweentype==TweenLite.UiAnimationType.ALPHA then
  122. v.anim_wnd.alpha = curr_value
  123. elseif v.tweentype==TweenLite.UiAnimationType.ALPHA_OLD then
  124. SetAlpha(v.anim_wnd,curr_value)
  125. elseif v.tweentype==TweenLite.UiAnimationType.WIDTH then
  126. SetSizeDeltaX(v.anim_wnd, curr_value)
  127. -- v.anim_wnd.sizeDelta = Vector2(curr_value,v.anim_wnd.sizeDelta.y)
  128. elseif v.tweentype==TweenLite.UiAnimationType.HEIGHT then
  129. SetSizeDeltaY(v.anim_wnd, curr_value)
  130. -- v.anim_wnd.sizeDelta = Vector2(v.anim_wnd.sizeDelta.x,curr_value)
  131. elseif v.tweentype == TweenLite.UiAnimationType.ROTATION then
  132. v.anim_wnd.localRotation = Quaternion.Euler(0,0,curr_value)
  133. elseif v.tweentype == TweenLite.UiAnimationType.ROTATION_Y then
  134. v.anim_wnd.localRotation = Quaternion.Euler(0,curr_value,0)
  135. elseif v.tweentype == TweenLite.UiAnimationType.FILLAMOUNT then
  136. v.anim_wnd:GetComponent("Image").fillAmount = curr_value
  137. -- elseif v.tweentype==TweenLite.UiAnimationType.CANVAS_POSY then
  138. -- anim_wnd:SetVectorR(ScrollViewProperty.CanvasPosition, pass_time)
  139. -- elseif v.tweentype==TweenLite.UiAnimationType.CANVAS_POSX then
  140. -- anim_wnd:SetVectorL(ScrollViewProperty.CanvasPosition, pass_time)
  141. elseif v.tweentype == TweenLite.UiAnimationType.SCALE then
  142. -- v.anim_wnd.localScale = curr_value
  143. SetLocalScale(v.anim_wnd, curr_value.x, curr_value.y)
  144. elseif v.tweentype == TweenLite.UiAnimationType.SRECT_HORIZONTAL then
  145. v.anim_wnd.horizontalNormalizedPosition = curr_value
  146. elseif v.tweentype == TweenLite.UiAnimationType.SRECT_VERTICAL then
  147. v.anim_wnd.verticalNormalizePosition = curr_value
  148. elseif v.tweentype==TweenLite.UiAnimationType.ANCHORED_POS and v.anim_wnd.anchoredPosition then
  149. v.anim_wnd.anchoredPosition = curr_value
  150. elseif v.tweentype==TweenLite.UiAnimationType.ANCHORED_POSX and v.anim_wnd.anchoredPosition then
  151. local vect = Vector2(curr_value,v.anim_wnd.anchoredPosition.y)
  152. v.anim_wnd.anchoredPosition = vect
  153. elseif v.tweentype==TweenLite.UiAnimationType.ANCHORED_POSY and v.anim_wnd.anchoredPosition then
  154. local vect = Vector2(v.anim_wnd.anchoredPosition.x, curr_value)
  155. v.anim_wnd.anchoredPosition = vect
  156. elseif v.tweentype==TweenLite.UiAnimationType.ALPHA_ALL then
  157. local component = v.anim_wnd:GetComponent("CanvasGroup")
  158. component.alpha = curr_value
  159. elseif v.tweentype==TweenLite.UiAnimationType.DELAY then
  160. end
  161. return false
  162. end
  163. function TweenLite.AddMovingItem(parent_wnd, anim_wnd, tweentype, startvalue, endValue, AnimaTime, anim_end_func, tween_type, loop)
  164. local info = {parent_wnd = parent_wnd, anim_wnd = anim_wnd, tweentype = tweentype, startvalue = startvalue, endValue = endValue, AnimaTime = AnimaTime,
  165. anim_end_func = anim_end_func, tween_func = tween_type and TweenFunc[tween_type] or TweenFunc[TweenFunc.LINEAR], startTime = Status.NowTime, loop = loop or false}
  166. TweenLite.curr_info_index = TweenLite.curr_info_index + 1
  167. if TweenLite.is_updating == false then
  168. TweenLite.moving_info_list[TweenLite.curr_info_index] = info
  169. else
  170. TweenLite.moving_info_list_delay[TweenLite.curr_info_index] = info
  171. end
  172. return TweenLite.curr_info_index
  173. end
  174. function TweenLite.Update()
  175. -- if G_DEBUG_STOP_ACTION then
  176. -- return
  177. -- end
  178. TweenLite.is_updating = true
  179. local delete_list = {} --更新完后再删除列表 避免更新过程中触发rehash产生影响
  180. for k, v in pairs(TweenLite.moving_info_list) do
  181. local usetime = Status.NowTime - v.startTime
  182. local need_delete = false
  183. if v.tween_func then
  184. --经过时间
  185. local ratio = math_min(usetime / v.AnimaTime,1)
  186. local progress = v.tween_func(ratio)
  187. need_delete = TweenLite.setMovingInfo(v,progress)
  188. end
  189. local timeout = false
  190. if usetime >= v.AnimaTime then
  191. if v.loop then
  192. v.startTime = Status.NowTime
  193. else
  194. timeout = true
  195. end
  196. end
  197. if need_delete or timeout then
  198. --结束回调
  199. if not need_delete and v.anim_end_func ~= nil then
  200. v.anim_end_func()
  201. end
  202. table_insert(delete_list,k)
  203. end
  204. end
  205. TweenLite.is_updating = false
  206. for i = 1,#delete_list do
  207. TweenLite.moving_info_list[delete_list[i]] = nil
  208. end
  209. for k, v in pairs(TweenLite.moving_info_list_delay) do
  210. v.startTime = Status.NowTime
  211. TweenLite.moving_info_list[k] = v
  212. end
  213. TweenLite.moving_info_list_delay = {}
  214. if IsTableEmpty(TweenLite.moving_info_list) then
  215. if TweenLite.delayfun_timer then
  216. GlobalTimerQuest:CancelQuest(TweenLite.delayfun_timer)
  217. TweenLite.delayfun_timer = nil
  218. -- print("GlobalTimerQuest:CancelQuest TweenLite.delayfun_timer")
  219. end
  220. end
  221. end
  222. function TweenLite.Stop(id)
  223. if not id then return end
  224. TweenLite.moving_info_list[id] = nil
  225. TweenLite.moving_info_list_delay[id] = nil
  226. end