CountTime = CountTime or BaseClass(BaseItem) function CountTime:__init(parent) self.parent = parent self.base_file = "common" self.layout_file = "CountTime" self.end_time = 0 self.prefix = "" self.font_color = "#EFD29AFF" self.font_size = 28 self:Load() end function CountTime:Load_callback() self.CountTime = self:GetChild("CountTime"):GetComponent("Text") self.bg_image = self:GetChild("image"):GetComponent("Image") if self.abName and self.resName then self:SetBgImage(self.abName, self.resName, self.width, self.height, self.image_type) end self.CountTime.fontSize = self.font_size if self.need_refreshData then self:SetData(self.end_time) end self:InitEvent() end function CountTime:InitEvent() end function CountTime:SetData( endTime ) self.end_time = endTime if self.is_loaded then self.need_refreshData = false self.CountTime.text = "" self:SetCountTime(self.endTime) else self.need_refreshData = true end end function CountTime:SetCountTime() if self.end_time <= TimeUtil:getServerTime() then self:CancelTime() self.CountTime.text = "" else self:StartTime() self:RefreshTime() end end function CountTime:StartTime() self:CancelTime() local function on_time() self:RefreshTime() end self.ac_time_id = TimerQuest.AddPeriodQuest(GlobalTimerQuest, on_time, 1, -1) end function CountTime:RefreshTime() local left_time = self.end_time - TimeUtil:getServerTime() if left_time <= 0 then self.CountTime.text = "" self:CancelTime() if self.end_callback then self.end_callback() end return end local time_text = self:GetTimeText(left_time) self.CountTime.text = time_text end function CountTime:GetTimeText(time) local dateStr = math.floor(time / 60 / 60 / 24) local hoursStr = math.floor(time / 60 / 60) % 24 local minutesStr = math.floor(time / 60) % 60 local secondStr = time % 60 local str if dateStr > 0 then str = string.format("%s天%s时%s分", self.font_color, dateStr, hoursStr,minutesStr) else if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end str = "" .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "" end return self.prefix..str end function CountTime:SetBgImage(abName,resName,width,height,image_type) self.abName = abName self.resName = resName self.width = width self.height = height self.image_type = image_type if self.is_loaded then lua_resM:setImageSprite(self, self.bg_image,abName,resName,setNativeSize) if width and height then SetSizeDelta(self.bg_image.transform, width, height) end if image_type then self.bg_image.type = image_type end end end function CountTime:SetPrefix(str) self.prefix = str if self.is_loaded then self:RefreshTime() end end function CountTime:SetTextColor(color_str) self.font_color = color_str if self.is_loaded then self:RefreshTime() end end function CountTime:SetFontSize(size) self.font_size = size if self.is_loaded then self.CountTime.fontSize = self.font_size end end function CountTime:SetEndCallBack(callback) self.end_callback = callback end function CountTime:SetGetTimeTextFunc(func) self.GetTimeText = func end function CountTime:CancelTime() if self.ac_time_id then TimerQuest.CancelQuest(GlobalTimerQuest, self.ac_time_id) self.ac_time_id = nil end end function CountTime:__delete( ) self:CancelTime() end