@H_301_266@
protected:@H_301_266@
public:@H_301_266@
static T& GetSingleton()@H_301_266@
static T _singleton;@H_301_266@
return _singleton;@H_301_266@
SINGLETON( GUISystem )@H_301_266@
private:@H_301_266@
typedef std::map<std::string , UIObject*>::iterator MapIter;@H_301_266@
private:@H_301_266@
void LoadAllUI();@H_301_266@
void ReadFromScript(const std::string& id);@H_301_266@
public:@H_301_266@
bool Initialize(LPDIRECT3DDEVICE9 pD3DDevice);@H_301_266@
void LoadCurUI(int sceneId);@H_301_266@
std::set<UIObject*>& GetCurUIList();@H_301_266@
UIObject* GetUIObject(const std::string id);这里需要说明一下,_pGameGUI的作用。CEGUI是以树形结构来管理每个UI部件的,所以在游戏场景中,我们需要这么一个根节点,_pGameGUI就是这个根的指针,也可以理解为顶层容器。如果你对CEGUI::DirectX9Render的使用有疑问,请参考在DirectX 3D中使用CEGUI一文,在此就不再迭述。下面是GUISystem.cpp代码:
@H_301_266@
_pCEGUIRender = new CEGUI::DirectX9Renderer(pD3DDevice , 0);@H_301_266@
new CEGUI::System(_pCEGUIRender);@H_301_266@
CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>@H_301_266@
(CEGUI::System::getSingleton().getResourceProvider());@H_301_266@
rp->setResourceGroupDirectory("schemes", "../datafiles/schemes/");@H_301_266@
rp->setResourceGroupDirectory("imagesets", "../datafiles/imagesets/");@H_301_266@
rp->setResourceGroupDirectory("fonts", "../datafiles/fonts/");@H_301_266@
rp->setResourceGroupDirectory("layouts", "../datafiles/layouts/");@H_301_266@
rp->setResourceGroupDirectory("looknfeels", "../datafiles/looknfeel/");@H_301_266@
CEGUI::Imageset::setDefaultResourceGroup("imagesets");@H_301_266@
CEGUI::Font::setDefaultResourceGroup("fonts");@H_301_266@
CEGUI::Scheme::setDefaultResourceGroup("schemes");@H_301_266@
CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");@H_301_266@
CEGUI::WindowManager::setDefaultResourceGroup("layouts");@H_301_266@
@H_301_266@
CEGUI::Imageset* taharezlookImage;@H_301_266@
taharezlookImage = CEGUI::ImagesetManager::getSingleton().createImageset("Vanilla.imageset");@H_821_1404@
...{@H_301_266@
AfxMessageBox(exc.getMessage().c_str());@H_301_266@
CEGUI::System::getSingleton().setDefaultMouseCursor(&taharezlookImage->getImage("MouseArrow"));@H_301_266@
@H_301_266@
CEGUI::FontManager::getSingleton().createFont("simfang.font");@H_301_266@
@H_691_1502@
@H_301_266@
CEGUI::WidgetLookManager::getSingleton().parseLookNFeelSpecification("Vanilla.looknfeel");@H_301_266@
@H_301_266@
CEGUI::SchemeManager::getSingleton().loadScheme("VanillaSkin.scheme");@H_301_266@
CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();@H_301_266@
@H_301_266@
_pGameGUI = winMgr.createWindow("DefaultWindow", "root_ui");@H_301_266@
@H_301_266@
CEGUI::System::getSingleton().setGUISheet(_pGameGUI);@H_301_266@
@H_301_266@
LoadAllUI();@H_301_266@
@H_301_266@
return true;@H_301_266@
UIObject* pUIObject = new ChatUI;@H_301_266@
_UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));@H_301_266@
pUIObject = new systemUI;@H_301_266@
_UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));@H_301_266@
pUIObject = new SmallMapUI;@H_301_266@
_UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));@H_301_266@
typedef std::set<UIObject*>::iterator Iter;@H_301_266@
std::set< UIObject* >::iterator iter = _curUIList.begin();@H_301_266@
for( ; iter != _curUIList.end() ; ++iter)@H_301_266@
_pGameGUI->removeChildWindow((*iter)->GetWnd());@H_301_266@
std::ostringstream sid;@H_301_266@
sid << "sui" << sceneId;@H_301_266@
ReadFromScript(sid.str());@H_301_266@
for(iter = _curUIList.begin() ; iter != _curUIList.end() ; ++iter)@H_301_266@
_pGameGUI->addChildWindow((*iter)->InitUI());@H_301_266@
LuaScriptSystem::GetSingleton().LoadScript("./script/sui.lua");@H_301_266@
const char* pStr = NULL;@H_301_266@
int i = 1;@H_301_266@
pStr = LuaScriptSystem::GetSingleton().GetValue(id.c_str() , i++);@H_301_266@
while(pStr)@H_301_266@
_curUIList.insert(_UIMap[pStr]);@H_301_266@
pStr = LuaScriptSystem::GetSingleton().GetValue("sui1" , i++);@H_301_266@
@H_301_266@
return _curUIList;@H_301_266@
MapIter iter = _UIMap.find(id);@H_301_266@
if(iter != _UIMap.end())@H_301_266@
return iter->second;@H_301_266@
else@H_301_266@
return NULL;其中,GUISystem::ReadFromScript作用是从Lua脚本中读取当前场景对应的UI组件名。之所以采用Lua作为数据脚本,是因为其自身就为实现数据脚本提供了很好的支持,需要编写的解析代码与采用xml、ini相比会少很多。本例利用了Lua中的数组来存储UI组建名,是Lua作为数据脚本一个不错的示例:
下面是Lua脚本解析类,也是一个Singleton:
@H_301_266@
SINGLETON2(LuaScriptSystem)@H_301_266@
private:@H_301_266@
LuaScriptSystem();@H_301_266@
~LuaScriptSystem();@H_301_266@
public:@H_301_266@
bool LoadScript(char* filename);@H_301_266@
const char* GetValue(const char* id , int index);@H_301_266@
private:
@H_301_266@
_pLuaVM = lua_open();@H_301_266@
if(luaL_dofile(_pLuaVM , filename))@H_301_266@
return false;@H_301_266@
return true;@H_301_266@
const char* pstr = NULL;@H_301_266@
lua_rawgeti(_pLuaVM , -1 , index);@H_301_266@
if(lua_isstring(_pLuaVM , -1))@H_301_266@
pstr = lua_tostring(_pLuaVM , -1);@H_301_266@
@H_301_266@
lua_pop(_pLuaVM , 2);@H_301_266@
@H_301_266@
return pstr;@H_301_266@
lua_close(_pLuaVM);Lua与外界的交流需要依靠解释器维护的栈来实现,这一点对于使用Lua的开发者应该铭记于心。在GetValue中,利用lua_getglobal来得到lua脚本中全局变量,如"sui1",此时,栈顶(用索引-1来表示)就应该保存着该全局变量。利用lua_rawgeti传入数组位于栈的索引(-1),以及数组索引(index从1开始),就能够得到对应索引的值,结果自然也是放在栈中,想想push一下,现在栈顶应该保存着结果了,最后用lua_tostring来得到。
在这个示例中,我们引入了三个UI组件,分别是ChatUI、SmallMapUI和systemUI,对应聊天框、小地图、系统按钮条。为了演示它们之间的交互,我们规定ChatUI受systemUI中Chat按钮的影响,可以让其显示或者隐藏,同时,SmallMapUI能够接受鼠标点击,并在ChatUI的文本框中显示一些点击信息。当然,这三个UI组件还必须对应着CEGUI的三个layout脚本文件。下面是它们的实现代码:
@H_729_3011@
@H_644_3014@
@H_76_3016@/**/ @H_110_3018@/// UIObject.h@H_301_266@
protected:@H_301_266@
std::string _id;@H_301_266@
CEGUI::Window* _pWnd;@H_301_266@
public:@H_502_3164@
const std::string& GetID() const ...{return _id;}@H_301_266@
virtual CEGUI::Window* InitUI() = 0;@H_301_266@
public:@H_301_266@
ChatUI(void);@H_301_266@
~ChatUI(void);@H_301_266@
CEGUI::Window* InitUI();@H_301_266@
_id = "ChatUI";@H_301_266@
@H_301_266@
if( NULL == _pWnd)@H_301_266@
_pWnd = WindowManager::getSingleton().loadWindowLayout("ChatUI.layout");@H_301_266@
_pWnd->hide();@H_301_266@
return _pWnd;@H_301_266@
public:@H_301_266@
SmallMapUI(void);@H_301_266@
~SmallMapUI(void);@H_301_266@
CEGUI::Window* InitUI();@H_301_266@
bool Click(const CEGUI::EventArgs& e);@H_301_266@
_id = "SmallMapUI";@H_301_266@
@H_301_266@
if( NULL == _pWnd )@H_301_266@
_pWnd = WindowManager::getSingleton().loadWindowLayout("SmallMapUI.layout");@H_301_266@
ImagesetManager::getSingleton().createImagesetFromImageFile("SmallMap", "ZoneMap.jpg");@H_301_266@
_pWnd->getChild("SmallMapUI/StaticImage")->setProperty("Image", "set:SmallMap image:full_image");@H_301_266@
_pWnd->getChild("SmallMapUI/StaticImage")->subscribeEvent(@H_301_266@
CEGUI::Window::EventMouseButtonDown,@H_301_266@
CEGUI::Event::Subscriber(&SmallMapUI::Click , this));@H_301_266@
@H_301_266@
return _pWnd;@H_301_266@
char text[100];@H_301_266@
sprintf(text , "你点击了地图,坐标为(%.1f , %.1f)" , static_cast<const MouseEventArgs&>(e).position.d_x , static_cast<const MouseEventArgs&>(e).position.d_x);@H_301_266@
WindowManager::getSingleton().getWindow("ChatUI/MsgBox")->setText((utf8*)text);@H_301_266@
@H_301_266@
return true;@H_301_266@
public:@H_301_266@
systemUI(void);@H_301_266@
~systemUI(void);@H_301_266@
CEGUI::Window* InitUI();@H_301_266@
bool systemUI::OnChatBtn(const CEGUI::EventArgs& e);@H_301_266@
bool systemUI::OnExitBtn(const CEGUI::EventArgs& e);@H_301_266@
_id = "systemUI";@H_301_266@
@H_301_266@
if( NULL == _pWnd)@H_301_266@
_pWnd = CEGUI::WindowManager::getSingleton().loadWindowLayout("systemUI.layout");@H_301_266@
_pWnd->getChild("systemUI/ChatBtn")->subscribeEvent(@H_301_266@
CEGUI::Window::EventMouseButtonDown,@H_301_266@
CEGUI::Event::Subscriber(&systemUI::OnChatBtn , this));@H_301_266@
_pWnd->getChild("systemUI/ExitBtn")->subscribeEvent(@H_301_266@
CEGUI::Window::EventMouseButtonDown,@H_301_266@
CEGUI::Event::Subscriber(&systemUI::OnExitBtn , this));@H_301_266@
return _pWnd;@H_301_266@
UIObject* pUIObj = GUISystem::GetSingleton().GetUIObject("ChatUI");@H_301_266@
if(!pUIObj)@H_301_266@
return false;@H_301_266@
CEGUI::Window* pWnd = pUIObj->GetWnd();@H_301_266@
if(pWnd)@H_301_266@
pWnd->isVisible() ? pWnd->hide() : pWnd->show();@H_301_266@
@H_301_266@
return true;@H_301_266@
::exit(0);@H_301_266@
@H_301_266@
return true;在使用CEGUILayoutEditor创建layout脚本时,你不能创建一个满屏的DefaultWindow,那样会让造成不能相应其他窗口的问题。但通常Editor会为我们默认创建它,这不要紧,你只需要在保存的layout文件中删除那个顶层的满屏window就可以了。
下面是程序的运行结果:
@H_763_5028@