阅读此文章前请先确认你已经阅读以下文章:
让HGE支持中文(1) - HGE中文显示 (必需)
让HGE支持中文(2) - HGE中文输入 (必需)
让HGE支持中文(3) - HGE字符串处理 (可选)
制作要点:遵循 hgeGUI 和 hgeGUIObject 类原则,即所有控件需从 hgeGUIObject 派生。
步骤:
1. 重载 hgeGUIObject 的相关函数,具体文件尾代码。
2. 在每一帧结束时通过 hge->Input_GetIMEEvent(&imeEvent) 进行中文和英文输入获取。
3. 取得中文和英文后添加到字符串列表中。
4. 绘制字符串列表。
5. 重复2-5步骤。以此循环。
代码:
1. 头文件
#pragma once
#include "helpers/hgegui.h"
#include "helpers/hgevector.h"
#include "helpers/hgerect.h"
#include "helpers/hgefontcn.h"
#include
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
class tEditBox : public hgeGUIObject
{
public:
tEditBox(HTEXTURE tex, int id,
float tx, float ty, float tw, float th,
hgeFontCN* font, int len = 0x10, float dstX = 0, float dstY = 0);
virtual void Update(float dt);
virtual void Render();
virtual void Leave();
virtual void MouseOver(bool bOver);
virtual void Focus(bool bFocused){ tudb_focus = bFocused, tudb_cursor = bFocused;}
virtual bool KeyClick(int key, int chr);
virtual bool MouseMove(float x, float y) { return true; }
CString GetString(){ return tudb_str; }
void SetString(CString str){ tudb_str = str; tudb_pos = tudb_str.GetLength(); reflength(); }
void Clear(){ SetString(""); }
void SetExpt(float x, float y){ tudb_ex = x, tudb_ey = y; }
private:
bool AddChar(char chr[3], bool isIME);
bool DelChar(bool front);
void reflength();
protected:
DWORD tudb_color;
bool tudb_focus, tudb_cursor;
int tudb_pos, tudb_maxlen;
float tudb_ex, tudb_ey, tudb_plen;
hgeFontCN* tudb_font;
hgeSprite* tudb_spr;
CString tudb_str;
};
2. 源文件
#include "hgeedit.h"
#include
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
tEditBox::tEditBox(HTEXTURE tex, int id, float tx, float ty, float tw, float th, hgeFontCN* font, int len, float dstX, float dstY)
:hgeGUIObject(id)
{
tudb_color = 0xFFFFFFFF;
tudb_focus = false;
tudb_cursor = false;
tudb_ex = tudb_ey = 2;
tudb_pos = 0;
tudb_spr = 0;
tudb_maxlen = len;
tudb_plen = tudb_ex + 1;
tudb_font = font;
tudb_spr = new hgeSprite(tex, tx, ty, tw, th);
tguo_rect.Set(dstX, dstY, tw + dstX, th + dstY);
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
void tEditBox::Update(float dt)
{
if (!tudb_focus)
return;
static float lstdt = 0;
lstdt += dt;
if (lstdt > 0.4f)
{
tudb_cursor = !tudb_cursor;
lstdt = 0;
}
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
void tEditBox::Render()
{
tudb_spr->Render(tguo_rect.x1, tguo_rect.y1);
tudb_font->SetColor(tudb_color);
tudb_font->Render(tguo_rect.x1 + tudb_ex, tguo_rect.y1 + tudb_ey, tudb_str);
if (!tudb_focus)
return;
if (tudb_cursor)
hge->Gfx_RenderLine(tguo_rect.x1 + tudb_plen,
tguo_rect.y1 + 2,
tguo_rect.x1 + tudb_plen,
tguo_rect.y2 - 2,
tudb_color);
hgeIMEEvent imeEvent;
while (hge->Input_GetIMEEvent(&imeEvent))
AddChar(imeEvent.value, imeEvent.isIME);
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
void tEditBox::Leave()
{
SAFE_DELETE(tudb_spr);
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
void tEditBox::MouseOver(bool bOver)
{
//hge->OnMouseOver(bOver, tguo_id);
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
bool tEditBox::KeyClick(int key, int chr)
{
if (!tudb_focus)
return false;
switch (key)
{
case HGEK_LEFT:
tudb_cursor = true;
if (tudb_pos > 1)
tudb_pos -= (tudb_str[tudb_pos - 1] < 0) ? 2 : 1;
else
tudb_pos = 0;
reflength();
break;
case HGEK_RIGHT:
tudb_cursor = true;
if (tudb_pos < tudb_str.GetLength())
tudb_pos += (tudb_str[tudb_pos] < 0) ? 2 : 1;
else
tudb_pos = tudb_str.GetLength();
reflength();
break;
case HGEK_BACKSPACE:
DelChar(true);
break;
case HGEK_DELETE:
DelChar(false);
break;
case HGEK_HOME:
tudb_pos = 0;
reflength();
break;
case HGEK_END:
tudb_pos = tudb_str.GetLength();
reflength();
break;
};
//hge->OnKey(key, chr, tguo_id);
return true;
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
bool tEditBox::AddChar(char chr[3], bool isIME)
{
if (tudb_str.GetLength() >= tudb_maxlen)
return false;
CString _str = tudb_str;
if (tudb_pos == 0)
{
tudb_str = chr + _str;
}
else if (tudb_pos == _str.GetLength())
{
tudb_str += chr;
}
else
{
tudb_str = _str.Mid(0, tudb_pos);
tudb_str += chr + _str.Mid(tudb_pos, _str.GetLength() - tudb_pos);
}
tudb_pos += isIME ? 2 : 1;
reflength();
return true;
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
bool tEditBox::DelChar(bool front)
{
if (tudb_str.GetLength() <= 0)
return false;
CString _str = tudb_str;
int _len = tudb_str.GetLength();
if (front)
{
if (tudb_pos <= 0)
return false;
int exp = _str[tudb_pos - 1] < 0 ? 2 : 1;
if (tudb_pos == exp)
{
tudb_str.Delete(0, exp);
}
else if (tudb_pos == _len)
{
tudb_str.Delete(_len - exp);
}
else
{
tudb_str = _str.Mid(0, tudb_pos - exp);
tudb_str += _str.Mid(tudb_pos, _len - tudb_pos);
}
tudb_pos -= exp;
if (tudb_pos < 0)
tudb_pos = 0;
reflength();
}
else
{
if (tudb_pos >= _len)
return false;
int exp = _str[tudb_pos] < 0 ? 2 : 1;
if (tudb_pos == 0)
{
tudb_str.Delete(0, exp);
}
else if (tudb_pos == _len - exp)
{
tudb_str.Delete(_len - exp);
}
else
{
tudb_str = _str.Mid(0, tudb_pos);
tudb_str += _str.Mid(tudb_pos + exp, _len - tudb_pos - exp);
}
}
return true;
}
//-------------------------------------------------------------
// 描述 :
//-------------------------------------------------------------
void tEditBox::reflength()
{
tudb_plen = tudb_ex + 1;
if (!tudb_str.IsEmpty())
{
CString _str = tudb_str.Mid(0, tudb_pos);
tudb_plen += tudb_font->GetStringWidth(_str);
}
}
此时可能你也会编不过。那是因为我已经将 hgeGUI 的文件中 hgeGUIObject 成员变量的名字改变过。
这里我也将 hgeGUI 的变量名发上来:
class hgeGUIObject
{
public:
hgeGUIObject(int id)
:tguo_id(id)
{
hge=hgeCreate(HGE_VERSION);
tguo_static = false;
tguo_enable = true;
tguo_visible = true;
}
virtual~tGUIObject() { hge->Release(); }
virtual void Render() = 0;
virtual void Update(float dt) {}
virtual void Enter() {}
virtual void Leave() {}
virtual void Reset() {}
virtual bool IsDone() { return true; }
virtual void Focus(bool bFocused) {}
virtual void MouseOver(bool bOver) {}
virtual bool MouseMove(float x, float y) { return false; }
virtual bool MouseLButton(bool bDown) { return false; }
virtual bool MouseRButton(bool bDown) { return false; }
virtual bool MouseWheel(int nNotches) { return false; }
virtual bool KeyClick(int key, int chr) { return false; }
const int tguo_id;
bool tguo_static;
bool tguo_enable;
bool tguo_visible;
hgeRect tguo_rect;
hgeGUI* tguo_gui;
hgeGUIObject* tguo_next;
hgeGUIObject* tguo_prev;
protected:
HGE* hge;
};
如果还有什么问题可以来问我,我QQ:29774874。