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.

189 lines
5.6 KiB

  1. cc.ScrollView.ClassName = "ScrollView"
  2. function cc.ScrollView:saveToXmlNode(xmlNode)
  3. cc.Layout.saveToXmlNode(self , xmlNode);
  4. xmlNode.BounceEnabled = self:isBounceEnabled();
  5. xmlNode.Direction = self:getDirection();
  6. xmlNode.InnerContainerSize = self:getInnerContainerSize();
  7. end
  8. function cc.ScrollView:loadFromXmlNode(xmlNode)
  9. cc.Layout.loadFromXmlNode(self , xmlNode);
  10. self:setBounceEnabled(xmlNode.BounceEnabled);
  11. self:setDirection(xmlNode.Direction);
  12. self:setInnerContainerSize(xmlNode.InnerContainerSize);
  13. end
  14. function cc.ScrollView:extend(node)
  15. cc.Layout:extend(node);
  16. node:setName("ScrollView");
  17. local vBar = node:getVBar();
  18. vBar:setBackGroundImage("jindutiao_01.png" , cc.TextureResType.plistType);
  19. vBar:setProgressImage("res/ui/zy_tongyong/jindutiao_1.png");
  20. local hBar = node:getHBar();
  21. hBar:setBackGroundImage("jindutiao_01.png" , cc.TextureResType.plistType);
  22. hBar:setProgressImage("res/ui/zy_tongyong/jindutiao_1.png");
  23. -- 0.1Ӣէ
  24. node:setChildFocusCancelOffset(cc.Device:getDPI() * 0.1);
  25. end
  26. function cc.ScrollView:hideAllBar()
  27. local vBar = self:getVBar();
  28. vBar:setVisible(false)
  29. local hBar = self:getHBar();
  30. hBar:setVisible(false)
  31. end
  32. function cc.ScrollView:jumpToTopOnSizeChanged()
  33. local function onEvent(ui, eventType)
  34. if eventType == 3 then
  35. self:jumpToTop();
  36. end
  37. end
  38. self:getInnerContainer():setWidgetEventListener(onEvent)
  39. end
  40. function cc.ScrollView:jumpToBottomOnSizeChanged()
  41. local function onEvent(ui, eventType)
  42. if eventType == 3 then
  43. self:jumpToBottom();
  44. end
  45. end
  46. self:getInnerContainer():setWidgetEventListener(onEvent)
  47. end
  48. -- ܱփؓޚ֣քλ׃љؖ҈ìࠉӔԃ4ַԃjumpToPercentHorizontalìscrollToֈگ˽ì׵ܘքˇ{x = [0-100] , y = [0-100]};
  49. function cc.ScrollView:getCenterPercent(childNode)
  50. local world = childNode:getWorldTranslation();
  51. local size = childNode:getContentSize();
  52. local anchor = childNode:getAnchorPoint();
  53. world.x = world.x + (size.width) / 2;
  54. world.y = world.y + (size.height) / 2;
  55. local nodePos = self:getInnerContainer():convertToNodeSpace(world);
  56. local percent = self:getPercent(nodePos);
  57. return cc.p(percent.x * 100 , percent.y * 100);
  58. end
  59. -- Ƀ֢ٶޚ֣ߓא
  60. function cc.ScrollView:scrollXToCenter(childNode , scrollTime)
  61. local x = self:getCenterPercent(childNode).x;
  62. --print("xxxx" , x , childNode:getPosition().x);
  63. self:scrollToPercentHorizontal(x , scrollTime , true);
  64. end
  65. -- Ƀ֢ٶޚ֣ߓא
  66. function cc.ScrollView:scrollYToCenter(childNode , scrollTime)
  67. self:scrollToPercentVertical(self:getCenterPercent(childNode).y , scrollTime , true);
  68. end
  69. -- 获得子节点的位置百分比,可以用来调用jumpToPercentHorizontal,scrollTo等函数,返回的是{x = [0-100] , y = [0-100]};
  70. function cc.ScrollView:getChildPercent(childNode)
  71. local world = childNode:getPosition();
  72. local nodeSize = childNode:getContentSize();
  73. local anchor = childNode:getAnchorPoint();
  74. world.x = world.x - (nodeSize.width) * anchor.x;
  75. world.y = world.y - (nodeSize.height) * anchor.y;
  76. local innerSize = self:getInnerContainerSize();
  77. local size = self:getSize();
  78. -- scrollView最下面是0位置而百分比上面是0
  79. world.y = innerSize.height - world.y - nodeSize.height
  80. local perscent = cc.p(0,0)
  81. local remainSize = cc.size(innerSize.width - size.width, innerSize.height - size.height)
  82. if remainSize.width == 0 then
  83. perscent.x = 0
  84. else
  85. perscent.x = world.x / remainSize.width
  86. end
  87. if remainSize.height == 0 then
  88. perscent.y = 0
  89. else
  90. perscent.y = world.y / remainSize.height
  91. end
  92. if perscent.x > 1 then
  93. perscent.x = 1
  94. end
  95. if perscent.y > 1 then
  96. perscent.y = 1
  97. end
  98. return cc.p(perscent.x * 100 , perscent.y * 100);
  99. end
  100. -- 滚动到节点所在的位置
  101. function cc.ScrollView:scrollXToChild(childNode , scrollTime)
  102. local x = self:getChildPercent(childNode).x;
  103. --print("xxxx" , x , childNode:getPosition().x);
  104. self:scrollToPercentHorizontal(x , scrollTime , true);
  105. end
  106. -- 滚动到节点所在的位置
  107. function cc.ScrollView:scrollYToChild(childNode , scrollTime)
  108. self:scrollToPercentVertical(self:getChildPercent(childNode).y , scrollTime , true);
  109. end
  110. function cc.ScrollView:createNode()
  111. local layer = cc.ScrollView:create();
  112. return layer;
  113. end
  114. -- ʨ׃Ĭɏֵ
  115. function cc.ScrollView:setDefaults()
  116. cc.Layout.setDefaults(self);
  117. self:setBackGroundColor(cc.c3b(255,150,100));
  118. self:setBounceEnabled(true);
  119. self:loadDefaults();
  120. -- ĬɏѨҪЬӦtouch˂ݾ
  121. self:setTouchEnabled(true)
  122. end
  123. function cc.ScrollView:loadDefaults()
  124. self:addProtectedChild(self:createTouchSizeDrawer());
  125. end
  126. -- ԴݨԴݨڶ֯ȸԲ
  127. function cc.ScrollView:createTouchSizeDrawer()
  128. local glNode = gl.glNodeCreate()
  129. glNode:setAnchorPoint(cc.p(0, 0))
  130. local function primitivesDraw(transform, transformUpdated)
  131. local isStencilEnabled = gl.isEnabled(gl.STENCIL_TEST)
  132. if isStencilEnabled then
  133. gl.disable(gl.STENCIL_TEST);
  134. end
  135. local isScissorEnabled = gl.isEnabled(gl.SCISSOR_TEST)
  136. if isScissorEnabled then
  137. gl.disable(gl.SCISSOR_TEST);
  138. end
  139. -- ѳ֨Рܺbˀާߘֳ
  140. cc.DrawPrimitives.setCamera(self:getActiveCamera());
  141. cc.DrawPrimitives.setWorldTransform(transform);
  142. -- ۭߘю
  143. gl.lineWidth(1)
  144. cc.DrawPrimitives.drawColor4B(255,255,255,255)
  145. local rect = cc.rect(0,self:getSize().height,self:getInnerContainerSize().width,-self:getInnerContainerSize().height);
  146. cc.DrawPrimitives.drawRect(cc.p(rect.x , rect.y), cc.p(rect.x + rect.width , rect.y + rect.height))
  147. -- ״̬ܖش
  148. gl.lineWidth(1)
  149. cc.DrawPrimitives.drawColor4B(255,255,255,255)
  150. cc.DrawPrimitives.setPointSize(1)
  151. if isStencilEnabled then
  152. gl.enable(gl.STENCIL_TEST);
  153. end
  154. if isScissorEnabled then
  155. gl.enable(gl.SCISSOR_TEST);
  156. end
  157. end
  158. glNode:registerScriptDrawHandler(primitivesDraw)
  159. return glNode;
  160. end