LoadingLayer_launcher.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. local LoadingLayer = class("LoadingLayer", function ()
  2. return cc.Layer:create()
  3. end)
  4. local function GetByteSize( byteSize )
  5. if byteSize >= 1024 * 1024 then
  6. return string.format("%.2fM", byteSize / 1024 / 1024)
  7. end
  8. return string.format("%.2fKB", byteSize / 1024)
  9. end
  10. function LoadingLayer:ctor()
  11. self._isCheckVersion = false
  12. self._loadingType = 0 -- 0. 1.自定义资源zip 2.自定义资源
  13. self._isForceLoad = false
  14. self._isDetails = false
  15. self._downSpeed = 0
  16. self._downSpeedNew= 0
  17. self._downSchID = nil
  18. self._isShowSpeed = global.L_GameEnvManager:GetEnvDataByKey("pc_close_secoend_tips") == 1 or global.L_GameEnvManager:GetEnvDataByKey("asset_speed_enable") == 1 or false
  19. end
  20. function LoadingLayer.create(...)
  21. local layer = LoadingLayer.new()
  22. if layer:Init(...) then
  23. return layer
  24. end
  25. return nil
  26. end
  27. function LoadingLayer:Init(data)
  28. local layoutName = global.IS_PORTRAIT and "loading/res_sync_portrait" or "loading/res_sync"
  29. self._root = CreateExport_launcher(layoutName)
  30. if not self._root then
  31. return false
  32. end
  33. self:addChild(self._root)
  34. self.ui = ui_delegate(self._root)
  35. local visibleSize = cc.Director:getInstance():getVisibleSize()
  36. self.ui.Panel_1:setContentSize({width = visibleSize.width, height = visibleSize.height})
  37. -- 背景图适配
  38. local contentSize = self.ui.Image_1:getContentSize()
  39. if visibleSize.width > contentSize.width or visibleSize.height > contentSize.height then
  40. self.ui.Image_1:setContentSize(visibleSize)
  41. end
  42. self.ui.Image_bar:setVisible(false)
  43. self.ui.LoadingBar_percent:setVisible(false)
  44. self.ui.LoadingBar_percent:setPercent(0)
  45. self.ui.Text_percent:setString("获取版本信息...")
  46. -- 检测版本/更新进度
  47. if data then
  48. self._isDetails = data.isDetails or false
  49. self._loadingType = data.loadingType or 0
  50. self._isCheckVersion = data.isCheckVersion
  51. if true == data.isCheckVersion then
  52. local formatStr = "获取版本信息..."
  53. if self._loadingType == 1 then
  54. formatStr = "比对自定义资源包..."
  55. elseif self._loadingType == 2 then
  56. formatStr = "比对自定义资源..."
  57. end
  58. self.ui.Text_percent:setString(formatStr)
  59. end
  60. if true == data.delay then
  61. self._root:setVisible(false)
  62. local sequence = cc.Sequence:create(cc.DelayTime:create(1.5), cc.Show:create())
  63. self._root:runAction(sequence)
  64. end
  65. end
  66. self._isForceLoad = tonumber(global.L_GameEnvManager:GetEnvDataByKey("loadCancleClose")) == 1
  67. if self._isForceLoad or global.L_GameEnvManager:GetEnvDataByKey("pc_close_secoend_tips") == 1 then
  68. global.L_NativeBridgeManager:GN_IsCan_Windown_Close_State({state=0})
  69. end
  70. --
  71. self:UpdateGMRes()
  72. self:ShowGameNameAndAuthor()
  73. return true
  74. end
  75. function LoadingLayer:UpdateDetailPercent(percent, totalSize)
  76. local formatStr = "已下载 %s/%s 下载速度 %s/s 请等待..."
  77. if self._loadingType == 1 then
  78. formatStr = "已下载 %s/%s 下载速度 %s/s 下载自定义资源包..."
  79. elseif self._loadingType == 2 then
  80. formatStr = "已下载 %s/%s 下载速度 %s/s 下载自定义资源..."
  81. end
  82. local speed = math.max((percent-self._downSpeed)/100 * totalSize, 1024)
  83. self.ui.Image_bar:setVisible(true)
  84. self.ui.LoadingBar_percent:setVisible(true)
  85. self.ui.LoadingBar_percent:setPercent(percent)
  86. self.ui.Text_percent:setString(string.format(formatStr, GetByteSize( percent/100 * totalSize ), GetByteSize(totalSize), GetByteSize(speed)))
  87. if self._downSpeedNew <= 0 then
  88. self._downSpeedNew = percent
  89. end
  90. if percent >= 100 then
  91. if self._downSchID then
  92. self.ui.Text_percent:stopAllActions()
  93. self._downSchID = nil
  94. end
  95. self.ui.Text_percent:runAction( cc.Sequence:create(cc.DelayTime:create(1), cc.CallFunc:create(function()
  96. self.ui.Text_percent:setString("正在释放资源...")
  97. end)))
  98. return
  99. end
  100. if not self._downSchID then
  101. local sequence = cc.Sequence:create(cc.DelayTime:create(1), cc.CallFunc:create(function()
  102. if self._downSpeedNew <= 0 then
  103. return
  104. end
  105. self._downSpeed = self._downSpeedNew
  106. self._downSpeedNew = 0
  107. end))
  108. local action = cc.RepeatForever:create(sequence)
  109. self.ui.Text_percent:runAction(action)
  110. end
  111. end
  112. function LoadingLayer:UpdatePercent(percent, totalSize)
  113. if self._isDetails and totalSize and self._isShowSpeed then
  114. self:UpdateDetailPercent(percent, totalSize)
  115. return
  116. end
  117. local formatStr = "%s%% 请等待..."
  118. if self._loadingType == 1 then
  119. formatStr = "%s%% 下载自定义资源包..."
  120. elseif self._loadingType == 2 then
  121. formatStr = "%s%% 下载自定义资源..."
  122. end
  123. self.ui.Image_bar:setVisible(true)
  124. self.ui.LoadingBar_percent:setVisible(true)
  125. self.ui.LoadingBar_percent:setPercent(percent)
  126. if totalSize then
  127. formatStr = formatStr .. "%s/%s"
  128. self.ui.Text_percent:setString(string.format(formatStr, percent, GetByteSize(percent/100 * totalSize), GetByteSize(totalSize)))
  129. else
  130. self.ui.Text_percent:setString(string.format(formatStr, percent))
  131. end
  132. if percent >= 100 then
  133. if self._downSchID then
  134. self.ui.Text_percent:stopAllActions()
  135. self._downSchID = nil
  136. end
  137. self.ui.Text_percent:runAction( cc.Sequence:create(cc.DelayTime:create(1), cc.CallFunc:create(function()
  138. self.ui.Text_percent:setString("正在释放资源...")
  139. end)))
  140. end
  141. end
  142. function LoadingLayer:UpdateGMRes()
  143. local directoryPath = global.FileUtilCtl:getWritablePath()
  144. local loadingData = global.L_GameEnvManager:GetGMLoadingData()
  145. if loadingData and loadingData.bg_name and global.FileUtilCtl:isFileExist(directoryPath .. loadingData.bg_name) then
  146. self.ui.Image_1:loadTexture(directoryPath .. loadingData.bg_name)
  147. self.ui.Image_1:ignoreContentAdaptWithSize(true)
  148. end
  149. if loadingData and loadingData.bar_bg_name and global.FileUtilCtl:isFileExist(directoryPath .. loadingData.bar_bg_name) then
  150. self.ui.Image_bar:loadTexture(directoryPath .. loadingData.bar_bg_name)
  151. self.ui.Image_bar:ignoreContentAdaptWithSize(true)
  152. end
  153. if loadingData and loadingData.bar_name and global.FileUtilCtl:isFileExist(directoryPath .. loadingData.bar_name) then
  154. self.ui.LoadingBar_percent:loadTexture(directoryPath .. loadingData.bar_name)
  155. self.ui.LoadingBar_percent:ignoreContentAdaptWithSize(true)
  156. end
  157. end
  158. function LoadingLayer:CloseLayer()
  159. if self._isForceLoad and global.L_GameEnvManager:GetEnvDataByKey("pc_close_secoend_tips") ~= 1 then
  160. global.L_NativeBridgeManager:GN_IsCan_Windown_Close_State({state=1})
  161. end
  162. if self._downSchID then
  163. self.ui.Text_percent:stopAllActions()
  164. self._downSchID = nil
  165. end
  166. end
  167. function LoadingLayer:ShowGameNameAndAuthor()
  168. if not global.L_ModuleManager or not global.L_ModuleManager:GetCurrentModule() then
  169. releasePrint("can not find current module!!")
  170. return
  171. end
  172. if not global.isBoxLogin then
  173. return
  174. end
  175. local Panel_launch = ccui.Layout:create()
  176. Panel_launch:ignoreContentAdaptWithSize(false)
  177. Panel_launch:setClippingEnabled(false)
  178. Panel_launch:setBackGroundColorOpacity(102)
  179. Panel_launch:setTouchEnabled(true);
  180. Panel_launch:setLayoutComponentEnabled(true)
  181. Panel_launch:setName("Panel_launch")
  182. Panel_launch:setTag(25)
  183. Panel_launch:setCascadeColorEnabled(true)
  184. Panel_launch:setCascadeOpacityEnabled(true)
  185. Panel_launch:setAnchorPoint(0.5000, 0.5000)
  186. Panel_launch:setPosition(567.8864, 313.0880)
  187. local layout = ccui.LayoutComponent:bindLayoutComponent(Panel_launch)
  188. layout:setPositionPercentXEnabled(true)
  189. layout:setPositionPercentYEnabled(true)
  190. layout:setPositionPercentX(0.4999)
  191. layout:setPositionPercentY(0.4892)
  192. layout:setPercentWidth(1.0000)
  193. layout:setPercentHeight(1.0000)
  194. layout:setSize({width = 1136.0000, height = 640.0000})
  195. layout:setLeftMargin(-0.1136)
  196. layout:setRightMargin(0.1135)
  197. layout:setTopMargin(6.9120)
  198. layout:setBottomMargin(-6.9120)
  199. self.ui.nativeUI:addChild(Panel_launch)
  200. local curModule = global.L_ModuleManager:GetCurrentModule()
  201. local curSubModule = curModule:GetCurrentSubMod()
  202. local modGameEnv = curSubModule:GetGameEnv()
  203. local loadingShowName = modGameEnv:GetCustomDataByKey("loadingShowName") or ""
  204. if global.isWindows then
  205. loadingShowName = modGameEnv:GetCustomDataByKey("boxPcLoadingShowName") or ""
  206. end
  207. local loadingAuthor = global.L_GameEnvManager:GetEnvDataByKey("loadingAuthor") or ""
  208. local visibleSize = cc.Director:getInstance():getVisibleSize()
  209. local loadingShowNameLabelPos = cc.p(100, visibleSize.height - 70)
  210. local loadingShowNameLabel = ccui.Text:create()
  211. loadingShowNameLabel:setFontName("fonts/font.ttf")
  212. loadingShowNameLabel:setFontSize(36)
  213. loadingShowNameLabel:setAnchorPoint(0,0)
  214. loadingShowNameLabel:setString(loadingShowName)
  215. loadingShowNameLabel:setPosition(loadingShowNameLabelPos)
  216. loadingShowNameLabel:enableOutline(cc.c3b(0x19,0x13,0x0D), 2)
  217. Panel_launch:addChild(loadingShowNameLabel)
  218. local loadingAuthorLabel = ccui.Text:create()
  219. loadingAuthorLabel:setFontName("fonts/font.ttf")
  220. loadingAuthorLabel:setFontSize(20)
  221. loadingAuthorLabel:setAnchorPoint(0, 1)
  222. loadingAuthorLabel:setString(loadingAuthor)
  223. loadingAuthorLabel:setPosition( loadingShowNameLabelPos.x, loadingShowNameLabelPos.y - 15)
  224. loadingAuthorLabel:setTextColor(cc.c3b(0x9A,0x9B,0x9B))
  225. loadingAuthorLabel:enableOutline(cc.c3b(0x19,0x13,0x0D), 2)
  226. Panel_launch:addChild(loadingAuthorLabel)
  227. end
  228. return LoadingLayer