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