UI = UI or {} --UI组件的基类 UI.UIComponent = UI.UIComponent or {} function UI.UIComponent:Init( view_owner ) --调用界面类的UIPartical:AddUIComponent会触发,view_owner就是界面类指针 self.view_owner = view_owner end function UI.UIComponent:OnLoad( ) --界面加载成功后触发,先于BaseView和BaseItem的load_callback end function UI.UIComponent:OnDestroy( ) --界面销毁后触发,后于BaseView的destroy_callback和BaseItem的__delete end --倒计时组件,内置管理定时器,界面销毁时会自动移除,请放心使用,注意参数的单位都是秒 UI.Countdown = UI.Countdown or BaseClass(UI.UIComponent) --传入结束时间end_time,截至该时间前每秒都会触发传入的step_call_back回调,其回调函数的参数为剩余时间 function UI.Countdown:CountdownByEndTime( end_time, step_call_back, duration ) if not end_time or not step_call_back then print('Cat:UIComponents.lua[UI.Countdown:CountdownByEndTime] failed! some arge nil:', tostring(end_time), tostring(step_call_back)) return end self.end_time = end_time self.step_call_back = step_call_back local function step_countdown( ) local curTime = TimeUtil:getServerTimeMs() local left_time = self.end_time - curTime/1000 self.step_call_back(left_time) if left_time <= 0 then self:OnDestroy() else self.is_counting = true end end if not self.step_countdown_id then self.step_countdown_id = GlobalTimerQuest:AddPeriodQuest(step_countdown, duration and duration or 1, -1) end step_countdown() end function UI.Countdown:CountdownByLeftTime( left_time, step_call_back, duration ) local curTime = TimeUtil:getServerTimeMs() self:CountdownByEndTime(curTime/1000+left_time, step_call_back, duration) end function UI.Countdown:DelayCallByLeftTime( left_time, step_call_back, duration ) self:CountdownByLeftTime(left_time, function(_left_time) if _left_time<=0 and step_call_back then step_call_back() end end, duration) end function UI.Countdown:IsCounting( ) return self.is_counting end function UI.Countdown:Stop( ) self:OnDestroy() end function UI.Countdown:OnDestroy( ) if self.step_countdown_id then GlobalTimerQuest:CancelQuest(self.step_countdown_id) self.step_countdown_id = nil end self.is_counting = false end --防止某函数在短时间内频繁调用 UI.PreventFastCall = UI.PreventFastCall or BaseClass(UI.UIComponent) function UI.PreventFastCall:OnLoad( ) self.last_handle_scroll_time = 0 self.prevent_time = 0.5 end function UI.PreventFastCall:SetPreventTime( prevent_time ) self.prevent_time = prevent_time end function UI.PreventFastCall:CallFunc(func, ...) local curTime = TimeUtil:getServerTimeMs() local left_time = curTime - self.last_handle_scroll_time if left_time < self.prevent_time then local functor = Functor(func, ...) local function delay_call_func( ) functor() self.last_handle_scroll_time = curTime end if not self.delay_call_func_id then self.delay_call_func_id = GlobalTimerQuest:AddPeriodQuest(delay_call_func, self.prevent_time-left_time, 1) end return false end func(...) self.last_handle_scroll_time = curTime end function UI.PreventFastCall:OnDestroy() if self.delay_call_func_id then GlobalTimerQuest:CancelQuest(self.delay_call_func_id) self.delay_call_func_id = nil end end UI.ViewEventListener = UI.ViewEventListener or {} function UI.ViewEventListener:OnLoad( ) if self.on_load_call_back then self.on_load_call_back() end end function UI.ViewEventListener:OnDestroy( ) if self.on_destroy_call_back then self.on_destroy_call_back() end end function UI.ViewEventListener:SetOnLoadCallBack( call_back ) self.on_load_call_back = call_back end function UI.ViewEventListener:SetOnDestroyCallBack( call_back ) self.on_destroy_call_back = call_back end --绑定组件 UI.BindEventComp = UI.BindEventComp or {} UI.BindEventComp.BindEvent = function(self, bindDispather, eventName, handleFunc) assert(bindDispather~=nil, "bindDispather must be not nil!") self.bindEventInfos = self.bindEventInfos or {} -- local hadBind = self.bindEventInfos[eventName..tostring(bindDispather)] -- assert(not hadBind, 'Cat:UINode.lua had bind event name : '..eventName) -- print('Cat:UINode.lua[69] eventName..tostring(bindDispather)', eventName..tostring(bindDispather)) local eventID = bindDispather:Bind(eventName, handleFunc) -- self.bindEventInfos[eventName..tostring(bindDispather)] = {bindDispather, eventID} table.insert(self.bindEventInfos, {bindDispather, eventID}) return eventID end UI.BindEventComp.UnBindAll = function(self) if self.bindEventInfos then for k,v in pairs(self.bindEventInfos) do if v[1] and v[2] then if v[2] == EventName.SHOW_EXIT_BTN_STATE then print("Cat:UIComponents [148] : ",debug.traceback()) end v[1]:UnBind(v[2]) end end self.bindEventInfos = nil end end