Advertisement
Guest User

Untitled

a guest
May 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.81 KB | None | 0 0
  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.                     env->addFileOpenDialog(L"Please choose a file.");
  134.                     return true;
  135.    
  136.  
  137.                 default:
  138.                     return false;
  139.                 }
  140.                 break;
  141.  
  142.             default:
  143.                 break;
  144.             }
  145.         }
  146.  
  147.         return false;
  148.     }
  149.  
  150. private:
  151.     SAppContext & Context;
  152. };
  153.  
  154.  
  155. /*
  156. Ok, now for the more interesting part. First, create the Irrlicht device. As in
  157. some examples before, we ask the user which driver he wants to use for this
  158. example:
  159. */
  160. int main()
  161. {
  162.     // ask user for driver
  163.     video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  164.     if (driverType==video::EDT_COUNT)
  165.         return 1;
  166.  
  167.     // create device and exit if creation failed
  168.  
  169.     IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
  170.  
  171.     if (device == 0)
  172.         return 1; // could not create selected driver.
  173.  
  174.     /* The creation was successful, now we set the event receiver and
  175.         store pointers to the driver and to the gui environment. */
  176.  
  177.     device->setWindowCaption(L"Irrlicht Engine - User Interface Demo");
  178.     device->setResizable(true);
  179.  
  180.     video::IVideoDriver* driver = device->getVideoDriver();
  181.     IGUIEnvironment* env = device->getGUIEnvironment();
  182.  
  183.     /*
  184.     To make the font a little bit nicer, we load an external font
  185.     and set it as the new default font in the skin.
  186.     To keep the standard font for tool tip text, we set it to
  187.     the built-in font.
  188.     */
  189.  
  190.     IGUISkin* skin = env->getSkin();
  191.     IGUIFont* font = env->getFont("../../media/sans8b.xml");
  192.     if (font)
  193.         skin->setFont(font);
  194.  
  195.     skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);
  196.  
  197.     /*
  198.     We add three buttons. The first one closes the engine. The second
  199.     creates a window and the third opens a file open dialog. The third
  200.     parameter is the id of the button, with which we can easily identify
  201.     the button in the event receiver.
  202.     */ 
  203.  
  204.     env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
  205.             L"Quit", L"Exits Program");
  206.     env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
  207.             L"New Window", L"Launches a new Window");
  208.     env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
  209.             L"File Open", L"Opens a file");
  210.     env->addButton(rect<s32>(10,340,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON+1,
  211.             L"Dupa", L"Lolizerzorer");
  212.  
  213.     /*
  214.     Now, we add a static text and a scrollbar, which modifies the
  215.     transparency of all gui elements. We set the maximum value of
  216.     the scrollbar to 255, because that's the maximal value for
  217.     a color value.
  218.     Then we create an other static text and a list box.
  219.     */
  220.  
  221.     env->addStaticText(L"Transparent Control:", rect<s32>(150,20,350,40), true);
  222.     IGUIScrollBar* scrollbar = env->addScrollBar(true,
  223.             rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
  224.     scrollbar->setMax(255);
  225.  
  226.     // set scrollbar position to alpha value of an arbitrary element
  227.     scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());
  228.  
  229.     env->addStaticText(L"Logging ListBox:", rect<s32>(50,110,250,130), true);
  230.     IGUIListBox * listbox = env->addListBox(rect<s32>(50, 140, 250, 210));
  231.     env->addEditBox(L"Editable Text", rect<s32>(350, 80, 550, 100));
  232.  
  233.     // Store the appropriate data in a context structure.
  234.     SAppContext context;
  235.     context.device = device;
  236.     context.counter = 0;
  237.     context.listbox = listbox;
  238.  
  239.     // Then create the event receiver, giving it that context structure.
  240.     MyEventReceiver receiver(context);
  241.  
  242.     // And tell the device to use our custom event receiver.
  243.     device->setEventReceiver(&receiver);
  244.  
  245.  
  246.     /*
  247.     And at last, we create a nice Irrlicht Engine logo in the top left corner.
  248.     */
  249.     env->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),
  250.             position2d<int>(10,10));
  251.  
  252.  
  253.     /*
  254.     That's all, we only have to draw everything.
  255.     */
  256.  
  257.     while(device->run() && driver)
  258.     if (device->isWindowActive())
  259.     {
  260.         driver->beginScene(true, true, SColor(0,200,200,200));
  261.  
  262.         env->drawAll();
  263.    
  264.         driver->endScene();
  265.     }
  266.  
  267.     device->drop();
  268.  
  269.     return 0;
  270. }
  271.  
  272. /*
  273. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement