Torque2D MIT learning notes (23) -- write your own multilingual text library

Posted by renny on Wed, 30 Oct 2019 20:12:00 +0100

Original link: http://www.cnblogs.com/KevinYuen/archive/2013/03/28/2987795.html

Preface

Torque2D's support for Chinese characters can be said to be very poor at present. Although it supports Freetype, the internal string encoding is UTF series. There is basically no Ascii,Unicode encoding format commonly used in Win32.

If you want your program to support Chinese character input, you need to change many things, but if it's display, it's another matter. You can use the LangTable provided by them to realize it (I have a hard time looking at it, it seems that it doesn't support keyword search, only index sequence, rather than line reading, and I don't know how to operate it. Is there any tool to compile first, anyway, Torque3D has one Compiled function, not in 2D.)

Recently, the authors have been busy with file formats and leaps. They have asked many times when to fix the output and input of many languages, but they haven't given a specific schedule. So they still need to do it by themselves. After all, it's MIT, and they can't rely on others.

target

1: use UTF8 text to configure a TextLib in the format of Key=TextContent, one line at a time.

2: write a class, load and query the text, so as to cross the ASCII to UTF8 conversion.

3: pass the test on a control

Code

// -------------------------------------------------------
// File name: textbin.h
// Description: 
// Date: 28 / 3 / 2013 
// By Kevin Yuen
// Copyright (C) 2011 - All Rights Reserved
// Remarks: 
// -------------------------------------------------------

#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:

	// Set current text library
	bool setTextBin( const UTF8* filename );

	// Get text
	const UTF8* getText( const UTF8* id, const UTF8* params );

protected:

	// Add text
	void AddCombinText( const U8* text );

protected:

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

#endif

// -------------------------------------------------------
// File name: textbin.cpp
// Description: 
// Date: 28 / 3 / 2013 
// By Kevin Yuen
// Copyright (C) 2011 - All Rights Reserved
// Remarks: 
// -------------------------------------------------------

#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()
{
}

// Set current text library
bool TextBin::setTextBin( const UTF8* filename )
{
	// File read
	Stream* pS = ResourceManager->openStream( (const char*)filename );
	if( !pS ) return false;

	// Clear history text
	mTextMap.clear();

	// Read by line
	while( pS->getStatus() != Stream::EOS )
	{
		static U8 Buffer[512];
		memset( (void*)Buffer, 0, 512 );
		pS->readLine( Buffer, 512 );
		// analysis
		AddCombinText( Buffer );
	}

	return true;
}

// Get text
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 "";

	// Parameter added later

	return iter->second.c_str();
}

// Add text
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 "";
}

Text

Start= start
 End= end

 

test

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", "" );
      };

Effect

Reproduced in: https://www.cnblogs.com/kevin yuen/archive/2013/03/28/2987795.html

Topics: encoding ascii