void MenuButton::onButton() { if (_source) { auto menu = getMenu(); CC_SAFE_RETAIN(menu); if (auto cb = _source->getCallback()) { cb(this, _source); } if (auto nextMenu = _source->getNextMenu()) { auto scene = Scene::getRunningScene(); auto size = scene->getContentSize(); auto posLeft = convertToWorldSpace(cocos2d::Vec2(0, _contentSize.height)); auto posRight = convertToWorldSpace(cocos2d::Vec2(_contentSize.width, _contentSize.height)); float pointLeft = posLeft.x; float pointRight = size.width - posRight.x; if (pointRight >= pointLeft) { construct<FloatingMenu>(nextMenu, posRight, FloatingMenu::Binding::OriginRight, dynamic_cast<FloatingMenu *>(_menu)); } else { construct<FloatingMenu>(nextMenu, posLeft, FloatingMenu::Binding::OriginLeft, dynamic_cast<FloatingMenu *>(_menu)); } } if (menu) { menu->onMenuButtonPressed(this); menu->release(); } } }
// on "init" you need to initialize your instance bool HelloWorld::init() { if ( !Layer::init() ) { return false; } FileUtils::getInstance()->addSearchPath("../QtCocos2d/Resources"); FileUtils::getInstance()->addSearchResolutionsOrder("resource-auto"); Director::getInstance()->setContentScaleFactor(2.0f); Director::getInstance()->setAnimationInterval(FLT_MIN); Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto centerNode = Node::create(); centerNode->setAnchorPoint(Point::ANCHOR_MIDDLE); // centerNode->setContentSize(visibleSize); centerNode->setPosition(origin + visibleSize/2); this->addChild(centerNode); auto frame = Sprite::create("system/frame_iphone4.png"); frame->setAnchorPoint(Point::ANCHOR_MIDDLE); centerNode->addChild(frame); auto worldPos = centerNode->convertToWorldSpace(frame->getPosition()); qDebug() << worldPos.x << "," << worldPos.y; auto listener = EventListenerMouse::create(); listener->onMouseScroll = [this, frame](Event* event){ EventMouse *e = dynamic_cast<EventMouse *>(event); if (NULL != e) { float scrollY = e->getScrollY(); frame->setScale(scrollY / 10.0f + frame->getScale()); } }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto baseNode = Node::create(); baseNode->setContentSize(Size(480, 320)); baseNode->setPosition(baseNode->getContentSize() / -2 + frame->getContentSize()/2); frame->addChild(baseNode); worldPos = frame->convertToWorldSpace(baseNode->getPosition()); qDebug() << worldPos.x << "," << worldPos.y; /* ControlButton * button = ControlButton::create(); button->setTitleLabelForState(Label::createWithSystemFont("aaa", "MS Gothic", 12), Control::State::NORMAL); baseNode->addChild(button); */ _metaInfo = InterfaceLoader::getInstance().loadJSon("interface/scene/scenetest.json"); if (_metaInfo) { _metaInfo->retain(); baseNode->addChild(_metaInfo->getBaseNode()); } return true; }
bool MyMap::mapReload(float xpos){ auto s = (Sprite*)curMaplist.begin()[0]; auto bp = convertToWorldSpace(s->getPosition()); if(bp.x+s->getContentSize().width<0){ s->removeFromParentAndCleanup(true); curMaplist.eraseObject(s); } auto lm =(Sprite*) curMaplist.at(curMaplist.size()-1); auto wp = convertToWorldSpace(lm->getPosition()); if(wp.x+lm->getContentSize().width < Director::getInstance()->getVisibleSize().width){ //auto p = (ParallaxNode*)lm->getParent(); if(layerMark ==MID_SCENE){ auto newMap =Sprite::create(PIC_MID); if(mapCount ==-1){ newMap->setFlippedX(true); } mapCount = mapCount*(-1); newMap->setAnchorPoint(Vec2(0,0)); mlayer->addChild(newMap,3,909); newMap->setPosition(lm->getPositionX()+lm->getContentSize().width,100); auto paraMove = MoveBy::create(2,Vec2(MID_RATE*SPEED*2,0)); auto seq =Sequence::create(paraMove,NULL); newMap->runAction(RepeatForever::create(seq)); curMaplist.pushBack(newMap); //float x = lm->getContentSize().width+lm->getPositionX()-MID_RATE*p->getPositionX()+p->getPositionX(); auto p = ParallaxNode::create(); //p->addChild(newMap,3,Vec2(MID_RATE,0),Vec2(x,100)); //newMap->setPosition(lm->getPositionX()+lm->getContentSize().width,100); } else{ auto newMap =Sprite::create(PIC_FAR); if(mapCount ==-1){ newMap->setFlippedX(true); } mapCount = mapCount*(-1); newMap->setAnchorPoint(Vec2(0,0)); mlayer->addChild(newMap); newMap->setPosition(lm->getPositionX()+lm->getContentSize().width,200); auto paraMove = MoveBy::create(2,Vec2(FAR_RATE*SPEED*2,0)); auto seq =Sequence::create(paraMove,NULL); newMap->runAction(RepeatForever::create(seq)); curMaplist.pushBack(newMap); //float x = lm->getContentSize().width+lm->getPositionX()-FAR_RATE*p->getPositionX()+p->getPositionX(); //p->addChild(newMap,2,Vec2(FAR_RATE,0),Vec2(x,0)); } return true; } else { return false; } }
void ClippingRectangleNode::onBeforeVisitScissor() { if (_clippingEnabled) { // record old ClipRegion GLView* glView = Director::getInstance()->getOpenGLView(); if (glView->isScissorEnabled()) { _preClipRegion = glView->getScissorRect(); } else { _preClipRegion = Rect::ZERO; } glEnable(GL_SCISSOR_TEST); float scaleX = _scaleX; float scaleY = _scaleY; Node *parent = this->getParent(); while (parent) { scaleX *= parent->getScaleX(); scaleY *= parent->getScaleY(); parent = parent->getParent(); } // push this ClipRegion const Point pos = convertToWorldSpace(Point(_clippingRegion.origin.x, _clippingRegion.origin.y)); glView->setScissorInPoints(pos.x, pos.y, _clippingRegion.size.width * scaleX, _clippingRegion.size.height * scaleY); } }
void Aimer::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) { float a = CC_RADIANS_TO_DEGREES((touch->getLocation()-convertToWorldSpace(getPosition())).getAngle()); setAngle(-a-getParent()->getParent()->getRotation()); }
bool ProjectileManager::isOutWindow(Projectile* aPorjectile) { // 先转换到世界坐标系中 auto tmpParent = aPorjectile->getShapePart()->getParent(); auto tmpPoint = tmpParent->convertToWorldSpace(aPorjectile->getMovingPart()->getPosition()); return !Rect(0, 0, 1280, 800).containsPoint(tmpPoint); }
void InteractiveBlock::setUnderEoloWind(LHNode* node, bool begin) { if (begin) { auto asset = dynamic_cast<LHAsset*>(this->getParent()); if (asset) { auto brazier = dynamic_cast<Brazier*>(asset->getChildNodeWithName(brazierDefs::BRAZIER_SHAPE)); if (brazier) { brazier->setUnderEoloEffect(); } } auto gameWorld = node->getParent(); auto objectAsset = this->getParent(); if (gameWorld && objectAsset) { Point screenCoord = gameWorld->convertToWorldSpace(node->getPosition()); Point relativePosition = objectAsset->convertToNodeSpace(screenCoord); _eoloStartOffset = this->getPosition() - relativePosition; this->startWindAnimation(); } } }
void BBAbstractSceneView::onTouchScale(float nDistance, float nFocusX, float nFocusY) { float oldScale = m_nScaleBeginScale; float newScale= oldScale * (nDistance / m_nScaleBeginDistance); setScale(newScale); // if(oldScale == getScale()) return; CCSize cs = getContentSize(); // CCLOG("scale cs{%d,%d}, {%f,%f}", (int)cs.width, (int)cs.height, m_ptScaleBeginFocusRate.x, m_ptScaleBeginFocusRate.y); CCPoint ptNewFocus = ccp( m_ptScaleBeginFocusRate.x * cs.width, m_ptScaleBeginFocusRate.y * cs.height ); ptNewFocus = convertToWorldSpace(ptNewFocus); CCPoint newPos = ccpAdd(getPosition(), ccpSub(m_ptScaleBeginFocus, ptNewFocus)); // CCLOG("newPos %d, %d", (int)newPos.x, (int)newPos.y); setPosition(newPos); //TODO: 实现一个移动并缩放地图的Action (动效) };
void DungeonLayer::characterMovedToLocation(CharacterDiceSprite* sprite, Vec2 location) { this->_resetCharacterMoveState(); auto newCoordinate = PositionUtil::coordinateForPosition(location); Game::getInstance()->characterMovedTo(newCoordinate); auto oldRoomSprite = (DungeonRoomSprite*) sprite->getParent(); auto newRoomSprite = (DungeonRoomSprite*) this->getRoomSpriteForCharacterCoordinate(); if (oldRoomSprite && newRoomSprite) { auto oldPosition = oldRoomSprite->convertToWorldSpace(sprite->getPosition()); auto newPosition = newRoomSprite->convertToNodeSpace(oldPosition); sprite->retain(); sprite->removeFromParent(); newRoomSprite->addChild(sprite); sprite->release(); newRoomSprite->setLocalZOrder(DUNGEON_ROOM_WITH_CHAR_Z_ORDER + OVERLAY_Z_ORDER); oldRoomSprite->setLocalZOrder(DUNGEON_ROOM_Z_ORDER + OVERLAY_Z_ORDER); sprite->setPosition(newPosition); oldRoomSprite->adjustChildren(); newRoomSprite->adjustChildren(); auto delay = DelayTime::create(MOVE_DICE_DURATION); auto restoreZOrder = CallFunc::create([=]{ newRoomSprite->setLocalZOrder(DUNGEON_ROOM_WITH_CHAR_Z_ORDER); oldRoomSprite->setLocalZOrder(DUNGEON_ROOM_Z_ORDER); }); this->runAction(Sequence::create(delay, restoreZOrder, NULL)); } }
void UINumberPicker::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *unused_event) { do { CC_BREAK_IF(!isVisible()); if (containsTouchObject(m_pTouches, touch)) { if (m_pTouches.size() == 1 && m_bDragging) { m_bTouchMoved = true; const Point &newPoint = convertTouchToNodeSpace(m_pTouches[0]); // Point moveDistance = Point(newPoint.x - m_tTouchPoint.x, // newPoint.y - m_tTouchPoint.y); m_tScrollDistance.set(newPoint.x - m_tTouchPoint.x, newPoint.y - m_tTouchPoint.y); m_tTouchPoint = newPoint; if (displayRect.containsPoint(convertToWorldSpace(newPoint))) { moveAllCell(m_tScrollDistance); } // m_tScrollDistance = moveDistance; } } } while (0); }
Vec2 getWorldPosition(Node* node) { Vec2 pt = node->getPosition(); auto parent = node->getParent(); if (parent) { pt = parent->convertToWorldSpace(pt); } return pt; }
NS_CC_EXT_BEGIN CCWebView::CCWebView(const CCPoint & pos,const CCSize &size,CCNode* parent) { parent->addChild(this); CCNode::setPosition(pos); CCNode::setContentSize(size); m_webviewImpl = new CCWebViewImpl(convertToWorldSpace(ccp(0,size.height)),size); }
void NS_CUSTOM::ParticleEffect::setFreeMode(bool isFree) { if (_freeMode == isFree)return; _freeMode = isFree; if (_freeMode){ const Vec2& pos = convertToWorldSpace(Vec2::ZERO); _lastWorldX = pos.x; _lastWorldY = pos.y; } }
cocos2d::Vec2 APTouchManager::getAbsolutePosition(cocos2d::Node* node) { auto position = node->getPosition(); auto parent = node->getParent(); if (parent != nullptr) { position = parent->convertToWorldSpace(position); } return position; }
void UIScrollLayer::setPosition(CCPoint point) { // set base layer's position CCSize size = getContentSize(); m_CenterWorldPos = convertToWorldSpace(point); m_ptCenter = point; m_ptOrigio.x = point.x - size.width / 2; m_ptOrigio.y = point.y - size.height / 2; m_baseLayer->setPosition(m_ptCenter); }
Rect TeachLayer::getNodeRect(cocos2d::Node *pNode) { if (!pNode) return Rect::ZERO; Rect rect = Rect(0, 0, 0, 0); auto pLayer = pNode->getParent(); rect.origin.x = pLayer->convertToWorldSpace(pNode->getPosition()).x - (pNode->getContentSize().width * 0.5); rect.origin.y = pLayer->convertToWorldSpace(pNode->getPosition()).y - (pNode->getContentSize().height * 0.5); rect.size.width = pNode->getContentSize().width * pNode->getScaleX(); rect.size.height = pNode->getContentSize().height * pNode->getScaleY(); return rect; }
cocos2d::Vec2 MaterialNode::getPositionForKeyShadow(float index) const { cocos2d::Vec2 vec = convertToWorldSpace(cocos2d::Vec2(_contentSize.width / 2, _contentSize.height / 2)); cocos2d::Size screenSize = cocos2d::Director::getInstance()->getWinSize(); cocos2d::Vec2 lightSource(screenSize.width / 2, screenSize.height); cocos2d::Vec2 normal = lightSource - vec; normal.normalize(); normal = normal - cocos2d::Vec2(0, -1); float keySeed = index * MATERIAL_SHADOW_KEY_MOD; return cocos2d::Vec2(-keySeed / 2 + _padding.left, -keySeed / 2 + _padding.bottom) - normal * _shadowIndex * 0.5; }
void IconLayer::createIconLabel(cocos2d::Point iconPosition, bool isLocked) { m_IconLabel->setLabelContents(isLocked); if (convertToWorldSpace(m_IconFrameDefault->getPosition()).y < m_WinHeight / 2 - 100) { m_IconLabel->setLabelPosition(iconPosition, true); } else { m_IconLabel->setLabelPosition(iconPosition, false); } m_Labeled = true; m_IconLabel->setGlobalZOrder(10); }
void IconLayer::setIconRect(cocos2d::Point iconPosition) { m_IconFrameDefault->setPosition(iconPosition); m_IconFrameLocked->setPosition(iconPosition); m_IconFrameSelected->setPosition(iconPosition); /* m_IconFrameClicked->setPosition(iconPosition);*/ m_IconContent->setPosition(iconPosition); m_IconRect.setRect(convertToWorldSpace(m_IconFrameDefault->getPosition()).x - m_IconFrameDefault->getContentSize().width * RESOLUTION / 2, convertToWorldSpace(m_IconFrameDefault->getPosition()).y - m_IconFrameDefault->getContentSize().height * RESOLUTION / 2, m_IconFrameDefault->getContentSize().width * RESOLUTION, m_IconFrameDefault->getContentSize().height * RESOLUTION); if (m_Labeled) { //Label 높이가 반영되면 Label 높이 계산해서 반영하도록! if (convertToWorldSpace(m_IconFrameDefault->getPosition()).y < m_WinHeight / 2 - 100) { m_IconLabel->setLabelPosition(iconPosition, true); } else { m_IconLabel->setLabelPosition(iconPosition, false); } } m_IconLabel->setGlobalZOrder(10); }
APTouchChecker APTouchManager::createCheckerWithRect(cocos2d::Node* node, const cocos2d::Rect& rect) { return [rect, node](cocos2d::Touch* touch)->bool { auto position = node->getPosition(); auto parent = node->getParent(); if (parent != nullptr) { position = parent->convertToWorldSpace(position); } auto r = cocos2d::Rect(rect); r.origin += position; return r.containsPoint(touch->getLocation()); }; }
bool Aimer::onTouchBegan(Touch* touch, Event* event) { if(convertToWorldSpace(_crosshair->getPosition()).getDistance(touch->getLocation()) < 50) { _pointer->runAction(ScaleTo::create(0.1, 2, _pointer->getScaleY())); _pointer->runAction(FadeTo::create(0.1, 255)); return true; } else { return false; } }
CCPoint CCNode::convertToWorldSpaceAR(CCPoint nodePoint) { CCPoint anchorInPoints; if( CC_CONTENT_SCALE_FACTOR() == 1 ) { anchorInPoints = m_tAnchorPointInPixels; } else { anchorInPoints = ccpMult( m_tAnchorPointInPixels, 1/CC_CONTENT_SCALE_FACTOR() ); } nodePoint = ccpAdd(nodePoint, anchorInPoints); return convertToWorldSpace(nodePoint); }
cocos2d::Rect APTouchManager::createDefaultRect(cocos2d::Node* node) { auto size = node->getContentSize(); auto position = node->getPosition(); auto anchor = node->getAnchorPoint(); auto parent = node->getParent(); if (parent != nullptr) { position = parent->convertToWorldSpace(position); } auto rect = cocos2d::Rect(position.x - size.width * anchor.x, position.y - size.height * anchor.y, size.width, size.height); //cocos2d::log("%f %f %f %f",position.x - size.width * anchor.x, //position.y - size.height * anchor.y, size.width, size.height); return rect; }
void ParticleEffect::update(float delta) { if (_freeMode){ const Vec2& pos = convertToWorldSpace(Vec2::ZERO); for (auto emitter : emitters){ emitter->translate(pos.x - _lastWorldX, pos.y - _lastWorldY); } _lastWorldX = pos.x; _lastWorldY = pos.y; } for (auto emitter : emitters){ emitter->update(delta); } if (isComplete()){ if (_completeListener) _completeListener(); removeFromParent(); } }
APTouchChecker APTouchManager::createDefaultChecker(cocos2d::Node* node) { auto size = node->getContentSize(); return [size,node](cocos2d::Touch* touch)->bool { auto position = node->getPosition(); auto anchor = node->getAnchorPoint(); auto parent = node->getParent(); if (parent != nullptr) { position = parent->convertToWorldSpace(position); } auto rect = cocos2d::Rect(position.x - size.width * anchor.x, position.y - size.height * anchor.y, size.width, size.height); ////cocos2d::log("name: %s, %f %f %f %f",node->getName().c_str(), rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); return rect.containsPoint(touch->getLocation()); }; }
void MethodStackLayer::reorderSprite() { CCPoint worldPosition = convertToWorldSpace(lastSprite->getPosition()); lastSprite->retain(); removeChild(lastSprite, false); scroll->addChild(lastSprite,1); CCPoint pos = scroll->convertToNodeSpace(worldPosition); if(methodStack.size() > stackCapasity) pos.y += scroll->getContentSize().height - BUTTON_TILE_SIZE * stackCapasity; lastSprite->setPosition(pos); lastSprite->release(); }
void CCClippingRegionNode::visit() { if (m_clippingEnabled) { glEnable(GL_SCISSOR_TEST); const CCPoint pos = convertToWorldSpace(CCPoint(m_clippingRegion.origin.x, m_clippingRegion.origin.y)); CCDirector::sharedDirector()->getOpenGLView()->setScissorInPoints(pos.x * m_fScaleX, pos.y * m_fScaleX, m_clippingRegion.size.width * m_fScaleY, m_clippingRegion.size.height * m_fScaleY); } CCNode::visit(); if (m_clippingEnabled) { glDisable(GL_SCISSOR_TEST); } }
void InfiniteScrollingNode::updatePosition() { int safeOffset = 0; // Get visible size Size visibleSize = Director::getInstance()->getVisibleSize(); // 1. For each child of an parallax node for(int i = 0; i < _children.size(); i++) { auto node = _children.at(i); // 2. We check whether it is out of the left side of the visible area if(convertToWorldSpace(node->getPosition()).y + node->getContentSize().height < safeOffset) // 3. Find PointObject that corresponds to current node for(int i = 0; i < _parallaxArray->num; i++) { auto po = (PointObject*)_parallaxArray->arr[i]; // If yes increase its current offset on the value of visible width if(po->getChild() == node) po->setOffset(po->getOffset() + Point(0, visibleSize.height + node->getContentSize().height)); } } }
void ClippingRectangleNode::onBeforeVisitScissor() { if (_clippingEnabled) { glEnable(GL_SCISSOR_TEST); float scaleX = _scaleX; float scaleY = _scaleY; Node *parent = this->getParent(); while (parent) { scaleX *= parent->getScaleX(); scaleY *= parent->getScaleY(); parent = parent->getParent(); } const Point pos = convertToWorldSpace(Point(_clippingRegion.origin.x, _clippingRegion.origin.y)); GLView* glView = Director::getInstance()->getOpenGLView(); glView->setScissorInPoints(pos.x, pos.y, _clippingRegion.size.width * scaleX, _clippingRegion.size.height * scaleY); } }
void CPuppet::update(float delta) { if (m_puppeteer && m_mapPhysics) { m_puppeteer->Update(delta); // Функция пытается переместить куклу, и возвращает обратно, если // на карте в новой позиции есть препятствия auto maybeMove = [this](float x, float y) { const Vec2 oldPosition = getPosition(); setPosition(oldPosition + Vec2(x, y)); if (!m_mapPhysics->CanStandOn(convertToWorldSpace(Vec2::ZERO))) { setPosition(oldPosition); return false; } return true; }; Vec2 offset = delta * m_puppeteer->GetVelocity(); maybeMove(offset.x, offset.y) || maybeMove(offset.x, 0) || maybeMove(0, offset.y); } }