利用CEGUI和Lua實現架構

來源:互聯網
上載者:User

在上一篇文章中,介紹了一種基於組件方式的遊戲UI架構設計方案,在這裡,筆者將介紹如何利用CEGUI和Lua來實現這種靈活的架構。
       CEGUI是一個相容OpenGL、DirectX的優秀開源GUI庫,關於她的介紹以及如何在Direct3D中使用她,可以參考http://blog.csdn.net/Lodger007/archive/2007/07/02/1675141.aspx一文。Lua是一種強大的指令碼語言,她使用棧作為資料介面,能夠很容易地與其它語言互動,關於她的介紹可以參考http://www.lua.org/,以及筆者以前翻譯的三篇系列文章:Lua入門(http://blog.csdn.net/Lodger007/archive/2006/06/26/836466.aspx)、調用Lua函數(http://blog.csdn.net/Lodger007/archive/2006/06/26/836897.aspx)、在Lua中調用C++函數(http://blog.csdn.net/Lodger007/archive/2006/06/26/837349.aspx)。
       在實現中,作為UI組件管理器的GUISystem是一個單件,這樣,你能夠很方便地在任何地方使用其全域唯一的對象。下面是Singleton和GUISystem的實現代碼:

/**//// Singleton.h

#pragma once

#define SINGLETON(class_name)
    friend class Singleton< class_name >;
    private:   
    class_name() ...{}   
    ~class_name() ...{}
    class_name(const class_name&);
    class_name& operator = (const class_name&);

#define SINGLETON2(class_name)
    friend class Singleton< class_name >;
    private:   
    class_name(const class_name&);
    class_name& operator = (const class_name&);

template< typename T > class Singleton
...{
protected:
    Singleton() ...{}
    virtual ~Singleton() ...{}
    Singleton(const Singleton< T >&) ...{}
    Singleton< T >& operator = (const Singleton< T >&) ...{}
public:
    static T& GetSingleton()
    ...{
        static T _singleton;
        return _singleton;
    }
};

/**//// GUISystem

#pragma once
#include "Singleton.h"
#include "UIObject.h"
#include <CEGUI.h>
#include <RendererModules/directx9GUIRenderer/d3d9renderer.h>
#include <map>
#include <set>

class GUISystem : public Singleton< GUISystem >
...{
SINGLETON( GUISystem )
private:
    std::map<std::string , UIObject*> _UIMap;        /**//// 遊戲中需要用到的所有UI對象
    typedef std::map<std::string , UIObject*>::iterator MapIter;
    std::set<UIObject*> _curUIList;            /**//// 當前情境中使用的UI對象列表
    CEGUI::DirectX9Renderer* _pCEGUIRender;        /**//// CEGUI Render
    CEGUI::Window* _pGameGUI;            /**//// 頂層UI
private:
    /**//** 載入所有UI對象 */
    void LoadAllUI();
    /**//** 從指令碼中讀入情境UI */
    void ReadFromScript(const std::string& id);
public:
    /**//** 初始化GUI系統 **/
    bool Initialize(LPDIRECT3DDEVICE9 pD3DDevice);
    /**//** 得到當前需要的UI對象 */
    void LoadCurUI(int sceneId);
    /**//** 得到當前情境所需的UI對象 */
    std::set<UIObject*>& GetCurUIList();
    /**//** 得到UI對象 */
    UIObject* GetUIObject(const std::string id);
};

        這裡需要說明一下,_pGameGUI的作用。CEGUI是以樹形結構來管理每個UI組件的,所以在遊戲情境中,我們需要這麼一個根節點,_pGameGUI就是這個根的指標,也可以理解為頂層容器。如果你對CEGUI::DirectX9Render的使用有疑問,請參考在DirectX 3D中使用CEGUI一文,在此就不再迭述。下面是GUISystem.cpp代碼:

#include "GUISystem.h"
#include "ChatUI.h"
#include "SystemUI.h"
#include "SmallMapUI.h"
#include <CEGUIDefaultResourceProvider.h>
#include "LuaScriptSystem.h"

bool GUISystem::Initialize(LPDIRECT3DDEVICE9 pD3DDevice)
...{
    _pCEGUIRender = new CEGUI::DirectX9Renderer(pD3DDevice , 0);   
    new CEGUI::System(_pCEGUIRender);
    /**//// 初始化GUI資源的預設路徑
    CEGUI::DefaultResourceProvider* rp = static_cast<CEGUI::DefaultResourceProvider*>
        (CEGUI::System::getSingleton().getResourceProvider());
    rp->setResourceGroupDirectory("schemes", "../datafiles/schemes/");
    rp->setResourceGroupDirectory("imagesets", "../datafiles/imagesets/");
    rp->setResourceGroupDirectory("fonts", "../datafiles/fonts/");
    rp->setResourceGroupDirectory("layouts", "../datafiles/layouts/");
    rp->setResourceGroupDirectory("looknfeels", "../datafiles/looknfeel/");
    /**//// 設定使用的預設資源
    CEGUI::Imageset::setDefaultResourceGroup("imagesets");
    CEGUI::Font::setDefaultResourceGroup("fonts");
    CEGUI::Scheme::setDefaultResourceGroup("schemes");
    CEGUI::WidgetLookManager::setDefaultResourceGroup("looknfeels");
    CEGUI::WindowManager::setDefaultResourceGroup("layouts");
    /**//// 設定GUI

    /// 得到GUI樣式的圖片集
    CEGUI::Imageset* taharezlookImage;
    try...{
        taharezlookImage = CEGUI::ImagesetManager::getSingleton().createImageset("Vanilla.imageset");
    }catch (CEGUI::Exception& exc)
    ...{
        AfxMessageBox(exc.getMessage().c_str());
    }
    /**//// 設定滑鼠圖示
    CEGUI::System::getSingleton().setDefaultMouseCursor(&taharezlookImage->getImage("MouseArrow"));

    /**//// 設定字型
    CEGUI::FontManager::getSingleton().createFont("simfang.font");

    /**//// 設定GUI皮膚
    CEGUI::WidgetLookManager::getSingleton().parseLookNFeelSpecification("Vanilla.looknfeel");

    /**//// 載入GUI規劃
    CEGUI::SchemeManager::getSingleton().loadScheme("VanillaSkin.scheme");
    /**//// 得到視窗管理單件
    CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();

    /**//// 建立頂層UI
    _pGameGUI = winMgr.createWindow("DefaultWindow", "root_ui");

    /**//// 設定GUI的Sheet(Sheet是CEGUI中視窗的容器)
    CEGUI::System::getSingleton().setGUISheet(_pGameGUI);
   
    /**//// 從GUISystem中載入所有情境UI
    LoadAllUI();

    return true;
}

void GUISystem::LoadAllUI()
...{
    /**//// 產生所有的UI對象,並放入映射表中
    UIObject* pUIObject = new ChatUI;
    _UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));
    pUIObject = new SystemUI;
    _UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));
    pUIObject = new SmallMapUI;
    _UIMap.insert(make_pair(pUIObject->GetID() , pUIObject));
}

void GUISystem::LoadCurUI(int sceneId)
...{
    /**//// 從頂層UI中移除所有UI 先清空當前UI列表
    typedef std::set<UIObject*>::iterator Iter;
    std::set< UIObject* >::iterator iter = _curUIList.begin();
    for( ; iter != _curUIList.end() ; ++iter)
        _pGameGUI->removeChildWindow((*iter)->GetWnd());
    /**//// 從指令碼中載入情境UI資料
    std::ostringstream sid;
    sid << "sui" << sceneId;
    ReadFromScript(sid.str());
    /**//// 加入情境UI
    for(iter = _curUIList.begin() ; iter != _curUIList.end() ; ++iter)
        _pGameGUI->addChildWindow((*iter)->InitUI());
}

void GUISystem::ReadFromScript(const std::string& id)
...{
    /**//// 從Lua指令碼中載入當前情境需要的UI,存入_curUIList中
    LuaScriptSystem::GetSingleton().LoadScript("./script/sui.lua");
    const char* pStr = NULL;
    int i = 1;
    pStr = LuaScriptSystem::GetSingleton().GetValue(id.c_str() , i++);
    while(pStr)
    ...{
        _curUIList.insert(_UIMap[pStr]);
        pStr = LuaScriptSystem::GetSingleton().GetValue("sui1" , i++);
    }

}

