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

145 lines
3.8 KiB

  1. CountTime = CountTime or BaseClass(BaseItem)
  2. function CountTime:__init(parent)
  3. self.parent = parent
  4. self.base_file = "common"
  5. self.layout_file = "CountTime"
  6. self.end_time = 0
  7. self.prefix = ""
  8. self.font_color = "#EFD29AFF"
  9. self.font_size = 28
  10. self:Load()
  11. end
  12. function CountTime:Load_callback()
  13. self.CountTime = self:GetChild("CountTime"):GetComponent("Text")
  14. self.bg_image = self:GetChild("image"):GetComponent("Image")
  15. if self.abName and self.resName then
  16. self:SetBgImage(self.abName, self.resName, self.width, self.height, self.image_type)
  17. end
  18. self.CountTime.fontSize = self.font_size
  19. if self.need_refreshData then
  20. self:SetData(self.end_time)
  21. end
  22. self:InitEvent()
  23. end
  24. function CountTime:InitEvent()
  25. end
  26. function CountTime:SetData( endTime )
  27. self.end_time = endTime
  28. if self.is_loaded then
  29. self.need_refreshData = false
  30. self.CountTime.text = ""
  31. self:SetCountTime(self.endTime)
  32. else
  33. self.need_refreshData = true
  34. end
  35. end
  36. function CountTime:SetCountTime()
  37. if self.end_time <= TimeUtil:getServerTime() then
  38. self:CancelTime()
  39. self.CountTime.text = ""
  40. else
  41. self:StartTime()
  42. self:RefreshTime()
  43. end
  44. end
  45. function CountTime:StartTime()
  46. self:CancelTime()
  47. local function on_time()
  48. self:RefreshTime()
  49. end
  50. self.ac_time_id = TimerQuest.AddPeriodQuest(GlobalTimerQuest, on_time, 1, -1)
  51. end
  52. function CountTime:RefreshTime()
  53. local left_time = self.end_time - TimeUtil:getServerTime()
  54. if left_time <= 0 then
  55. self.CountTime.text = ""
  56. self:CancelTime()
  57. if self.end_callback then
  58. self.end_callback()
  59. end
  60. return
  61. end
  62. local time_text = self:GetTimeText(left_time)
  63. self.CountTime.text = time_text
  64. end
  65. function CountTime:GetTimeText(time)
  66. local dateStr = math.floor(time / 60 / 60 / 24)
  67. local hoursStr = math.floor(time / 60 / 60) % 24
  68. local minutesStr = math.floor(time / 60) % 60
  69. local secondStr = time % 60
  70. local str
  71. if dateStr > 0 then
  72. str = string.format("<color=%s>%s天%s时%s分</color>", self.font_color, dateStr, hoursStr,minutesStr)
  73. else
  74. if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
  75. if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
  76. if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
  77. str = "<color="..self.font_color..">" .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "</color>"
  78. end
  79. return self.prefix..str
  80. end
  81. function CountTime:SetBgImage(abName,resName,width,height,image_type)
  82. self.abName = abName
  83. self.resName = resName
  84. self.width = width
  85. self.height = height
  86. self.image_type = image_type
  87. if self.is_loaded then
  88. lua_resM:setImageSprite(self, self.bg_image,abName,resName,setNativeSize)
  89. if width and height then
  90. SetSizeDelta(self.bg_image.transform, width, height)
  91. end
  92. if image_type then
  93. self.bg_image.type = image_type
  94. end
  95. end
  96. end
  97. function CountTime:SetPrefix(str)
  98. self.prefix = str
  99. if self.is_loaded then
  100. self:RefreshTime()
  101. end
  102. end
  103. function CountTime:SetTextColor(color_str)
  104. self.font_color = color_str
  105. if self.is_loaded then
  106. self:RefreshTime()
  107. end
  108. end
  109. function CountTime:SetFontSize(size)
  110. self.font_size = size
  111. if self.is_loaded then
  112. self.CountTime.fontSize = self.font_size
  113. end
  114. end
  115. function CountTime:SetEndCallBack(callback)
  116. self.end_callback = callback
  117. end
  118. function CountTime:SetGetTimeTextFunc(func)
  119. self.GetTimeText = func
  120. end
  121. function CountTime:CancelTime()
  122. if self.ac_time_id then
  123. TimerQuest.CancelQuest(GlobalTimerQuest, self.ac_time_id)
  124. self.ac_time_id = nil
  125. end
  126. end
  127. function CountTime:__delete( )
  128. self:CancelTime()
  129. end