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