irrlicht/doc/html/example005.html

330 lines
22 KiB
HTML
Raw Normal View History

2020-05-16 23:31:28 +02:00
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Irrlicht 3D Engine: Tutorial 5: User Interface</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="irrlichtlogo.png"/></td>
<td style="padding-left: 0.5em;">
<div id="projectname">Irrlicht 3D Engine
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="dynsections.js"></script>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
initNavTree('example005.html','');
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Tutorial 5: User Interface </div> </div>
</div>
<div class="contents">
<div class="textblock"><div class="image">
<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>
<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. </p>
<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 &quot;<a class="code" href="driver_choice_8h.html">driverChoice.h</a>&quot;</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, &quot;Irrlicht.lib&quot;)</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
{
<a class="code" href="classirr_1_1_irrlicht_device.html" title="The Irrlicht device. You can create it with createDevice() or createDeviceEx().">IrrlichtDevice</a> *device;
<a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> counter;
IGUIListBox* listbox;
};
<span class="comment">// Define some values that we&#39;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>Set the skin transparency by changing the alpha values of all skin-colors </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">void</span> setSkinTransparency(<a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> alpha, <a class="code" href="classirr_1_1gui_1_1_i_g_u_i_skin.html" title="A skin modifies the look of the GUI elements.">irr::gui::IGUISkin</a> * skin)
{
<span class="keywordflow">for</span> (<a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> i=0; i&lt;<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467aaf340f49e2e0827c0f06fdf65098554af">irr::gui::EGDC_COUNT</a> ; ++i)
{
video::SColor col = skin-&gt;<a class="code" href="classirr_1_1gui_1_1_i_g_u_i_skin.html#ad1afa2e5e34c30e0cbfb85b1dee2dbe3" title="returns default color">getColor</a>((<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">EGUI_DEFAULT_COLOR</a>)i);
col.setAlpha(alpha);
skin-&gt;<a class="code" href="classirr_1_1gui_1_1_i_g_u_i_skin.html#ab9782296ba881872207a0915a81d7807" title="sets a default color">setColor</a>((<a class="code" href="namespaceirr_1_1gui.html#abd15860fde29833c48daff5f95d5467a" title="Enumeration for skin colors.">EGUI_DEFAULT_COLOR</a>)i, col);
}
}
</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. </p>
<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#ac9eed96e06e85ce3c86fcbbbe9e48a0cae85bb44dd09a29c879d64a05047fc1d2" title="An event of the graphical user interface.">EET_GUI_EVENT</a>)
{
<a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" 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. </p>
<div class="fragment"><pre class="fragment"> <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1gui.html#aeac71ad17341a4b6e9026ae11d576808a2eea536494edcde2bb2608bda9d352b2" 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#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> pos = ((IGUIScrollBar*)event.GUIEvent.Caller)-&gt;getPos();
setSkinTransparency(pos, env-&gt;getSkin());
}
<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. </p>
<div class="fragment"><pre class="fragment"> <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1gui.html#aeac71ad17341a4b6e9026ae11d576808a308ee345c92444931f83e48354072d98" 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">&quot;Window created&quot;</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">&quot;Test window&quot;</span>);
env-&gt;addStaticText(L<span class="stringliteral">&quot;Please close me&quot;</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">&quot;File open&quot;</span>);
<span class="comment">// There are some options for the file open dialog</span>
<span class="comment">// We set the title, make it a modal window, and make sure</span>
<span class="comment">// that the working directory is restored after the dialog</span>
<span class="comment">// is finished.</span>
env-&gt;addFileOpenDialog(L<span class="stringliteral">&quot;Please choose a file.&quot;</span>, <span class="keyword">true</span>, 0, -1, <span class="keyword">true</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">case</span> <a class="code" href="namespaceirr_1_1gui.html#aeac71ad17341a4b6e9026ae11d576808a5b6504cf6b541d5ad95407c384632873" title="A file has been selected in the file dialog.">EGET_FILE_SELECTED</a>:
{
<span class="comment">// show the model filename, selected in the file dialog</span>
IGUIFileOpenDialog* dialog =
(IGUIFileOpenDialog*)event.GUIEvent.Caller;
Context.listbox-&gt;addItem(dialog-&gt;getFileName());
}
<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: </p>
<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#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType=driverChoiceConsole();
<span class="keywordflow">if</span> (driverType==<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0ae685cada50f8c100403134d932d0414c" title="No driver, just for counting the elements.">video::EDT_COUNT</a>)
<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#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.">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><p> The creation was successful, now we set the event receiver and store pointers to the driver and to the gui environment. </p>
<div class="fragment"><pre class="fragment"> device-&gt;setWindowCaption(L<span class="stringliteral">&quot;Irrlicht Engine - User Interface Demo&quot;</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. </p>
<div class="fragment"><pre class="fragment"> IGUISkin* skin = env-&gt;getSkin();
IGUIFont* font = env-&gt;<a class="code" href="classirr_1_1gui_1_1_i_g_u_i_skin.html#ab1b2623d04688c4dfe106ca407171d6e" title="returns the default font">getFont</a>(<span class="stringliteral">&quot;../../media/fonthaettenschweiler.bmp&quot;</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#a9e598ab5e8644d142e55d66e8d308441a9939ed432e179bb20810ee46a82ffcb8" 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. </p>
<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">&quot;Quit&quot;</span>, L<span class="stringliteral">&quot;Exits Program&quot;</span>);
env-&gt;addButton(rect&lt;s32&gt;(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,
L<span class="stringliteral">&quot;New Window&quot;</span>, L<span class="stringliteral">&quot;Launches a new Window&quot;</span>);
env-&gt;addButton(rect&lt;s32&gt;(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,
L<span class="stringliteral">&quot;File Open&quot;</span>, L<span class="stringliteral">&quot;Opens a file&quot;</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. </p>
<div class="fragment"><pre class="fragment"> env-&gt;addStaticText(L<span class="stringliteral">&quot;Transparent Control:&quot;</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);
scrollbar-&gt;setPos(255);
setSkinTransparency( scrollbar-&gt;getPos(), env-&gt;getSkin());
<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#abd15860fde29833c48daff5f95d5467aab60eb00de0efde8c5a948ab15d9aa0a7" title="Window background.">EGDC_WINDOW</a>).getAlpha());
env-&gt;addStaticText(L<span class="stringliteral">&quot;Logging ListBox:&quot;</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">&quot;Editable Text&quot;</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. </p>
<div class="fragment"><pre class="fragment"> env-&gt;addImage(driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlichtlogo2.png&quot;</span>),
position2d&lt;int&gt;(10,10));
</pre></div><p>That's all, we only have to draw everything. </p>
<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></div>
</div>
<div id="nav-path" class="navpath">
<ul>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<li class="footer">
<a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht
Engine</a> Documentation &copy; 2003-2012 by Nikolaus Gebhardt. Generated on Sat Jul 9 2016 18:18:25 for Irrlicht 3D Engine by
<a href="http://www.doxygen.org/index.html" target="_blank">Doxygen</a> 1.7.5.1 </li>
</ul>
</div>
</body>
</html>