cocos2d-x學習日誌(16)

來源:互聯網
上載者:User

      最近做聊天系統,遇到棘手的問題,就是字型要支援多顏色、換行、表情(圖片)、超連結!我們不會從OpenGL底層來做這個工作,因為那樣工作量非常大,不現實,考慮在已有的cocos2d-x介面上進行處理,來組合出我們需要富文本。因Android IOS 似乎都支援 freetype2,所以就優先考慮了。


1.下載準備:


      freetype2:http://download.savannah.gnu.org/releases/freetype/

      擴充庫:https://github.com/happykevins/cocos2dx-ext


2.搭建環境


2.1 配置freetype2


2.2.建立工程,添加檔案,如下:

      工程根目錄:




      class檔案夾:




       vs2010工程目錄:


                                                      

3. 代碼:


HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "cocos-ext.h"#include <renren-ext.h>USING_NS_CC;USING_NS_CC_EXT;class HelloWorld : public cocos2d::CCLayer{public:    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone    virtual bool init();      // there's no 'id' in cpp, so we recommend returning the class instance pointer    static cocos2d::CCScene* scene();        // a selector callback    void menuCloseCallback(CCObject* pSender);        // implement the "static node()" method manually    CREATE_FUNC(HelloWorld);bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent);// HTML eventsvoid onHTMLClicked(IRichNode* root, IRichElement* ele, int _id);void onHTMLMoved(IRichNode* root, IRichElement* ele, int _id,const CCPoint& location, const CCPoint& delta);};#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp

static CCHTMLLabel* s_htmlLabel = NULL;std::string tt;// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->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    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                        "CloseNormal.png",                                        "CloseSelected.png",                                        this,                                        menu_selector(HelloWorld::menuCloseCallback));    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,                                origin.y + pCloseItem->getContentSize().height/2));    // create menu, it's an autorelease object    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);    pMenu->setPosition(CCPointZero);    this->addChild(pMenu, 1);using namespace dfont;CCLayerColor* l = CCLayerColor::create(ccc4(0xb0, 0xb0, 0xb0, 0xff));l->setContentSize(this->getContentSize());this->addChild(l);//控制項文字樣式(尺寸、對齊、字型等等)// font1FontCatalog* font_catalog = NULL;font_catalog = FontFactory::instance()->create_font("font1", "simhei.ttf", 0xffffffff, 32, e_plain, 0.0f, 0xffffffff, 0);// font2font_catalog = FontFactory::instance()->create_font("font2", "simkai.ttf", 0xffffffff, 24, e_shadow, 1.0f, 0xff000000, 0);font_catalog->add_hackfont("htmltest/Marker Felt.ttf", latin_charset(), -1);// font3font_catalog = FontFactory::instance()->create_font("font3", "simli.ttf", 0xffffffff, 20, e_border, 1.0f, 0xff000000, 0);font_catalog->add_hackfont("simhei.ttf", latin_charset(), 5);CCSize vsize = CCDirector::sharedDirector()->getVisibleSize();CCString* str_utf8 = CCString::createWithContentsOfFile("html.htm");CCHTMLLabel* htmllabel = CCHTMLLabel::createWithString(str_utf8->getCString(), CCSize(vsize.width*0.8f, vsize.height), "default");htmllabel->setAnchorPoint(ccp(0.5f,0.5f));htmllabel->setPosition(ccp(vsize.width*0.5f, vsize.height*0.5f));addChild(htmllabel);s_htmlLabel = htmllabel;//建立超連結htmllabel->registerListener(this, &HelloWorld::onHTMLClicked, &HelloWorld::onHTMLMoved );FontFactory::instance()->dump_textures();    return true;}void HelloWorld::onHTMLClicked(IRichNode* root, IRichElement* ele, int _id){CCLog("[On Clicked] id=%d", _id);if ( !s_htmlLabel ){return;}else if ( _id == 1002 ) // close{s_htmlLabel->setVisible(false);}else if ( _id == 2000 ) //reload{CCString* str_utf8 = CCString::createWithContentsOfFile("html.htm");s_htmlLabel->setString(str_utf8->getCString());}}void HelloWorld::onHTMLMoved(IRichNode* root, IRichElement* ele, int _id,const CCPoint& location, const CCPoint& delta){CCLog("[On Moved] id=%d", _id);if ( !s_htmlLabel ){return;}else if ( _id == 1001 ){s_htmlLabel->setPosition(ccpAdd(delta, s_htmlLabel->getPosition()));}}bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){return true;}void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}

運行:



      點擊超連結:


                     


      控制台顯示:

                       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.