Torque2D MIT 学习笔记(23) ---- 编写自己的多语言文本库

前言

  Torque2D对于汉字的支持可以说目前很差,虽然支持Freetype,但是内部的字符串编码是UTF系列,基本没有Ascii,Unicode这种Win32常用的编码格式.

  如果你想让你的程序支持汉字输入,那就要改不少东西,但是如果是显示,那就另当别论了,你可以使用它们提供的LangTable来实现(本人看的吃力,貌似不支持关键字查找,只能索引序列,而且不是按行读取的,不知道它是怎么操作的,难道有什么工具先编译,反正Torque3D有个编译的函数,2D里没有.)

  作者们最近忙文件格式和Leap,问了好多次什么时候修好多语言输出输入,都没有给出具体的日程安排.所以还是自己动手,毕竟是MIT的,不能全靠人家.

目标

  1: 使用UTF8文本配置一个TextLib,格式为Key=TextContent,一行一个

  2: 写一个类,加载和查询文本,从而越过ASCII到UTF8的转换.

  3: 在一个控件上测试通过

代码  

// -------------------------------------------------------
// 文件名: textbin.h
// 描  述: 
// 日  期: 28/3/2013 
// 作  者: KevinYuen
// 版  权: Copyright (C) 2011 - All Rights Reserved
// 备  注: 
// -------------------------------------------------------

#include "sim/simBase.h"
#include <string>
#include <map>

#ifndef _TEXTBIN_H_
#define _TEXTBIN_H_

class TextBin : public SimObject
{
	typedef SimObject Parent;

public:

	DECLARE_CONOBJECT( TextBin );

public:

	TextBin();
	virtual ~TextBin();

public:

	// 设置当前文本库
	bool setTextBin( const UTF8* filename );

	// 获取文本
	const UTF8* getText( const UTF8* id, const UTF8* params );

protected:

	// 添加文本
	void AddCombinText( const U8* text );

protected:

	typedef std::map<std::string,std::string> TextMap;
	TextMap mTextMap;
};

#endif

// -------------------------------------------------------
// 文件名: textbin.cpp
// 描  述: 
// 日  期: 28/3/2013 
// 作  者: KevinYuen
// 版  权: Copyright (C) 2011 - All Rights Reserved
// 备  注: 
// -------------------------------------------------------

#include "platform/platform.h"
#include "io/stream.h"
#include "io/fileStream.h"
#include "io/resource/resourceManager.h"
#include "console/console.h"
#include "console/consoleInternal.h"
#include "console/ast.h"
#include "console/compiler.h"

#include "textbin.h"

IMPLEMENT_CONOBJECT( TextBin );

TextBin::TextBin()
{
}

TextBin::~TextBin()
{
}

// 设置当前文本库
bool TextBin::setTextBin( const UTF8* filename )
{
	// 文件读取
	Stream* pS = ResourceManager->openStream( (const char*)filename );
	if( !pS ) return false;

	// 历史文本清空
	mTextMap.clear();

	// 逐行读取
	while( pS->getStatus() != Stream::EOS )
	{
		static U8 Buffer[512];
		memset( (void*)Buffer, 0, 512 );
		pS->readLine( Buffer, 512 );
		// 解析
		AddCombinText( Buffer );
	}

	return true;
}

// 获取文本
const UTF8* TextBin::getText( const UTF8* id, const UTF8* params )
{
	if( !id || id == '\0' ) return "";

	TextMap::iterator iter = mTextMap.find( id );
	if( iter == mTextMap.end() ) return "";

	// 参数后期增加

	return iter->second.c_str();
}

// 添加文本
void TextBin::AddCombinText( const U8* text )
{
	std::string	CombineStr = text ? (const char*)text : "";
	if( CombineStr.empty() ) return;

	U32 nPos = CombineStr.find( "=" );
	if( nPos == std::string::npos ) return;

	std::string Key = CombineStr.substr( 0, nPos );
	std::string Value = CombineStr.substr( nPos+1, CombineStr.size() - 1 );
	mTextMap[Key] = Value;
}

ConsoleMethod( TextBin, setTextBin, bool, 3, 3, "(string) ")
{
	UTF8 scriptFilenameBuffer[1024];
	Con::expandPath((char*)scriptFilenameBuffer, sizeof(scriptFilenameBuffer), argv[2]);
	return object->setTextBin( scriptFilenameBuffer );
}

ConsoleMethod( TextBin, getText, const char *, 4, 4, "(string) ")
{
	return (const char*)object->getText( argv[2], argv[3] );
	//char *ret;
	//const char *str;

	//if( str = (const char*)object->getText( argv[2], argv[3] ) )
	//{
	//	ret = Con::getReturnBuffer(dStrlen(str) + 1);
	//	dStrcpy(ret, str);
	//	return ret;
	//}

	//return "";
}

文本  

Start=开始
End=结束

 

测试

function LaunchStage::Language( %this )
{
   new TextBin( GameText );
   GameText.setTextBin( "^Game/assets/languages/text_CN.txt" );
}


   %startbtn = new GuiButtonCtrl()
      {
        canSaveDynamicFields = "0";
        HorizSizing = "relative";
        class = "LoginStart";
        VertSizing = "relative";
        isContainer = "0";
        Profile = "BlueButtonProfile";
        Position = "0 0";
        Extent = "300 80";
        Visible = "1";
        isContainer = "0";
        Active = "1";
        groupNum = "-1";
        buttonType = "PushButton";
        useMouseEvents = "0";
        text = GameText.getText( "Start", "" );
      };

 效果

posted on 2013-03-28 22:37  Kevin Game  阅读(929)  评论(0编辑  收藏  举报