/*用户接口GUI代码,但这不是irr文档中的源代码,而是中文版的,我这里用的是字体是微软雅黑,因此如果想正确运行,请在网上找irr支持中文的资料,这里不赘述。当然,为了方便,我已经将编译好的.dll文件和.lib文件传到了CSDN的资源里,直接覆盖即可(请注意版本)。*/
#include <windows.h>
#include <irrlicht/irrlicht.h>
#include <iostream>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
struct SAppContext //该程序中列表框所要用到的结构
{
IrrlichtDevice *device; //设备指针
s32 counter; //列表框计数器
IGUIListBox* listbox; //列表框指针
};
//ID号
enum
{
GUI_ID_QUIT_BUTTON = 101,
GUI_ID_NEW_WINDOW_BUTTON,
GUI_ID_FILE_OPEN_BUTTON,
GUI_ID_TRANSPARENCY_SCROLL_BAR
};
//这是个派生的消息类
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver(SAppContext & context) : Context(context) { }
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
IGUIEnvironment* env = Context.device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
case EGET_SCROLL_BAR_CHANGED:
if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
{
//获得滚动条位置
s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
for (u32 i=0; i<EGDC_COUNT ; ++i)
{
//颜色对象,得到颜色,设置alpha值,重新设置颜色
SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
col.setAlpha(pos);
env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
}
}
break;
case EGET_BUTTON_CLICKED:
switch(id)
{
case GUI_ID_QUIT_BUTTON:
//结束irr程序
Context.device->closeDevice();
return true;
case GUI_ID_NEW_WINDOW_BUTTON:
{
Context.listbox->addItem(L"Window created");
Context.counter += 30;
if (Context.counter > 200)
Context.counter = 0;
//添加一个窗口,即对话框,还有模式和非模式呢,呵呵
IGUIWindow* window = env->addWindow(
rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
false, // modal?
L"Test window");
env->addStaticText(L"Please close me",
rect<s32>(35,35,140,50),
true, // border?
false, // wordwrap?
window);
}
return true;
case GUI_ID_FILE_OPEN_BUTTON:
Context.listbox->addItem(L"File open");
//看来MFC之类的框架都被“模拟”出来了
env->addFileOpenDialog(L"Please choose a file.");
return true;
default:
return false;
}
break;
default:
break;
}
}
return false;
}
private:
SAppContext & Context;
};
int main()
{
IrrlichtDevice * device = createDevice(video::EDT_OPENGL, core::dimension2d<s32>(640, 480));
if (device == 0)
return 1; // could not create selected driver.
device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
video::IVideoDriver* driver = device->getVideoDriver();
//创建一个“控件”指针,在前面有些代码中使用的是guienv,这里是env。
IGUIEnvironment* env = device->getGUIEnvironment();
//IGUISkin类设置表象,如字体,控件样式等
IGUISkin* skin = env->getSkin();
//字体类,创建的时候请一定要指明字体大小等属性,否则不能创建字体!第三个参数设置为true效果可能会好些哦
IGUIFont* font = env->getFont("c://windows//fonts//msyhbd.ttf",12,true,false);
if (font)
skin->setFont(font);
else
skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);
//添加3个按钮
env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
L"Quit", L"Exits Program");
env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
L"New Window", L"Launches a new Window");
env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
L"File Open", L"Opens a file");
//添加静态文本框和滚动条
env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
IGUIScrollBar* scrollbar = env->addScrollBar(true,
rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
scrollbar->setMax(255);
scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
//添加一个编辑框
env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
SAppContext context;
context.device = device;
context.counter = 0;
context.listbox = listbox;
//定义一个消息接收器,传入context
MyEventReceiver receiver(context);
//绑定
device->setEventReceiver(&receiver);
while(device->run() && driver)
{
//这里是个小技巧,只有当窗口处于激活状态时才重绘,否则让程序睡觉,这样窗口非激活时CPU占用为0了哈
if (device->isWindowActive())
{
driver->beginScene(true, true, SColor(0,200,200,200));
//绘制控件
env->drawAll();
driver->endScene();
}
else
Sleep(10);
}
device->drop();
return 0;
}