cocos2dx图文混排(三)
2014-01-11 14:29
176 查看
有了文字渲染 那么图片渲染是很简单的(是个人都知道拉 CCSprite* S = CCSprite::createWidthSpriteFrame(FrameName) );
那么我们只要创建一个 CCNode* N然后把 CCSprite和写满字体的CCSpriteBatchNode都加到N的子节点就可以了撒
相信是比较简单的 但是为了给外界一个方便的接口 我们还是是用SetText(const std::string&) 作为我们的接口
那么这样如何区到底是图片还是文字呢? 你可以使用Html, xml 等等数据结构来描述我们的文本
由于我们项目使用的是PrototBuff , 可以很方便的序列化和反序列化字符串
所以我们只要使用google::protobuf::TextFormat::Parser 来反序列化我们的字符串就可以了
伪代码如下
最后把N放到需要显示的地方就可以了
那么我们只要创建一个 CCNode* N然后把 CCSprite和写满字体的CCSpriteBatchNode都加到N的子节点就可以了撒
相信是比较简单的 但是为了给外界一个方便的接口 我们还是是用SetText(const std::string&) 作为我们的接口
那么这样如何区到底是图片还是文字呢? 你可以使用Html, xml 等等数据结构来描述我们的文本
由于我们项目使用的是PrototBuff , 可以很方便的序列化和反序列化字符串
message TextStyle { optional string FontName = 1; //字体 空使用程序默认 optional uint32 FontSize = 2; //大小 0则使用程序默认 optional bool UnderLine = 3[default=false]; //下划线 optional string Image = 4; //图片帧 optional string UserID = 5; //自定义ID 0表示没有事件 optional uint32 R = 6[default=255]; //字体颜色值 optional uint32 G = 7[default=255]; optional uint32 B = 8[default=255]; optional bool Enter = 9; //回车 optional float ImageScaleFactor = 10[default=1.0];//图片帧缩放 }; message TextContent { optional TextStyle Style = 1; //样式 optional string Content = 2; //正文 }; message TextContentArray { repeated TextContent Contents = 1; }
所以我们只要使用google::protobuf::TextFormat::Parser 来反序列化我们的字符串就可以了
伪代码如下
SetText( const string& Text ) { CCNode* N = CCNode::create(); TextContentArray Array; google::protobuf::TextFormat::Parser P; P.ParseFromString( Text, &Array ); for each ( TextContent Content in Array ) { if( Content.style().Image().empty() ) { CCSprite* Sp = CCSprite::createWidthSpriteFrame( Content.style().Image().c_str() ); N->addChild(Sp); } else { CCSpriteBatchNode* B = CCSpriteBatchNode::createWithTexture(RenderTexture); N->addChild(B); for each( unicodeChar in Content.content() ) { find TextCharInfo in RenderTexture { CCSprite* S = CCSprite::createWithTexture( B->getTexture(), TextCharInfo. get CharRect ); B->addChild(S); S->setPosition( YourCoursorX, YourCoursorY ); } } } } }
最后把N放到需要显示的地方就可以了
相关文章推荐
- cocos2dx 3.0 之 用std::bind替换CC_CALLBACK_N
- 新建cocos2dx开发遇到问题及解决办法
- cocos2dx中对lua脚本进行覆盖率测试
- cocos2dx 命令解释
- cocos2dx人物行走例子
- 【独立开发者er Cocos2d-x实战 007】使用Cocos2dx UserDefault.xml
- <转>cocos2dx3.0rc创建及编译到安卓详细步骤
- cocos2dx 3.6 CCPhysicsBody.h 翻译
- cocos2dx3.0 vector删除元素
- [cocos2dx]重要父类——CCNode节点类
- 区分上下左右手势cocos2dx
- cocos2dx 3.x创建工程
- Cocos2dx 3.0 过渡篇(十)资源加载进度条Loading...
- 搭建cocos2d-x-android环境 Windows XP3 + Eclipse + NDKR7(或ndkr7b)+COCOS2DX(没有用到cygwin和minigw)
- Cocos2dx 3.0正式版本UI解析错乱解决方案
- Cocos2dx-角色创建与动画实现
- cocos2dx 字体
- cocos2dx-3.1 接入多盟广告sdk+Android (3)
- [ISSUE]invalid 'cobj' in function 'lua_cocos2dx_EventDispatcher_dispatchCustomEvent'
- 通过RTLabel、RCLabel、FTCoreText及webView结合SDWebImage等4种方式实现左滑或下拉中cell中图文混排使用场景方案