CTextRich* CTextRich::create() { CTextRich* pRet = new CTextRich(); if( pRet && pRet->init() ) { pRet->autorelease(); return pRet; } CC_SAFE_DELETE(pRet); return NULL; }
CTextRich* CTextRich::create() { CTextRich* widget = new (std::nothrow) CTextRich(); if (widget && widget->init()) { widget->setTouchEnabled(true); widget->autorelease(); return widget; } CC_SAFE_DELETE(widget); return nullptr; }
bool CTextRichBasicTest::init() { CTextRichTestSceneBase::init(); setTitle("CTextRichBasicTest"); setDescription("a basic text rich widget with a single line"); CTextRich* pTextRich = CTextRich::create(); pTextRich->insertElement("TEXT", "", 20); pTextRich->insertElement("Rich", "", 45); pTextRich->insertElement("And", "", 25, Color3B::RED); pTextRich->insertElement("cocos2d-x", "", 70, Color3B::BLUE); pTextRich->setPosition(Vec2(960/2, 640/2)); pTextRich->reloadData(); CColorView* pColor = CColorView::create(Color4B(255, 0, 0, 50)); pColor->setContentSize(pTextRich->getContentSize()); pColor->setPosition(Vec2(960/2, 640/2)); m_pWindow->addChild(pColor); m_pWindow->addChild(pTextRich); return true; }
CTextRich *TuiManager::createTextRich(float tag, const char *text, int maxLen, float x, float y, float w, float h, float rotation, int isUseFrame){ CTextRich *pTextRich = CTextRich::create(); pTextRich->setMaxLineLength(maxLen); char* buf = new char[string(text).size() + 1]; memcpy(buf, text, string(text).size() + 1); xml_document<> doc; doc.parse<0>(buf); for (xml_node<char> *item = doc.first_node("e"); item != NULL; item = item->next_sibling()){ if (strcmp(item->first_attribute("type")->value(), kTuiControlLabel) == 0){ //label const char *text = item->first_attribute("text")->value(); float textSize = 22; const char *fontName = ""; int r = 255, g = 255, b = 255; if (item->first_attribute("size")) textSize = atof(item->first_attribute("size")->value()); if (item->first_attribute("font")) fontName = item->first_attribute("font")->value(); if (item->first_attribute("r")) r = atoi(item->first_attribute("r")->value()); if (item->first_attribute("g")) g = atoi(item->first_attribute("g")->value()); if (item->first_attribute("b")) b = atoi(item->first_attribute("b")->value()); pTextRich->insertElement(TuiUtil::replace_all(text, "\\n", "\n").c_str(), fontName, textSize, Color3B(r, g, b)); }else if (strcmp(item->first_attribute("type")->value(), kTuiControlImage) == 0){//image const char *imgSrc = item->first_attribute("src")->value(); int len = atoi(item->first_attribute("len")->value()); if (isUseFrame){ pTextRich->insertElement(CImageView::createWithSpriteFrameName(imgSrc), len); }else{ pTextRich->insertElement(CImageView::create(imgSrc), len); } }else if (strcmp(item->first_attribute("type")->value(), kTuiControlAnim) == 0){//anim const char *name = item->first_attribute("name")->value(); const char *plist = item->first_attribute("src")->value(); int len = atoi(item->first_attribute("len")->value()); Sprite *pAnim = createAnim(0, name, plist, 0, 0, 0); pTextRich->insertElement(pAnim, len); } } delete[] buf; pTextRich->reloadData(); pTextRich->setAnchorPoint(Vec2(0, 1)); pTextRich->setRotation(rotation); pTextRich->setPosition(Vec2(x, -y)); pTextRich->setTag(tag); return pTextRich; }
bool CTextRichEventTest::init() { CTextRichTestSceneBase::init(); setTitle("CTextRichBasicTest"); setDescription("mutil line and event test"); CTextRich* pTextRich = CTextRich::create(); pTextRich->setMaxLineLength(20); pTextRich->setVerticalSpacing(15.0f); pTextRich->insertElement("[System]:", "", 30); pTextRich->insertElement("cocos2d-x ", "", 30, Color3B::RED); pTextRich->insertElement("is a good mobile phone game engine and "); pTextRich->insertElement("cocoswidget ", "", 30, Color3B::RED); pTextRich->insertElement("be helpful for it ", "", 30, Color3B::ORANGE); pTextRich->insertElement("[ click me ] ", "", 30, Color3B::MAGENTA, "Label"); pTextRich->insertElement("my name ", "", 30); pTextRich->insertElement("Jason lee - Lijunlin", "", 30, Color3B::YELLOW, "Jason lee - Lijunlin"); Sprite* pIcon = Sprite::create("icon.png"); pIcon->runAction(RepeatForever::create(Blink::create(1.0f, 5))); pTextRich->insertElement(pIcon, 4, "Image"); pTextRich->insertElement("hehe ByeBye","", 30); pTextRich->setPosition(Vec2(960/2, 640/2)); pTextRich->reloadData(); pTextRich->setOnTextRichClickListener(this, ccw_textrichclick_selector(CTextRichEventTest::onClickTextRich)); m_pWindow->addChild(pTextRich); m_pLabel = CLabel::createWithSystemFont("None", "", 30); m_pLabel->setPosition(Vec2(800, 320)); m_pWindow->addChild(m_pLabel); return true; }