Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-release > by-pkgid > 58828b263d8f56d90ac336dea07a4586 > files > 736

irrlicht-doc-1.6.1-1mdv2010.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Irrlicht Engine: Tutorial 5: User Interface</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<table class="irrlicht" >
  <tr valign="middle"> 
    <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> 
      | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> 
      | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> 
      | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical 
      List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> 
      Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> 
      | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> 
      Namespace&nbsp;Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class 
      members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File 
      members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td>
  </tr>
</table>
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="example005">Tutorial 5: User Interface </a></h1><div align="center">
<img src="005shot.jpg" alt="005shot.jpg">
</div>
 <p>
This tutorial shows how to use the built in User Interface of the Irrlicht Engine. It will give a brief overview and show how to create and use windows, buttons, scroll bars, static texts, and list boxes.<p>
As always, we include the header files, and use the irrlicht namespaces. We also store a pointer to the Irrlicht device, a counter variable for changing the creation position of a window, and a pointer to a listbox. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
<span class="preprocessor">#include &lt;iostream&gt;</span>

<span class="keyword">using namespace </span>irr;

<span class="keyword">using namespace </span>core;
<span class="keyword">using namespace </span>scene;
<span class="keyword">using namespace </span>video;
<span class="keyword">using namespace </span>io;
<span class="keyword">using namespace </span>gui;

<span class="preprocessor">#ifdef _IRR_WINDOWS_</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span>
<span class="preprocessor"></span><span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="comment">// Declare a structure to hold some context for the event receiver so that it</span>
<span class="comment">// has it available inside its OnEvent() method.</span>
<span class="keyword">struct </span>SAppContext
{
        IrrlichtDevice *device;
        <a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a>                             counter;
        IGUIListBox*    listbox;
};

<span class="comment">// Define some values that we'll use to identify individual GUI controls.</span>
<span class="keyword">enum</span>
{
        GUI_ID_QUIT_BUTTON = 101,
        GUI_ID_NEW_WINDOW_BUTTON,
        GUI_ID_FILE_OPEN_BUTTON,
        GUI_ID_TRANSPARENCY_SCROLL_BAR
};
</pre></div><p>
The Event Receiver is not only capable of getting keyboard and mouse input events, but also events of the graphical user interface (gui). There are events for almost everything: Button click, Listbox selection change, events that say that a element was hovered and so on. To be able to react to some of these events, we create an event receiver. We only react to gui events, and if it's such an event, we get the id of the caller (the gui element which caused the event) and get the pointer to the gui environment. <div class="fragment"><pre class="fragment"><span class="keyword">class </span>MyEventReceiver : <span class="keyword">public</span> IEventReceiver
{
<span class="keyword">public</span>:
        MyEventReceiver(SAppContext &amp; context) : Context(context) { }

        <span class="keyword">virtual</span> <span class="keywordtype">bool</span> OnEvent(<span class="keyword">const</span> SEvent&amp; event)
        {
                <span class="keywordflow">if</span> (event.EventType == <a class="code" href="namespaceirr.html#c9eed96e06e85ce3c86fcbbbe9e48a0ce85bb44dd09a29c879d64a05047fc1d2" title="An event of the graphical user interface.">EET_GUI_EVENT</a>)
                {
                        <a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> <span class="keywordtype">id</span> = <span class="keyword">event</span>.GUIEvent.Caller-&gt;getID();
                        IGUIEnvironment* env = Context.device-&gt;getGUIEnvironment();

                        <span class="keywordflow">switch</span>(event.GUIEvent.EventType)
                        {
</pre></div><p>
If a scrollbar changed its scroll position, and it is 'our' scrollbar (the one with id GUI_ID_TRANSPARENCY_SCROLL_BAR), then we change the transparency of all gui elements. This is a very easy task: There is a skin object, in which all color settings are stored. We simply go through all colors stored in the skin and change their alpha value. <div class="fragment"><pre class="fragment">                        <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1gui.html#eac71ad17341a4b6e9026ae11d5768082eea536494edcde2bb2608bda9d352b2" title="A scrollbar has changed its position.">EGET_SCROLL_BAR_CHANGED</a>:
                                <span class="keywordflow">if</span> (<span class="keywordtype">id</span> == GUI_ID_TRANSPARENCY_SCROLL_BAR)
                                {
                                        <a class="code" href="namespaceirr.html#c66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> pos = ((IGUIScrollBar*)event.GUIEvent.Caller)-&gt;getPos();
                                        
                                        <span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> i=0; i&lt;<a class="code" href="namespaceirr_1_1gui.html#bd15860fde29833c48daff5f95d5467af340f49e2e0827c0f06fdf65098554af">EGDC_COUNT</a> ; ++i)
                                        {
                                                SColor col = env-&gt;getSkin()-&gt;getColor((<a class="code" href="namespaceirr_1_1gui.html#bd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">EGUI_DEFAULT_COLOR</a>)i);
                                                col.setAlpha(pos);
                                                env-&gt;getSkin()-&gt;setColor((<a class="code" href="namespaceirr_1_1gui.html#bd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">EGUI_DEFAULT_COLOR</a>)i, col);
                                        }
                                        
                                }
                                <span class="keywordflow">break</span>;
</pre></div><p>
If a button was clicked, it could be one of 'our' three buttons. If it is the first, we shut down the engine. If it is the second, we create a little window with some text on it. We also add a string to the list box to log what happened. And if it is the third button, we create a file open dialog, and add also this as string to the list box. That's all for the event receiver. <div class="fragment"><pre class="fragment">                        <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1gui.html#eac71ad17341a4b6e9026ae11d576808308ee345c92444931f83e48354072d98" title="A button was clicked.">EGET_BUTTON_CLICKED</a>:
                                <span class="keywordflow">switch</span>(<span class="keywordtype">id</span>)
                                {
                                <span class="keywordflow">case</span> GUI_ID_QUIT_BUTTON:
                                        Context.device-&gt;closeDevice();
                                        <span class="keywordflow">return</span> <span class="keyword">true</span>;

                                <span class="keywordflow">case</span> GUI_ID_NEW_WINDOW_BUTTON:
                                        {
                                        Context.listbox-&gt;addItem(L<span class="stringliteral">"Window created"</span>);
                                        Context.counter += 30;
                                        <span class="keywordflow">if</span> (Context.counter &gt; 200)
                                                Context.counter = 0;

                                        IGUIWindow* window = env-&gt;addWindow(
                                                rect&lt;s32&gt;(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),
                                                <span class="keyword">false</span>, <span class="comment">// modal?</span>
                                                L<span class="stringliteral">"Test window"</span>);

                                        env-&gt;addStaticText(L<span class="stringliteral">"Please close me"</span>,
                                                rect&lt;s32&gt;(35,35,140,50),
                                                <span class="keyword">true</span>, <span class="comment">// border?</span>
                                                <span class="keyword">false</span>, <span class="comment">// wordwrap?</span>
                                                window);
                                        }
                                        <span class="keywordflow">return</span> <span class="keyword">true</span>;

                                <span class="keywordflow">case</span> GUI_ID_FILE_OPEN_BUTTON:
                                        Context.listbox-&gt;addItem(L<span class="stringliteral">"File open"</span>);
                                        env-&gt;addFileOpenDialog(L<span class="stringliteral">"Please choose a file."</span>);
                                        <span class="keywordflow">return</span> <span class="keyword">true</span>;

                                <span class="keywordflow">default</span>:
                                        <span class="keywordflow">return</span> <span class="keyword">false</span>;
                                }
                                <span class="keywordflow">break</span>;

                        <span class="keywordflow">default</span>:
                                <span class="keywordflow">break</span>;
                        }
                }

                <span class="keywordflow">return</span> <span class="keyword">false</span>;
        }

