Posted By

xterminhate on 03/09/13


Tagged

CGUIAdvancedWindow


Versions (?)

main.cpp


 / Published in: C++
 

A new custom GUI Element, based on the Irrlicht GUI Window, with these features : - Close behavior (nothing, remove, set visible false), - Configurable system buttons, - Minimize button, - Double click on minimized window maximizes it, - Restore button shows up when window is minimized, - Pinned button, to prevent moving the window, - Programmable user buttons, with attributes (show/hide for each window state) and user sprite bank, posting event catched by the user defined EventReceiver, - Double click on window bar hides window client area, - Window frame renderer using cursor coordinates as light source) and default/user skin. - Configurable minimized window position, - Flag preventing window minimizing, - Notification system, making the tittle bar to blink.

  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. #include "CGUIAdvancedWindow.h"
  16.  
  17. using namespace irr;
  18.  
  19. using namespace core;
  20. using namespace scene;
  21. using namespace video;
  22. using namespace io;
  23. using namespace gui;
  24.  
  25. #ifdef _IRR_WINDOWS_
  26. #pragma comment(lib, "Irrlicht.lib")
  27. #endif
  28.  
  29.  
  30.  
  31.  
  32. /// \todo add check box to select the close behavior of the next created window
  33. /// \todo make a lsit of current window
  34. /// \todo make a button to set visible all window and set invisible
  35. /// \todo make a button to close all, to remove all, to minimize all
  36.  
  37.  
  38.  
  39.  
  40.  
  41. // Declare a structure to hold some context for the event receiver so that it
  42. // has it available inside its OnEvent() method.
  43. struct SAppContext
  44. {
  45. IrrlichtDevice *device;
  46. IVideoDriver *driver;
  47. s32 counter;
  48. IGUIListBox* listbox;
  49. CGUIAdvancedWindow* last;
  50. };
  51.  
  52. // Define some values that we'll use to identify individual GUI controls.
  53. enum
  54. {
  55. GUI_ID_QUIT_BUTTON = 101,
  56. GUI_ID_NEW_WINDOW_BUTTON,
  57. GUI_ID_FILE_OPEN_BUTTON,
  58. GUI_ID_TRANSPARENCY_SCROLL_BAR,
  59. GUI_ID_WINDOW_SPECIAL_BUTTON,
  60. GUI_ID_NOTIFY_WINDOW
  61. };
  62.  
  63. /*
  64. The Event Receiver is not only capable of getting keyboard and
  65. mouse input events, but also events of the graphical user interface
  66. (gui). There are events for almost everything: Button click,
  67. Listbox selection change, events that say that a element was hovered
  68. and so on. To be able to react to some of these events, we create
  69. an event receiver.
  70. We only react to gui events, and if it's such an event, we get the
  71. id of the caller (the gui element which caused the event) and get
  72. the pointer to the gui environment.
  73. */
  74. class MyEventReceiver : public IEventReceiver
  75. {
  76. public:
  77. MyEventReceiver(SAppContext & context) : Context(context) { }
  78.  
  79. virtual bool OnEvent(const SEvent& event)
  80. {
  81. if (event.EventType == EET_GUI_EVENT)
  82. {
  83. s32 id = event.GUIEvent.Caller->getID();
  84. IGUIEnvironment* env = Context.device->getGUIEnvironment();
  85.  
  86. switch(event.GUIEvent.EventType)
  87. {
  88.  
  89. /*
  90. If a scrollbar changed its scroll position, and it is
  91. 'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change
  92. the transparency of all gui elements. This is a very
  93. easy task: There is a skin object, in which all color
  94. settings are stored. We simply go through all colors
  95. stored in the skin and change their alpha value.
  96. */
  97. case EGET_SCROLL_BAR_CHANGED:
  98. if (id == GUI_ID_TRANSPARENCY_SCROLL_BAR)
  99. {
  100. s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();
  101.  
  102. for (u32 i=0; i<EGDC_COUNT ; ++i)
  103. {
  104. SColor col = env->getSkin()->getColor((EGUI_DEFAULT_COLOR)i);
  105. col.setAlpha(pos);
  106. env->getSkin()->setColor((EGUI_DEFAULT_COLOR)i, col);
  107. }
  108.  
  109. }
  110. break;
  111.  
  112. /*
  113. If a button was clicked, it could be one of 'our'
  114. three buttons. If it is the first, we shut down the engine.
  115. If it is the second, we create a little window with some
  116. text on it. We also add a string to the list box to log
  117. what happened. And if it is the third button, we create
  118. a file open dialog, and add also this as string to the list box.
  119. That's all for the event receiver.
  120. */
  121. case EGET_BUTTON_CLICKED:
  122. switch(id)
  123. {
  124. case GUI_ID_QUIT_BUTTON:
  125. Context.device->closeDevice();
  126. return true;
  127.  
  128. case GUI_ID_NEW_WINDOW_BUTTON:
  129. {
  130. Context.listbox->addItem(L"Window created");
  131.  
  132. core::array<CGUIAdvancedWindow::buttoninfo> ButtonInfo;
  133.  
  134. CGUIAdvancedWindow::buttoninfo CloseButton;
  135. CloseButton.Type = EWBT_CLOSE;
  136. CloseButton.VisibleWhenNormal = true;
  137. CloseButton.VisibleWhenBar = true;
  138. CloseButton.VisibleWhenMinimized = false;
  139. ButtonInfo.push_back(CloseButton);
  140.  
  141. CGUIAdvancedWindow::buttoninfo MinimizeButton;
  142. MinimizeButton.Type = EWBT_MINIMIZE;
  143. ButtonInfo.push_back(MinimizeButton);
  144.  
  145. CGUIAdvancedWindow::buttoninfo PinButton;
  146. PinButton.Type = EWBT_PIN;
  147. PinButton.VisibleWhenNormal = true;
  148. PinButton.VisibleWhenBar = true;
  149. PinButton.VisibleWhenMinimized = false;
  150. ButtonInfo.push_back(PinButton);
  151.  
  152. CGUIAdvancedWindow::buttoninfo MySpecialButton;
  153. MySpecialButton.Type = EWBT_USER_DEFINED;
  154. IGUISpriteBank* bank = env->getSpriteBank("SpecialButton");
  155. if(!bank)
  156. {
  157. bank = env->addEmptySpriteBank("SpecialButton");
  158. bank->addTextureAsSprite(Context.driver->getTexture("../../media/reload16.png"));
  159. Context.driver->makeColorKeyTexture(Context.driver->getTexture("../../media/reload16.png"), position2di(0,0));
  160. }
  161. MySpecialButton.Sprite = bank;
  162. MySpecialButton.SpriteIndex = 0;
  163. MySpecialButton.VisibleWhenNormal = true;
  164. MySpecialButton.VisibleWhenBar = false;
  165. MySpecialButton.VisibleWhenMinimized = false;
  166. MySpecialButton.Name = L"";
  167. MySpecialButton.ToolTipText = L"My Special Button !";
  168. MySpecialButton.UserEventId = GUI_ID_WINDOW_SPECIAL_BUTTON;
  169. ButtonInfo.push_back(MySpecialButton);
  170.  
  171. CGUIAdvancedWindow* window = new CGUIAdvancedWindow( env, env->getRootGUIElement(), -1,
  172. rect<s32>(100 + (30*Context.counter)%200, 100 + (30*Context.counter)%200, 300 + (30*Context.counter)%200, 200 + (30*Context.counter)%200),
  173. ButtonInfo, irr::gui::EWC_REMOVE, core::position2di((Context.counter*200)%600,455-22*(Context.counter/3)), Context.device->getCursorControl() );
  174. window->setText(L"New Advanced Window");
  175.  
  176. env->addStaticText(L"Please close me",
  177. rect<s32>(35,35,140,50),
  178. true, // border?
  179. false, // wordwrap?
  180. window);
  181. Context.last = window;
  182. ++Context.counter;
  183. }
  184. return true;
  185.  
  186. case GUI_ID_FILE_OPEN_BUTTON:
  187. Context.listbox->addItem(L"File open");
  188. // There are some options for the file open dialog
  189. // We set the title, make it a modal window, and make sure
  190. // that the working directory is restored after the dialog
  191. // is finished.
  192. env->addFileOpenDialog(L"Please choose a file.", true, 0, -1, true);
  193. return true;
  194.  
  195. case GUI_ID_WINDOW_SPECIAL_BUTTON:
  196. Context.listbox->addItem(L"Click on Window SPECIAL BUTTON!");
  197. return true;
  198.  
  199. case GUI_ID_NOTIFY_WINDOW:
  200. Context.last->notify(false);
  201. return true;
  202.  
  203. default:
  204. return false;
  205. }
  206. break;
  207.  
  208. case EGET_FILE_SELECTED:
  209. {
  210. // show the model filename, selected in the file dialog
  211. IGUIFileOpenDialog* dialog =
  212. (IGUIFileOpenDialog*)event.GUIEvent.Caller;
  213. Context.listbox->addItem(dialog->getFileName());
  214. }
  215. break;
  216.  
  217. default:
  218. break;
  219. }
  220. }
  221.  
  222. return false;
  223. }
  224.  
  225. private:
  226. SAppContext & Context;
  227. };
  228.  
  229.  
  230. /*
  231. Ok, now for the more interesting part. First, create the Irrlicht device. As in
  232. some examples before, we ask the user which driver he wants to use for this
  233. example:
  234. */
  235. int main()
  236. {
  237. // ask user for driver
  238. // video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  239. // if (driverType==video::EDT_COUNT)
  240. // return 1;
  241.  
  242. // create device and exit if creation failed
  243.  
  244. //IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
  245. IrrlichtDevice * device = createDevice(EDT_OPENGL , core::dimension2d<u32>(640, 480));
  246.  
  247. if (device == 0)
  248. return 1; // could not create selected driver.
  249.  
  250. /* The creation was successful, now we set the event receiver and
  251. store pointers to the driver and to the gui environment. */
  252.  
  253. device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
  254. device->setResizable(true);
  255.  
  256. video::IVideoDriver* driver = device->getVideoDriver();
  257. IGUIEnvironment* env = device->getGUIEnvironment();
  258.  
  259. /*
  260. To make the font a little bit nicer, we load an external font
  261. and set it as the new default font in the skin.
  262. To keep the standard font for tool tip text, we set it to
  263. the built-in font.
  264. */
  265.  
  266. IGUISkin* skin = env->getSkin();
  267. IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
  268. if (font)
  269. skin->setFont(font);
  270.  
  271. skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);
  272.  
  273. /*
  274. We add three buttons. The first one closes the engine. The second
  275. creates a window and the third opens a file open dialog. The third
  276. parameter is the id of the button, with which we can easily identify
  277. the button in the event receiver.
  278. */
  279.  
  280. env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
  281. L"Quit", L"Exits Program");
  282. env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
  283. L"New Window", L"Launches a new Window");
  284. env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
  285. L"File Open", L"Opens a file");
  286. env->addButton(rect<s32>(10,360,110,360 + 32), 0, GUI_ID_NOTIFY_WINDOW,
  287. L"Notify", L"Notif the last window");
  288.  
  289. /*
  290. Now, we add a static text and a scrollbar, which modifies the
  291. transparency of all gui elements. We set the maximum value of
  292. the scrollbar to 255, because that's the maximal value for
  293. a color value.
  294. Then we create an other static text and a list box.
  295. */
  296.  
  297. env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
  298. IGUIScrollBar* scrollbar = env->addScrollBar(true,
  299. rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
  300. scrollbar->setMax(255);
  301.  
  302. // set scrollbar position to alpha value of an arbitrary element
  303. scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
  304.  
  305. env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
  306. IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
  307. env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
  308.  
  309. // Store the appropriate data in a context structure.
  310. SAppContext context;
  311. context.device = device;
  312. context.driver = driver;
  313. context.counter = 0;
  314. context.listbox = listbox;
  315.  
  316. // Then create the event receiver, giving it that context structure.
  317. MyEventReceiver receiver(context);
  318.  
  319. // And tell the device to use our custom event receiver.
  320. device->setEventReceiver(&receiver);
  321.  
  322.  
  323. /*
  324. And at last, we create a nice Irrlicht Engine logo in the top left corner.
  325. */
  326. env->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),
  327. position2d<int>(10,10));
  328.  
  329.  
  330. /*
  331. That's all, we only have to draw everything.
  332. */
  333.  
  334. while(device->run() && driver)
  335. if (device->isWindowActive())
  336. {
  337. driver->beginScene(true, true, SColor(0,200,200,200));
  338.  
  339. env->drawAll();
  340.  
  341. driver->endScene();
  342. }
  343.  
  344. device->drop();
  345.  
  346. return 0;
  347. }
  348.  
  349. /*
  350. **/

Report this snippet  

You need to login to post a comment.