void PopLayer::onEnter() { Layer::onEnter(); Size winSize = Director::getInstance()->getVisibleSize(); Vec2 pCenter(winSize.width / 2, winSize.height / 2); Size contentSize; // 设定好参数,在运行时加载 if (getContentSize().equals(Size::ZERO)) { getSpriteBackGround()->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); this->addChild(getSpriteBackGround(), 0, 0); contentSize = getSpriteBackGround()->getTexture()->getContentSize(); } else { Scale9Sprite *background = getSprite9BackGround(); background->setContentSize(getContentSize()); background->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); this->addChild(background, 0, 0); contentSize = getContentSize(); } // 添加按钮,并设置其位置 this->addChild(getMenuButton()); float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1); Vector<Node *> array = getMenuButton()->getChildren(); getMenuButton()->alignItemsHorizontallyWithPadding(50); getMenuButton()->setPosition(Vec2(winSize.width / 2, winSize.height / 2 - contentSize.height / 3)); // int i = 0; // for(auto &bt: array) // { // Node* node = dynamic_cast<Node*>(bt); // node->setPosition(Vec2( winSize.width / 2 - contentSize.width / 2 + btnWidth * (i + 1), winSize.height / 2 - contentSize.height / 3)); // i++; // } // 显示对话框标题 if (getLabelTitle()){ getLabelTitle()->setPosition((pCenter + Vec2(0, contentSize.height / 2 - 35.0f))); this->addChild(getLabelTitle()); } if(getImgTitle()) { getImgTitle()->setPosition((pCenter + Vec2(0, contentSize.height / 2 - 40.0f))); this->addChild(getImgTitle()); } // 显示文本内容 if (getLabelContentText()){ LabelTTF* ltf = getLabelContentText(); //ltf->setPosition(Vec2(winSize.width / 2, winSize.height / 2)); ltf->setPosition(pCenter); ltf->setDimensions(Size(contentSize.width - m_contentPadding * 2, contentSize.height - m_contentPaddingTop)); ltf->setHorizontalAlignment(TextHAlignment::LEFT); this->addChild(ltf); } // 弹出效果 Action* popupLayer = Sequence::create(ScaleTo::create(0.0, 0.0), ScaleTo::create(0.06, 1.05), ScaleTo::create(0.08, 0.95), ScaleTo::create(0.08, 1.0), NULL); this->runAction(popupLayer); }
bool ChatScene::init() { if(!Layer::init()) { return false; } Rect visibleRect = VisibleRect::getVisibleRect(); m_tBeginPoint = Vec2(0, VisibleRect::top().y-50); auto backLabel = LabelTTF::create("Back", "Arial", 25); MenuItem* pBackMenuItem = MenuItemLabel::create(backLabel, std::bind(&ChatScene::backCallback, this, std::placeholders::_1)); Menu* pBackMenu = Menu::create(pBackMenuItem, NULL); pBackMenu->setAnchorPoint(Point::ZERO); pBackMenu->setPosition(Vec2(VisibleRect::right().x+35, VisibleRect::top().y-20)); this->addChild(pBackMenu, 10); auto pUserLabel = LabelTTF::create("Users", "Arial", 25); MenuItemLabel* pUserMenuItem = MenuItemLabel::create(pUserLabel, std::bind(&ChatScene::userCallback, this, std::placeholders::_1)); Menu* pUserMenu = Menu::create(pUserMenuItem, NULL); pUserMenu->setAnchorPoint(Vec2::ZERO); pUserMenu->setPosition(Vec2(VisibleRect::right().x -40,VisibleRect::top().y -20)); this->addChild(pUserMenu, 10); auto pSendLabel = LabelTTF::create("Send", "Arial", 25); MenuItemLabel* pSendMenuItem = MenuItemLabel::create(pSendLabel, std::bind(&ChatScene::sendCallback, this, std::placeholders::_1)); Menu* pSendMenu = Menu::create(pSendMenuItem, NULL); pSendMenu->setAnchorPoint(Vec2::ZERO); pSendMenu->setPosition(Vec2(VisibleRect::right().x-45, 25)); addChild(pSendMenu,10); this->m_pChannelText = LabelTTF::create(s_channel.c_str(), "Arial", 25); this->m_pChannelText->setPosition(Vec2(VisibleRect::center().x, VisibleRect::top().y - 20)); addChild(this->m_pChannelText, 10); m_layer = Layer::create(); for (int i = 0; i<visibleRect.size.height/LINE_SPACE; ++i) { LabelTTF* pLabel; if(i<TESTS_COUNT) { pLabel = LabelTTF::create(global_text[i].c_str(), "Arial", 23); pLabel->setDimensions(Size(visibleRect.size.width, 50)); } else { pLabel = LabelTTF::create("", "Arial", 23); pLabel->setDimensions(Size(visibleRect.size.width, 50)); } pLabel->setColor(Color3B(32, 32, 32)); pLabel->setAnchorPoint(Vec2(0, 0)); pLabel->setPosition(Vec2(VisibleRect::left().x+10, (m_tBeginPoint.y-(i+1)*LINE_SPACE))); m_layer->addChild(pLabel, 10, i); } m_layer->setContentSize(Size(Vec2(VisibleRect::getVisibleRect().size.width-30, (TESTS_COUNT+1)*(LINE_SPACE)))); m_layer->setPosition(s_tCurPos); addChild(m_layer,2); LayerColor* pLayer1 = LayerColor::create(Color4B(255, 255, 255, 255), visibleRect.size.width, visibleRect.size.height); pLayer1->setCascadeColorEnabled(false); pLayer1->setPosition(Vec2(0,0)); addChild(pLayer1,1); LayerColor* pLayer2 = LayerColor::create(Color4B(0, 0, 0, 255), visibleRect.size.width, 40); pLayer2->setCascadeColorEnabled(false); pLayer2->setPosition(Vec2(0, VisibleRect::top().y-40)); addChild(pLayer2,8); LayerColor* pLayer3 = LayerColor::create(Color4B(72, 72, 72, 255), visibleRect.size.width, 50); pLayer3->setCascadeColorEnabled(false); pLayer3->setPosition(Vec2(0, 0)); addChild(pLayer3,8); m_pTextField = TextFieldTTF::textFieldWithPlaceHolder("", FONT_NAME, FONT_SIZE); m_pTextField->setAnchorPoint(Vec2::ZERO); m_pTextField->setPosition(VisibleRect::left().x+10,5); //m_pTextField->setColorSpaceHolder(Color3B(255,0,0)); m_pTextField->setString("1111111111111111111111111111111111111111111111111111111111"); addChild(m_pTextField,12); m_pTextField->setDelegate(this); return true; }
void PopupLayer::onEnter() { Layer::onEnter(); //Size winSize = Director::getInstance()->getWinSize(); Point pCenter = ccp(winSize.width / 2, winSize.height / 2); Size contentSize; // 设定好参数,在运行时加载 if (getContentSize().equals(CCSizeZero)) { getSpriteBackGround()->setPosition(ccp(winSize.width / 2, winSize.height / 2)); this->addChild(getSpriteBackGround(), 0, 0); contentSize = getSpriteBackGround()->getTexture()->getContentSize(); } else { Scale9Sprite *background = getSprite9BackGround(); background->setContentSize(getContentSize()); background->setPosition(ccp(winSize.width / 2, winSize.height / 2)); this->addChild(background, 0, 0); contentSize = getContentSize(); } // 添加按钮,并设置其位置 this->addChild(getMenuButton()); float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1); Vector<Node*> vecArray = getMenuButton()->getChildren(); int j=0; for(auto it=vecArray.begin();it!=vecArray.end();it++) { Node* node = dynamic_cast<Node*>(*it); node->setPosition(Point(winSize.width/2 - contentSize.width/2+btnWidth*(j+1),winSize.height/2-contentSize.height/3)); j++; } // 显示对话框标题 if (getLabelTitle()) { getLabelTitle()->setPosition(ccpAdd(pCenter, ccp(0, contentSize.height / 2 -20))); getLabelTitle()->setColor(ccc3(0,0,0)); this->addChild(getLabelTitle()); } switch(pop_type) { case LOTTERY: { setLotteryContext(contentSize); break; } case LOTTERY_PUBLISH: { setPublishLotteryContext(contentSize); break; } case LOADGAME: { setLoadGameContext(contentSize); break; } default: { // 显示文本内容 if (getLabelContentText()) { LabelTTF* ltf = getLabelContentText(); ltf->setPosition(ccp(winSize.width / 2, winSize.height / 2)); ltf->setDimensions(CCSizeMake(contentSize.width - m_contentPadding * 2, contentSize.height - m_contentPaddingTop)); ltf->setHorizontalAlignment(kCCTextAlignmentLeft); ltf->setColor(ccc3(0,0,0)); this->addChild(ltf); } } } // 弹出效果 Action* popupLayer = Sequence::create(ScaleTo::create(0.0, 0.0), ScaleTo::create(0.15, 1.05), ScaleTo::create(0.08, 0.95), ScaleTo::create(0.08, 1.0), NULL); this->runAction(popupLayer); }