HGE 改进的对话框组件
自定义大小、位置,显示脸部图片,角色姓名,对话信息。
但是对话信息依然不支持自动换行,需要自行输入"\n"。
截图如下:
【显示对话、角色姓名与脸部图片】
【显示对话】
【显示对话与头像】
代码:
【头文件】MessagePanel.h
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/28 // Describe: 对话框组件 ////////////////////////////////////////////////////////////////////////// #ifndef _YOYO_MESSAGE_PANEL_H_ #define _YOYO_MESSAGE_PANEL_H_ #if _MSC_VER > 1000 #pragma once #endif // end of _MSC_VER > 1000 ////////////////////////////////////////////////////////////////////////// #include "../hge/include/hge.h" #include "../hge/include/hgefont.h" #include "../hge/include/hgesprite.h" #include "../hge/include/hgeanim.h" #include "../hge/include/hgegui.h" #include <string> #include <queue> ////////////////////////////////////////////////////////////////////////// // 对话信息结构体 typedef struct { char* m_szName; // 姓名内容 hgeSprite* m_psFace; // 脸部纹理精灵 char* m_szMessage; // 信息内容 }MESSAGE; ////////////////////////////////////////////////////////////////////////// // 对话信息队列 typedef std::queue<MESSAGE> MESSAGE_QUE; ////////////////////////////////////////////////////////////////////////// // 对话框类 class CMessagePanel: public hgeGUIObject { ////////////////////////////////////////////////////////////////////////// // 构造与析构方法 public: CMessagePanel(int nId, int xPos, int yPos, int iWidth, int iHeight); ~CMessagePanel(); ////////////////////////////////////////////////////////////////////////// // 公开方法 public: ////////////////////////////////////////////////////////////////////////// // 逻辑方法与渲染方法 virtual bool MouseLButton(bool bDown); virtual bool KeyClick(int key, int chr); virtual void Render(); ////////////////////////////////////////////////////////////////////////// // 对话框显示控制 void Show(bool fShow = true); ////////////////////////////////////////////////////////////////////////// // 添加要在对话框中显示的信息 void AddMessage(char* szMessage, char* szName = NULL, hgeSprite* psFace = NULL); void AddMessage(char* szMessage, hgeSprite* psFace); ////////////////////////////////////////////////////////////////////////// // 成员变量 private: MESSAGE_QUE m_queMessage; // 信息队列(为空时亦不显示对话框) int m_xPos, m_yPos; int m_iWidth, m_iHeight; HTEXTURE m_tBack; HTEXTURE m_tBorder; HTEXTURE m_tNext; hgeFont* m_pfFontName; hgeFont* m_pfFontMessage; hgeSprite* m_psBack; hgeSprite* m_psBorder[8]; hgeAnimation* m_psNext; int m_iBorderSize[8][4]; }; ////////////////////////////////////////////////////////////////////////// #endif // end of define _YOYO_MESSAGE_PANEL_H_
【源文件】MessagePanel.cpp
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/28 // Describe: 对话框组件 ////////////////////////////////////////////////////////////////////////// #include "MessagePanel.h" #include <iostream> ////////////////////////////////////////////////////////////////////////// // 构造方法:加载资源 CMessagePanel::CMessagePanel(int nID, int xPos, int yPos, int iWidth, int iHeight) { id = nID; bStatic = false; bVisible = false; bEnabled = true; rect.Set(0, 0, hge->System_GetState(HGE_SCREENWIDTH), hge->System_GetState(HGE_SCREENHEIGHT)); this->m_xPos = xPos, this->m_yPos = yPos; this->m_iWidth = iWidth, this->m_iHeight = iHeight; this->m_tBack = hge->Texture_Load("../对话框/res/window/window_bg.png"); this->m_tBorder = hge->Texture_Load("../对话框/res/window/window_border.png"); this->m_tNext = hge->Texture_Load("../对话框/res/window/window_arrow_next.png"); this->m_pfFontName = new hgeFont("../对话框/res/font/Arial_Normal_20.fnt"); this->m_pfFontName->SetColor(0xffffffff); this->m_pfFontMessage = new hgeFont("../对话框/res/font/MS_PGothic_Bold_20.fnt"); this->m_pfFontMessage->SetColor(0xffcecece); this->m_psBack = new hgeSprite(m_tBack, 0, 0, 128, 128); this->m_psNext = new hgeAnimation(m_tNext, 4, 2, 0, 0, 16, 16); this->m_psNext->Play(); m_iBorderSize[0][0] = 0, m_iBorderSize[0][1] = 0; m_iBorderSize[0][2] = 24, m_iBorderSize[0][3] = 24; m_iBorderSize[1][0] = 24, m_iBorderSize[1][1] = 0; m_iBorderSize[1][2] = m_iWidth - m_iBorderSize[0][2]*2/*16*/, m_iBorderSize[1][3] = 12; m_iBorderSize[2][0] = 40, m_iBorderSize[2][1] = 0; m_iBorderSize[2][2] = 24, m_iBorderSize[2][3] = 24; m_iBorderSize[3][0] = 0, m_iBorderSize[3][1] = 24; m_iBorderSize[3][2] = 12, m_iBorderSize[3][3] = m_iHeight - m_iBorderSize[0][3]*2/*16*/; m_iBorderSize[4][0] = 52, m_iBorderSize[4][1] = 24; m_iBorderSize[4][2] = 12, m_iBorderSize[4][3] = m_iHeight - m_iBorderSize[0][3]*2/*16*/; m_iBorderSize[5][0] = 0, m_iBorderSize[5][1] = 40; m_iBorderSize[5][2] = 24, m_iBorderSize[5][3] = 24; m_iBorderSize[6][0] = 24, m_iBorderSize[6][1] = 52; m_iBorderSize[6][2] = m_iWidth - m_iBorderSize[0][2]*2/*16*/, m_iBorderSize[6][3] = 12; m_iBorderSize[7][0] = 40, m_iBorderSize[7][1] = 40; m_iBorderSize[7][2] = 24, m_iBorderSize[7][3] = 24; for (int i = 0; i < 8; i++) { this->m_psBorder[i] = new hgeSprite(m_tBorder, m_iBorderSize[i][0], m_iBorderSize[i][1], m_iBorderSize[i][2], m_iBorderSize[i][3]); } } ////////////////////////////////////////////////////////////////////////// // 析构方法:释放资源 CMessagePanel::~CMessagePanel() { hge->Texture_Free(m_tBack); hge->Texture_Free(m_tBorder); hge->Texture_Free(m_tNext); delete m_pfFontName; delete m_pfFontMessage; delete m_psBack; delete[] m_psBorder; delete m_psNext; } ////////////////////////////////////////////////////////////////////////// // 逻辑方法:监听对话框显示时的鼠标左键/回车/空格状态 bool CMessagePanel::MouseLButton(bool bDown) { if (bDown) { if (this->m_queMessage.size() == 0) { return false; } if (hge->Input_GetKeyState(HGEK_SPACE) || hge->Input_GetKeyState(HGEK_ENTER) || hge->Input_GetKeyState(HGEK_LBUTTON)) { m_queMessage.pop(); if (m_queMessage.size() == 0) { bVisible = false; } } } return false; } bool CMessagePanel::KeyClick(int key, int chr) { switch(key) { case HGEK_ENTER: case HGEK_SPACE: this->MouseLButton(true); break; default: break; } return false; } ////////////////////////////////////////////////////////////////////////// // 渲染方法:显示当前信息 void CMessagePanel::Render() { gui->SetFocus(id); if (this->m_queMessage.size() == 0) { return; } m_psBack->RenderStretch(m_xPos + 2, m_yPos + 2, m_xPos + m_iWidth - 4, m_yPos + m_iHeight - 4); m_psBorder[0]->Render(m_xPos, m_yPos); m_psBorder[1]->Render(m_xPos + m_iBorderSize[0][2], m_yPos); m_psBorder[2]->Render(m_xPos + m_iWidth - m_iBorderSize[2][2], m_yPos); m_psBorder[3]->Render(m_xPos, m_yPos + m_iBorderSize[0][3]); m_psBorder[4]->Render(m_xPos + m_iWidth - m_iBorderSize[4][2], m_yPos + m_iBorderSize[0][3]); m_psBorder[5]->Render(m_xPos, m_yPos + m_iHeight - m_iBorderSize[5][3]); m_psBorder[6]->Render(m_xPos + m_iBorderSize[0][2], m_yPos + m_iHeight - m_iBorderSize[6][3]); m_psBorder[7]->Render(m_xPos + m_iWidth - m_iBorderSize[7][2], m_yPos + m_iHeight - m_iBorderSize[7][3]); int tmp_xPos = m_xPos + 20, tmp_yPos = m_yPos + 20; if (this->m_queMessage.front().m_psFace != NULL) { this->m_queMessage.front().m_psFace->Render(tmp_xPos + 50, tmp_yPos + 50); tmp_xPos += 120; } if (this->m_queMessage.front().m_szName != NULL) { m_pfFontName->printf(tmp_xPos, tmp_yPos, 0, "%s", this->m_queMessage.front().m_szName); tmp_xPos += 20; tmp_yPos += 30; } if (this->m_queMessage.front().m_szMessage != NULL) { m_pfFontMessage->printf(tmp_xPos, tmp_yPos, 0, "%s", this->m_queMessage.front().m_szMessage); } m_psNext->Render((m_xPos + m_iWidth - 4)/2, (m_yPos + m_iHeight - 4) - 32); m_psNext->Update(hge->Timer_GetDelta()); } ////////////////////////////////////////////////////////////////////////// // 控制对话框是否显示 void CMessagePanel::Show(bool fShow) { bVisible = fShow; } ////////////////////////////////////////////////////////////////////////// // 添加信息 void CMessagePanel::AddMessage(char* szMessage, char* szName, hgeSprite* psFace) { MESSAGE message; message.m_szMessage = szMessage; message.m_szName = szName; message.m_psFace = psFace; this->m_queMessage.push(message); } void CMessagePanel::AddMessage(char* szMessage, hgeSprite* psFace) { this->AddMessage(szMessage, NULL, psFace); }
【DEMO】Main.cpp(其中//~~~注释部分为使用对话框的代码)
////////////////////////////////////////////////////////////////////////// // CopyRight(c) 2009, YOYO, All Rights Reserved. // Author: LIN YiQian // Created: 2009/08/28 // Describe: 对话框测试 ////////////////////////////////////////////////////////////////////////// #include "hge/include/hge.h" #include "hge/include/hgegui.h" #include "widget/MessagePanel.h" #pragma comment(lib, "hge/lib/vc/hge.lib") #pragma comment(lib, "hge/lib/vc/hgehelp.lib") ////////////////////////////////////////////////////////////////////////// HGE* pHge; //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pGui->Render(); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pHge->Gfx_EndScene(); return false; } int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { pHge = hgeCreate(HGE_VERSION); pHge->System_SetState(HGE_TITLE, "对话框测试"); pHge->System_SetState(HGE_FPS, 100); pHge->System_SetState(HGE_WINDOWED, true); pHge->System_SetState(HGE_HIDEMOUSE, false); pHge->System_SetState(HGE_SHOWSPLASH, false); pHge->System_SetState(HGE_SCREENBPP, 32); pHge->System_SetState(HGE_SCREENWIDTH, 440); pHge->System_SetState(HGE_SCREENHEIGHT, 280); pHge->System_SetState(HGE_FRAMEFUNC, FrameFunc); pHge->System_SetState(HGE_RENDERFUNC, RenderFunc); if (pHge->System_Initiate()) { HTEXTURE tmp_tFace = pHge->Texture_Load("../对话框组件/res/face/lancer.png"); hgeSprite* tmp_pFace = new hgeSprite(tmp_tFace, 0, 0, 100, 100); tmp_pFace->SetHotSpot(50, 50); HTEXTURE tmp_tFaceBig = pHge->Texture_Load("../对话框组件/res/face/lancer_big.png"); hgeSprite* tmp_pFaceBig = new hgeSprite(tmp_tFaceBig, 0, 0, 200, 312); tmp_pFaceBig->SetHotSpot(100, 156); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pGui = new hgeGUI(); CMessagePanel* pMsgPanel = new CMessagePanel(1989/*MessagePanelID*/, 10, 132, 420, 140); pMsgPanel->AddMessage("test Demo..\ntest test test test..", "YOYO", tmp_pFace); pMsgPanel->AddMessage("GO..", tmp_pFaceBig); pMsgPanel->AddMessage("WHAT DO YOU SEE? \nWHAT DO YOU SEE? WHAT???"); pMsgPanel->AddMessage("AFALSDKFJASLDFKJ..", "YOYO"); pMsgPanel->Show(); pGui->AddCtrl(pMsgPanel); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ pHge->System_Start(); delete tmp_pFace; delete tmp_pFaceBig; pHge->Texture_Free(tmp_tFaceBig); pHge->Texture_Free(tmp_tFace); } pHge->System_Shutdown(); pHge->Release(); return 0; }
使用的素材下载:
【纹理】角色头像(res/face/lancer.png) 大头像(res/face/lancer_big.png)
对话框:边框(res/window/window_border.png) 背景(res/window/window_bg.png) 下一条图标(res/window/window_arrow_next.png)
【字体】姓名字体(res/font/Arial_Normal_20.fnt png) 对话字体(res/font/MS_PGothic_Bold_20.fnt png)