void TextAtlasReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textAtlasOptions) { TextAtlas* labelAtlas = static_cast<TextAtlas*>(node); auto options = (TextAtlasOptions*)textAtlasOptions; auto cmftDic = options->charMapFileData(); int cmfType = cmftDic->resourceType(); switch (cmfType) { case 0: { const char* cmfPath = cmftDic->path()->c_str(); std::string stringValue = options->stringValue()->c_str(); int itemWidth = options->itemWidth(); int itemHeight = options->itemHeight(); labelAtlas->setProperty(stringValue, cmfPath, itemWidth, itemHeight, options->startCharMap()->c_str()); break; } case 1: CCLOG("Wrong res type of LabelAtlas!"); break; default: break; } auto widgetReader = WidgetReader::getInstance(); widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions()); }
void UI_Bag_Gem_Layer::ClearEachCell() { /************************************************************************/ // 清除每个Cell里面的信息 for (int nIndex = 0; nIndex < MAX_TAG_COUNT; ++nIndex) { // 1. 读取Json文件获取包裹每一个单元格 Widget *pLayer = dynamic_cast<Widget*>(m_pBagGemScrollView->getChildByTag(nIndex)); if (pLayer == NULL) return; // 2. 设置Icon ImageView * pMaterialImage = dynamic_cast<ui::ImageView*>(Helper::seekWidgetByName(pLayer, "Icon_ImagerView")); if (pMaterialImage == NULL) return; pMaterialImage->setVisible(false); // 3. 设置物品选中框 ImageView * pMaterialSelectedImage = dynamic_cast<ui::ImageView*>(Helper::seekWidgetByName(pLayer, "Select_Box")); if (pMaterialSelectedImage == NULL) return; pMaterialSelectedImage->setVisible(false); // 4. 设置物品的个数 TextAtlas * pMateriCount = dynamic_cast<ui::TextAtlas*>(Helper::seekWidgetByName(pLayer, "Count_Text")); if (pMateriCount == NULL) return; pMateriCount->setVisible(false); } }
void TextAtlasReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textAtlasOptions) { TextAtlas* labelAtlas = static_cast<TextAtlas*>(node); auto options = (TextAtlasOptions*)textAtlasOptions; auto cmftDic = options->charMapFileData(); int cmfType = cmftDic->resourceType(); switch (cmfType) { case 0: { const char* cmfPath = cmftDic->path()->c_str(); bool fileExist = false; std::string errorFilePath = ""; if (FileUtils::getInstance()->isFileExist(cmfPath)) { fileExist = true; std::string stringValue = options->stringValue()->c_str(); int itemWidth = options->itemWidth(); int itemHeight = options->itemHeight(); labelAtlas->setProperty(stringValue, cmfPath, itemWidth, itemHeight, options->startCharMap()->c_str()); } else { errorFilePath = cmfPath; fileExist = false; } //if (!fileExist) //{ // auto label = Label::create(); // label->setString(__String::createWithFormat("%s missed", errorFilePath.c_str())->getCString()); // labelAtlas->addChild(label); //} break; } case 1: CCLOG("Wrong res type of LabelAtlas!"); break; default: break; } auto widgetReader = WidgetReader::getInstance(); widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions()); labelAtlas->ignoreContentAdaptWithSize(true); }
TextAtlas* TextAtlas::create() { TextAtlas* widget = new TextAtlas(); if (widget && widget->init()) { widget->autorelease(); return widget; } CC_SAFE_DELETE(widget); return nullptr; }
void TextAtlasReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode) { this->beginSetBasicProperties(widget); TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget); stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader); Widget::TextureResType type; std::string charMapFileName; std::string stringValue; std::string startCharMap; float itemWidth; float itemHeight; for (int i = 0; i < cocoNode->GetChildNum(); ++i) { std::string key = stChildArray[i].GetName(cocoLoader); std::string value = stChildArray[i].GetValue(cocoLoader); //read all basic properties of widget CC_BASIC_PROPERTY_BINARY_READER //read all color related properties of widget CC_COLOR_PROPERTY_BINARY_READER else if (key == P_StringValue) { stringValue = value; } else if(key == P_CharMapFileData){ stExpCocoNode *backGroundChildren = stChildArray[i].GetChildArray(cocoLoader); std::string resType = backGroundChildren[2].GetValue(cocoLoader);; Widget::TextureResType imageFileNameType = (Widget::TextureResType)valueToInt(resType); std::string backgroundValue = this->getResourcePath(cocoLoader, &stChildArray[i], imageFileNameType); charMapFileName = backgroundValue; type = imageFileNameType; }else if(key == P_ItemWidth){ itemWidth = valueToFloat(value); }else if(key == P_ItemHeight){ itemHeight = valueToFloat(value); }else if(key == P_StartCharMap){ startCharMap = value; } } //end of for loop if (type == (Widget::TextureResType)0) { labelAtlas->setProperty(stringValue, charMapFileName, itemWidth, itemHeight, startCharMap); } this->endSetBasicProperties(widget); }
bool UI_Bag_Gem_Layer::SetSingleCellInfo(const int nTagID, Game_Data::Item_Container * pInfo) { /************************************************************************/ // 设置单个Cell的信息 // 1. 判断索引ID是否异常 if (nTagID >= MAX_TAG_COUNT) return false; // 2. 获取该物品的Config Game_Data::Item_Config * pItemConfig = ITEM_CONFIG_MGR::instance()->get_item_config(pInfo->item_id); if (pItemConfig == NULL) return false; // 3. 读取Json文件获取包裹每一个单元格 Widget *pLayer = dynamic_cast<Widget*>(m_pBagGemScrollView->getChildByTag(nTagID)); if (pLayer == NULL) return false; /************************************************************************/ // 条件满足设置物品信息 int nTemp = 0; const char * szTemp; // 1. 设置Icon ImageView * pMaterialImage = dynamic_cast<ui::ImageView*>(Helper::seekWidgetByName(pLayer, "Icon_ImagerView")); if (pMaterialImage == NULL) return false; szTemp = ITEM_CONFIG_MGR::instance()->get_icon_path(pItemConfig->icon); std::string p("icon/"); p+=szTemp; nTemp = strcmp(p.c_str(), ""); if (nTemp <= 0) return false; pMaterialImage->loadTexture(p.c_str()); pMaterialImage->setVisible(true); // 2. 设置物品的个数 TextAtlas * pMateriCount = dynamic_cast<ui::TextAtlas*>(Helper::seekWidgetByName(pLayer, "Count_Text")); if (pMaterialImage == NULL) return false; pMateriCount->setString(CCString::createWithFormat("%d", pInfo->item_count)->getCString()); pMateriCount->setVisible(true); // 3. 为每件物品注册点击事件 pMaterialImage->setTag(nTagID); pMaterialImage->addTouchEventListener(CC_CALLBACK_2(UI_Bag_Gem_Layer::OnBtnClickMaterialCallBack, this)); return true; }
TextAtlas* TextAtlas::create(const std::string &stringValue, const std::string &charMapFile, int itemWidth, int itemHeight, const std::string &startCharMap) { TextAtlas* widget = new TextAtlas(); if (widget && widget->init()) { widget->autorelease(); widget->setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap); return widget; } CC_SAFE_DELETE(widget); return nullptr; }
void TextAtlasReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options) { WidgetReader::setPropsFromJsonDictionary(widget, options); std::string jsonPath = GUIReader::getInstance()->getFilePath(); TextAtlas* labelAtlas = static_cast<TextAtlas*>(widget); // bool sv = DICTOOL->checkObjectExist_json(options, P_StringValue); // bool cmf = DICTOOL->checkObjectExist_json(options, P_CharMapFile); // bool iw = DICTOOL->checkObjectExist_json(options, P_ItemWidth); // bool ih = DICTOOL->checkObjectExist_json(options, P_ItemHeight); // bool scm = DICTOOL->checkObjectExist_json(options, P_StartCharMap); const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_CharMapFileData); int cmfType = DICTOOL->getIntValue_json(cmftDic, P_ResourceType); switch (cmfType) { case 0: { std::string tp_c = jsonPath; const char* cmfPath = DICTOOL->getStringValue_json(cmftDic, P_Path); const char* cmf_tp = tp_c.append(cmfPath).c_str(); labelAtlas->setProperty(DICTOOL->getStringValue_json(options, P_StringValue,"12345678"), cmf_tp, DICTOOL->getIntValue_json(options, P_ItemWidth,24), DICTOOL->getIntValue_json(options,P_ItemHeight,32), DICTOOL->getStringValue_json(options, P_StartCharMap)); break; } case 1: CCLOG("Wrong res type of LabelAtlas!"); break; default: break; } WidgetReader::setColorPropsFromJsonDictionary(widget, options); }
bool UITextAtlasTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getSize(); // Add the alert Text* alert = Text::create("TextAtlas","fonts/Marker Felt.ttf",30); alert->setColor(Color3B(159, 168, 176)); alert->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 1.75f)); _uiLayer->addChild(alert); // Create the text atlas TextAtlas* textAtlas = TextAtlas::create("1234567890", "cocosui/labelatlas.png", 17, 22, "0"); textAtlas->setPosition(Vec2((widgetSize.width) / 2, widgetSize.height / 2.0f)); _uiLayer->addChild(textAtlas); return true; } return false; }
bool UITextAtlasETC1ShadowTest::init() { if (UIScene::init()) { Size widgetSize = _widget->getContentSize(); // Add the alert Text* alert = Text::create("TextAtlas With ETC1 format","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 text atlas TextAtlas* textAtlas = nullptr; if (Director::getInstance()->getWinSizeInPixels().height > 320.f) { textAtlas = TextAtlas::create("1234567890", "cocosui/labelatlas.pkm", 34, 44, "0"); } else { textAtlas = TextAtlas::create("1234567890", "cocosui/labelatlas.pkm", 17, 22, "0"); } textAtlas->setPosition(Vec2((widgetSize.width) / 2, widgetSize.height / 2.0f)); _uiLayer->addChild(textAtlas); auto labelAtlas = (Label*)textAtlas->getVirtualRenderer(); labelAtlas->enableShadow(Color4B::GREEN); _textAtlas = textAtlas; TTFConfig ttfConfig("fonts/arial.ttf", 15); auto label1 = Label::createWithTTF(ttfConfig, "Print Resources"); auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(UITextAtlasETC1ShadowTest::printWidgetResources, this)); item1->setPosition(Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3)); auto pMenu1 = Menu::create(item1, nullptr); pMenu1->setPosition(Vec2(0, 0)); this->addChild(pMenu1, 10); return true; } return false; }