LoadingHelper.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. local LoadingHelper = class("LoadingHelper")
  2. local Queue = requireUtil( "queue" )
  3. function LoadingHelper:ctor( interval )
  4. self._timerID = nil
  5. self._timerInterval = interval or 0
  6. self._checkFinish = true
  7. self._taskQueue = Queue.new()
  8. self._taskCount = 0
  9. self._firstRun = true
  10. self._forEnterWorld = false
  11. self._isCompleted = false
  12. self._extraRef = 0
  13. self._lastParam = nil -- for skip load failed
  14. end
  15. function LoadingHelper:CheckFinishEnable( enable )
  16. self._checkFinish = enable
  17. end
  18. function LoadingHelper:setForEnterWorldLoading( value )
  19. self._forEnterWorld = value
  20. end
  21. function LoadingHelper:AddLoadingTask( loadingVec, callback, loadingUnit, step )
  22. local task = {}
  23. task.loadingVec = loadingVec
  24. task.callback = callback
  25. task.loadingUnit = loadingUnit
  26. task.size = #loadingVec
  27. task.index = 1
  28. task.step = step or 5
  29. self._taskQueue:push( task )
  30. self._taskCount = self._taskCount + task.size
  31. if not self._timerID then
  32. self:TimerBegan()
  33. end
  34. end
  35. function LoadingHelper:Tick()
  36. if self._taskQueue:empty() then -- loading finish
  37. if self._checkFinish then
  38. self:Finish()
  39. end
  40. return nil
  41. end
  42. local task = self._taskQueue:front()
  43. local step = task.step or 3
  44. if self._firstRun then -- avoid black
  45. step = 1
  46. self._firstRun = false
  47. end
  48. for i = 1, step do
  49. if not self:run( task ) then
  50. break
  51. end
  52. end
  53. end
  54. function LoadingHelper:run( task )
  55. if task.index > task.size then
  56. return false
  57. end
  58. local value = task.loadingVec[task.index]
  59. self._taskCount = self._taskCount - 1
  60. task.index = task.index + 1
  61. if task.callback and value ~= self._lastParam then
  62. -- loading
  63. task.callback( value )
  64. end
  65. self._lastParam = value
  66. -- loading progress
  67. if self._forEnterWorld then
  68. EnterWorldLoadingProgress( task.loadingUnit )
  69. end
  70. if task.index > task.size then -- loading next
  71. self._taskQueue:pop()
  72. return false
  73. end
  74. return true
  75. end
  76. function LoadingHelper:TimerBegan()
  77. if not self._timerID then
  78. local interval = self._timerInterval or 0
  79. self._timerID = Schedule( handler( self, self.Tick ), interval )
  80. end
  81. end
  82. function LoadingHelper:TimerEnded()
  83. if self._timerID then
  84. UnSchedule( self._timerID )
  85. self._timerID = nil
  86. self._taskQueue:clear()
  87. end
  88. end
  89. function LoadingHelper:Finish()
  90. self:TimerEnded()
  91. self._isCompleted = true
  92. if self._extraRef <= 0 then -- extra task is completed
  93. self:Completed()
  94. end
  95. end
  96. function LoadingHelper:Completed()
  97. if self._forEnterWorld then
  98. global.LoadingHelper = nil -- clear self
  99. global.Facade:sendNotification( global.NoticeTable.LoadingCompleted )
  100. end
  101. end
  102. function LoadingHelper:AddExtraRef()
  103. self._extraRef = self._extraRef + 1
  104. end
  105. function LoadingHelper:ReleaseExtraRef()
  106. self._extraRef = self._extraRef - 1
  107. if 0 == self._extraRef and self._isCompleted then
  108. self:Completed()
  109. end
  110. end
  111. return LoadingHelper