mirror of
https://github.com/minetest/irrlicht.git
synced 2024-11-05 09:50:41 +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
206 lines
45 KiB
HTML
206 lines
45 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 24: CursorControl</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 24: CursorControl </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><div class="image">
|
|
<img src="../../media/example_screenshots/024shot.jpg" alt="024shot.jpg"/>
|
|
</div>
|
|
<p>Show how to modify cursors and offer some useful tool-functions for creating cursors. It can also be used for experiments with the mouse in general. </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"><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="keyword">const</span> <span class="keywordtype">int</span> DELAY_TIME = 3000;</div><div class="line"></div><div class="line"><span class="keyword">enum</span> ETimerAction</div><div class="line">{</div><div class="line"> ETA_MOUSE_VISIBLE,</div><div class="line"> ETA_MOUSE_INVISIBLE,</div><div class="line">};</div></div><!-- fragment --><p> Structure to allow delayed execution of some actions. </p><div class="fragment"><div class="line"><span class="keyword">struct </span>TimerAction</div><div class="line">{</div><div class="line"> u32 TargetTime;</div><div class="line"> ETimerAction Action;</div><div class="line">};</div></div><!-- fragment --> <div class="fragment"><div class="line"><span class="keyword">struct </span>SAppContext</div><div class="line">{</div><div class="line"> SAppContext()</div><div class="line"> : Device(0), InfoStatic(0), EventBox(0), CursorBox(0), SpriteBox(0)</div><div class="line"> , ButtonSetVisible(0), ButtonSetInvisible(0), ButtonSimulateBadFps(0)</div><div class="line"> , ButtonChangeIcon(0)</div><div class="line"> , SimulateBadFps(false)</div><div class="line"> {</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordtype">void</span> update()</div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> (!Device)</div><div class="line"> <span class="keywordflow">return</span>;</div><div class="line"> u32 timeNow = Device->getTimer()->getTime();</div><div class="line"> <span class="keywordflow">for</span> ( u32 i=0; i < TimerActions.size(); ++i )</div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> ( timeNow >= TimerActions[i].TargetTime )</div><div class="line"> {</div><div class="line"> runTimerAction(TimerActions[i]);</div><div class="line"> TimerActions.erase(i);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> ++i;</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordtype">void</span> runTimerAction(<span class="keyword">const</span> TimerAction& action)</div><div class="line"> {</div><div class="line"> <span class="keywordflow">if</span> (ETA_MOUSE_VISIBLE == action.Action)</div><div class="line"> {</div><div class="line"> Device->getCursorControl()->setVisible(<span class="keyword">true</span>);</div><div class="line"> ButtonSetVisible->setEnabled(<span class="keyword">true</span>);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( ETA_MOUSE_INVISIBLE == action.Action)</div><div class="line"> {</div><div class="line"> Device->getCursorControl()->setVisible(<span class="keyword">false</span>);</div><div class="line"> ButtonSetInvisible->setEnabled(<span class="keyword">true</span>);</div><div class="line"> }</div><div class="line"> }</div></div><!-- fragment --><p> Add another icon which the user can click and select as cursor later on. </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> addIcon(<span class="keyword">const</span> stringw& name, <span class="keyword">const</span> SCursorSprite &sprite, <span class="keywordtype">bool</span> addCursor=<span class="keyword">true</span>)</div><div class="line">{</div><div class="line"> <span class="comment">// Sprites are just icons - not yet cursors. They can be displayed by Irrlicht sprite functions and be used to create cursors.</span></div><div class="line"> SpriteBox->addItem(name.c_str(), sprite.SpriteId);</div><div class="line"> Sprites.push_back(sprite);</div><div class="line"></div><div class="line"> <span class="comment">// create the cursor together with the icon?</span></div><div class="line"> <span class="keywordflow">if</span> ( addCursor )</div><div class="line"> {</div></div><!-- fragment --><p> Here we create a hardware cursor from a sprite</p><div class="fragment"><div class="line"> Device->getCursorControl()->addIcon(sprite);</div><div class="line"></div><div class="line"> <span class="comment">// ... and add it to the cursors selection listbox to the other system cursors.</span></div><div class="line"> CursorBox->addItem(name.c_str());</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> IrrlichtDevice * Device;</div><div class="line"> gui::IGUIStaticText * InfoStatic;</div><div class="line"> gui::IGUIListBox * EventBox;</div><div class="line"> gui::IGUIListBox * CursorBox;</div><div class="line"> gui::IGUIListBox * SpriteBox;</div><div class="line"> gui::IGUIButton * ButtonSetVisible;</div><div class="line"> gui::IGUIButton * ButtonSetInvisible;</div><div class="line"> gui::IGUIButton * ButtonSimulateBadFps;</div><div class="line"> gui::IGUIButton * ButtonChangeIcon;</div><div class="line"> array<TimerAction> TimerActions;</div><div class="line"> <span class="keywordtype">bool</span> SimulateBadFps;</div><div class="line"> array<SCursorSprite> Sprites;</div><div class="line">};</div></div><!-- fragment --><p> Helper function to print mouse event names into a stringw </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> PrintMouseEventName(<span class="keyword">const</span> SEvent& event, stringw &result)</div><div class="line">{</div><div class="line"> <span class="keywordflow">switch</span> ( event.MouseInput.Event )</div><div class="line"> {</div><div class="line"> <span class="keywordflow">case</span> EMIE_LMOUSE_PRESSED_DOWN: result += stringw(L<span class="stringliteral">"EMIE_LMOUSE_PRESSED_DOWN"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_RMOUSE_PRESSED_DOWN: result += stringw(L<span class="stringliteral">"EMIE_RMOUSE_PRESSED_DOWN"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MMOUSE_PRESSED_DOWN: result += stringw(L<span class="stringliteral">"EMIE_MMOUSE_PRESSED_DOWN"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_LMOUSE_LEFT_UP: result += stringw(L<span class="stringliteral">"EMIE_LMOUSE_LEFT_UP"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_RMOUSE_LEFT_UP: result += stringw(L<span class="stringliteral">"EMIE_RMOUSE_LEFT_UP"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MMOUSE_LEFT_UP: result += stringw(L<span class="stringliteral">"EMIE_MMOUSE_LEFT_UP"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MOUSE_MOVED: result += stringw(L<span class="stringliteral">"EMIE_MOUSE_MOVED"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MOUSE_WHEEL: result += stringw(L<span class="stringliteral">"EMIE_MOUSE_WHEEL"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_LMOUSE_DOUBLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_LMOUSE_DOUBLE_CLICK"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_RMOUSE_DOUBLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_RMOUSE_DOUBLE_CLICK"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MMOUSE_DOUBLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_MMOUSE_DOUBLE_CLICK"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_LMOUSE_TRIPLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_LMOUSE_TRIPLE_CLICK"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_RMOUSE_TRIPLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_RMOUSE_TRIPLE_CLICK"</span>); <span class="keywordflow">break</span>;</div><div class="line"> <span class="keywordflow">case</span> EMIE_MMOUSE_TRIPLE_CLICK: result += stringw(L<span class="stringliteral">"EMIE_MMOUSE_TRIPLE_CLICK"</span>); <span class="keywordflow">break</span>;</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><!-- fragment --><p> Helper function to print all the state information which get from a mouse-event into a stringw </p><div class="fragment"><div class="line"><span class="keywordtype">void</span> PrintMouseState(<span class="keyword">const</span> SEvent& event, stringw &result)</div><div class="line">{</div><div class="line"> result += stringw(L<span class="stringliteral">"X: "</span>);</div><div class="line"> result += stringw(event.MouseInput.X);</div><div class="line"> result += stringw(L<span class="stringliteral">"\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"Y: "</span>);</div><div class="line"> result += stringw(event.MouseInput.Y);</div><div class="line"> result += stringw(L<span class="stringliteral">"\n"</span>);</div><div class="line"></div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"Wheel: "</span>);</div><div class="line"> result += stringw(event.MouseInput.Wheel);</div><div class="line"> result += stringw(L<span class="stringliteral">"\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"Shift: "</span>);</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.Shift )</div><div class="line"> result += stringw(L<span class="stringliteral">"true\n"</span>);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> result += stringw(L<span class="stringliteral">"false\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"Control: "</span>);</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.Control )</div><div class="line"> result += stringw(L<span class="stringliteral">"true\n"</span>);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> result += stringw(L<span class="stringliteral">"false\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"ButtonStates: "</span>);</div><div class="line"> result += stringw(event.MouseInput.ButtonStates);</div><div class="line"> result += stringw(L<span class="stringliteral">"\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"isLeftPressed: "</span>);</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.isLeftPressed() )</div><div class="line"> result += stringw(L<span class="stringliteral">"true\n"</span>);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> result += stringw(L<span class="stringliteral">"false\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"isRightPressed: "</span>);</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.isRightPressed() )</div><div class="line"> result += stringw(L<span class="stringliteral">"true\n"</span>);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> result += stringw(L<span class="stringliteral">"false\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"isMiddlePressed: "</span>);</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.isMiddlePressed() )</div><div class="line"> result += stringw(L<span class="stringliteral">"true\n"</span>);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> result += stringw(L<span class="stringliteral">"false\n"</span>);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"Event: "</span>);</div><div class="line"></div><div class="line"> PrintMouseEventName(event, result);</div><div class="line"></div><div class="line"> result += stringw(L<span class="stringliteral">"\n"</span>);</div><div class="line">}</div></div><!-- fragment --><p> A typical event receiver. </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"> <span class="keywordflow">switch</span> ( event.GUIEvent.EventType )</div><div class="line"> {</div><div class="line"> <span class="keywordflow">case</span> EGET_BUTTON_CLICKED:</div><div class="line"> {</div><div class="line"> u32 timeNow = Context.Device->getTimer()->getTime();</div><div class="line"> TimerAction action;</div><div class="line"> action.TargetTime = timeNow + DELAY_TIME;</div><div class="line"> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == Context.ButtonSetVisible )</div><div class="line"> {</div><div class="line"> action.Action = ETA_MOUSE_VISIBLE;</div><div class="line"> Context.TimerActions.push_back(action);</div><div class="line"> Context.ButtonSetVisible->setEnabled(<span class="keyword">false</span>);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == Context.ButtonSetInvisible )</div><div class="line"> {</div><div class="line"> action.Action = ETA_MOUSE_INVISIBLE;</div><div class="line"> Context.TimerActions.push_back(action);</div><div class="line"> Context.ButtonSetInvisible->setEnabled(<span class="keyword">false</span>);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == Context.ButtonSimulateBadFps )</div><div class="line"> {</div><div class="line"> Context.SimulateBadFps = Context.ButtonSimulateBadFps->isPressed();</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == Context.ButtonChangeIcon )</div><div class="line"> {</div></div><!-- fragment --><p> Replace an existing cursor icon by another icon. The user has to select both - the icon which should be replaced and the icon which will replace it. </p><div class="fragment"><div class="line">s32 selectedCursor = Context.CursorBox->getSelected();</div><div class="line">s32 selectedSprite = Context.SpriteBox->getSelected();</div><div class="line"><span class="keywordflow">if</span> ( selectedCursor >= 0 && selectedSprite >= 0 )</div><div class="line">{</div></div><!-- fragment --><p> This does replace the icon. </p><div class="fragment"><div class="line">Context.Device->getCursorControl()->changeIcon((ECURSOR_ICON)selectedCursor, Context.Sprites[selectedSprite] );</div></div><!-- fragment --><p> Do also show the new icon. </p><div class="fragment"><div class="line"> Context.Device->getCursorControl()->setActiveIcon( ECURSOR_ICON(selectedCursor) );</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"><span class="keywordflow">break</span>;</div><div class="line"><span class="keywordflow">case</span> EGET_LISTBOX_CHANGED:</div><div class="line"><span class="keywordflow">case</span> EGET_LISTBOX_SELECTED_AGAIN:</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> ( event.GUIEvent.Caller == Context.CursorBox )</div><div class="line"> {</div></div><!-- fragment --><p> Find out which cursor the user selected </p><div class="fragment"><div class="line">s32 selected = Context.CursorBox->getSelected();</div><div class="line"><span class="keywordflow">if</span> ( selected >= 0 )</div><div class="line">{</div></div><!-- fragment --><p> Here we set the new cursor icon which will now be used within our window. </p><div class="fragment"><div class="line"> Context.Device->getCursorControl()->setActiveIcon( ECURSOR_ICON(selected) );</div><div class="line"> }</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keywordflow">break</span>;</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">if</span> (event.EventType == EET_MOUSE_INPUT_EVENT)</div><div class="line"> {</div><div class="line"> stringw infoText;</div><div class="line"> PrintMouseState(event, infoText);</div><div class="line"> Context.InfoStatic->setText(infoText.c_str());</div><div class="line"> <span class="keywordflow">if</span> ( event.MouseInput.Event != EMIE_MOUSE_MOVED && event.MouseInput.Event != EMIE_MOUSE_WHEEL ) <span class="comment">// no spam</span></div><div class="line"> {</div><div class="line"> infoText = L<span class="stringliteral">""</span>;</div><div class="line"> PrintMouseEventName(event, infoText);</div><div class="line"> Context.EventBox->insertItem(0, infoText.c_str(), -1);</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> Use several imagefiles as animation frames for a sprite which can be used as cursor icon. The images in those files all need to have the same size. Return sprite index on success or -1 on failure </p><div class="fragment"><div class="line">s32 AddAnimatedIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, <span class="keyword">const</span> array< io::path >& files, u32 frameTime )</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> ( !spriteBank || !driver || !files.size() )</div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line"> video::ITexture * tex = driver->getTexture( files[0] );</div><div class="line"> <span class="keywordflow">if</span> ( tex )</div><div class="line"> {</div><div class="line"> array< rect<s32> >& spritePositions = spriteBank->getPositions();</div><div class="line"> u32 idxRect = spritePositions.size();</div><div class="line"> spritePositions.push_back( rect<s32>(0,0, tex->getSize().Width, tex->getSize().Height) );</div><div class="line"></div><div class="line"> SGUISprite sprite;</div><div class="line"> sprite.frameTime = frameTime;</div><div class="line"></div><div class="line"> array< SGUISprite >& sprites = spriteBank->getSprites();</div><div class="line"> u32 startIdx = spriteBank->getTextureCount();</div><div class="line"> <span class="keywordflow">for</span> ( u32 f=0; f < files.size(); ++f )</div><div class="line"> {</div><div class="line"> tex = driver->getTexture( files[f] );</div><div class="line"> <span class="keywordflow">if</span> ( tex )</div><div class="line"> {</div><div class="line"> spriteBank->addTexture( driver->getTexture(files[f]) );</div><div class="line"> gui::SGUISpriteFrame frame;</div><div class="line"> frame.rectNumber = idxRect;</div><div class="line"> frame.textureNumber = startIdx+f;</div><div class="line"> sprite.Frames.push_back( frame );</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> sprites.push_back( sprite );</div><div class="line"> <span class="keywordflow">return</span> sprites.size()-1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line">}</div></div><!-- fragment --><p> Use several images within one imagefile as animation frames for a sprite which can be used as cursor icon The sizes of the icons within that file all need to have the same size Return sprite index on success or -1 on failure </p><div class="fragment"><div class="line">s32 AddAnimatedIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, <span class="keyword">const</span> io::path& file, <span class="keyword">const</span> array< rect<s32> >& rects, u32 frameTime )</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> ( !spriteBank || !driver || !rects.size() )</div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line"> video::ITexture * tex = driver->getTexture( file );</div><div class="line"> <span class="keywordflow">if</span> ( tex )</div><div class="line"> {</div><div class="line"> array< rect<s32> >& spritePositions = spriteBank->getPositions();</div><div class="line"> u32 idxRect = spritePositions.size();</div><div class="line"> u32 idxTex = spriteBank->getTextureCount();</div><div class="line"> spriteBank->addTexture( tex );</div><div class="line"></div><div class="line"> SGUISprite sprite;</div><div class="line"> sprite.frameTime = frameTime;</div><div class="line"></div><div class="line"> array< SGUISprite >& sprites = spriteBank->getSprites();</div><div class="line"> <span class="keywordflow">for</span> ( u32 i=0; i < rects.size(); ++i )</div><div class="line"> {</div><div class="line"> spritePositions.push_back( rects[i] );</div><div class="line"></div><div class="line"> gui::SGUISpriteFrame frame;</div><div class="line"> frame.rectNumber = idxRect+i;</div><div class="line"> frame.textureNumber = idxTex;</div><div class="line"> sprite.Frames.push_back( frame );</div><div class="line"> }</div><div class="line"></div><div class="line"> sprites.push_back( sprite );</div><div class="line"> <span class="keywordflow">return</span> sprites.size()-1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line">}</div></div><!-- fragment --><p> Create a non-animated icon from the given file and position and put it into the spritebank. We can use this icon later on in a cursor. </p><div class="fragment"><div class="line">s32 AddIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, <span class="keyword">const</span> io::path& file, <span class="keyword">const</span> core::rect<s32>& rect )</div><div class="line">{</div><div class="line"> <span class="keywordflow">if</span> ( !spriteBank || !driver )</div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line"></div><div class="line"> video::ITexture * tex = driver->getTexture( file );</div><div class="line"> <span class="keywordflow">if</span> ( tex )</div><div class="line"> {</div><div class="line"> core::array< core::rect<irr::s32> >& spritePositions = spriteBank->getPositions();</div><div class="line"> spritePositions.push_back( rect );</div><div class="line"> array< SGUISprite >& sprites = spriteBank->getSprites();</div><div class="line"> spriteBank->addTexture( tex );</div><div class="line"></div><div class="line"> gui::SGUISpriteFrame frame;</div><div class="line"> frame.rectNumber = spritePositions.size()-1;</div><div class="line"> frame.textureNumber = spriteBank->getTextureCount()-1;</div><div class="line"></div><div class="line"> SGUISprite sprite;</div><div class="line"> sprite.frameTime = 0;</div><div class="line"> sprite.Frames.push_back( frame );</div><div class="line"></div><div class="line"> sprites.push_back( sprite );</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> sprites.size()-1;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> -1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</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"> IrrlichtDevice * device = createDevice(driverType, dimension2d<u32>(640, 480));</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 class="line"></div><div class="line"> <span class="comment">// It's sometimes of interest to know how the mouse behaves after a resize</span></div><div class="line"> device->setResizable(<span class="keyword">true</span>);</div><div class="line"></div><div class="line"> device->setWindowCaption(L<span class="stringliteral">"Cursor control - Irrlicht engine tutorial"</span>);</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"> gui::IGUISpriteBank * SpriteBankIcons;</div><div class="line"></div><div class="line"> SAppContext context;</div><div class="line"> context.Device = device;</div><div class="line"></div><div class="line"> rect< s32 > rectInfoStatic(10,10, 200, 200);</div><div class="line"> env->addStaticText (L<span class="stringliteral">"Cursor state information"</span>, rectInfoStatic, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"> rectInfoStatic.UpperLeftCorner += dimension2di(0, 15);</div><div class="line"> context.InfoStatic = env->addStaticText (L<span class="stringliteral">""</span>, rectInfoStatic, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"> rect< s32 > rectEventBox(10,210, 200, 400);</div><div class="line"> env->addStaticText (L<span class="stringliteral">"click events (new on top)"</span>, rectEventBox, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"> rectEventBox.UpperLeftCorner += dimension2di(0, 15);</div><div class="line"> context.EventBox = env->addListBox(rectEventBox);</div><div class="line"> rect< s32 > rectCursorBox(210,10, 400, 250);</div><div class="line"> env->addStaticText (L<span class="stringliteral">"cursors, click to set the active one"</span>, rectCursorBox, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"> rectCursorBox.UpperLeftCorner += dimension2di(0, 15);</div><div class="line"> context.CursorBox = env->addListBox(rectCursorBox);</div><div class="line"> rect< s32 > rectSpriteBox(210,260, 400, 400);</div><div class="line"> env->addStaticText (L<span class="stringliteral">"sprites"</span>, rectSpriteBox, <span class="keyword">true</span>, <span class="keyword">true</span>);</div><div class="line"> rectSpriteBox.UpperLeftCorner += dimension2di(0, 15);</div><div class="line"> context.SpriteBox = env->addListBox(rectSpriteBox);</div><div class="line"></div><div class="line"> context.ButtonSetVisible = env->addButton( rect<s32>( 410, 20, 560, 40 ), 0, -1, L<span class="stringliteral">"set visible (delayed)"</span> );</div><div class="line"> context.ButtonSetInvisible = env->addButton( rect<s32>( 410, 50, 560, 70 ), 0, -1, L<span class="stringliteral">"set invisible (delayed)"</span> );</div><div class="line"> context.ButtonSimulateBadFps = env->addButton( rect<s32>( 410, 80, 560, 100 ), 0, -1, L<span class="stringliteral">"simulate bad FPS"</span> );</div><div class="line"> context.ButtonSimulateBadFps->setIsPushButton(<span class="keyword">true</span>);</div><div class="line"> context.ButtonChangeIcon = env->addButton( rect<s32>( 410, 140, 560, 160 ), 0, -1, L<span class="stringliteral">"replace cursor icon\n(cursor+sprite must be selected)"</span> );</div><div class="line"></div><div class="line"> <span class="comment">// set the names for all the system cursors</span></div><div class="line"> <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i < (int)gui::ECI_COUNT; ++i )</div><div class="line"> {</div><div class="line"> context.CursorBox->addItem(stringw( GUICursorIconNames[i] ).c_str());</div><div class="line"> }</div></div><!-- fragment --><p> Create sprites which then can be used as cursor icons. </p><div class="fragment"><div class="line"> SpriteBankIcons = env->addEmptySpriteBank(io::path(<span class="stringliteral">"cursor_icons"</span>));</div><div class="line"> context.SpriteBox->setSpriteBank(SpriteBankIcons);</div><div class="line"></div><div class="line"> <span class="keyword">const</span> io::path mediaPath = getExampleMediaPath();</div><div class="line"></div><div class="line"> <span class="comment">// create one animated icon from several files</span></div><div class="line"> array< io::path > files;</div><div class="line"> files.push_back( io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16bw1.png"</span>) );</div><div class="line"> files.push_back( io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16bw2.png"</span>) );</div><div class="line"> files.push_back( io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16bw3.png"</span>) );</div><div class="line"> files.push_back( io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16bw3.png"</span>) );</div><div class="line"> files.push_back( io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16bw2.png"</span>) );</div><div class="line"> SCursorSprite spriteBw; <span class="comment">// the sprite + some additional information needed for cursors</span></div><div class="line"> spriteBw.SpriteId = AddAnimatedIconToSpriteBank( SpriteBankIcons, driver, files, 200 );</div><div class="line"> spriteBw.SpriteBank = SpriteBankIcons;</div><div class="line"> spriteBw.HotSpot = position2d<s32>(7,7);</div><div class="line"> context.addIcon(L<span class="stringliteral">"crosshair_bw"</span>, spriteBw);</div><div class="line"></div><div class="line"> <span class="comment">// create one animated icon from one file</span></div><div class="line"> array< rect<s32> > iconRects;</div><div class="line"> iconRects.push_back( rect<s32>(0,0, 16, 16) );</div><div class="line"> iconRects.push_back( rect<s32>(16,0, 32, 16) );</div><div class="line"> iconRects.push_back( rect<s32>(0,16, 16, 32) );</div><div class="line"> iconRects.push_back( rect<s32>(0,16, 16, 32) );</div><div class="line"> iconRects.push_back( rect<s32>(16,0, 32, 16) );</div><div class="line"> SCursorSprite spriteCol; <span class="comment">// the sprite + some additional information needed for cursors</span></div><div class="line"> spriteCol.SpriteId = AddAnimatedIconToSpriteBank( SpriteBankIcons, driver, io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16col.png"</span>), iconRects, 200 );</div><div class="line"> spriteCol.HotSpot = position2d<s32>(7,7);</div><div class="line"> spriteCol.SpriteBank = SpriteBankIcons;</div><div class="line"> context.addIcon(L<span class="stringliteral">"crosshair_colored"</span>, spriteCol);</div><div class="line"></div><div class="line"> <span class="comment">// Create some non-animated icons</span></div><div class="line"> rect<s32> rectIcon;</div><div class="line"> SCursorSprite spriteNonAnimated(SpriteBankIcons, 0, position2d<s32>(7,7));</div><div class="line"></div><div class="line"> rectIcon = rect<s32>(0,0, 16, 16);</div><div class="line"> spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16col.png"</span>), rectIcon );</div><div class="line"> context.addIcon(L<span class="stringliteral">"crosshair_col1"</span>, spriteNonAnimated, <span class="keyword">false</span>);</div><div class="line"></div><div class="line"> rectIcon = rect<s32>(16,0, 32, 16);</div><div class="line"> spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16col.png"</span>), rectIcon );</div><div class="line"> context.addIcon(L<span class="stringliteral">"crosshair_col2"</span>, spriteNonAnimated, <span class="keyword">false</span>);</div><div class="line"></div><div class="line"> rectIcon = rect<s32>(0,16, 16, 32);</div><div class="line"> spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path(mediaPath + <span class="stringliteral">"icon_crosshairs16x16col.png"</span>), rectIcon );</div><div class="line"> context.addIcon(L<span class="stringliteral">"crosshair_col3"</span>, spriteNonAnimated, <span class="keyword">false</span>);</div><div class="line"></div><div class="line"></div><div class="line"> MyEventReceiver receiver(context);</div><div class="line"> device->setEventReceiver(&receiver);</div><div class="line"></div><div class="line"> <span class="keywordflow">while</span>(device->run() && driver)</div><div class="line"> {</div><div class="line"> <span class="comment">// if (device->isWindowActive())</span></div><div class="line"> {</div><div class="line"> u32 realTimeNow = device->getTimer()->getRealTime();</div><div class="line"></div><div class="line"> context.update();</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"> <span class="comment">// draw custom sprite with Irrlicht functions for comparison. It should usually look the same as the cursors.</span></div><div class="line"> <span class="keywordflow">if</span> ( context.SpriteBox )</div><div class="line"> {</div><div class="line"> s32 selectedSprite = context.SpriteBox->getSelected();</div><div class="line"> <span class="keywordflow">if</span> ( selectedSprite >= 0 && context.Sprites[selectedSprite].SpriteId >= 0 )</div><div class="line"> {</div><div class="line"> SpriteBankIcons->draw2DSprite(u32(context.Sprites[selectedSprite].SpriteId), position2di(580, 140), 0, video::SColor(255, 255, 255, 255), 0, realTimeNow);</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> driver->endScene();</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// By simulating bad fps we can find out if hardware-support for cursors works or not. If it works the cursor will move as usual,while it otherwise will just update with 2 fps now.</span></div><div class="line"> <span class="keywordflow">if</span> ( context.SimulateBadFps )</div><div class="line"> {</div><div class="line"> device->sleep(500); <span class="comment">// 2 fps</span></div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> device->sleep(10);</div><div class="line"> }</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>
|