void CCScrollBar::attachToUIScrollView(ScrollView* scrollView, ccInsets insets, bool horizontal) { // save flag m_horizontal = horizontal; // add to scroll view float thumbLength = 0; Widget* svParent = dynamic_cast<Widget*>(scrollView->getParent()); CCSize svSize = scrollView->getSize(); CCPoint svOrigin = CCUtils::getOrigin(scrollView); CCSize innerSize = scrollView->getInnerContainerSize(); CCSize sbSize; if(horizontal) { sbSize = CCSizeMake(m_track->getContentSize().width, svSize.width - insets.left - insets.right); setContentSize(sbSize); setAnchorPoint(ccp(0, 0.5f)); setPosition(ccp(svOrigin.x + svSize.width / 2, svOrigin.y + insets.bottom)); setRotation(-90); svParent->addNode(this, MAX_INT); // thumb length if(m_fixedThumb) thumbLength = m_fixedThumb->getContentSize().height; else thumbLength = MIN(1, svSize.width / innerSize.width) * sbSize.height; } else { sbSize = CCSizeMake(m_track->getContentSize().width, svSize.height - insets.top - insets.bottom); setContentSize(sbSize); setAnchorPoint(ccp(1, 0.5f)); setPosition(ccp(svOrigin.x + svSize.width - insets.right, svOrigin.y + svSize.height / 2)); svParent->addNode(this, MAX_INT); // thumb length if(m_fixedThumb) thumbLength = m_fixedThumb->getContentSize().height; else thumbLength = MIN(1, svSize.height / innerSize.height) * sbSize.height; } // add track m_track->setPreferredSize(sbSize); m_track->setPosition(CCUtils::getLocalCenter(this)); addChild(m_track); // clipping node to hold thumb CCClippingNode* thumbClipping = CCClippingNode::create(m_track); thumbClipping->ignoreAnchorPointForPosition(false); thumbClipping->setAnchorPoint(ccp(0.5f, 0.5f)); thumbClipping->setContentSize(sbSize); thumbClipping->setPosition(CCUtils::getLocalCenter(this)); thumbClipping->setAlphaThreshold(0.5f); thumbClipping->setScaleX((sbSize.width - 4) / sbSize.width); thumbClipping->setScaleY((sbSize.height - 4) / sbSize.height); addChild(thumbClipping); // thumb or fixed thumb if(m_thumb) { m_thumb->setPreferredSize(CCSizeMake(sbSize.width, thumbLength)); m_thumb->setPosition(ccp(sbSize.width / 2, sbSize.height - thumbLength / 2)); thumbClipping->addChild(m_thumb); } else { m_fixedThumb->setPosition(ccp(sbSize.width / 2, sbSize.height - thumbLength / 2)); thumbClipping->addChild(m_fixedThumb); } // sync thumb position syncThumbPositionForUIScrollView(scrollView); // listen to scrollview scrolling event scrollView->addEventListenerScrollView(this, scrollvieweventselector(CCScrollBar::onUIScrollViewEvent)); // init fade out if(m_initFadeOut) { m_fadingOut = true; CCUtils::setOpacityRecursively(this, 0); } }
void CCScrollBar::attachToCCScrollView(CCScrollView* scrollView, ccInsets insets, bool horizontal) { // it must have parent node CCNode* svParent = scrollView->getParent(); if(!svParent) { CCLOGWARN("CCScrollView must be added to one node before calling attachToCCScrollView"); return; } // save flag m_horizontal = horizontal; // add to scroll view float thumbLength = 0; CCPoint svOrigin = CCUtils::getOrigin(scrollView); CCSize svSize = scrollView->getViewSize(); CCSize innerSize = scrollView->getContainer()->getContentSize(); CCSize sbSize; if(horizontal) { sbSize = CCSizeMake(m_track->getContentSize().width, svSize.width - insets.left - insets.right); setContentSize(sbSize); setAnchorPoint(ccp(0, 0.5f)); setPosition(ccp(svOrigin.x + svSize.width / 2, svOrigin.y + insets.bottom)); setRotation(-90); UIWidget* svpWidght = dynamic_cast<UIWidget*>(svParent); if(svpWidght) svpWidght->addNode(this, MAX_INT); else svParent->addChild(this, MAX_INT); // thumb length if(m_fixedThumb) thumbLength = m_fixedThumb->getContentSize().height; else thumbLength = MIN(1, svSize.width / innerSize.width) * sbSize.height; } else { sbSize = CCSizeMake(m_track->getContentSize().width, svSize.height - insets.top - insets.bottom); setContentSize(sbSize); setAnchorPoint(ccp(1, 0.5f)); setPosition(ccp(svOrigin.x + svSize.width - insets.right, svOrigin.y + svSize.height / 2)); UIWidget* svpWidght = dynamic_cast<UIWidget*>(svParent); if(svpWidght) svpWidght->addNode(this, MAX_INT); else svParent->addChild(this, MAX_INT); // thumb length if(m_fixedThumb) thumbLength = m_fixedThumb->getContentSize().height; else thumbLength = MIN(1, svSize.height / innerSize.height) * sbSize.height; } // add track m_track->setPreferredSize(sbSize); m_track->setPosition(CCUtils::getLocalCenter(this)); addChild(m_track); // clipping node to hold thumb CCClippingNode* thumbClipping = CCClippingNode::create(m_track); thumbClipping->ignoreAnchorPointForPosition(false); thumbClipping->setAnchorPoint(ccp(0.5f, 0.5f)); thumbClipping->setContentSize(sbSize); thumbClipping->setPosition(CCUtils::getLocalCenter(this)); thumbClipping->setAlphaThreshold(0.5f); thumbClipping->setScaleX((sbSize.width - 4) / sbSize.width); thumbClipping->setScaleY((sbSize.height - 4) / sbSize.height); addChild(thumbClipping); // thumb or fixed thumb if(m_thumb) { m_thumb->setPreferredSize(CCSizeMake(sbSize.width, thumbLength)); m_thumb->setPosition(ccp(sbSize.width / 2, sbSize.height - thumbLength / 2)); thumbClipping->addChild(m_thumb); } else { m_fixedThumb->setPosition(ccp(sbSize.width / 2, sbSize.height - thumbLength / 2)); thumbClipping->addChild(m_fixedThumb); } // sync thumb position syncThumbPositionForCCScrollView(scrollView); // delegate m_oldCCDelegate = scrollView->getDelegate(); scrollView->setDelegate(this); // init fade out if(m_initFadeOut) { m_fadingOut = true; CCUtils::setOpacityRecursively(this, 0); } }