cocos2dx图文混排(三)

13 篇文章 0 订阅
3 篇文章 0 订阅

有了文字渲染 那么图片渲染是很简单的(是个人都知道拉 CCSprite* S = CCSprite::createWidthSpriteFrame(FrameName) );


那么我们只要创建一个 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放到需要显示的地方就可以了


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值