mirror of
https://github.com/minetest/irrlicht.git
synced 2024-11-14 03:33:49 +01:00
c481179825
Add some for newer examples which didn't have those so far. Only updating this once now as some were broken. Next time on release. git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6204 dfc29bdd-3216-0410-991c-e03cc46cb475
207 lines
23 KiB
HTML
207 lines
23 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.13"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Tutorial 5: User Interface</title>
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
|
|
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
|
|
Feel free to improve :)
|
|
-->
|
|
<style>
|
|
body, table, div, p, dl {
|
|
font: 400 14px/22px;
|
|
}
|
|
body {
|
|
background-color: #F0F0F0;
|
|
color: black;
|
|
margin-left: 5%;
|
|
margin-right: 5%;
|
|
}
|
|
p.reference, p.definition {
|
|
font: 400 14px/22px;
|
|
}
|
|
.title {
|
|
font: 400 14px/28px;
|
|
font-size: 150%;
|
|
font-weight: bold;
|
|
margin: 10px 2px;
|
|
}
|
|
h1, h2, h3, h4, h5, h6 {
|
|
-webkit-transition: text-shadow 0.5s linear;
|
|
-moz-transition: text-shadow 0.5s linear;
|
|
-ms-transition: text-shadow 0.5s linear;
|
|
-o-transition: text-shadow 0.5s linear;
|
|
transition: text-shadow 0.5s linear;
|
|
margin-right: 15px;
|
|
}
|
|
caption {
|
|
font-weight: bold;
|
|
}
|
|
h3.version {
|
|
font-size: 90%;
|
|
text-align: center;
|
|
}
|
|
a {
|
|
color: #3D578C;
|
|
font-weight: normal;
|
|
text-decoration: none;
|
|
}
|
|
.contents a:visited {
|
|
color: #4665A2;
|
|
}
|
|
a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
a.el {
|
|
font-weight: bold;
|
|
}
|
|
a.code, a.code:visited, a.line, a.line:visited {
|
|
color: #4665A2;
|
|
}
|
|
a.codeRef, a.codeRef:visited, a.lineRef, a.lineRef:visited {
|
|
color: #4665A2;
|
|
}
|
|
pre.fragment {
|
|
border: 1px solid #C4CFE5;
|
|
background-color: #FBFCFD;
|
|
padding: 4px 6px;
|
|
margin: 4px 8px 4px 2px;
|
|
overflow: auto;
|
|
word-wrap: break-word;
|
|
font-size: 9pt;
|
|
line-height: 125%;
|
|
font-family: monospace, fixed;
|
|
font-size: 105%;
|
|
}
|
|
div.fragment {
|
|
padding: 0px;
|
|
margin: 4px 8px 4px 2px;
|
|
background-color: #FBFCFD;
|
|
border: 1px solid #C4CFE5;
|
|
}
|
|
div.line {
|
|
font-family: monospace, fixed;
|
|
font-size: 13px;
|
|
min-height: 13px;
|
|
line-height: 1.0;
|
|
text-wrap: unrestricted;
|
|
white-space: -moz-pre-wrap; /* Moz */
|
|
white-space: -pre-wrap; /* Opera 4-6 */
|
|
white-space: -o-pre-wrap; /* Opera 7 */
|
|
white-space: pre-wrap; /* CSS3 */
|
|
word-wrap: break-word; /* IE 5.5+ */
|
|
text-indent: -53px;
|
|
padding-left: 53px;
|
|
padding-bottom: 0px;
|
|
margin: 0px;
|
|
-webkit-transition-property: background-color, box-shadow;
|
|
-webkit-transition-duration: 0.5s;
|
|
-moz-transition-property: background-color, box-shadow;
|
|
-moz-transition-duration: 0.5s;
|
|
-ms-transition-property: background-color, box-shadow;
|
|
-ms-transition-duration: 0.5s;
|
|
-o-transition-property: background-color, box-shadow;
|
|
-o-transition-duration: 0.5s;
|
|
transition-property: background-color, box-shadow;
|
|
transition-duration: 0.5s;
|
|
}
|
|
div.contents {
|
|
margin-top: 10px;
|
|
margin-left: 12px;
|
|
margin-right: 8px;
|
|
}
|
|
div.center {
|
|
text-align: center;
|
|
margin-top: 0px;
|
|
margin-bottom: 0px;
|
|
padding: 0px;
|
|
}
|
|
div.center img {
|
|
border: 0px;
|
|
}
|
|
span.keyword {
|
|
color: #008000
|
|
}
|
|
span.keywordtype {
|
|
color: #604020
|
|
}
|
|
span.keywordflow {
|
|
color: #e08000
|
|
}
|
|
span.comment {
|
|
color: #800000
|
|
}
|
|
span.preprocessor {
|
|
color: #806020
|
|
}
|
|
span.stringliteral {
|
|
color: #002080
|
|
}
|
|
span.charliteral {
|
|
color: #008080
|
|
}
|
|
blockquote {
|
|
background-color: #F7F8FB;
|
|
border-left: 2px solid #9CAFD4;
|
|
margin: 0 24px 0 4px;
|
|
padding: 0 12px 0 16px;
|
|
}
|
|
hr {
|
|
height: 0px;
|
|
border: none;
|
|
border-top: 1px solid #4A6AAA;
|
|
}
|
|
address {
|
|
font-style: normal;
|
|
color: #2A3D61;
|
|
}
|
|
div.header {
|
|
background-image:url('nav_h.png');
|
|
background-repeat:repeat-x;
|
|
background-color: #F9FAFC;
|
|
margin: 0px;
|
|
border-bottom: 1px solid #C4CFE5;
|
|
}
|
|
div.headertitle {
|
|
padding: 5px 5px 5px 10px;
|
|
}
|
|
.image {
|
|
text-align: center;
|
|
}
|
|
.caption {
|
|
font-weight: bold;
|
|
}
|
|
div.zoom {
|
|
border: 1px solid #90A5CE;
|
|
}
|
|
tr.heading h2 {
|
|
margin-top: 12px;
|
|
margin-bottom: 4px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<!--END TITLEAREA-->
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.13 -->
|
|
</div><!-- top -->
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Tutorial 5: User Interface </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><div class="image">
|
|
<img src="../../media/example_screenshots/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"><div class="line"><span class="preprocessor">#include <irrlicht.h></span></div><div class="line"><span class="preprocessor">#include "driverChoice.h"</span></div><div class="line"><span class="preprocessor">#include "exampleHelper.h"</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>irr;</div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>core;</div><div class="line"><span class="keyword">using namespace </span>scene;</div><div class="line"><span class="keyword">using namespace </span>video;</div><div class="line"><span class="keyword">using namespace </span>io;</div><div class="line"><span class="keyword">using namespace </span>gui;</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef _MSC_VER</span></div><div class="line"><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="comment">// Declare a structure to hold some context for the event receiver so that it</span></div><div class="line"><span class="comment">// has it available inside its OnEvent() method.</span></div><div class="line"><span class="keyword">struct </span>SAppContext</div><div class="line">{</div><div class="line"> IrrlichtDevice *device;</div><div class="line"> s32 counter;</div><div class="line"> IGUIListBox* listbox;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="comment">// Define some values that we'll use to identify individual GUI controls.</span></div><div class="line"><span class="keyword">enum</span></div><div class="line">{</div><div class="line"> GUI_ID_QUIT_BUTTON = 101,</div><div class="line"> GUI_ID_NEW_WINDOW_BUTTON,</div><div class="line"> GUI_ID_FILE_OPEN_BUTTON,</div><div class="line"> GUI_ID_TRANSPARENCY_SCROLL_BAR</div><div class="line">};</div></div><!-- fragment --><p> Set the skin transparency by changing the alpha values of all skin-colors </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> setSkinTransparency(s32 alpha, irr::gui::IGUISkin * skin)</div><div class="line">{</div><div class="line"> <span class="keywordflow">for</span> (s32 i=0; i<irr::gui::EGDC_COUNT ; ++i)</div><div class="line"> {</div><div class="line"> video::SColor col = skin->getColor((EGUI_DEFAULT_COLOR)i);</div><div class="line"> col.setAlpha(alpha);</div><div class="line"> skin->setColor((EGUI_DEFAULT_COLOR)i, col);</div><div class="line"> }</div><div class="line">}</div></div><!-- fragment --><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"><div class="line"><span class="keyword">class </span>MyEventReceiver : <span class="keyword">public</span> IEventReceiver</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"> MyEventReceiver(SAppContext & context) : Context(context) { }</div><div class="line"></div><div class="line"> <span class="keyword">virtual</span> <span class="keywordtype">bool</span> OnEvent(<span class="keyword">const</span> SEvent& event)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> (event.EventType == EET_GUI_EVENT)</div><div class="line"> {</div><div class="line"> s32 <span class="keywordtype">id</span> = <span class="keyword">event</span>.GUIEvent.Caller->getID();</div><div class="line"> IGUIEnvironment* env = Context.device->getGUIEnvironment();</div><div class="line"></div><div class="line"> <span class="keywordflow">switch</span>(event.GUIEvent.EventType)</div><div class="line"> {</div></div><!-- fragment --><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 an 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"><div class="line"><span class="keywordflow">case</span> EGET_SCROLL_BAR_CHANGED:</div><div class="line"> <span class="keywordflow">if</span> (<span class="keywordtype">id</span> == GUI_ID_TRANSPARENCY_SCROLL_BAR)</div><div class="line"> {</div><div class="line"> s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();</div><div class="line"> setSkinTransparency(pos, env->getSkin());</div><div class="line"> }</div><div class="line"> <span class="keywordflow">break</span>;</div></div><!-- fragment --><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"><div class="line"> <span class="keywordflow">case</span> EGET_BUTTON_CLICKED:</div><div class="line"> <span class="keywordflow">switch</span>(<span class="keywordtype">id</span>)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">case</span> GUI_ID_QUIT_BUTTON:</div><div class="line"> Context.device->closeDevice();</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">case</span> GUI_ID_NEW_WINDOW_BUTTON:</div><div class="line"> {</div><div class="line"> Context.listbox->addItem(L<span class="stringliteral">"Window created"</span>);</div><div class="line"> Context.counter += 30;</div><div class="line"> <span class="keywordflow">if</span> (Context.counter > 200)</div><div class="line"> Context.counter = 0;</div><div class="line"></div><div class="line"> IGUIWindow* window = env->addWindow(</div><div class="line"> rect<s32>(100 + Context.counter, 100 + Context.counter, 300 + Context.counter, 200 + Context.counter),</div><div class="line"> <span class="keyword">false</span>, <span class="comment">// modal?</span></div><div class="line"> L<span class="stringliteral">"Test window"</span>);</div><div class="line"></div><div class="line"> env->addStaticText(L<span class="stringliteral">"Please close me"</span>,</div><div class="line"> rect<s32>(35,35,140,50),</div><div class="line"> <span class="keyword">true</span>, <span class="comment">// border?</span></div><div class="line"> <span class="keyword">false</span>, <span class="comment">// wordwrap?</span></div><div class="line"> window);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">case</span> GUI_ID_FILE_OPEN_BUTTON:</div><div class="line"> Context.listbox->addItem(L<span class="stringliteral">"File open"</span>);</div><div class="line"> <span class="comment">// There are some options for the file open dialog</span></div><div class="line"> <span class="comment">// We set the title, make it a modal window, and make sure</span></div><div class="line"> <span class="comment">// that the working directory is restored after the dialog</span></div><div class="line"> <span class="comment">// is finished.</span></div><div class="line"> env->addFileOpenDialog(L<span class="stringliteral">"Please choose a file."</span>, <span class="keyword">true</span>, 0, -1, <span class="keyword">true</span>);</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">default</span>:</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">case</span> EGET_FILE_SELECTED:</div><div class="line"> {</div><div class="line"> <span class="comment">// show the event and the selected model filename from the file dialog</span></div><div class="line"> IGUIFileOpenDialog* dialog =</div><div class="line"> (IGUIFileOpenDialog*)event.GUIEvent.Caller;</div><div class="line"> Context.listbox->addItem(L<span class="stringliteral">"EGET_FILE_SELECTED"</span>);</div><div class="line"> Context.listbox->addItem(dialog->getFileName());</div><div class="line"> }</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">case</span> EGET_DIRECTORY_SELECTED:</div><div class="line"> {</div><div class="line"> <span class="comment">// show the event and the selected directory name from the file dialog</span></div><div class="line"> IGUIFileOpenDialog* dialog =</div><div class="line"> (IGUIFileOpenDialog*)event.GUIEvent.Caller;</div><div class="line"> Context.listbox->addItem(L<span class="stringliteral">"EGET_DIRECTORY_SELECTED"</span>);</div><div class="line"> Context.listbox->addItem(dialog->getDirectoryNameW());</div><div class="line"> }</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"></div><div class="line"> <span class="keywordflow">default</span>:</div><div class="line"> <span class="keywordflow">break</span>;</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"> }</div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line"> SAppContext & Context;</div><div class="line">};</div></div><!-- fragment --><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"><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line"> <span class="comment">// ask user for driver</span></div><div class="line"> video::E_DRIVER_TYPE driverType=driverChoiceConsole();</div><div class="line"> <span class="keywordflow">if</span> (driverType==video::EDT_COUNT)</div><div class="line"> <span class="keywordflow">return</span> 1;</div><div class="line"></div><div class="line"> <span class="comment">// create device and exit if creation failed</span></div><div class="line"> IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (device == 0)</div><div class="line"> <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span></div></div><!-- fragment --><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"><div class="line">device->setWindowCaption(L<span class="stringliteral">"Irrlicht Engine - User Interface Demo"</span>);</div><div class="line">device->setResizable(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">video::IVideoDriver* driver = device->getVideoDriver();</div><div class="line">IGUIEnvironment* env = device->getGUIEnvironment();</div><div class="line"></div><div class="line"><span class="keyword">const</span> io::path mediaPath = getExampleMediaPath();</div></div><!-- fragment --><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"><div class="line">IGUISkin* skin = env->getSkin();</div><div class="line">IGUIFont* font = env->getFont(mediaPath + <span class="stringliteral">"fonthaettenschweiler.bmp"</span>);</div><div class="line"><span class="keywordflow">if</span> (font)</div><div class="line"> skin->setFont(font);</div><div class="line"></div><div class="line">skin->setFont(env->getBuiltInFont(), EGDF_TOOLTIP);</div></div><!-- fragment --><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"><div class="line">env->addButton(rect<s32>(10,240,110,240 + 32), 0, GUI_ID_QUIT_BUTTON,</div><div class="line"> L<span class="stringliteral">"Quit"</span>, L<span class="stringliteral">"Exits Program"</span>);</div><div class="line">env->addButton(rect<s32>(10,280,110,280 + 32), 0, GUI_ID_NEW_WINDOW_BUTTON,</div><div class="line"> L<span class="stringliteral">"New Window"</span>, L<span class="stringliteral">"Launches a new Window"</span>);</div><div class="line">env->addButton(rect<s32>(10,320,110,320 + 32), 0, GUI_ID_FILE_OPEN_BUTTON,</div><div class="line"> L<span class="stringliteral">"File Open"</span>, L<span class="stringliteral">"Opens a file"</span>);</div></div><!-- fragment --><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"><div class="line">env->addStaticText(L<span class="stringliteral">"Transparent Control:"</span>, rect<s32>(150,20,350,40), <span class="keyword">true</span>);</div><div class="line">IGUIScrollBar* scrollbar = env->addScrollBar(<span class="keyword">true</span>,</div><div class="line"> rect<s32>(150, 45, 350, 60), 0, GUI_ID_TRANSPARENCY_SCROLL_BAR);</div><div class="line">scrollbar->setMax(255);</div><div class="line">scrollbar->setPos(255);</div><div class="line">setSkinTransparency( scrollbar->getPos(), env->getSkin());</div><div class="line"></div><div class="line"><span class="comment">// set scrollbar position to alpha value of an arbitrary element</span></div><div class="line">scrollbar->setPos(env->getSkin()->getColor(EGDC_WINDOW).getAlpha());</div><div class="line"></div><div class="line">env->addStaticText(L<span class="stringliteral">"Logging ListBox:"</span>, rect<s32>(10,110,350,130), <span class="keyword">true</span>);</div><div class="line">IGUIListBox * listbox = env->addListBox(rect<s32>(10, 140, 350, 210));</div><div class="line">env->addEditBox(L<span class="stringliteral">"Editable Text"</span>, rect<s32>(350, 80, 550, 100));</div><div class="line"></div><div class="line"><span class="comment">// Store the appropriate data in a context structure.</span></div><div class="line">SAppContext context;</div><div class="line">context.device = device;</div><div class="line">context.counter = 0;</div><div class="line">context.listbox = listbox;</div><div class="line"></div><div class="line"><span class="comment">// Then create the event receiver, giving it that context structure.</span></div><div class="line">MyEventReceiver receiver(context);</div><div class="line"></div><div class="line"><span class="comment">// And tell the device to use our custom event receiver.</span></div><div class="line">device->setEventReceiver(&receiver);</div></div><!-- fragment --><p> And at last, we create a nice Irrlicht Engine logo in the top left corner. </p><div class="fragment"><div class="line">env->addImage(driver->getTexture(mediaPath + <span class="stringliteral">"irrlichtlogo3.png"</span>),</div><div class="line"> position2d<int>(10,10));</div></div><!-- fragment --><p> That's all, we only have to draw everything. </p><div class="fragment"><div class="line"> <span class="keywordflow">while</span>(device->run() && driver)</div><div class="line"> <span class="keywordflow">if</span> (device->isWindowActive())</div><div class="line"> {</div><div class="line"> driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, SColor(0,200,200,200));</div><div class="line"></div><div class="line"> env->drawAll();</div><div class="line"></div><div class="line"> driver->endScene();</div><div class="line"> }</div><div class="line"></div><div class="line"> device->drop();</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
|
|
<!-- HTML footer for doxygen 1.8.13-->
|
|
<!-- start footer part -->
|
|
<p> </p>
|
|
</body>
|
|
</html>
|