void ScrollViewDemo::setup() { CCClippingNode *clipper = CCClippingNode::create(); clipper->setTag( kTagClipperNode ); clipper->setContentSize( CCSizeMake(200, 200) ); clipper->setAnchorPoint( ccp(0.5, 0.5) ); clipper->setPosition( ccp(this->getContentSize().width / 2, this->getContentSize().height / 2) ); clipper->runAction(CCRepeatForever::create(CCRotateBy::create(1, 45))); this->addChild(clipper); CCDrawNode *stencil = CCDrawNode::create(); CCPoint rectangle[4]; rectangle[0] = ccp(0, 0); rectangle[1] = ccp(clipper->getContentSize().width, 0); rectangle[2] = ccp(clipper->getContentSize().width, clipper->getContentSize().height); rectangle[3] = ccp(0, clipper->getContentSize().height); ccColor4F white = {1, 1, 1, 1}; stencil->drawPolygon(rectangle, 4, white, 1, white); clipper->setStencil(stencil); CCSprite *content = CCSprite::create(s_back2); content->setTag( kTagContentNode ); content->setAnchorPoint( ccp(0.5, 0.5) ); content->setPosition( ccp(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) ); clipper->addChild(content); m_bScrolling = false; this->setTouchEnabled(true); }
void NestedTest::setup() { static int depth = 9; CCNode *parent = this; for (int i = 0; i < depth; i++) { int size = 225 - i * (225 / (depth * 2)); CCClippingNode *clipper = CCClippingNode::create(); clipper->setContentSize(CCSizeMake(size, size)); clipper->setAnchorPoint(ccp(0.5, 0.5)); clipper->setPosition( ccp(parent->getContentSize().width / 2, parent->getContentSize().height / 2) ); clipper->setAlphaThreshold(0.05f); clipper->runAction(CCRepeatForever::create(CCRotateBy::create(i % 3 ? 1.33 : 1.66, i % 2 ? 90 : -90))); parent->addChild(clipper); CCNode *stencil = CCSprite::create(s_pPathGrossini); stencil->setScale( 2.5 - (i * (2.5 / depth)) ); stencil->setAnchorPoint( ccp(0.5, 0.5) ); stencil->setPosition( ccp(clipper->getContentSize().width / 2, clipper->getContentSize().height / 2) ); stencil->setVisible(false); stencil->runAction(CCSequence::createWithTwoActions(CCDelayTime::create(i), CCShow::create())); clipper->setStencil(stencil); clipper->addChild(stencil); parent = clipper; } }
void BasicTest::setup() { CCSize s = CCDirector::sharedDirector()->getWinSize(); CCNode *stencil = this->stencil(); stencil->setTag( kTagStencilNode ); stencil->setPosition( ccp(50, 50) ); CCClippingNode *clipper = this->clipper(); clipper->setTag( kTagClipperNode ); clipper->setAnchorPoint(ccp(0.5, 0.5)); clipper->setPosition( ccp(s.width / 2 - 50, s.height / 2 - 50) ); clipper->setStencil(stencil); this->addChild(clipper); CCNode *content = this->content(); content->setPosition( ccp(50, 50) ); clipper->addChild(content); }
bool CSignLayer::init() { if (BaseLayer::init()) { MaskLayer* lay = MaskLayer::create("CSignLayermask"); lay->setContentSize(CCSizeMake(2824,640)); LayerManager::instance()->push(lay); m_ui = LoadComponent("Sign.xaml"); // SelectSkill m_ui->setPosition(VCENTER); this->addChild(m_ui); this->setOpacity(180); //背景框 CImageView* pImageRect = (CImageView*)m_ui->findWidgetById("board_sign"); //添加一个裁切层 CCClippingNode* pClip = CCClippingNode::create(); CImageView* pImage = UICloneMgr::cloneImageView(pImageRect); pImage->setAnchorPoint(ccp(0.5f, 0.5f)); pImage->setPosition(ccp(pImage->getContentSize().width/2, pImage->getContentSize().height/2)); pClip->setStencil(pImage); pClip->setContentSize(pImageRect->getContentSize()); pClip->setAnchorPoint(pImageRect->getAnchorPoint()); pClip->setPosition(pImageRect->getPosition()); pImageRect->getParent()->addChild(pClip, pImageRect->getZOrder()+1); m_pClip = pClip; //展示区图片 m_show_info_scroll = (CScrollView*)m_ui->findWidgetById("scroll_info"); m_show_info_scroll->setDirection(eScrollViewDirectionVertical); m_show_info_scroll->setBounceable(true); m_pInfo1 = (CLabel*)m_ui->findWidgetById("info_1"); CC_SAFE_RETAIN(m_pInfo1); m_pInfo1->removeFromParentAndCleanup(false); m_show_info_scroll->getContainer()->addChild(m_pInfo1); m_pInfo1->setAnchorPoint(ccp(0, 1)); CC_SAFE_RELEASE(m_pInfo1); m_pInfo2 = (CLabel*)m_ui->findWidgetById("info_2"); CC_SAFE_RETAIN(m_pInfo2); m_pInfo2->removeFromParentAndCleanup(false); m_show_info_scroll->getContainer()->addChild(m_pInfo2); m_pInfo2->setAnchorPoint(ccp(0, 1)); CC_SAFE_RELEASE(m_pInfo2); updateShowInfoScroll(); m_show_info_scroll->setVisible(false); CCNode* pMaskInfo = (CCNode*)m_ui->findWidgetById("mask_info"); pMaskInfo->setVisible(false); m_pLineEffect = new CLineLightEffect; m_pLineEffect->bindUI(m_ui); m_pLineEffect->bindRectEffect(pImageRect); this->addChild(m_pLineEffect, 999); m_pYellowTip = CCSprite::create("sign/lightbox5.png"); m_pYellowTip->setAnchorPoint(ccp(0.5f, 0.0f)); m_pYellowTip->setScale(0.8f); m_pYellowTip->runAction(CCRepeatForever::create(CCSequence::createWithTwoActions(CCMoveBy::create(0.3f, ccp(0, -10)), CCMoveBy::create(0.3f, ccp(0, 10))))); m_ui->addChild(m_pYellowTip, 50); m_pYellowTipText = UICloneMgr::cloneLable((CLabel*)m_ui->findWidgetById("day1")); m_pYellowTipText->setAnchorPoint(ccp(0.5f, 0.5f)); m_pYellowTipText->setPosition(ccp(m_pYellowTip->getContentSize().width/2, m_pYellowTip->getContentSize().height/2+12)); m_pYellowTipText->setScale(1.2f); m_pYellowTip->addChild(m_pYellowTipText); m_pYellowTip->setVisible(false); return true; } return false; }
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); } }
bool ImageEditLayer::init(){ if (DialogLayer::init()) { this->setTitle("头像编辑"); CCPoint middle=ccp(286, 154); CCSize size=CCSizeMake(570,300); CCClippingNode* clippingNode = CCClippingNode::create(); //设置裁剪区域大小 clippingNode->setContentSize(size); clippingNode->setAnchorPoint(ccp(0.5, 0.5)); clippingNode->setPosition(middle); m_contentLayer->addChild(clippingNode); CCTexture2D* textrue=new CCTexture2D(); textrue->autorelease(); textrue->initWithImage(_image); _sprite=CCSprite::createWithTexture(textrue); _initPoint=ccp(clippingNode->getContentSize().width/2, clippingNode->getContentSize().height/2); _sprite->setPosition(_initPoint); CCSize spriteSize=_sprite->getContentSize(); if (size.width/spriteSize.width<size.height/spriteSize.height) { _scale=size.width/spriteSize.width; }else{ _scale=size.height/spriteSize.height; } _sprite->setScale(_scale); clippingNode->addChild(_sprite); //创建裁剪模板,裁剪节点将按照这个模板来裁剪区域 CCDrawNode *stencil = CCDrawNode::create(); CCPoint rectangle[4]; rectangle[0] = ccp(0, 0); rectangle[1] = ccp(clippingNode->getContentSize().width, 0); rectangle[2] = ccp(clippingNode->getContentSize().width, clippingNode->getContentSize().height); rectangle[3] = ccp(0, clippingNode->getContentSize().height); ccColor4F white = {1, 1, 1, 1}; //画一个多边形 这画一个200x200的矩形作为模板 stencil->drawPolygon(rectangle, 4, white, 1, white); clippingNode->setStencil(stencil); //用来设置显示裁剪区域还是非裁剪区域的 clippingNode->setInverted(false);//在裁剪区域内显示加入的内容 _clip=CCClippingNode::create();//创建裁剪节点,成员变量 _clip->setInverted(true);//设置底板可见 _clip->setAlphaThreshold(0.0f);//设置alpha为0 m_contentLayer->addChild(_clip);//添加裁剪节点 _mask=CCLayerColor::create(ccc4(0,0,0,160),size.width,size.height); CCPoint point=ccp(middle.x-size.width/2,middle.y-size.height/2); _mask->setPosition(point); _clip->addChild(_mask);//为裁剪节点添加一个黑色带透明(看起了是灰色)的底板 _stencil=CCSprite::create("default_avatar.png");//使用头像原图作为模板 _stencil->setPosition(middle); _stencil->setScale(STENCIL_SCALE); _clip->setStencil(_stencil);//设置模版 CCSprite* queding=CCSprite::createWithSpriteFrameName("touxiang_queding.png"); CCMenuItemSprite* item=CCMenuItemSprite::create(queding, queding, this, menu_selector(ImageEditLayer::menuCallback)); item->setPosition(ccp(290,-30));//注意contentlayer坐标原点位置 item->setScale(0.7); item->setTag(kTagConfirm); this->addMenuItem(item,true); CCSprite* fanhui=CCSprite::createWithSpriteFrameName("touxiang_fanhui.png"); fanhui->setScale(0.87); CCMenuItemSprite* fanhuiItem=CCMenuItemSprite::create(fanhui, fanhui, this, menu_selector(ImageEditLayer::menuCallback)); fanhuiItem->setPosition(ccp(19,368)); fanhuiItem->setTag(kTagBack); this->addMenuItem(fanhuiItem,true); CCPoint contentPoint=m_contentLayer->getPosition(); _rect=CCRectMake(contentPoint.x+point.x, contentPoint.y+point.y, size.width,size.height); return true; } return false; }