bool UIButtonTest_Title::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add a label in which the text button events will be displayed _displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32); _displayValueLabel->setAnchorPoint(Vec2(0.5f, -1)); _displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); _uiLayer->addChild(_displayValueLabel); // Add the alert Text* alert = Text::create("Button with title, title should be flipped!", "fonts/Marker Felt.ttf", 30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f)); _uiLayer->addChild(alert); // Create the button with title Button* button = Button::create("cocosui/backtotoppressed.png", "cocosui/backtotopnormal.png"); button->setTitleText("Title Button!"); button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)); button->setTitleColor(Color3B::YELLOW); CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!"); button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this)); _uiLayer->addChild(button); button->setFlippedX(true); auto label = button->getTitleRenderer(); label->setScale(4.0); button->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(1.0f, 1.2f), ScaleTo::create(1.0f, 1.0f),nullptr))); TextBMFont *text = TextBMFont::create("BMFont", "cocosui/bitmapFontTest2.fnt"); text->setPosition(button->getPosition() + Vec2(button->getContentSize().width/2 + 50,0)); text->setColor(Color3B::YELLOW); text->setOpacity(50); text->setName("text"); _uiLayer->addChild(text); return true; } return false; }
void TextBMFontReader::setPropsFromXML(cocos2d::ui::Widget *widget, const tinyxml2::XMLElement *objectData) { WidgetReader::setPropsFromXML(widget, objectData); TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget); std::string xmlPath = GUIReader::getInstance()->getFilePath(); std::string text = ""; int opacity = 255; // attributes const tinyxml2::XMLAttribute* attribute = objectData->FirstAttribute(); while (attribute) { std::string name = attribute->Name(); std::string value = attribute->Value(); if (name == "LabelText") { text = value; } else if (name == "Alpha") { opacity = atoi(value.c_str()); } attribute = attribute->Next(); } // child elements const tinyxml2::XMLElement* child = objectData->FirstChildElement(); while (child) { std::string name = child->Name(); if (name == "LabelBMFontFile_CNB") { attribute = child->FirstAttribute(); int resourceType = 0; std::string path = "", plistFile = ""; while (attribute) { name = attribute->Name(); std::string value = attribute->Value(); if (name == "Path") { path = value; } else if (name == "Type") { resourceType = (value == "Normal" || value == "Default" || value == "MarkedSubImage") ? 0 : 1; } else if (name == "Plist") { plistFile = value; } attribute = attribute->Next(); } switch (resourceType) { case 0: { labelBMFont->setFntFile(xmlPath + path); break; } default: break; } } child = child->NextSiblingElement(); } labelBMFont->setString(text); labelBMFont->setOpacity(opacity); }