main.cpp 8.9 KB

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