<span class="keyword">private</span>:
        SAppContext &amp; Context;
};
</pre></div><p>
Ok, now for the more interesting part. First, create the Irrlicht device. As in some examples before, we ask the user which driver he wants to use for this example: <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
{
        <span class="comment">// ask user for driver</span>
        
        <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType;

        printf(<span class="stringliteral">"Please select the driver you want for this example:\n"</span>\
                <span class="stringliteral">" (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"</span>\
                <span class="stringliteral">" (d) Software Renderer\n (e) Burning's Software Renderer\n"</span>\
                <span class="stringliteral">" (f) NullDevice\n (otherKey) exit\n\n"</span>);

        <span class="keywordtype">char</span> i;
        std::cin &gt;&gt; i;

        <span class="keywordflow">switch</span>(i)
        {
                <span class="keywordflow">case</span> <span class="charliteral">'a'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d04691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'b'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d08cc3807f6f28404f3424ad7e31b3142f" title="Direct3D8 device, only available on Win32 platforms.">video::EDT_DIRECT3D8</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'c'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d02715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>;   <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'d'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d01598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">video::EDT_SOFTWARE</a>; <span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'e'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0e85481da26159b967191ccc6de1e4a05" title="The Burning&amp;#39;s Software Renderer, an alternative software renderer.">video::EDT_BURNINGSVIDEO</a>;<span class="keywordflow">break</span>;
                <span class="keywordflow">case</span> <span class="charliteral">'f'</span>: driverType = <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d0cfdbd476cbfd4d05e72f9adffcc42210" title="Null driver, useful for applications to run the engine without visualisation.">video::EDT_NULL</a>;     <span class="keywordflow">break</span>;
                <span class="keywordflow">default</span>: <span class="keywordflow">return</span> 1;
        }       

        <span class="comment">// create device and exit if creation failed</span>

        IrrlichtDevice * device = <a class="code" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice</a>(driverType, core::dimension2d&lt;u32&gt;(640, 480));

        <span class="keywordflow">if</span> (device == 0)
                <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>
</pre></div> The creation was successful, now we set the event receiver and store pointers to the driver and to the gui environment. <div class="fragment"><pre class="fragment">        device-&gt;setWindowCaption(L<span class="stringliteral">"Irrlicht Engine - User Interface Demo"</span>);
        device-&gt;setResizable(<span class="keyword">true</span>);

        video::IVideoDriver* driver = device-&gt;getVideoDriver();
        IGUIEnvironment* env = device-&gt;getGUIEnvironment();
</pre></div><p>
To make the font a little bit nicer, we load an external font and set it as the new default font in the skin. To keep the standard font for tool tip text, we set it to the built-in font. <div class="fragment"><pre class="fragment">        IGUISkin* skin = env-&gt;getSkin();
        IGUIFont* font = env-&gt;getFont(<span class="stringliteral">"../../media/fonthaettenschweiler.bmp"</span>);
        <span class="keywordflow">if</span> (font)
                skin-&gt;setFont(font);

        skin-&gt;setFont(env-&gt;getBuiltInFont(), <a class="code" href="namespaceirr_1_1gui.html#9e598ab5e8644d142e55d66e8d3084419939ed432e179bb20810ee46a82ffcb8" title="Font for tooltips.">EGDF_TOOLTIP</a>);
</pre></div><p>
We add three buttons. The first one closes the engine. The second creates a window and the third opens a file open dialog. The third parameter is the id of the button, with which we can easily identify the button in the event receiver. <div class="fragment"><pre class="fragment">        env-&gt;addButton(rect&lt;s32&gt;(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,
                        L<span class="stringliteral">"Quit"</span>, L<span class="stringliteral">"Exits Program"</span>);
        env-&gt;addButton(rect&lt;s32&gt;(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
                        L<span class="stringliteral">"New Window"</span>, L<span class="stringliteral">"Launches a new Window"</span>);
        env-&gt;addButton(rect&lt;s32&gt;(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
                        L<span class="stringliteral">"File Open"</span>, L<span class="stringliteral">"Opens a file"</span>);
</pre></div><p>
Now, we add a static text and a scrollbar, which modifies the transparency of all gui elements. We set the maximum value of the scrollbar to 255, because that's the maximal value for a color value. Then we create an other static text and a list box. <div class="fragment"><pre class="fragment">        env-&gt;addStaticText(L<span class="stringliteral">"Transparent Control:"</span>, rect&lt;s32&gt;(150,20,350,40), <span class="keyword">true</span>);
        IGUIScrollBar* scrollbar = env-&gt;addScrollBar(<span class="keyword">true</span>,
                        rect&lt;s32&gt;(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);
        scrollbar-&gt;setMax(255);

        <span class="comment">// set scrollbar position to alpha value of an arbitrary element</span>
        scrollbar-&gt;setPos(env-&gt;getSkin()-&gt;getColor(<a class="code" href="namespaceirr_1_1gui.html#bd15860fde29833c48daff5f95d5467ab60eb00de0efde8c5a948ab15d9aa0a7" title="Window background.">EGDC_WINDOW</a>).getAlpha());

        env-&gt;addStaticText(L<span class="stringliteral">"Logging ListBox:"</span>, rect&lt;s32&gt;(50,110,250,130), <span class="keyword">true</span>);
        IGUIListBox * listbox = env-&gt;addListBox(rect&lt;s32&gt;(50, 140, 250, 210));
        env-&gt;addEditBox(L<span class="stringliteral">"Editable Text"</span>, rect&lt;s32&gt;(350, 80, 550, 100));

        <span class="comment">// Store the appropriate data in a context structure.</span>
        SAppContext context;
        context.device = device;
        context.counter = 0;
        context.listbox = listbox;

        <span class="comment">// Then create the event receiver, giving it that context structure.</span>
        MyEventReceiver receiver(context);

        <span class="comment">// And tell the device to use our custom event receiver.</span>
        device-&gt;setEventReceiver(&amp;receiver);
</pre></div><p>
And at last, we create a nice Irrlicht Engine logo in the top left corner. <div class="fragment"><pre class="fragment">        env-&gt;addImage(driver-&gt;getTexture(<span class="stringliteral">"../../media/irrlichtlogo2.png"</span>),
                        position2d&lt;int&gt;(10,10));
</pre></div><p>
That's all, we only have to draw everything. <div class="fragment"><pre class="fragment">        <span class="keywordflow">while</span>(device-&gt;run() &amp;&amp; driver)
        <span class="keywordflow">if</span> (device-&gt;isWindowActive())
        {
                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, SColor(0,200,200,200));

                env-&gt;drawAll();
        
                driver-&gt;endScene();
        }

        device-&gt;drop();

        <span class="keywordflow">return</span> 0;
}
</pre></div> </div>
<hr size="1">
<address style="align: right;">
<small> </small>
</address>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr> 
    <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td>
    <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
        Engine</a> Documentation &copy; 2003-2009 by Nikolaus Gebhardt. Generated 
        on Sun Jan 10 09:24:06 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.5.6)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>