Commit e0bf67e0 authored by Rogerborg's avatar Rogerborg

Tidy up the User Interface example to pass a context instead of using globals,...

Tidy up the User Interface example to pass a context instead of using globals, and enumerate values instead of using magic numbers.  Just for niceness.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@1813 dfc29bdd-3216-0410-991c-e03cc46cb475
parent ed72437e
......@@ -25,11 +25,23 @@ using namespace gui;
#pragma comment(lib, "Irrlicht.lib")
#endif
// Declare a structure to hold some context for the event receiver so that it
// has it available inside its OnEvent() method.
struct SAppContext
{
IrrlichtDevice *device;
s32 counter;
IGUIListBox* listbox;
};
IrrlichtDevice *device = 0;
s32 cnt = 0;
IGUIListBox* listbox = 0;
// Define some values that we'll use to identify individual GUI controls.
enum
{
GUI_ID_QUIT_BUTTON = 101,
GUI_ID_NEW_WINDOW_BUTTON,
GUI_ID_FILE_OPEN_BUTTON,
GUI_ID_TRANSPARENCY_SCROLL_BAR
};
/*
The Event Receiver is not only capable of getting keyboard and
......@@ -45,26 +57,28 @@ the pointer to the gui environment.
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 = device->getGUIEnvironment();
IGUIEnvironment* env = Context.device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
/*
If a scrollbar changed its scroll position, and it is
'our' scrollbar (the one with id 104), then we change
'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change
the transparency of all gui elements. This is a very
easy task: There is a skin object, in which all color
settings are stored. We simply go through all colors
stored in the skin and change their alpha value.
*/
case EGET_SCROLL_BAR_CHANGED:
if (id == 104)
if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
{
s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
......@@ -88,22 +102,21 @@ public:
That's all for the event receiver.
*/
case EGET_BUTTON_CLICKED:
if (id == 101)
switch(id)
{
device->closeDevice();
case GUI_ID_QUIT_BUTTON:
Context.device->closeDevice();
return true;
}
if (id == 102)
case GUI_ID_NEW_WINDOW_BUTTON:
{
listbox->addItem(L"Window created");
cnt += 30;
if (cnt > 200)
cnt = 0;
Context.listbox->addItem(L"Window created");
Context.counter += 30;
if (Context.counter > 200)
Context.counter = 0;
IGUIWindow* window = env->addWindow(
rect<s32>(100 + cnt, 100 + cnt, 300 + cnt, 200 + cnt),
rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
false, // modal?
L"Test window");
......@@ -112,18 +125,19 @@ public:
true, // border?
false, // wordwrap?
window);
return true;
}
return true;
if (id == 103)
{
listbox->addItem(L"File open");
case GUI_ID_FILE_OPEN_BUTTON:
Context.listbox->addItem(L"File open");
env->addFileOpenDialog(L"Please choose a file.");
return true;
}
default:
return false;
}
break;
default:
break;
}
......@@ -131,6 +145,9 @@ public:
return false;
}
private:
SAppContext & Context;
};
......@@ -166,7 +183,7 @@ int main()
// create device and exit if creation failed
device = createDevice(driverType, core::dimension2d<s32>(640, 480));
IrrlichtDevice * device = createDevice(driverType, core::dimension2d<s32>(640, 480));
if (device == 0)
return 1; // could not create selected driver.
......@@ -174,8 +191,6 @@ int main()
/* The creation was successful, now we set the event receiver and
store pointers to the driver and to the gui environment. */
MyEventReceiver receiver;
device->setEventReceiver(&receiver);
device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
video::IVideoDriver* driver = device->getVideoDriver();
......@@ -202,11 +217,11 @@ int main()
the button in the event receiver.
*/
env->addButton(rect<s32>(10,240,110,240 + 32), 0, 101,
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, 102,
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, 103,
env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
L"File Open", L"Opens a file");
/*
......@@ -219,16 +234,29 @@ int main()
env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
IGUIScrollBar* scrollbar = env->addScrollBar(true,
rect<s32>(150, 45, 350, 60), 0, 104);
rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
scrollbar->setMax(255);
// set scrollbar position to alpha value of an arbitrary element
scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
// Store the appropriate data in a context structure.
SAppContext context;
context.device = device;
context.counter = 0;
context.listbox = listbox;
// Then create the event receiver, giving it that context structure.
MyEventReceiver receiver(context);
// And tell the device to use our custom event receiver.
device->setEventReceiver(&receiver);
/*
And at last, we create a nice Irrlicht Engine logo in the top left corner.
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment