Layout* UI::createLayout(int i){ auto lo=Layout::create(); lo->setBackGroundImage("HelloWorld.png"); lo->setBackGroundImageScale9Enabled(true); lo->setContentSize(Size(480,320)*1.5); for (int i=0; i<5; i++) { for (int j=0; j<7; j++) { auto ub=Button::create(); ub->loadTextureNormal("editBG.png"); ub->loadTexturePressed("editBG.png"); ub->loadTextureDisabled("editBg.png"); ub->setScale9Enabled(true); ub->setContentSize(Size(100,50)); ub->setPressedActionEnabled(true); ub->setZoomScale(0.5f); ub->setTitleFontName("fonts/Marker Felt.ttf"); ub->setTitleFontSize(30); ub->setTitleColor(Color3B::GREEN); ub->setTitleText(StringUtils::format("%d--%d",i,j)); ub->addClickEventListener(CC_CALLBACK_1(UI::buttonClick, this)); ub->setTag(i); ub->setPosition(Vec2(70+120*i, 35+j*60)); lo->addChild(ub); } } return lo; }
bool UIButtonTextOnly::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Text Only Button", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create(); button->setNormalizedPosition(Vec2(0.5f, 0.5f)); button->setTitleText("PLAY GAME"); CCLOG("content size should be greater than 0: width = %f, height = %f", button->getContentSize().width, button->getContentSize().height); button->setZoomScale(0.3f); button->setPressedActionEnabled(true); button->addClickEventListener([this](Ref* sender) { CCLOG("clicked!"); }); _uiLayer->addChild(button); return true; } return false; }
bool UIButtonIgnoreContentSizeTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button IgnoreContent Size Test", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->ignoreContentAdaptWithSize(false); button->setContentSize(Size(200,100)); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setZoomScale(0.3f); button->setPressedActionEnabled(true); button->addClickEventListener([=](Ref* sender) { CCLOG("clicked!"); button->setScale(1.2f); }); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->ignoreContentAdaptWithSize(false); button2->setContentSize(Size(200,100)); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); button2->setZoomScale(0.3f); button2->setPressedActionEnabled(true); button2->addClickEventListener([=](Ref* sender) { button2->runAction(ScaleTo::create(1.0f, 1.2f)); CCLOG("clicked!"); }); _uiLayer->addChild(button2); return true; } return false; }
// on "init" you need to initialize your instance bool SecondScene::init() { ////////////////////////////// // 1. super init first if ( !LayerColor::initWithColor(Color4B::BLUE) ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(SecondScene::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Second Scene \nwith Colored Background", "Arial", 24); // position the label on the center of the screen label->setPosition(Vec2(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); auto button = cocos2d::ui::Button::create(); button->loadTextures("button.png", "button_r.png"); Size layerSize = this->getContentSize(); button->setPosition(Vec2(layerSize.width/2, layerSize.height/2)); button->setPressedActionEnabled(true); button->addTouchEventListener(CC_CALLBACK_2(SecondScene::onButtonTouchEvent, this)); button->setTitleFontSize(36.0f); button->setTitleText("Back"); this->addChild(button, 10); return true; }
ui::Button* McdLayer::decorateButtonEx(const Btn_info& btnInfo, BTN_CALLBACK callback){ auto button = ui::Button::create(btnInfo.img, btnInfo.img_p); button->setPressedActionEnabled(true); button->setPosition(genPos(btnInfo.pos)); auto mytag = _tagAI++; button->setTag(mytag); button->addTouchEventListener(CC_CALLBACK_2(McdLayer::touchEvent, this)); button->setScale(btnInfo.scale); button->setTitleText(btnInfo.text); button->setTitleFontName(uic::font_zh); button->setTitleFontSize(20); this->addChild(button, 1); _cbMap[mytag] = callback; return button; }
ui::Button* McdLayer::decorateButton(const Btn_info& btnInfo , int tag) { auto button = ui::Button::create(btnInfo.img, btnInfo.img_p); button->setPressedActionEnabled(true); // button->setAnchorPoint({0,0}); button->setPosition(genPos(btnInfo.pos)); button->setTag(tag); button->addTouchEventListener(CC_CALLBACK_2(McdLayer::touchEvent, this)); button->setScale(btnInfo.scale); button->setTitleText(btnInfo.text); button->setTitleFontName(uic::font_zh); button->setTitleFontSize(30); this->addChild(button, 1); return button; }
void Button::copySpecialProperties(Widget *widget) { Button* button = dynamic_cast<Button*>(widget); if (button) { _prevIgnoreSize = button->_prevIgnoreSize; setScale9Enabled(button->_scale9Enabled); // clone the inner sprite: https://github.com/cocos2d/cocos2d-x/issues/16924 button->_buttonNormalRenderer->copyTo(_buttonNormalRenderer); _normalFileName = button->_normalFileName; _normalTextureSize = button->_normalTextureSize; _normalTexType = button->_normalTexType; _normalTextureLoaded = button->_normalTextureLoaded; setupNormalTexture(!_normalFileName.empty()); button->_buttonClickedRenderer->copyTo(_buttonClickedRenderer); _clickedFileName = button->_clickedFileName; _pressedTextureSize = button->_pressedTextureSize; _pressedTexType = button->_pressedTexType; _pressedTextureLoaded = button->_pressedTextureLoaded; setupPressedTexture(!_clickedFileName.empty()); button->_buttonDisabledRenderer->copyTo(_buttonDisabledRenderer); _disabledFileName = button->_disabledFileName; _disabledTextureSize = button->_disabledTextureSize; _disabledTexType = button->_disabledTexType; _disabledTextureLoaded = button->_disabledTextureLoaded; setupDisabledTexture(!_disabledFileName.empty()); setCapInsetsNormalRenderer(button->_capInsetsNormal); setCapInsetsPressedRenderer(button->_capInsetsPressed); setCapInsetsDisabledRenderer(button->_capInsetsDisabled); if(nullptr != button->getTitleRenderer()) { setTitleText(button->getTitleText()); setTitleFontName(button->getTitleFontName()); setTitleFontSize(button->getTitleFontSize()); setTitleColor(button->getTitleColor()); } setPressedActionEnabled(button->_pressedActionEnabled); setZoomScale(button->_zoomScale); } }
bool UIButtonTitleEffectTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button Title Effect", "fonts/Marker Felt.ttf",32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setTitleFontName("fonts/Marker Felt.ttf"); button->setZoomScale(0.3f); button->setScale(2.0f); button->setPressedActionEnabled(true); Label *title = button->getTitleRenderer(); button->setTitleColor(Color3B::RED); title->enableShadow(Color4B::BLACK,Size(2,-2)); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); auto title2 = button2->getTitleRenderer(); title2->enableOutline(Color4B::GREEN, 3); _uiLayer->addChild(button2); return true; } return false; }
bool UIButtonFlipTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the button events will be displayed _displayValueLabel = Text::create("Button X Flipped", "fonts/Marker Felt.ttf",20); _displayValueLabel->setNormalizedPosition(Vec2(0.3, 0.7)); _uiLayer->addChild(_displayValueLabel); // Create the button auto button = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button->setNormalizedPosition(Vec2(0.3f, 0.5f)); button->setTitleText("PLAY GAME"); button->setTitleFontName("fonts/Marker Felt.ttf"); button->setZoomScale(0.3f); button->setScale(2.0f); button->setFlippedX(true); button->setPressedActionEnabled(true); _uiLayer->addChild(button); // Create the button auto button2 = Button::create("cocosui/animationbuttonnormal.png", "cocosui/animationbuttonpressed.png"); button2->setNormalizedPosition(Vec2(0.8f, 0.5f)); button2->setTitleText("PLAY GAME"); button2->setFlippedY(true); _uiLayer->addChild(button2); auto titleLabel = Text::create("Button Y flipped", "Arial", 20); titleLabel->setNormalizedPosition(Vec2(0.8, 0.7)); this->addChild(titleLabel); return true; } return false; }
void Button::copySpecialProperties(Widget *widget) { Button* button = dynamic_cast<Button*>(widget); if (button) { _prevIgnoreSize = button->_prevIgnoreSize; setScale9Enabled(button->_scale9Enabled); loadTextureNormal(button->_normalFileName.c_str(), button->_normalTexType); loadTexturePressed(button->_clickedFileName.c_str(), button->_pressedTexType); loadTextureDisabled(button->_disabledFileName.c_str(), button->_disabledTexType); setCapInsetsNormalRenderer(button->_capInsetsNormal); setCapInsetsPressedRenderer(button->_capInsetsPressed); setCapInsetsDisabledRenderer(button->_capInsetsDisabled); setTitleText(button->getTitleText()); setTitleFontName(button->getTitleFontName()); setTitleFontSize(button->getTitleFontSize()); setTitleColor(button->getTitleColor()); setPressedActionEnabled(button->_pressedActionEnabled); } }
//按钮 void UI:: initButton(){ auto visibleSize=Director::getInstance()->getVisibleSize(); auto ub=Button::create(); ub->loadTextureNormal("0.png"); ub->loadTexturePressed("2.png"); ub->loadTextureDisabled("2.png"); ub->setScale9Enabled(true); ub->setContentSize(cocos2d::Size(128,128)); ub->setPressedActionEnabled(true); ub->setZoomScale(2); ub->setTitleFontName("fonts/Marker Felt.ttf"); ub->setTitleFontSize(30); ub->setTitleColor(cocos2d::Color3B::YELLOW); ub->setTitleText("HHLLO Button"); // ub->addClickEventListener(CC_CALLBACK_1(UI::buttonClick, this)); //添加触摸事件 ub->addTouchEventListener (this,toucheventselector(UI::buttonClick)); ub->setPosition(visibleSize/2); addChild(ub); }
void UI::initListView(){ auto lv=ListView::create(); lv->setContentSize(cocos2d::Size(640,480)); lv->setItemsMargin(20);//设置间隔 lv->setDirection(cocos2d::ui::ScrollView::Direction::VERTICAL); for (int i=0; i<50; i++) { auto ub=Button::create(); ub->loadTextureNormal("0.png"); ub->loadTexturePressed("1.png"); ub->loadTextureDisabled("0.png"); ub->setScale9Enabled(true); ub->setContentSize(Size(600,400)); ub->setPressedActionEnabled(true); ub->setZoomScale(0.5f); ub->addClickEventListener(CC_CALLBACK_1(UI::buttonClick, this)); ub->setTag(i); lv->pushBackCustomItem(ub); } lv->addEventListener(CC_CALLBACK_2(UI::ListViewCall, this)); addChild(lv); }
cocos2d::ui::Button* GameLayer::create_button(std::string text, cocos2d::ccMenuCallback cb) { auto param = ui::LinearLayoutParameter::create(); param->setGravity(ui::LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); param->setMargin(ui::Margin(0, menu_fontsize/2, 0, 0)); auto btn = ui::Button::create(); btn->setTitleFontName(menu_font); btn->setTitleFontSize(menu_fontsize); btn->setTitleColor(Color3B::GREEN); btn->setLayoutParameter(param); btn->setTouchEnabled(true); btn->setPressedActionEnabled(true); btn->addTouchEventListener([this,cb](Ref* ref, ui::Widget::TouchEventType event) { if (event == ui::Widget::TouchEventType::ENDED){ cb(NULL); } }); btn->setTitleText(text); return btn; };
void Button::copySpecialProperties(Widget *widget) { Button* button = dynamic_cast<Button*>(widget); if (button) { _prevIgnoreSize = button->_prevIgnoreSize; setScale9Enabled(button->_scale9Enabled); auto normalSprite = button->_buttonNormalRenderer->getSprite(); if (nullptr != normalSprite) { loadTextureNormal(normalSprite->getSpriteFrame()); } auto clickedSprite = button->_buttonClickedRenderer->getSprite(); if (nullptr != clickedSprite) { loadTexturePressed(clickedSprite->getSpriteFrame()); } auto disabledSprite = button->_buttonDisableRenderer->getSprite(); if (nullptr != disabledSprite) { loadTextureDisabled(disabledSprite->getSpriteFrame()); } setCapInsetsNormalRenderer(button->_capInsetsNormal); setCapInsetsPressedRenderer(button->_capInsetsPressed); setCapInsetsDisabledRenderer(button->_capInsetsDisabled); if(nullptr != button->getTitleRenderer()) { setTitleText(button->getTitleText()); setTitleFontName(button->getTitleFontName()); setTitleFontSize(button->getTitleFontSize()); setTitleColor(button->getTitleColor()); } setPressedActionEnabled(button->_pressedActionEnabled); setZoomScale(button->_zoomScale); } }
cocos2d::ui::Button* GameLayer::create_button(std::string text, BoolFuncNoArgs cb, bool start_disabled, ButtonState* btn_state) { auto param = ui::LinearLayoutParameter::create(); param->setGravity(ui::LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL); auto disable_button = [](ui::Button* button) { button->setTitleColor(Color3B::GRAY); button->setTouchEnabled(false); button->setPressedActionEnabled(false); }; auto btn = ui::Button::create("main_UI_export_10_x4.png", "", "", ui::TextureResType::PLIST); btn->setTitleFontName(menu_font); btn->setTitleFontSize(menu_fontsize); btn->setTitleColor(Color3B::BLACK); btn->getTitleRenderer()->getTexture()->setAliasTexParameters(); btn->setColor(Color3B(114, 160, 72)); btn->getVirtualRenderer()->setScale(sx(1.0f)); btn->setScaleX(sx(1.0f)); btn->setScaleY(sy(1.0f)); param->setMargin(ui::Margin(0, sy(15.0f)*btn->getScaleY(), 0, sy(15.0f)*btn->getScaleY())); btn->setLayoutParameter(param); btn->setTouchEnabled(true); btn->setPressedActionEnabled(true); if (start_disabled) { disable_button(btn); }; btn->setTitleText(text); if (btn_state) { //update the text btn_state->swap_state(btn, btn_state->current); }; btn->addTouchEventListener( [btn_state, cb, btn, disable_button](Ref* ref, ui::Widget::TouchEventType event) { if (event == ui::Widget::TouchEventType::ENDED) { if (!cb()) { disable_button(btn); }; if (btn_state) { btn_state->current = !btn_state->current; btn_state->swap_state(btn, btn_state->current); }; } } ); return btn; };
bool CLoginScene::init() { IBaseScene::init() ; // as when MSG_PlAYER_OTHER_LOGIN event occured , other scene may disconnect from svr // so here ,we will try to connect svr ; if ( CClientApp::getInstance()->isConnecting() == false ) { CClientApp::getInstance()->connectToSvr() ; } // read cocostuido Node* pRoot = CSLoader::getInstance()->createNodeWithFlatBuffersFile("res/login.csb"); addChild(pRoot); auto cSize = pRoot->getContentSize(); pRoot->setContentSize(Director::getInstance()->getWinSize()) ; ui::Helper::doLayout(pRoot); auto bg = pRoot->getChildByName("signup_bg_2") ; m_pAccount = dynamic_cast<ui::TextField*>(bg->getChildByName("account")); m_pName = dynamic_cast<ui::TextField*>(bg->getChildByName("name")); m_pPassword = dynamic_cast<ui::TextField*>(bg->getChildByName("password")); auto Btn = dynamic_cast<ui::Button*>(bg->getChildByName("btnLogin")) ; Btn->setPressedActionEnabled(true); Btn->addClickEventListener([=](Ref* sender){ // on login pressed if ( strlen(m_pPassword->getString().c_str()) == 0 || 0 == strlen(m_pAccount->getString().c_str()) ) { cocos2d::MessageBox("account or password is null","error") ; return ; } else { stMsgLogin msgLogin ; memset(msgLogin.cAccount,0,sizeof(msgLogin.cAccount)) ; sprintf_s(msgLogin.cAccount,sizeof(msgLogin.cAccount),"%s",m_pAccount->getString().c_str()) ; memset(msgLogin.cPassword,0,sizeof(msgLogin.cPassword)) ; sprintf_s(msgLogin.cPassword,sizeof(msgLogin.cPassword),"%s",m_pPassword->getString().c_str()) ; sendMsg(&msgLogin,sizeof(msgLogin)) ; } }); Btn = dynamic_cast<ui::Button*>(bg->getChildByName("btnRegister")) ; Btn->setPressedActionEnabled(true); Btn->addClickEventListener([=](Ref* sender){ // on register pressed if ( strlen(m_pPassword->getString().c_str()) == 0 || 0 == strlen(m_pAccount->getString().c_str()) ) { cocos2d::MessageBox("account or password is null","error") ; return ; } else { stMsgRegister msgRegister ; msgRegister.cRegisterType = 1 ; msgRegister.nChannel = 0 ; memset(msgRegister.cAccount,0,sizeof(msgRegister.cAccount)) ; sprintf_s(msgRegister.cAccount,sizeof(msgRegister.cAccount),"%s",m_pAccount->getString().c_str()) ; memset(msgRegister.cPassword,0,sizeof(msgRegister.cPassword)) ; sprintf_s(msgRegister.cPassword,sizeof(msgRegister.cPassword),"%s",m_pPassword->getString().c_str()) ; sendMsg(&msgRegister,sizeof(msgRegister)) ; } }); return true ; }
//添加一些显示标签 void GameLayer:: addSprite(){ //显第几关的label auto temp = __String::createWithFormat("Level: %d",level); levelLabel = Label::createWithSystemFont(temp->getCString(), "fonts/Marker Felt.ttf", 32); levelLabel->setPosition(Point(80, visibleSize.height-40)); levelLabel->setColor(Color3B(255, 255, 255)); this->addChild(levelLabel,1); //显分数的label xmller.XmlParse(str); std::string level02= xmller.getLevel02(); int number=atoi(level02.c_str()); log("xml score=%d",number); auto sc = __String::createWithFormat("Money: %d",number); scoreLabel = Label::createWithSystemFont(sc->getCString(), "fonts/Marker Felt.ttf", 32); scoreLabel->setPosition(Point(110, visibleSize.height-80)); scoreLabel->setColor(Color3B(255, 255, 0)); this->addChild(scoreLabel,1); //显示子弹数量 std::string level03 = xmller.getLevel03(); int bb = atoi(level03.c_str()); auto cc = __String::createWithFormat("Bullet: %d",bb); bulletLabel = Label::createWithSystemFont(cc->getCString(), "fonts/Marker Felt.ttf", 32); bulletLabel->setPosition(Point(110, visibleSize.height-120)); bulletLabel->setColor(Color3B(0, 255, 0)); this->addChild(bulletLabel,1); //暂停的按钮 auto ub=Button::create(); ub->loadTextureNormal("newPause_Normal.png"); ub->loadTexturePressed("newPause_Select.png"); ub->setScale(0.4); ub->setScale9Enabled(true); ub->setContentSize(Size(128,128)); ub->setPressedActionEnabled(false); ub->setZoomScale(2.0f);//大于1有效小于1无效 ub->addClickEventListener([this](Ref*){ auto l = PauseLayer::createLayer(); l->setDelegate(this); //设置代理---------------------- this->addChild(l); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan=([](Touch*, Event*){ return true; }); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, l); Director::getInstance()->pause(); }); ub->setPosition(Vec2(visibleSize.width-50,50)); this->addChild(ub); }