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

154 rivejä
4.8 KiB

1 kuukausi sitten
  1. UI = UI or {}
  2. --UI组件的基类
  3. UI.UIComponent = UI.UIComponent or {}
  4. function UI.UIComponent:Init( view_owner )
  5. --调用界面类的UIPartical:AddUIComponent会触发,view_owner就是界面类指针
  6. self.view_owner = view_owner
  7. end
  8. function UI.UIComponent:OnLoad( )
  9. --界面加载成功后触发,先于BaseView和BaseItem的load_callback
  10. end
  11. function UI.UIComponent:OnDestroy( )
  12. --界面销毁后触发,后于BaseView的destroy_callback和BaseItem的__delete
  13. end
  14. --倒计时组件,内置管理定时器,界面销毁时会自动移除,请放心使用,注意参数的单位都是秒
  15. UI.Countdown = UI.Countdown or BaseClass(UI.UIComponent)
  16. --传入结束时间end_time,截至该时间前每秒都会触发传入的step_call_back回调,其回调函数的参数为剩余时间
  17. function UI.Countdown:CountdownByEndTime( end_time, step_call_back, duration )
  18. if not end_time or not step_call_back then
  19. print('Cat:UIComponents.lua[UI.Countdown:CountdownByEndTime] failed! some arge nil:', tostring(end_time), tostring(step_call_back))
  20. return
  21. end
  22. self.end_time = end_time
  23. self.step_call_back = step_call_back
  24. local function step_countdown( )
  25. local curTime = TimeUtil:getServerTimeMs()
  26. local left_time = self.end_time - curTime/1000
  27. self.step_call_back(left_time)
  28. if left_time <= 0 then
  29. self:OnDestroy()
  30. else
  31. self.is_counting = true
  32. end
  33. end
  34. if not self.step_countdown_id then
  35. self.step_countdown_id = GlobalTimerQuest:AddPeriodQuest(step_countdown, duration and duration or 1, -1)
  36. end
  37. step_countdown()
  38. end
  39. function UI.Countdown:CountdownByLeftTime( left_time, step_call_back, duration )
  40. local curTime = TimeUtil:getServerTimeMs()
  41. self:CountdownByEndTime(curTime/1000+left_time, step_call_back, duration)
  42. end
  43. function UI.Countdown:DelayCallByLeftTime( left_time, step_call_back, duration )
  44. self:CountdownByLeftTime(left_time, function(_left_time)
  45. if _left_time<=0 and step_call_back then
  46. step_call_back()
  47. end
  48. end, duration)
  49. end
  50. function UI.Countdown:IsCounting( )
  51. return self.is_counting
  52. end
  53. function UI.Countdown:Stop( )
  54. self:OnDestroy()
  55. end
  56. function UI.Countdown:OnDestroy( )
  57. if self.step_countdown_id then
  58. GlobalTimerQuest:CancelQuest(self.step_countdown_id)
  59. self.step_countdown_id = nil
  60. end
  61. self.is_counting = false
  62. end
  63. --防止某函数在短时间内频繁调用
  64. UI.PreventFastCall = UI.PreventFastCall or BaseClass(UI.UIComponent)
  65. function UI.PreventFastCall:OnLoad( )
  66. self.last_handle_scroll_time = 0
  67. self.prevent_time = 0.5
  68. end
  69. function UI.PreventFastCall:SetPreventTime( prevent_time )
  70. self.prevent_time = prevent_time
  71. end
  72. function UI.PreventFastCall:CallFunc(func, ...)
  73. local curTime = TimeUtil:getServerTimeMs()
  74. local left_time = curTime - self.last_handle_scroll_time
  75. if left_time < self.prevent_time then
  76. local functor = Functor(func, ...)
  77. local function delay_call_func( )
  78. functor()
  79. self.last_handle_scroll_time = curTime
  80. end
  81. if not self.delay_call_func_id then
  82. self.delay_call_func_id = GlobalTimerQuest:AddPeriodQuest(delay_call_func, self.prevent_time-left_time, 1)
  83. end
  84. return false
  85. end
  86. func(...)
  87. self.last_handle_scroll_time = curTime
  88. end
  89. function UI.PreventFastCall:OnDestroy()
  90. if self.delay_call_func_id then
  91. GlobalTimerQuest:CancelQuest(self.delay_call_func_id)
  92. self.delay_call_func_id = nil
  93. end
  94. end
  95. UI.ViewEventListener = UI.ViewEventListener or {}
  96. function UI.ViewEventListener:OnLoad( )
  97. if self.on_load_call_back then
  98. self.on_load_call_back()
  99. end
  100. end
  101. function UI.ViewEventListener:OnDestroy( )
  102. if self.on_destroy_call_back then
  103. self.on_destroy_call_back()
  104. end
  105. end
  106. function UI.ViewEventListener:SetOnLoadCallBack( call_back )
  107. self.on_load_call_back = call_back
  108. end
  109. function UI.ViewEventListener:SetOnDestroyCallBack( call_back )
  110. self.on_destroy_call_back = call_back
  111. end
  112. --绑定组件
  113. UI.BindEventComp = UI.BindEventComp or {}
  114. UI.BindEventComp.BindEvent = function(self, bindDispather, eventName, handleFunc)
  115. assert(bindDispather~=nil, "bindDispather must be not nil!")
  116. self.bindEventInfos = self.bindEventInfos or {}
  117. -- local hadBind = self.bindEventInfos[eventName..tostring(bindDispather)]
  118. -- assert(not hadBind, 'Cat:UINode.lua had bind event name : '..eventName)
  119. -- print('Cat:UINode.lua[69] eventName..tostring(bindDispather)', eventName..tostring(bindDispather))
  120. local eventID = bindDispather:Bind(eventName, handleFunc)
  121. -- self.bindEventInfos[eventName..tostring(bindDispather)] = {bindDispather, eventID}
  122. table.insert(self.bindEventInfos, {bindDispather, eventID})
  123. return eventID
  124. end
  125. UI.BindEventComp.UnBindAll = function(self)
  126. if self.bindEventInfos then
  127. for k,v in pairs(self.bindEventInfos) do
  128. if v[1] and v[2] then
  129. if v[2] == EventName.SHOW_EXIT_BTN_STATE then
  130. print("Cat:UIComponents [148] : ",debug.traceback())
  131. end
  132. v[1]:UnBind(v[2])
  133. end
  134. end
  135. self.bindEventInfos = nil
  136. end
  137. end