示例#1
0
void UIButtonTest_Title::touchEvent(Ref *pSender, Widget::TouchEventType type)
{
    switch (type)
    {
        case Widget::TouchEventType::BEGAN:
            _displayValueLabel->setString(String::createWithFormat("Touch Down")->getCString());
            break;
            
        case Widget::TouchEventType::MOVED:
            _displayValueLabel->setString(String::createWithFormat("Touch Move")->getCString());
            break;
            
        case Widget::TouchEventType::ENDED:
        {
            _displayValueLabel->setString(String::createWithFormat("Touch Up")->getCString());
            TextBMFont *text = (TextBMFont*)_uiLayer->getChildByName("text");
            text->setFntFile("cocosui/bitmapFontTest2.fnt");
            if (text->getString() == "BMFont") {
                text->setString("Hello");
            }
            else{
                text->setString("BMFont");
            }
        }
            break;
            
        case Widget::TouchEventType::CANCELED:
            _displayValueLabel->setString(String::createWithFormat("Touch Cancelled")->getCString());
            break;
            
        default:
            break;
    }
}
示例#2
0
 void TextBMFontReader::setPropsFromJsonDictionary(Widget *widget, const rapidjson::Value &options)
 {
     WidgetReader::setPropsFromJsonDictionary(widget, options);
     
     
     std::string jsonPath = GUIReader::getInstance()->getFilePath();
     
     TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
     
     const rapidjson::Value& cmftDic = DICTOOL->getSubDictionary_json(options, P_FileNameData);
     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();
             labelBMFont->setFntFile(cmf_tp);
             break;
         }
         case 1:
             CCLOG("Wrong res type of LabelAtlas!");
             break;
         default:
             break;
     }
     
     const char* text = DICTOOL->getStringValue_json(options, P_Text,"Text Label");
     labelBMFont->setString(text);
     
     
     WidgetReader::setColorPropsFromJsonDictionary(widget, options);
 }
示例#3
0
 void TextBMFontReader::setPropsFromBinary(cocos2d::ui::Widget *widget, CocoLoader *cocoLoader, stExpCocoNode *cocoNode)
 {
     this->beginSetBasicProperties(widget);
     
     TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
     
     
     stExpCocoNode *stChildArray = cocoNode->GetChildArray(cocoLoader);
     
     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_FileNameData){
             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);
             if (imageFileNameType == (Widget::TextureResType)0) {
                 labelBMFont->setFntFile(backgroundValue);
             }
             
         }else if(key == P_Text){
             labelBMFont->setString(value);
         }
     } //end of for loop
     this->endSetBasicProperties(widget);
 }
示例#4
0
 void TextBMFontReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textBMFontOptions)
 {
     TextBMFont* labelBMFont = static_cast<TextBMFont*>(node);
     auto options = (TextBMFontOptions*)textBMFontOptions;
     
     auto cmftDic = options->fileNameData();
     int cmfType = cmftDic->resourceType();
     switch (cmfType)
     {
         case 0:
         {
             const char* cmfPath = cmftDic->path()->c_str();
             labelBMFont->setFntFile(cmfPath);
             break;
         }
             
         case 1:
             CCLOG("Wrong res type of LabelAtlas!");
             break;
             
         default:
             break;
     }
     
     std::string text = options->text()->c_str();
     labelBMFont->setString(text);
     
     auto widgetReader = WidgetReader::getInstance();
     widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
 }
示例#5
0
 void TextBMFontReader::setPropsWithFlatBuffers(cocos2d::Node *node, const flatbuffers::Table *textBMFontOptions)
 {
     TextBMFont* labelBMFont = static_cast<TextBMFont*>(node);
     auto options = (TextBMFontOptions*)textBMFontOptions;
     
     auto cmftDic = options->fileNameData();
     bool fileExist = false;
     std::string errorFilePath = "";
     std::string errorContent = "";
     std::string path = cmftDic->path()->c_str();
     int cmfType = cmftDic->resourceType();
     switch (cmfType)
     {
         case 0:
         {
             if (FileUtils::getInstance()->isFileExist(path))
             {
                 FontAtlas* newAtlas = FontAtlasCache::getFontAtlasFNT(path);
                 if (newAtlas)
                 {
                     fileExist = true;
                 }
                 else
                 {
                     errorContent = "has problem";
                     fileExist = false;
                 }
             }
             else
             {
                 errorContent = "missed";
                 fileExist = false;
             }
             break;
         }
             
         default:
             break;
     }
     if (fileExist)
     {
         labelBMFont->setFntFile(path);
     }
     else
     {
         errorFilePath = path;
         auto label = Label::create();
         label->setString(__String::createWithFormat("%s %s", errorFilePath.c_str(), errorContent.c_str())->getCString());
         labelBMFont->addChild(label);
     }
     
     std::string text = options->text()->c_str();
     labelBMFont->setString(text);
     
     auto widgetReader = WidgetReader::getInstance();
     widgetReader->setPropsWithFlatBuffers(node, (Table*)options->widgetOptions());
     
     labelBMFont->ignoreContentAdaptWithSize(true);
 }
TextBMFont* TextBMFont::create()
{
    TextBMFont* widget = new TextBMFont();
    if (widget && widget->init())
    {
        widget->autorelease();
        return widget;
    }
    CC_SAFE_DELETE(widget);
    return nullptr;
}
示例#7
0
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 HelloWorld::textInput(Object* object,TextFiledEventType type)
{
    switch (type) {
    case TEXTFIELD_EVENT_DETACH_WITH_IME:
    {
        TextField* textField = dynamic_cast<TextField*>(object);
        std::string words = textField->getStringValue();
        TextBMFont* label = dynamic_cast<TextBMFont*>(textField->getParent()->getChildByTag(100));
        label->setText(words.c_str());
        textField->setText("");

        UserDefault::getInstance()->setStringForKey("userStr", words);
        UserDefault::getInstance()->flush();
    }
    break;

    default:
        break;
    }
}
示例#9
0
 void TextBMFontReader::setPropsFromProtocolBuffers(ui::Widget *widget, const protocolbuffers::NodeTree &nodeTree)
 {
     WidgetReader::setPropsFromProtocolBuffers(widget, nodeTree);
     
     std::string jsonPath = GUIReader::getInstance()->getFilePath();
     
     TextBMFont* labelBMFont = static_cast<TextBMFont*>(widget);
     const protocolbuffers::TextBMFontOptions& options = nodeTree.textbmfontoptions();
     
     
     const protocolbuffers::ResourceData& cmftDic = options.filenamedata();
     int cmfType = cmftDic.resourcetype();
     switch (cmfType)
     {
         case 0:
         {
             std::string tp_c = jsonPath;
             const char* cmfPath = cmftDic.path().c_str();
             const char* cmf_tp = tp_c.append(cmfPath).c_str();
             labelBMFont->setFntFile(cmf_tp);
             break;
         }
         case 1:
             CCLOG("Wrong res type of LabelAtlas!");
             break;
         default:
             break;
     }
     
     const char* text = (options.has_text()) ? options.text().c_str() : "Text Label";
     labelBMFont->setString(text);
     
     
     // other commonly protperties
     WidgetReader::setColorPropsFromProtocolBuffers(widget, nodeTree);
 }
示例#10
0
 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);
 }