GUIQuickCell.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. local GUIQuickCell = class("GUIQuickCell", function()
  2. return ccui.Widget:create()
  3. end)
  4. -- self._data.wid default 100
  5. -- self._data.hei default 50
  6. -- self._data.anchor default {x=0, y=0}
  7. -- self._data.tick_interval default 0.02
  8. -- self._data.activeEvent default inview
  9. -- self._data.createCell default nil
  10. function GUIQuickCell:ctor()
  11. self._entered = false
  12. self._active = true
  13. end
  14. function GUIQuickCell:Create( data )
  15. local cell = GUIQuickCell.new()
  16. if cell and cell:Init( data ) then
  17. return cell
  18. end
  19. return nil
  20. end
  21. function GUIQuickCell:Init( data )
  22. self._data = data
  23. local wid = self._data.wid or 100
  24. local hei = self._data.hei or 50
  25. local anchor = self._data.anchor or {x=0, y=0}
  26. self:setContentSize( {width = wid, height = hei} )
  27. self:setAnchorPoint( anchor )
  28. self:Active()
  29. return true
  30. end
  31. function GUIQuickCell:Update( data )
  32. local createCell = data.createCell
  33. self._data.createCell = createCell
  34. -- 执行以下退出,方便立即刷新
  35. self:Exit()
  36. end
  37. function GUIQuickCell:Enter()
  38. if self._entered then
  39. return false
  40. end
  41. self._entered = true
  42. local createCell = self._data.createCell
  43. local wid = self._data.wid or 100
  44. local hei = self._data.hei or 50
  45. if not createCell then
  46. return false
  47. end
  48. local cell = createCell(self)
  49. cell:setAnchorPoint({x=0.5, y=0.5})
  50. cell:setPosition({x=wid/2, y=hei/2})
  51. end
  52. function GUIQuickCell:Exit()
  53. if not self._entered then
  54. return false
  55. end
  56. self._entered = false
  57. self:removeAllChildren()
  58. end
  59. function GUIQuickCell:Refresh()
  60. if not self._active then
  61. return false
  62. end
  63. local active = true
  64. local activeEvent = self._data.activeEvent
  65. if activeEvent then
  66. active = activeEvent(self)
  67. elseif self._data.eventX then
  68. local viewSize = global.Director:getVisibleSize()
  69. local pos = self:getWorldPosition()
  70. local wid = self._data.wid or 100
  71. local hei = self._data.hei or 50
  72. active = not ( pos.x >= viewSize.width + wid or pos.x <= -wid )
  73. else
  74. local viewSize = global.Director:getVisibleSize()
  75. local pos = self:getWorldPosition()
  76. local wid = self._data.wid or 100
  77. local hei = self._data.hei or 50
  78. active = not ( pos.y >= viewSize.height + hei or pos.y <= -hei )
  79. end
  80. if active then
  81. self:Enter()
  82. else
  83. self:Exit()
  84. end
  85. end
  86. function GUIQuickCell:Active()
  87. self._active = true
  88. local tick_interval = self._data.tick_interval or 0.02
  89. self:stopAllActions()
  90. schedule(self, function()
  91. self:Refresh()
  92. end, tick_interval)
  93. end
  94. function GUIQuickCell:Sleep()
  95. self._active = false
  96. self:stopAllActions()
  97. end
  98. return GUIQuickCell