HGE 简单对话框组件
采用直接贴图的方式,因此固定了图片和大小。
带姓名框的对话框…… = = 待完善
图片、字体路径需要自己修改。
创建方法是CDialog* pDialog = new CDialog(id, messages); 其中id是控件ID(不要重复),messages是queue< pair<string, string> >。第一项为姓名,第二项为文字。然后交给gui去处理。详细看demo、、、
头文件:
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/27 // Describe: 对话框组件 ////////////////////////////////////////////////////////////////////////// #ifndef _YOYO_DIALOG_H_ #define _YOYO_DIALOG_H_ #if _MSC_VER > 1000 #pragma once #endif #include "../hge/include/hge.h" #include "../hge/include/hgefont.h" #include "../hge/include/hgesprite.h" #include "../hge/include/hgegui.h" #include <queue> #include <string> typedef std::pair<std::string, std::string> MESSAGE; typedef std::queue<MESSAGE> MESSAGE_QUEUE; class CDialog: public hgeGUIObject { public: CDialog(int nID, MESSAGE_QUEUE messages); ~CDialog(void); public: // 设置信息 void SetMessage(MESSAGE_QUEUE messages); virtual bool MouseLButton(bool bDown); virtual bool KeyClick(int key, int chr); virtual void Render(); // 对话是否完成 bool IsReadFinish(); void SetNameField(int x, int y); // 设置姓名框显示位置 void SetNameFieldText(int x, int y); // 设置姓名框文字显示位置 void SetMessageArea(int x, int y); // 设置对话框显示位置 void SetMessageAreaText(int x, int y); // 设置对话框文字显示位置 private: bool m_bReadNext; bool m_bReadFinish; MESSAGE_QUEUE m_qsMessages; hgeFont* m_pfName; hgeFont* m_pfMessage; HTEXTURE m_textureNameBack; HTEXTURE m_textureNameFront; HTEXTURE m_textureTextBack; HTEXTURE m_textureTextFront; hgeSprite* m_pSpriteNameBack; hgeSprite* m_pSpriteNameFront; hgeSprite* m_pSpriteTextBack; hgeSprite* m_pSpriteTextFront; int m_nNameX, m_nNameY; int m_nNameXText, m_nNameYText; int m_nTextX, m_nTextY; int m_nTextXText, m_nTextYText; }; #endif // end of define _YOYO_DIALOG_H_
源文件:
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/27 // Describe: 对话框组件 ////////////////////////////////////////////////////////////////////////// #include "Dialog.h" CDialog::CDialog(int nID, MESSAGE_QUEUE messages) { id = nID; bStatic = false; bVisible = true; bEnabled = true; rect.Set(0, 0, 640, 480); this->m_bReadNext = false; this->m_textureNameBack = hge->Texture_Load("../对话框/res/image/dialog/160x42_back.png"); this->m_textureNameFront = hge->Texture_Load("../对话框/res/image/dialog/160x42_front.png"); this->m_textureTextBack = hge->Texture_Load("../对话框/res/image/dialog/640x140_back.png"); this->m_textureTextFront = hge->Texture_Load("../对话框/res/image/dialog/640x140_front.png"); this->m_pSpriteNameBack = new hgeSprite(m_textureNameBack, 1, 0, 158, 42); this->m_pSpriteNameFront = new hgeSprite(m_textureNameFront, 1, 0, 158, 42); this->m_pSpriteTextBack = new hgeSprite(m_textureTextBack, 1, 0, 640, 141); this->m_pSpriteTextFront = new hgeSprite(m_textureTextFront, 1, 0, 640, 141); this->m_pfName = new hgeFont("../对话框/res/font/Arial_Normal_20.fnt"); this->m_pfName->SetColor(0xffffffff); this->m_pfMessage = new hgeFont("../对话框/res/font/MS_PGothic_Bold_20.fnt"); this->m_pfMessage->SetColor(0xffffffff); this->m_nNameX = 16; this->m_nNameY = 296; this->m_nTextX = 0; this->m_nTextY = 340; this->m_nNameXText = 32; this->m_nNameYText = 305; this->m_nTextXText = 20; this->m_nTextYText = 365; this->SetMessage(messages); } CDialog::~CDialog() { delete m_pfName; delete m_pfMessage; delete m_pSpriteNameBack; delete m_pSpriteNameFront; delete m_pSpriteTextBack; delete m_pSpriteTextFront; hge->Texture_Free(m_textureNameBack); hge->Texture_Free(m_textureNameFront); hge->Texture_Free(m_textureTextBack); hge->Texture_Free(m_textureTextFront); } void CDialog::Render() { gui->SetFocus(id); if (m_qsMessages.size() == 0) { m_bReadFinish = true; } if (m_qsMessages.size() > 0) { m_pSpriteNameBack->Render(m_nNameX, m_nNameY); m_pSpriteNameFront->Render(m_nNameX, m_nNameY); m_pSpriteTextBack->Render(m_nTextX, m_nTextY); m_pSpriteTextFront->Render(m_nTextX, m_nTextY); m_pfName->printf(m_nNameXText, m_nNameYText, 0, "%s", m_qsMessages.front().first.data()); m_pfMessage->printf(m_nTextXText, m_nTextYText, 0, "%s", m_qsMessages.front().second.data()); if (m_bReadNext) { m_qsMessages.pop(); m_bReadNext = false; } } } bool CDialog::MouseLButton(bool bDown) { // 如果是按任意键 则使用 m_pHge->Input_GetKey() != NULL if (bDown) { m_bReadNext = true; } else { m_bReadNext = false; } return false; } bool CDialog::KeyClick(int key, int chr) { switch(key) { case HGEK_ENTER: case HGEK_SPACE: return MouseLButton(true); break; default: break; } return false; } void CDialog::SetMessage(MESSAGE_QUEUE messages) { this->m_qsMessages = messages; this->m_bReadFinish = false; } bool CDialog::IsReadFinish() { return m_bReadFinish; } void CDialog::SetNameField(int x, int y) { this->m_nNameX = x; this->m_nNameY = y; } void CDialog::SetNameFieldText(int x, int y) { this->m_nNameXText = x; this->m_nNameYText = y; } void CDialog::SetMessageArea(int x, int y) { this->m_nTextX = x; this->m_nTextY = y; } void CDialog::SetMessageAreaText(int x, int y) { this->m_nTextXText = x; this->m_nTextYText = y; }
示例Main文件(其中//~~包括部分是使用对话框的代码)
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/27 // Describe: HGE 对话框显示DEMO ////////////////////////////////////////////////////////////////////////// #include "hge/include/hge.h" #include "hge/include/hgesprite.h" #include "hge/include/hgegui.h" //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #include "widget/Dialog.h" //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #pragma comment(lib, "hge/lib/vc/hge.lib") #pragma comment(lib, "hge/lib/vc/hgehelp.lib") ////////////////////////////////////////////////////////////////////////// HGE* pHge; HTEXTURE textureBack; HTEXTURE textureCursor; hgeSprite* pSpriteBack; hgeSprite* pSpriteCursor; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ hgeGUI* pGui; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ////////////////////////////////////////////////////////////////////////// bool FrameFunc() { if (pHge->Input_GetKeyState(HGEK_ESCAPE)) { return true; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pGui->Update(pHge->Timer_GetDelta()); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ return false; } bool RenderFunc() { pHge->Gfx_BeginScene(); pHge->Gfx_Clear(0); pSpriteBack->Render(0, 0); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pGui->Render(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pHge->Gfx_EndScene(); return false; } ////////////////////////////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { pHge = hgeCreate(HGE_VERSION); pHge->System_SetState(HGE_TITLE, "对话框DEMO"); pHge->System_SetState(HGE_WINDOWED, true); pHge->System_SetState(HGE_SHOWSPLASH, false); pHge->System_SetState(HGE_SCREENBPP, 32); pHge->System_SetState(HGE_FPS, 100); pHge->System_SetState(HGE_SCREENWIDTH, 640); pHge->System_SetState(HGE_SCREENHEIGHT, 480); pHge->System_SetState(HGE_FRAMEFUNC, FrameFunc); pHge->System_SetState(HGE_RENDERFUNC, RenderFunc); if (pHge->System_Initiate()) { textureBack = pHge->Texture_Load("../对话框/res/image/bg/back.jpg"); pSpriteBack = new hgeSprite(textureBack, 0, 0, 640, 480); textureCursor = pHge->Texture_Load("../对话框/res/image/cursor/cursor.png"); pSpriteCursor = new hgeSprite(textureCursor, 0, 0, 32, 32); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pGui = new hgeGUI; pGui->SetCursor(pSpriteCursor); MESSAGE_QUEUE messages; // 创建对话框信息 messages.push(MESSAGE_QUEUE::value_type("YOYO", "Hello World!")); messages.push(MESSAGE_QUEUE::value_type("Lin", "?")); messages.push(MESSAGE_QUEUE::value_type("YOYO", "This is a test.")); messages.push(MESSAGE_QUEUE::value_type("Lin", "er...")); // 不会自动换行,需要手工调整 messages.push(MESSAGE_QUEUE::value_type("YOYO", "go\ngo\ngo")); CDialog* pDialog = new CDialog(999, messages); // 创建对话框 pGui->AddCtrl(pDialog); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pHge->System_Start(); delete pGui; delete pSpriteBack; delete pSpriteCursor; pHge->Texture_Free(textureCursor); pHge->Texture_Free(textureBack); } pHge->System_Shutdown(); pHge->Release(); return 0; }
本例使用的资源下载:(均来自网上收集 = =)
纹理(res/image/bg/back.jpg 背景),(res/image/cursor/cursor.png 鼠标指针),
(res/image/dialog/160x42_back.png 160x42_front.png 姓名框),(res/image/dialog/640x140_back.png 640x140_front.png 对话框)
字体(res/font/Arial_Normal_20.fnt png 姓名字体),(res/font/MS_PGothic_Bold_20.fnt png 对话字体)
纹理(res/image/bg/back.jpg 背景),(res/image/cursor/cursor.png 鼠标指针),
(res/image/dialog/160x42_back.png 160x42_front.png 姓名框),(res/image/dialog/640x140_back.png 640x140_front.png 对话框)
字体(res/font/Arial_Normal_20.fnt png 姓名字体),(res/font/MS_PGothic_Bold_20.fnt png 对话字体)