aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp')
-rw-r--r--libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp566
1 files changed, 283 insertions, 283 deletions
diff --git a/libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp b/libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp
index ec52cd5..f45c081 100644
--- a/libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp
+++ b/libraries/irrlicht-1.8/examples/05.UserInterface/main.cpp
@@ -1,283 +1,283 @@
1/** Example 005 User Interface 1/** Example 005 User Interface
2 2
3This tutorial shows how to use the built in User Interface of 3This tutorial shows how to use the built in User Interface of
4the Irrlicht Engine. It will give a brief overview and show 4the Irrlicht Engine. It will give a brief overview and show
5how to create and use windows, buttons, scroll bars, static 5how to create and use windows, buttons, scroll bars, static
6texts, and list boxes. 6texts, and list boxes.
7 7
8As always, we include the header files, and use the irrlicht 8As always, we include the header files, and use the irrlicht
9namespaces. We also store a pointer to the Irrlicht device, 9namespaces. We also store a pointer to the Irrlicht device,
10a counter variable for changing the creation position of a window, 10a counter variable for changing the creation position of a window,
11and a pointer to a listbox. 11and a pointer to a listbox.
12*/ 12*/
13#include <irrlicht.h> 13#include <irrlicht.h>
14#include "driverChoice.h" 14#include "driverChoice.h"
15 15
16using namespace irr; 16using namespace irr;
17 17
18using namespace core; 18using namespace core;
19using namespace scene; 19using namespace scene;
20using namespace video; 20using namespace video;
21using namespace io; 21using namespace io;
22using namespace gui; 22using namespace gui;
23 23
24#ifdef _IRR_WINDOWS_ 24#ifdef _IRR_WINDOWS_
25#pragma comment(lib, "Irrlicht.lib") 25#pragma comment(lib, "Irrlicht.lib")
26#endif 26#endif
27 27
28// Declare a structure to hold some context for the event receiver so that it 28// Declare a structure to hold some context for the event receiver so that it
29// has it available inside its OnEvent() method. 29// has it available inside its OnEvent() method.
30struct SAppContext 30struct SAppContext
31{ 31{
32 IrrlichtDevice *device; 32 IrrlichtDevice *device;
33 s32 counter; 33 s32 counter;
34 IGUIListBox* listbox; 34 IGUIListBox* listbox;
35}; 35};
36 36
37// Define some values that we'll use to identify individual GUI controls. 37// Define some values that we'll use to identify individual GUI controls.
38enum 38enum
39{ 39{
40 GUI_ID_QUIT_BUTTON = 101, 40 GUI_ID_QUIT_BUTTON = 101,
41 GUI_ID_NEW_WINDOW_BUTTON, 41 GUI_ID_NEW_WINDOW_BUTTON,
42 GUI_ID_FILE_OPEN_BUTTON, 42 GUI_ID_FILE_OPEN_BUTTON,
43 GUI_ID_TRANSPARENCY_SCROLL_BAR 43 GUI_ID_TRANSPARENCY_SCROLL_BAR
44}; 44};
45 45
46/* 46/*
47The Event Receiver is not only capable of getting keyboard and 47The Event Receiver is not only capable of getting keyboard and
48mouse input events, but also events of the graphical user interface 48mouse input events, but also events of the graphical user interface
49(gui). There are events for almost everything: Button click, 49(gui). There are events for almost everything: Button click,
50Listbox selection change, events that say that a element was hovered 50Listbox selection change, events that say that a element was hovered
51and so on. To be able to react to some of these events, we create 51and so on. To be able to react to some of these events, we create
52an event receiver. 52an event receiver.
53We only react to gui events, and if it's such an event, we get the 53We only react to gui events, and if it's such an event, we get the
54id of the caller (the gui element which caused the event) and get 54id of the caller (the gui element which caused the event) and get
55the pointer to the gui environment. 55the pointer to the gui environment.
56*/ 56*/
57class MyEventReceiver : public IEventReceiver 57class MyEventReceiver : public IEventReceiver
58{ 58{
59public: 59public:
60 MyEventReceiver(SAppContext & context) : Context(context) { } 60 MyEventReceiver(SAppContext & context) : Context(context) { }
61 61
62 virtual bool OnEvent(const SEvent& event) 62 virtual bool OnEvent(const SEvent& event)
63 { 63 {
64 if (event.EventType == EET_GUI_EVENT) 64 if (event.EventType == EET_GUI_EVENT)
65 { 65 {
66 s32 id = event.GUIEvent.Caller->getID(); 66 s32 id = event.GUIEvent.Caller->getID();
67 IGUIEnvironment* env = Context.device->getGUIEnvironment(); 67 IGUIEnvironment* env = Context.device->getGUIEnvironment();
68 68
69 switch(event.GUIEvent.EventType) 69 switch(event.GUIEvent.EventType)
70 { 70 {
71 71
72 /* 72 /*
73 If a scrollbar changed its scroll position, and it is 73 If a scrollbar changed its scroll position, and it is
74 'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change 74 'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change
75 the transparency of all gui elements. This is a very 75 the transparency of all gui elements. This is a very
76 easy task: There is a skin object, in which all color 76 easy task: There is a skin object, in which all color
77 settings are stored. We simply go through all colors 77 settings are stored. We simply go through all colors
78 stored in the skin and change their alpha value. 78 stored in the skin and change their alpha value.
79 */ 79 */
80 case EGET_SCROLL_BAR_CHANGED: 80 case EGET_SCROLL_BAR_CHANGED:
81 if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR) 81 if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
82 { 82 {
83 s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos(); 83 s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
84 84
85 for (u32 i=0; i<EGDC_COUNT ; ++i) 85 for (u32 i=0; i<EGDC_COUNT ; ++i)
86 { 86 {
87 SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i); 87 SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
88 col.setAlpha(pos); 88 col.setAlpha(pos);
89 env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col); 89 env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
90 } 90 }
91 91
92 } 92 }
93 break; 93 break;
94 94
95 /* 95 /*
96 If a button was clicked, it could be one of 'our' 96 If a button was clicked, it could be one of 'our'
97 three buttons. If it is the first, we shut down the engine. 97 three buttons. If it is the first, we shut down the engine.
98 If it is the second, we create a little window with some 98 If it is the second, we create a little window with some
99 text on it. We also add a string to the list box to log 99 text on it. We also add a string to the list box to log
100 what happened. And if it is the third button, we create 100 what happened. And if it is the third button, we create
101 a file open dialog, and add also this as string to the list box. 101 a file open dialog, and add also this as string to the list box.
102 That's all for the event receiver. 102 That's all for the event receiver.
103 */ 103 */
104 case EGET_BUTTON_CLICKED: 104 case EGET_BUTTON_CLICKED:
105 switch(id) 105 switch(id)
106 { 106 {
107 case GUI_ID_QUIT_BUTTON: 107 case GUI_ID_QUIT_BUTTON:
108 Context.device->closeDevice(); 108 Context.device->closeDevice();
109 return true; 109 return true;
110 110
111 case GUI_ID_NEW_WINDOW_BUTTON: 111 case GUI_ID_NEW_WINDOW_BUTTON:
112 { 112 {
113 Context.listbox->addItem(L"Window created"); 113 Context.listbox->addItem(L"Window created");
114 Context.counter += 30; 114 Context.counter += 30;
115 if (Context.counter > 200) 115 if (Context.counter > 200)
116 Context.counter = 0; 116 Context.counter = 0;
117 117
118 IGUIWindow* window = env->addWindow( 118 IGUIWindow* window = env->addWindow(
119 rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter), 119 rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
120 false, // modal? 120 false, // modal?
121 L"Test window"); 121 L"Test window");
122 122
123 env->addStaticText(L"Please close me", 123 env->addStaticText(L"Please close me",
124 rect<s32>(35,35,140,50), 124 rect<s32>(35,35,140,50),
125 true, // border? 125 true, // border?
126 false, // wordwrap? 126 false, // wordwrap?
127 window); 127 window);
128 } 128 }
129 return true; 129 return true;
130 130
131 case GUI_ID_FILE_OPEN_BUTTON: 131 case GUI_ID_FILE_OPEN_BUTTON:
132 Context.listbox->addItem(L"File open"); 132 Context.listbox->addItem(L"File open");
133 // There are some options for the file open dialog 133 // There are some options for the file open dialog
134 // We set the title, make it a modal window, and make sure 134 // We set the title, make it a modal window, and make sure
135 // that the working directory is restored after the dialog 135 // that the working directory is restored after the dialog
136 // is finished. 136 // is finished.
137 env->addFileOpenDialog(L"Please choose a file.", true, 0, -1, true); 137 env->addFileOpenDialog(L"Please choose a file.", true, 0, -1, true);
138 return true; 138 return true;
139 139
140 default: 140 default:
141 return false; 141 return false;
142 } 142 }
143 break; 143 break;
144 144
145 case EGET_FILE_SELECTED: 145 case EGET_FILE_SELECTED:
146 { 146 {
147 // show the model filename, selected in the file dialog 147 // show the model filename, selected in the file dialog
148 IGUIFileOpenDialog* dialog = 148 IGUIFileOpenDialog* dialog =
149 (IGUIFileOpenDialog*)event.GUIEvent.Caller; 149 (IGUIFileOpenDialog*)event.GUIEvent.Caller;
150 Context.listbox->addItem(dialog->getFileName()); 150 Context.listbox->addItem(dialog->getFileName());
151 } 151 }
152 break; 152 break;
153 153
154 default: 154 default:
155 break; 155 break;
156 } 156 }
157 } 157 }
158 158
159 return false; 159 return false;
160 } 160 }
161 161
162private: 162private:
163 SAppContext & Context; 163 SAppContext & Context;
164}; 164};
165 165
166 166
167/* 167/*
168Ok, now for the more interesting part. First, create the Irrlicht device. As in 168Ok, now for the more interesting part. First, create the Irrlicht device. As in
169some examples before, we ask the user which driver he wants to use for this 169some examples before, we ask the user which driver he wants to use for this
170example: 170example:
171*/ 171*/
172int main() 172int main()
173{ 173{
174 // ask user for driver 174 // ask user for driver
175 video::E_DRIVER_TYPE driverType=driverChoiceConsole(); 175 video::E_DRIVER_TYPE driverType=driverChoiceConsole();
176 if (driverType==video::EDT_COUNT) 176 if (driverType==video::EDT_COUNT)
177 return 1; 177 return 1;
178 178
179 // create device and exit if creation failed 179 // create device and exit if creation failed
180 180
181 IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480)); 181 IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
182 182
183 if (device == 0) 183 if (device == 0)
184 return 1; // could not create selected driver. 184 return 1; // could not create selected driver.
185 185
186 /* The creation was successful, now we set the event receiver and 186 /* The creation was successful, now we set the event receiver and
187 store pointers to the driver and to the gui environment. */ 187 store pointers to the driver and to the gui environment. */
188 188
189 device->setWindowCaption(L"Irrlicht Engine - User Interface Demo"); 189 device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
190 device->setResizable(true); 190 device->setResizable(true);
191 191
192 video::IVideoDriver* driver = device->getVideoDriver(); 192 video::IVideoDriver* driver = device->getVideoDriver();
193 IGUIEnvironment* env = device->getGUIEnvironment(); 193 IGUIEnvironment* env = device->getGUIEnvironment();
194 194
195 /* 195 /*
196 To make the font a little bit nicer, we load an external font 196 To make the font a little bit nicer, we load an external font
197 and set it as the new default font in the skin. 197 and set it as the new default font in the skin.
198 To keep the standard font for tool tip text, we set it to 198 To keep the standard font for tool tip text, we set it to
199 the built-in font. 199 the built-in font.
200 */ 200 */
201 201
202 IGUISkin* skin = env->getSkin(); 202 IGUISkin* skin = env->getSkin();
203 IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp"); 203 IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
204 if (font) 204 if (font)
205 skin->setFont(font); 205 skin->setFont(font);
206 206
207 skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP); 207 skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);
208 208
209 /* 209 /*
210 We add three buttons. The first one closes the engine. The second 210 We add three buttons. The first one closes the engine. The second
211 creates a window and the third opens a file open dialog. The third 211 creates a window and the third opens a file open dialog. The third
212 parameter is the id of the button, with which we can easily identify 212 parameter is the id of the button, with which we can easily identify
213 the button in the event receiver. 213 the button in the event receiver.
214 */ 214 */
215 215
216 env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON, 216 env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
217 L"Quit", L"Exits Program"); 217 L"Quit", L"Exits Program");
218 env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON, 218 env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
219 L"New Window", L"Launches a new Window"); 219 L"New Window", L"Launches a new Window");
220 env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON, 220 env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
221 L"File Open", L"Opens a file"); 221 L"File Open", L"Opens a file");
222 222
223 /* 223 /*
224 Now, we add a static text and a scrollbar, which modifies the 224 Now, we add a static text and a scrollbar, which modifies the
225 transparency of all gui elements. We set the maximum value of 225 transparency of all gui elements. We set the maximum value of
226 the scrollbar to 255, because that's the maximal value for 226 the scrollbar to 255, because that's the maximal value for
227 a color value. 227 a color value.
228 Then we create an other static text and a list box. 228 Then we create an other static text and a list box.
229 */ 229 */
230 230
231 env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true); 231 env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
232 IGUIScrollBar* scrollbar = env->addScrollBar(true, 232 IGUIScrollBar* scrollbar = env->addScrollBar(true,
233 rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR); 233 rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
234 scrollbar->setMax(255); 234 scrollbar->setMax(255);
235 235
236 // set scrollbar position to alpha value of an arbitrary element 236 // set scrollbar position to alpha value of an arbitrary element
237 scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha()); 237 scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
238 238
239 env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true); 239 env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
240 IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210)); 240 IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
241 env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100)); 241 env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
242 242
243 // Store the appropriate data in a context structure. 243 // Store the appropriate data in a context structure.
244 SAppContext context; 244 SAppContext context;
245 context.device = device; 245 context.device = device;
246 context.counter = 0; 246 context.counter = 0;
247 context.listbox = listbox; 247 context.listbox = listbox;
248 248
249 // Then create the event receiver, giving it that context structure. 249 // Then create the event receiver, giving it that context structure.
250 MyEventReceiver receiver(context); 250 MyEventReceiver receiver(context);
251 251
252 // And tell the device to use our custom event receiver. 252 // And tell the device to use our custom event receiver.
253 device->setEventReceiver(&receiver); 253 device->setEventReceiver(&receiver);
254 254
255 255
256 /* 256 /*
257 And at last, we create a nice Irrlicht Engine logo in the top left corner. 257 And at last, we create a nice Irrlicht Engine logo in the top left corner.
258 */ 258 */
259 env->addImage(driver->getTexture("../../media/irrlichtlogo2.png"), 259 env->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),
260 position2d<int>(10,10)); 260 position2d<int>(10,10));
261 261
262 262
263 /* 263 /*
264 That's all, we only have to draw everything. 264 That's all, we only have to draw everything.
265 */ 265 */
266 266
267 while(device->run() && driver) 267 while(device->run() && driver)
268 if (device->isWindowActive()) 268 if (device->isWindowActive())
269 { 269 {
270 driver->beginScene(true, true, SColor(0,200,200,200)); 270 driver->beginScene(true, true, SColor(0,200,200,200));
271 271
272 env->drawAll(); 272 env->drawAll();
273 273
274 driver->endScene(); 274 driver->endScene();
275 } 275 }
276 276
277 device->drop(); 277 device->drop();
278 278
279 return 0; 279 return 0;
280} 280}
281 281
282/* 282/*
283**/ 283**/