std::set<UIObject*>& GUISystem::GetCurUIList()
...{
    return _curUIList;
}

UIObject* GUISystem::GetUIObject(const std::string id)
...{
    MapIter iter = _UIMap.find(id);
    if(iter != _UIMap.end())
        return iter->second;
    else
        return NULL;
}
        其中,GUISystem::ReadFromScript作用是從Lua指令碼中讀取當前情境對應的UI組件名。之所以採用Lua作為資料指令碼,是因為其自身就為實現資料指令碼提供了很好的支援,需要編寫的解析代碼與採用xml、ini相比會少很多。本例利用了Lua中的數組來儲存UI組建名,是Lua作為資料指令碼一個不錯的樣本:

-- Scene GUI
sui1 = {"SystemUI","SmallMapUI","ChatUI"}
sui2 = {"ChatUI"}
sui3 = {"SmallMapUI"}

        下面是Lua指令碼解析類,也是一個Singleton:

#pragma once

#include "Singleton.h"
#include <lua.hpp>

class LuaScriptSystem : public Singleton< LuaScriptSystem >
...{
    SINGLETON2(LuaScriptSystem)
private:
    LuaScriptSystem();
    ~LuaScriptSystem();
public:
    bool LoadScript(char* filename);
    const char* GetValue(const char* id , int index);
private:
    lua_State* _pLuaVM;        /**//// Lua狀態物件指標
};
 

/**//// LuaScriptSystem.cpp

#include "LuaScriptSystem.h"

LuaScriptSystem::LuaScriptSystem()
...{
    /**//// 初始化lua
    _pLuaVM = lua_open();
}

bool LuaScriptSystem::LoadScript(char* filename)
...{
    if(luaL_dofile(_pLuaVM , filename))
        return false;
    return true;
}

const char* LuaScriptSystem::GetValue(const char* id , int index)
...{
    const char* pstr = NULL;
    lua_getglobal(_pLuaVM , id);    /**//// 得到配置實體
    lua_rawgeti(_pLuaVM , -1 , index);
    if(lua_isstring(_pLuaVM , -1))
        pstr = lua_tostring(_pLuaVM , -1);

    lua_pop(_pLuaVM , 2);

    return pstr;
}

