bool MainScene::init() { if (!Layer::init()) return false; auto director = Director::getInstance(); auto pView = director->getOpenGLView(); pView->setDesignResolutionSize(RESOLUTION_WIDTH, RESOLUTION_HEIGHT, ResolutionPolicy::SHOW_ALL); Size visibleSize = director->getVisibleSize(); Vec2 origin = director->getVisibleOrigin(); //Title 'Kid Sudoku' auto labelTitle = Label::createWithTTF("Kid Remember It", "fonts/Marker Felt.ttf", 188); labelTitle->setTextColor(Color4B(0xe3, 0x54, 0xf4, 0xff)); labelTitle->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - labelTitle->getContentSize().height/2 - 40) ); this->addChild(labelTitle); //Cool Papa-Legend announcement auto labelAnn = Label::createWithTTF("Cool Papa-Legend : Produce product for our kids.", "fonts/Marker Felt.ttf", 30); labelAnn->setTextColor(Color4B(0x00, 0xff, 0x00, 0xff)); labelAnn->setPosition(Vec2(origin.x + visibleSize.width / 2, labelTitle->getPosition().y - labelTitle->getContentSize().height/2 - 60 - labelAnn->getContentSize().height / 2) ); this->addChild(labelAnn); //Campaign Button auto btnPlay = ui::Button::create("btn-menu-0.png", "btn-menu-1.png", "btn-menu-2.png"); btnPlay->setScale9Enabled(true); btnPlay->setCapInsets(Rect(15, 15, 4, 4)); btnPlay->setTitleText("Play"); btnPlay->setTitleFontSize(40); btnPlay->setContentSize(Size(300, 90)); btnPlay->setPosition(Vec2(origin.x + visibleSize.width / 2, labelAnn->getPosition().y - labelAnn->getContentSize().height / 2 - 140 - btnPlay->getContentSize().height/2) ); btnPlay->addClickEventListener(CC_CALLBACK_1(MainScene::onMenuPlayClicked, this)); this->addChild(btnPlay, 1); //Exit Button auto btnExit = ui::Button::create("btn-menu-0.png", "btn-menu-1.png", "btn-menu-2.png"); btnExit->setScale9Enabled(true); btnExit->setCapInsets(Rect(15, 15, 4, 4)); btnExit->setTitleText("Exit"); btnExit->setTitleFontSize(40); btnExit->setContentSize(Size(300, 90)); btnExit->setPosition(Vec2(origin.x + visibleSize.width / 2, btnPlay->getPosition().y - btnPlay->getContentSize().height / 2 - 40 - btnExit->getContentSize().height/2) ); btnExit->addClickEventListener(CC_CALLBACK_1(MainScene::onMenuExitClicked, this)); this->addChild(btnExit, 1); return true; }
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; }
// on "init" you need to initialize your instance bool GameOver::init() { if (!Layer::init()) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); //Ìí¼Ó±³¾° auto background = Sprite::create("map/end_bg.jpg"); background->setPosition(visibleSize.width / 2, visibleSize.height / 2); addChild(background, 0); string a = Global::getWinner(); a = a + " Win"; auto field = Label::create(a, "fonts/pan.ttf", 70); field->setColor(ccc3(51, 63, 80)); field->setPosition(Size(visibleSize.width / 2, visibleSize.height / 4 * 3 - 20)); this->addChild(field, 2); //restart auto start_button = Button::create(); start_button->setTitleText("Restart"); start_button->setTitleFontSize(60); start_button->setTitleFontName("fonts/pan.ttf"); start_button->setColor(ccc3(132, 60, 12)); start_button->setPosition(Size(visibleSize.width / 2, visibleSize.height / 4 - 20)); start_button->addClickEventListener(CC_CALLBACK_1(GameOver::menuChangeCallback, this)); this->addChild(start_button, 2); return true; }
bool SimplePage::initWithIndex(int newIndex) { if(!Node::init()) return false; setName("secondPage"); auto visibleSize=Director::getInstance()->getVisibleSize(); auto rootNode= CSLoader::createNode("simplePage.csb"); rootNode->setPosition(visibleSize/2); addChild(rootNode); auto listView=rootNode->getChildByName<ui::ListView *>("listView"); const auto& data=CSVGlobalConfig::getInstance()->data; for(const auto& lineData : data) { auto buttonTitle=lineData[0]; auto button=ui::Button::create("button.png"); button->setScale9Enabled(true); button->setTitleFontSize(25); button->setContentSize(Size( 300,30)); button->setTitleText(buttonTitle); button->addClickEventListener(CC_CALLBACK_1(SimplePage::onButtonClickCallBack, this)); listView->pushBackCustomItem(button); } return true; }
Button * Mx::createDialogButton(DialogBtn button, string text){ auto buttonConfirm = Button::create("sys_dialog_button_normal.png","sys_dialog_button_down.png"); buttonConfirm->setScale(.7); buttonConfirm->setTitleText(text); buttonConfirm->setTitleFontSize(22); return buttonConfirm; }
// 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; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto name = ui::Text::create(LHLocalizedCString("appname"), Common_Font, 70); name->setColor(Color3B(64, 119, 215)); name->setPosition(Vec2(visibleSize.width/2, visibleSize.height/3*2) ); this->addChild(name); auto play = ui::Button::create("blue.png"); play->setTitleFontSize(40); play->setTitleText(LHLocalizedCString("play")); play->setPosition(Vec2(name->getPositionX(), visibleSize.height/2)); play->addTouchEventListener([](Ref *ps,ui::Widget::TouchEventType type){ if (type == ui::Widget::TouchEventType::ENDED) { Director::getInstance()->replaceScene(PlayScene::createScene(nullptr)); CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("pop.wav"); } }); this->addChild(play); auto dbt = DeveloperInfo::DevInfoButton("devinfo.png"); dbt->setPosition(Vec2(visibleSize.width - 80, visibleSize.height - 80)); this->addChild(dbt); auto gbt = GuideScene::guideButton("q.png"); gbt->setPosition(Vec2(dbt->getPosition().x - gbt->getContentSize().width/2*3, visibleSize.height - 80)); this->addChild(gbt); if (UserDefault::getInstance()->getBoolForKey("guide", true)) { auto gt = ui::Text::create(LHLocalizedCString("guidetext"), Common_Font, 35); gt->setColor(Color3B(250, 201, 11)); gt->setPosition(Vec2(gbt->getPosition().x - gt->getContentSize().width/2 - gbt->getContentSize().width/2 - 2, gbt->getPosition().y)); MoveBy *byl = MoveBy::create(0.4, Vec2(-20, 0)); MoveBy *byr = MoveBy::create(0.2, Vec2(20, 0)); Sequence *sq = Sequence::create(byl,byr,NULL); RepeatForever *rp = RepeatForever::create(sq); gt->runAction(rp); this->addChild(gt); UserDefault::getInstance()->setBoolForKey("guide", false); } return true; }
TouchScreenMenu::TouchScreenMenu(TouchScreenMenuItem items[], unsigned int fontSize, unsigned int spacing, unsigned int padding, Justify justify, char *title){ _controller = &TSC; setSpacing(spacing); setPadding(padding); setFontSize(fontSize); setTitleFontSize(2); setJustification(justify); setItems(items); setTitle(title); setTitleColors(0xffff, 0x001f); setClearScreenOnDraw(false); }
TouchScreenMenu::TouchScreenMenu(TouchScreenMenuItem items[]){ _controller = &TSC; setSpacing(5); setPadding(2); setFontSize(2); setTitleFontSize(2); setJustification(LEFTJ); setItems(items); setTitle(NULL); setTitleColors(0xffff, 0x001f); setClearScreenOnDraw(false); }
Button* OptionScene::createButton(std::string text, Size size, std::function<void(Ref*)> callback) { auto button = Button::create("UI/ButtonNormal.png", "UI/ButtonSelected.png"); button->setTitleText(text); button->setTitleFontName(MyCreator::normalFont); button->setTitleColor(Color3B::BLACK); button->setTitleFontSize(30); button->addClickEventListener(callback); button->setScale9Enabled(true); button->setContentSize(size); return button; }
bool HelloWorld::init() { if (!Layer::init()) { return false; } m_score = 0; Size visibleSize = Director::getInstance()->getVisibleSize(); //退出程序 auto listenerEnd = EventListenerKeyboard::create(); listenerEnd->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listenerEnd, this); auto layerColorBG = LayerColor::create(Color4B(180, 170, 160, 255)); addChild(layerColorBG); auto labTTFCardNumberName = Label::createWithSystemFont("Score", "fonts/arial.ttf", 60); labTTFCardNumberName->setPosition(Point(visibleSize.width / 5, visibleSize.height - 70)); addChild(labTTFCardNumberName); labTTFCardNumber = Label::createWithSystemFont("0", "fonts/arial.ttf", 60); labTTFCardNumber->setPosition(Point(visibleSize.width / 2 + 100, visibleSize.height - 70)); addChild(labTTFCardNumber); auto listen = EventListenerTouchOneByOne::create(); listen->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); listen->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen, this); //设置重来按钮 auto m_bunt = ui::Button::create("normal.png", "selected.png"); m_bunt->setScale9Enabled(true);//可拉伸图片 m_bunt->setTitleText("Begen"); m_bunt->setTitleFontSize(50); m_bunt->setTitleFontName("fonts/arial.ttf"); m_bunt->setContentSize(cocos2d::Size(visibleSize.width / 10+200, visibleSize.height / 15)); m_bunt->setPosition(Point(visibleSize.width / 5 + 180, visibleSize.height - 150)); m_bunt->addTouchEventListener(CC_CALLBACK_2(HelloWorld::touchEvent, this)); addChild(m_bunt); creatCard(visibleSize); autoCreateCardNumber(); autoCreateCardNumber(); return true; }
TouchScreenForm::TouchScreenForm(char *title){ _controller = &TSC; setTitleSpacing(2); setTitlePadding(5); setTitleFontSize(2); setTitle(title); setTitleColors(0xffff, 0x001f); setClearScreenOnDraw(false); setLabels(NULL); setButtons(NULL); setCircleButtons(NULL); setArrowButtons(NULL); setSliders(NULL); }
TouchScreenForm::TouchScreenForm(char *title, unsigned int titleForeColor, unsigned int titleBackColor, unsigned int titleFontSize){ _controller = &TSC; setTitleSpacing(2); setTitlePadding(5); setTitleFontSize(titleFontSize); setTitle(title); setTitleColors(titleForeColor, titleBackColor); setClearScreenOnDraw(false); setLabels(NULL); setButtons(NULL); setCircleButtons(NULL); setArrowButtons(NULL); setSliders(NULL); }
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; }
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; }
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); } }
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); } }
bool MainScene::init() { Size sizeWin = DIRECTOR->getWinSize(); float listWidth = sizeWin.width - sizeWin.height; // background layout auto layerColor = LayerColor::create(Color4B::WHITE, sizeWin.width, sizeWin.height); auto drawNodeV = DrawNode::create(); drawNodeV->drawSegment(Point(listWidth, 0), Point(listWidth, sizeWin.height), 2, Color4F::GRAY); layerColor->addChild(drawNodeV); auto drawNodeH = DrawNode::create(); drawNodeH->drawSegment(Point(listWidth, sizeWin.height / 3), Point(sizeWin.width, sizeWin.height / 3), 2, Color4F::GRAY); layerColor->addChild(drawNodeH); addChild(layerColor); // title auto btnTitle = Button::create(); btnTitle->setTouchEnabled(true); btnTitle->ignoreContentAdaptWithSize(false); btnTitle->setTitleFontName("Marker Felt"); btnTitle->setTitleText("Cocos2d Manual"); btnTitle->setTitleColor(Color3B::GREEN); btnTitle->setTitleFontSize(30); btnTitle->setSize(Size(listWidth, 40)); btnTitle->setPosition(Point(listWidth / 2, sizeWin.height - 30)); BIND_LISTENER_TOUCH(btnTitle, this, MainScene::onTouchTitle); addChild(btnTitle); // manual layer auto layerDisplay = ManualDisplay::create(); auto layerCode = ManualCode::create(); auto layerList = ManualList::create(); layerCode->setDisplay(layerDisplay); layerCode->setTag(Tag::TAG_CODELAYER); layerList->setCode(layerCode); addChild(layerDisplay); addChild(layerCode); addChild(layerList); return Layer::init(); }
//按钮 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); }
bool Pause::init() { if (!Setting::init()) { return false; } auto resumeButton = ui::Button::create("Pics/button/OptionNormal.png", "Pics/button/OptionSelected.png"); resumeButton->setTitleFontName("Fonts/Amplify.ttf"); resumeButton->setTitleFontSize(36); resumeButton->setTitleText("RESUME"); resumeButton->setTitleColor(Color3B(53, 54, 165)); resumeButton->addTouchEventListener([](Ref* pSender, ui::Widget::TouchEventType type) { if (type == ui::Widget::TouchEventType::ENDED) { Director::getInstance()->popScene(); if (UserDefault::getInstance()->getBoolForKey(SOUND_KEY)) { SimpleAudioEngine::getInstance()->playEffect(FILE_SOUND_1); } } }); resumeButton->setPosition(Vec2(480, 200)); this->addChild(resumeButton); return true; }
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); } }
Alert* Alert::create() { //usual cocos create, note that I'm skipping handling errors. should fix that Alert* alert = new (std::nothrow) Alert(); if (alert && alert->init()) { alert->autorelease(); } Size visibleSize = Director::getInstance()->getVisibleSize(); Size node_size = visibleSize*0.80f; alert->setContentSize(node_size); alert->setLayoutType(ui::Layout::Type::RELATIVE); //alert->setBackGroundColor(Color3B::BLACK); //alert->setBackGroundColorOpacity(255/1.5); //alert->setBackGroundColorType(ui::LayoutBackGroundColorType::SOLID); alert->setBackGroundImage("main_UI_export_10_x4.png", TextureResType::PLIST); alert->setBackGroundImageScale9Enabled(true); alert->setBackGroundImageColor(Color3B(114, 160, 72)); //alert->setBackGroundImageOpacity(200.0f); //layout->setClippingEnabled(true); auto create_txt = [&](std::string msg, ui::RelativeLayoutParameter* param) { auto txt = ui::Text::create(msg, DEFAULT_FONT, sx(25.0f)); Label* lbl = (Label*)txt->getVirtualRenderer(); lbl->getFontAtlas()->setAliasTexParameters(); txt->setTextColor(Color4B::BLACK); //txt->enableOutline(Color4B::BLACK, 2); txt->ignoreContentAdaptWithSize(false); //word wrap or something alert->addChild(txt); txt->setLayoutParameter(param); return txt; }; auto header_param = ui::RelativeLayoutParameter::create(); header_param->setRelativeName("header_param"); header_param->setAlign(ui::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL); header_param->setMargin(ui::Margin(sx(10), sy(30), sx(10), sy(10))); alert->header_txt = create_txt("Title Here", header_param); auto sub_header_param = ui::RelativeLayoutParameter::create(); sub_header_param->setRelativeName("sub_header_param"); sub_header_param->setAlign(ui::RelativeAlign::LOCATION_BELOW_CENTER); sub_header_param->setMargin(ui::Margin(sx(10), sy(10), sx(10), sy(10))); alert->sub_header_txt = create_txt("Sub header", sub_header_param); sub_header_param->setRelativeToWidgetName("header_param"); auto body_param = ui::RelativeLayoutParameter::create(); body_param->setAlign(ui::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL); body_param->setMargin(ui::Margin(sx(30), sy(10), sx(10), sy(10))); alert->body_txt = create_txt("Body content", body_param); Size body_size = alert->body_txt->getAutoRenderSize(); alert->body_txt->setTextAreaSize(Size( body_size.width, body_size.height )); auto close_btn = ui::Button::create(); close_btn->addTouchEventListener([alert](Ref*, TouchEventType type) { if (type == TouchEventType::ENDED) { Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Vec2 pos = Vec2( origin.x + visibleSize.width - 20, origin.y + 20 ); alert->shrink_close(pos); }; }); close_btn->setTitleText("X"); close_btn->setTitleColor(Color3B::RED); close_btn->setTitleFontSize(sx(40.0f)); close_btn->getTitleRenderer()->enableOutline(Color4B::GRAY, 10); close_btn->setScaleX(sx(1.0f)); close_btn->setScaleY(sy(1.0f)); alert->close_btn = close_btn; ui::RelativeLayoutParameter* close_param = ui::RelativeLayoutParameter::create(); close_param->setAlign(ui::RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT); close_param->setMargin(ui::Margin(sx(30), sy(20), sx(30), sy(30))); alert->close_btn->setLayoutParameter(close_param); alert->addChild(alert->close_btn); auto done_btn = ui::Button::create(); done_btn->addTouchEventListener([alert](Ref*, TouchEventType type) { if (type == TouchEventType::ENDED) { Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Vec2 pos = Vec2( origin.x + visibleSize.width - 20, origin.y + 20 ); alert->shrink_close(pos); }; }); done_btn->setScale9Enabled(true); done_btn->loadTextureNormal("main_UI_export_10_scale9_x4.png", ui::TextureResType::PLIST); done_btn->setTitleText("Done"); done_btn->setTitleFontSize(40.0f); done_btn->setTitleFontName(DEFAULT_FONT); auto lbl_size = done_btn->getTitleRenderer()->getContentSize(); done_btn->setContentSize( Size( lbl_size.width * 1.5f, lbl_size.height * 1.5f ) ); done_btn->ignoreContentAdaptWithSize(false); //word wrap or something done_btn->setTitleColor(Color3B::BLACK); done_btn->setScaleX(sx(1.0f)); done_btn->setScaleY(sy(1.0f)); alert->done_btn = done_btn; ui::RelativeLayoutParameter* done_param = ui::RelativeLayoutParameter::create(); done_param->setAlign(ui::RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL); done_param->setMargin(ui::Margin(sx(30), sy(20), sx(30), sy(30))); done_param->setRelativeName("done_btn"); alert->done_btn->setLayoutParameter(done_param); alert->addChild(alert->done_btn); auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(Alert::onTouchBegan, alert); listener->onTouchEnded = CC_CALLBACK_2(Alert::onTouchEnded, alert); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, alert); return alert; };
bool GameOverScene::initDict(cocos2d::CCDictionary *dic) { ThirdPartyHelper::setAd(SET_AD_SHOW); Size vs = Director::getInstance()->getVisibleSize(); Vec2 vo = Director::getInstance()->getVisibleOrigin(); godelay = 0; CCInteger *score = (CCInteger*)dic->objectForKey("score"); auto bg = Sprite::createWithSpriteFrameName("bg.png"); bg->setPosition(Vec2(vs.width/2 + vo.x, vs.height/2 + vo.y)); this->addChild(bg); auto panel = Sprite::create("panel.png"); panel->setPosition(bg->getPosition()); panel->setScaleX(2); panel->setScaleY(3.5); this->addChild(panel); auto gameover = ui::Text::create(LHLocalizedCString("gameover"), Common_Font, 70); gameover->setPosition(Vec2(vs.width/2 + vo.x, vs.height/3*2 + vo.y)); gameover->setColor(Color3B::BLACK); this->addChild(gameover); show(gameover); int best = UserDefault::getInstance()->getIntegerForKey("best", 0); auto scorelabel = ui::Text::create(StringUtils::format("%s %d",LHLocalizedCString("level"),score->getValue()-1), Common_Font, 50); scorelabel->setPosition(Vec2(vs.width/2 + vo.x, vs.height/2+scorelabel->getContentSize().height/2 + + vo.y)); scorelabel->setColor(Color3B::BLACK); this->addChild(scorelabel); show(scorelabel); auto bestlabel = ui::Text::create(StringUtils::format("%s %d",LHLocalizedCString("best"),best), Common_Font, 50); bestlabel->setPosition(Vec2(vs.width/2 + vo.x, vs.height/2-bestlabel->getContentSize().height/2+ vo.y)); bestlabel->setColor(Color3B::BLACK); this->addChild(bestlabel); show(bestlabel); float by = vs.height/6 + vo.y; float fs = 40; auto replay = ui::Button::create("replay.png"); replay->setTitleFontSize(fs); replay->setPosition(Vec2(vs.width/2+ vo.x, vs.height/3+ vo.y)); replay->addTouchEventListener([](Ref *ps,ui::Widget::TouchEventType type){ if (type == ui::Widget::TouchEventType::ENDED) { Director::getInstance()->replaceScene(PlayScene::createScene(nullptr)); } }); this->addChild(replay); show(replay); auto back = ui::Button::create("back.png"); back->setPosition(Vec2(vs.width/4 + vo.x, by)); back->addTouchEventListener([](Ref *ps,ui::Widget::TouchEventType type){ if (type == ui::Widget::TouchEventType::ENDED) { Director::getInstance()->replaceScene(HelloWorld::createScene()); } }); this->addChild(back); show(back); auto share = LHShareButton::defaultButton("share.png","LuckyGame"); share->setPosition(Vec2(vs.width/2 + vo.x, by)); this->addChild(share); show(share); std::string lb = "lb.png"; auto leader = LHLeaderBoard::defaultButton(lb); leader->setPosition(Vec2(vs.width/4*3 + vo.x, by)); this->addChild(leader); show(leader); return true; }
bool GameOverScene::initDict(cocos2d::CCDictionary *dic) { ThirdPartyHelper::setAd(SET_AD_SHOW); godelay = 0; Size vs = Director::getInstance()->getVisibleSize(); Vec2 vo = Director::getInstance()->getVisibleOrigin(); int step = ((CCInteger*)dic->objectForKey("step"))->getValue(); int level = ((CCInteger*)dic->objectForKey("level"))->getValue(); UserDefault::getInstance()->setBoolForKey(StringUtils::format("level%d_lock",level+1).c_str(), false); UserDefault::getInstance()->flush(); auto gameover = ui::Text::create(StringUtils::format("Level %d Pass!",level+1), Common_Font, 70); gameover->setPosition(Vec2(vs.width/2, vs.height/3*2 + vo.y)); gameover->setColor(Color3B::BLACK); this->addChild(gameover); show(gameover); int best = UserDefault::getInstance()->getIntegerForKey(StringUtils::format("level%d_step",level).c_str(), INT16_MAX); if (step<best) { best = step; UserDefault::getInstance()->setIntegerForKey(StringUtils::format("level%d_step",level).c_str(), best); } auto scorelabel = ui::Text::create(StringUtils::format("Step:%d",step), Common_Font, 50); scorelabel->setPosition(Vec2(vs.width/2, vs.height/2+scorelabel->getContentSize().height/2 + vo.y)); scorelabel->setColor(Color3B::BLACK); this->addChild(scorelabel); show(scorelabel); auto bestlabel = ui::Text::create(StringUtils::format("Best:%d",best), Common_Font, 50); bestlabel->setPosition(Vec2(vs.width/2, vs.height/2-bestlabel->getContentSize().height/2 + vo.y)); bestlabel->setColor(Color3B::BLACK); this->addChild(bestlabel); show(bestlabel); float by = vs.height/6 + vo.y; float fs = 40; if (level >= LVCOUNT-1) { }else{ auto replay = ui::Button::create("rb.png"); replay->setTitleText("Next"); replay->setTitleFontSize(fs); replay->setPosition(Vec2(vs.width/2, vs.height/3 + vo.y)); replay->addTouchEventListener([level](Ref *ps,ui::Widget::TouchEventType type){ if (type == ui::Widget::TouchEventType::ENDED) { Dictionary *dict = Dictionary::create(); dict->setObject(CCInteger::create(level+1),"level"); Director::getInstance()->replaceScene(PlayScene::createScene(dict)); } }); this->addChild(replay); show(replay); } auto back = ui::Button::create("rr.png"); back->setPosition(Vec2(vs.width/3, by)); back->setTitleFontSize(fs); back->setTitleText("Back"); back->addTouchEventListener([](Ref *ps,ui::Widget::TouchEventType type){ if (type == ui::Widget::TouchEventType::ENDED) { Director::getInstance()->replaceScene(HelloWorld::createScene()); } }); this->addChild(back); show(back); auto share = LHShareButton::defaultButton("rr.png","Lucky Puzzle!"); share->setTitleFontSize(fs); share->setTitleText("Share"); share->setPosition(Vec2(vs.width/3*2, by)); this->addChild(share); show(share); return true; }
bool ProvisionalScene::init() { if(!Layer::init()){return false;} auto sprite = Sprite::create("ImageFile/Menubg.png"); sprite->setPosition(winSize/2); this->addChild(sprite,3); Layer* optionLayer = Layer::create(); //メニューリスト生成-----------------------------------------------------> menuList->setContentSize(Size(winSize.width,winSize.height)); menuList->Node::setPosition((winSize - menuList->getContentSize())/2); addChild(menuList,1); //スライダーバー生成-----------------------------------------------------> for (int i = 0; i < 3; i++) { slider[i] = Slider::create(); slider[i]->loadBarTexture("ImageFile/sliderTrack.png"); slider[i]->loadSlidBallTextures("ImageFile/sliderThumb.png", "ImageFile/sliderThumb.png",""); slider[i]->loadProgressBarTexture("ImageFile/sliderProgress.png"); slider[i]->Node::setScale(2); } slider[0]->setPosition(Vec2(1225, -350)); slider[1]->setPosition(Vec2(1225, -500)); slider[2]->setPosition(Vec2(1225, -650)); //Closeボタン生成---------------------------------------------------> auto closeButton = Button::create("ImageFile/button1.png", "ImageFile/button1selected.png"); closeButton->setPosition(Vec2(winSize.width/2, -780)); closeButton->setScale(1.8,1.8); closeButton->setTitleText("閉じる"); closeButton->setTitleFontSize(16); closeButton->setTitleColor(Color3B::WHITE); //レイアウト生成------------------------------------> auto layout = Layout::create(); layout->setPosition(menuList->getContentSize()/2); //レイアウトの背景画像生成-------------------------------------------> auto imageView = ImageView::create("ImageFile/option/option_window.png"); imageView->setPosition(Vec2(winSize.width/2,-winSize.height/2)); imageView->setScale(1.5, 1.5); layout->addChild(imageView); for (int i = 0; i<3; i++) { layout->addChild(slider[i]); } layout->addChild(closeButton); menuList->addChild(layout); menuList->setEnabled(false); optionButton->setPosition(Vec2(200,100)); optionButton->setScale(0.1); addChild(optionButton,4); optionButton->addTouchEventListener([&,this](Ref *pSender, Widget::TouchEventType type){ this->reorderChild(menuList, 3); menuList->setEnabled(true); }); return true; }
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; };
// on "init" you need to initialize your instance bool PageTest::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } scheduleUpdate(); percent = 0; Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto loadingBar = ui::LoadingBar::create("sliderProgress.png"); loadingBar->setTag(0); loadingBar->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2)); loadingBar->setPercent(percent); this->addChild(loadingBar); auto checkBox = ui::CheckBox::create("check_box_normal.png", "check_box_normal_press.png", "check_box_active.png", "check_box_normal_disable.png", "check_box_active_disable.png"); checkBox->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 100)); checkBox->addEventListener(CC_CALLBACK_2(PageTest::selectedEvent, this)); this->addChild(checkBox); auto slider = ui::Slider::create(); slider->loadBarTexture("sliderTrack.png"); slider->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", ""); slider->loadProgressBarTexture("sliderProgress.png"); slider->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 200)); slider->addEventListener(CC_CALLBACK_2(PageTest::sliderEvent, this)); this->addChild(slider); // auto recustom = ui::RichElementCustomNode::create(1, Color3B::WHITE, 255, pAr); // richText->pushBackElement(recustom); // androidだとバックグランドが緑がデフォ? // androidだと盤面全体が背景白になる // http://discuss.cocos2d-x.org/t/ui-pageview-bug-with-v3-rc1-green-background-on-android/12897 // auto layout = ui::Layout::create(); layout->setSize(Size(visibleSize.width, 200.0f)); layout->setPosition(Vec2::ZERO); layout->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID); layout->setBackGroundColor(Color3B::BLUE); this->addChild(layout); auto pageView = ui::PageView::create(); /* pageView->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID); pageView->setBackGroundColor(Color3B::RED); */ pageView->setSize(Size(visibleSize.width, 130.0f)); pageView->setPosition(Vec2::ZERO); pageView->setContentSize(Size(visibleSize.width, 130.0f)); for (int i = 0; i < 3; ++i) { auto layout = ui::Layout::create(); layout->setBackGroundImage(""); layout->setBackGroundColor(Color3B::YELLOW); layout->setBackGroundColorType(ui::Layout::BackGroundColorType::SOLID); layout->setSize(Size(240.0f, 130.0f)); // layout->addTouchEventListener(<#ccWidgetTouchCallback callback#>) auto imageView = ui::ImageView::create("HelloWorld.png"); imageView->setScale9Enabled(true); imageView->setSize(Size(240, 130)); imageView->setPosition(Vec2(layout->getSize().width / 2.0f, layout->getSize().height / 2.0f)); layout->addChild(imageView); auto label = ui::Text::create(StringUtils::format("page %d",(i+1)), "Arial", 30); label->setColor(Color3B(192, 192, 192)); label->setPosition(Vec2(layout->getSize().width / 2.0f, layout->getSize().height / 2.0f)); layout->addChild(label); auto btn = ui::Button::create(""); btn->setTitleText(""); btn->setTitleFontSize(12); btn->setTitleFontName(""); pageView->addPage(layout); } // pageView->scrollToPage(2); // うまくいかない。handleReleaseLogicのboundaryのためだと思われる pageView->addEventListener(CC_CALLBACK_2(PageTest::pageViewEvent, this)); layout->addChild(pageView); auto richText = ui::RichText::create(); richText->ignoreContentAdaptWithSize(false); richText->setSize(Size(100, 100)); richText->setPosition(Vec2(visibleSize.width / 2.0f, visibleSize.height - 300)); this->addChild(richText); auto re1 = ui::RichElementText::create(1, Color3B::WHITE, 255, "This color is white. ", "Arial", 10); richText->pushBackElement(re1); auto re2 = ui::RichElementText::create(2, Color3B::YELLOW, 255, "This color is yellow. ", "Arial", 24); richText->insertElement(re2, 1); auto reimg = ui::RichElementImage::create(6, Color3B::WHITE, 255, "sliderballnormal.png"); richText->pushBackElement(reimg); return true; }