前段时间一直在看CEGUI,研究samples的时候,想随便改改代码,发现类似windows中的消息提示函数或类,是没有的,需要自己写。
就简单的写了一个
.h文件代码
//start of namespace MYTEST
namespace MYTEST
{
//MessageBox
class MessageBox
{
public:
~MessageBox();
static void addMessageBox(const CEGUI::String& id_name,const CEGUI::String& text,CEGUI::Window* parent = 0);
private:
MessageBox();
static const unsigned int d_OkButtonID;
static const unsigned int d_LableID;
static bool handleButtonClicked(const CEGUI::EventArgs& args);
static void Close(CEGUI::Window* wd);
};
} //end of namespace MYTEST
.cpp文件代码
namespace MYTEST
{
//MessageBox class
const unsigned int MessageBox::d_OkButtonID = 1;
const unsigned int MessageBox::d_LableID = 10;
MessageBox::MessageBox()
{
}
MessageBox::~MessageBox()
{
}
bool MessageBox::handleButtonClicked(const CEGUI::EventArgs& args)
{
const CEGUI::Window* wd = static_cast<const CEGUI::WindowEventArgs&>(args).window;
CEGUI::WindowManager::getSingleton().writeWindowLayoutToStream(*wd,std::cout);
Close(wd->getParent());
return true;
}
void MessageBox::Close(CEGUI::Window* wd)
{
wd->setModalState(false);
CEGUI::WindowManager::getSingleton().destroyWindow(wd);
}
void MessageBox::addMessageBox(const CEGUI::String& id_name,const CEGUI::String& text,CEGUI::Window* parent)
{
using namespace CEGUI;
Window* root = CEGUI::WindowManager::getSingleton().loadWindowLayout("MessageBox.layout",id_name);
assert(root);
root->setDestroyedByParent(false);
root->getChild(MessageBox::d_OkButtonID)->
subscribeEvent(PushButton::EventClicked,Event::Subscriber(&MessageBox::handleButtonClicked));
parent = parent ? parent : CEGUI::System::getSingleton().getGUISheet();
if (parent)
{
parent->addChildWindow(root);
}
root->getChild(MessageBox::d_LableID)->setText(text);
root->setModalState(true);
}
} //end of namespace MYTEST
.layout文件
<?xml version="1.0" ?>
<GUILayout>
<Window Type="Vanilla/FrameWindow" Name="Vanilla/MessageBox">
<Property Name="AlwaysOnTop" Value="True" />
<Property Name="UnifiedMinSize" Value="{{0.2,0},{0.2,0}}" />
<Property Name="UnifiedMaxSize" Value="{{0.8,0},{0.8,0}}" />
<Property Name="UnifiedPosition" Value="{{0.5,0},{0.5,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.3,0}}" />
<Property Name="Text" Value="" />
<Property Name="CloseButtonEnabled" Value="False" />
<Window Type="Vanilla/StaticText" Name="Vanilla/MessageBox/label1">
<Property Name="ID" Value="10" />
<Property Name="BackgroundEnabled" Value="True" />
<Property Name="FrameEnabled" Value="True" />
<Property Name="VerticalAlignment" Value="Top" />
<Property Name="HorizontalAlignment" Value="Left" />
<Property Name="UnifiedMaxSize" Value="{{1.0,0},{1.0,0}}" />
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.2,0}}" />
<Property Name="UnifiedSize" Value="{{0.7,0},{0.3,0}}" />
<Property Name="Text" Value="Name:" />
</Window>
<Window Type="Vanilla/Button" Name="Vanilla/MessageBox/OK">
<Property Name="ID" Value="1" />
<Property Name="VerticalAlignment" Value="Bottom" />
<Property Name="HorizontalAlignment" Value="Centre" />
<Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" />
<Property Name="UnifiedPosition" Value="{{0,-5},{0,-10}}" />
<Property Name="UnifiedSize" Value="{{0.25,0},{0,30}}" />
<Property Name="Text" Value="OK" />
</Window>
</Window>
</GUILayout>
调用代码
MYTEST::MessageBox::addMessageBox("MessageBox","aaaaaaaaaaaaaaaaaaaaa");
需要注意的是,由于CEGUI中的重入问题,上述函数如果连着调用两次,会抛出异常,因为CEGUI中每个窗口的名字都是唯一的。