LuaScriptSystem::~LuaScriptSystem()
...{
    /**//// 關閉lua
    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指令檔。下面是它們的實現代碼:

/**//// UIObject.h

#pragma once

#include <CEGUI.h>

class UIObject
...{
protected:
    std::string _id;
    CEGUI::Window* _pWnd;
public:
    UIObject(void) : _pWnd(NULL) ...{}
    virtual ~UIObject(void) ...{}
    const std::string& GetID() const ...{return _id;}
    CEGUI::Window* GetWnd() const ...{return _pWnd;}
    virtual CEGUI::Window* InitUI() = 0;
};

/**//// ChatUI.h
#pragma once
#include "uiobject.h"

class ChatUI : public UIObject
...{
public:
    ChatUI(void);
    ~ChatUI(void);
    CEGUI::Window* InitUI();
};

/**//// ChatUI.cpp
#include "chatui.h"

using namespace CEGUI;

ChatUI::ChatUI(void)
...{
    _id = "ChatUI";
}

ChatUI::~ChatUI(void)
...{

}

Window* ChatUI::InitUI()
...{
    /**//// 簡單載入,沒有訊息處理
    if( NULL == _pWnd)
        _pWnd =  WindowManager::getSingleton().loadWindowLayout("ChatUI.layout");
    /**//// 先隱藏聊天框
    _pWnd->hide();
    return _pWnd;
}

/**//// SmallMapUI.h
#pragma once
#include "uiobject.h"

class SmallMapUI : public UIObject
...{
public:
    SmallMapUI(void);
    ~SmallMapUI(void);
    CEGUI::Window* InitUI();
    /**//** 在小地圖上點擊的訊息響應函數 */
    bool Click(const CEGUI::EventArgs& e);
};

/**//// SmallMapUI.cpp
#include "smallmapui.h"
#include "GUISystem.h"

using namespace CEGUI;

SmallMapUI::SmallMapUI(void)
...{
    _id = "SmallMapUI";
}

SmallMapUI::~SmallMapUI(void)
...{

}

Window* SmallMapUI::InitUI()
...{
    /**//// 簡單載入,只處理在靜態二維地圖上點擊左鍵
    if( NULL == _pWnd )
    ...{
        _pWnd = WindowManager::getSingleton().loadWindowLayout("SmallMapUI.layout");
        /**//// 載入一幅靜態地圖
        ImagesetManager::getSingleton().createImagesetFromImageFile("SmallMap", "ZoneMap.jpg");
        _pWnd->getChild("SmallMapUI/StaticImage")->setProperty("Image", "set:SmallMap image:full_image");
        /**//// 處理滑鼠點擊事件
        _pWnd->getChild("SmallMapUI/StaticImage")->subscribeEvent(
            CEGUI::Window::EventMouseButtonDown,
            CEGUI::Event::Subscriber(&SmallMapUI::Click , this));
    }

    return _pWnd;
}

bool SmallMapUI::Click(const CEGUI::EventArgs& e)
...{
    char text[100];
    sprintf(text , "你點擊了地圖,座標為(%.1f , %.1f)" , static_cast<const MouseEventArgs&>(e).position.d_x , static_cast<const MouseEventArgs&>(e).position.d_x);
    /**//// 通過CEGUI直接存取聊天框
    WindowManager::getSingleton().getWindow("ChatUI/MsgBox")->setText((utf8*)text);

    return true;
}

/**//// SystemUI.h
#pragma once
#include "uiobject.h"

class SystemUI : public UIObject
...{
public:
    SystemUI(void);
    ~SystemUI(void);
    CEGUI::Window* InitUI();
    bool SystemUI::OnChatBtn(const CEGUI::EventArgs& e);
    bool SystemUI::OnExitBtn(const CEGUI::EventArgs& e);
};

/**//// SystemUI.cpp
#include "SystemUI.h"
#include "GUISystem.h"

SystemUI::SystemUI(void)
...{
    _id = "SystemUI";
}

SystemUI::~SystemUI(void)
...{

}

CEGUI::Window* SystemUI::InitUI()
...{
    if( NULL == _pWnd)
    ...{
        _pWnd =  CEGUI::WindowManager::getSingleton().loadWindowLayout("SystemUI.layout");
        /**//// 處理ChatBtn訊息
        _pWnd->getChild("SystemUI/ChatBtn")->subscribeEvent(
            CEGUI::Window::EventMouseButtonDown,
            CEGUI::Event::Subscriber(&SystemUI::OnChatBtn , this));
        /**//// 處理ExitBtn訊息
        _pWnd->getChild("SystemUI/ExitBtn")->subscribeEvent(
            CEGUI::Window::EventMouseButtonDown,
            CEGUI::Event::Subscriber(&SystemUI::OnExitBtn , this));
    }
    return _pWnd;
}

bool SystemUI::OnChatBtn(const CEGUI::EventArgs& e)
...{
    /**//// 顯示聊天框
    UIObject* pUIObj = GUISystem::GetSingleton().GetUIObject("ChatUI");
    if(!pUIObj)
        return false;
    CEGUI::Window* pWnd = pUIObj->GetWnd();
    if(pWnd)
    ...{
        pWnd->isVisible() ? pWnd->hide() : pWnd->show();
    }

    return true;
}

bool SystemUI::OnExitBtn(const CEGUI::EventArgs& e)
...{
    /**//// 簡單地退出
    ::exit(0);

    return true;
}
        在使用CEGUILayoutEditor建立layout指令碼時,你不能建立一個滿屏的DefaultWindow,那樣會讓造成不能相應其他視窗的問題。但通常Editor會為我們預設建立它,這不要緊,你只需要在儲存的layout檔案中刪除那個頂層的滿屏window就可以了。

        下面是程式的運行結果:

文章出處:http://www.diybl.com/course/3_program/game/20071221/92862.html

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/sgdgoodboy/archive/2009/07/20/4363749.aspx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.