Commit afe6da7d authored by cutealien's avatar cutealien

Improve CursorControl example.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@3309 dfc29bdd-3216-0410-991c-e03cc46cb475
parent ac6befaf
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "24.CursorControl_vc9", "CursorControl_vc9.vcproj", "{02B67A37-50E1-49DB-BECF-905BC029C2FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{02B67A37-50E1-49DB-BECF-905BC029C2FE}.Debug|Win32.ActiveCfg = Debug|Win32
{02B67A37-50E1-49DB-BECF-905BC029C2FE}.Debug|Win32.Build.0 = Debug|Win32
{02B67A37-50E1-49DB-BECF-905BC029C2FE}.Release|Win32.ActiveCfg = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
......@@ -63,7 +63,7 @@
<Tool
Name="VCLinkerTool"
OutputFile="..\..\bin\Win32-VisualStudio\24.CursorControl.exe"
LinkIncremental="2"
LinkIncremental="1"
AdditionalLibraryDirectories="..\..\lib\Win32-visualstudio"
GenerateDebugInformation="true"
ProgramDatabaseFile="$(OutDir)/CursorControl.pdb"
......
......@@ -25,12 +25,17 @@ enum ETimerAction
ETA_MOUSE_INVISIBLE,
};
/*
Structure to allow delayed execution of some actions.
*/
struct TimerAction
{
u32 TargetTime;
ETimerAction Action;
};
/*
*/
struct SAppContext
{
SAppContext()
......@@ -46,7 +51,7 @@ struct SAppContext
if (!Device)
return;
u32 timeNow = Device->getTimer()->getTime();
for ( irr::u32 i=0; i < TimerActions.size(); ++i )
for ( u32 i=0; i < TimerActions.size(); ++i )
{
if ( timeNow >= TimerActions[i].TargetTime )
{
......@@ -74,21 +79,44 @@ struct SAppContext
}
}
/*
Add another icon which the user can click and select as cursor later on.
*/
void addIcon(const stringw& name, const SCursorSprite &sprite, bool addCursor=true)
{
// Sprites are just icons - not yet cursors. They can be displayed by Irrlicht sprite functions and be used to create cursors.
SpriteBox->addItem(name.c_str(), sprite.SpriteId);
Sprites.push_back(sprite);
// create the cursor together with the icon?
if ( addCursor )
{
/* Here we create a hardware cursor from a sprite */
Device->getCursorControl()->addIcon(sprite);
// ... and add it to the cursors selection listbox to the other system cursors.
CursorBox->addItem(name.c_str());
}
}
IrrlichtDevice * Device;
irr::gui::IGUIStaticText * InfoStatic;
irr::gui::IGUIListBox * EventBox;
irr::gui::IGUIListBox * CursorBox;
irr::gui::IGUIListBox * SpriteBox;
irr::gui::IGUIButton * ButtonSetVisible;
irr::gui::IGUIButton * ButtonSetInvisible;
irr::gui::IGUIButton * ButtonSimulateBadFps;
irr::gui::IGUIButton * ButtonChangeIcon;
irr::core::array<TimerAction> TimerActions;
gui::IGUIStaticText * InfoStatic;
gui::IGUIListBox * EventBox;
gui::IGUIListBox * CursorBox;
gui::IGUIListBox * SpriteBox;
gui::IGUIButton * ButtonSetVisible;
gui::IGUIButton * ButtonSetInvisible;
gui::IGUIButton * ButtonSimulateBadFps;
gui::IGUIButton * ButtonChangeIcon;
array<TimerAction> TimerActions;
bool SimulateBadFps;
irr::core::array<SCursorSprite> Sprites;
array<SCursorSprite> Sprites;
};
void PrintMouseEventName(const SEvent& event, irr::core::stringw &result)
/*
Helper function to print mouse event names into a stringw
*/
void PrintMouseEventName(const SEvent& event, stringw &result)
{
switch ( event.MouseInput.Event )
{
......@@ -111,7 +139,10 @@ void PrintMouseEventName(const SEvent& event, irr::core::stringw &result)
}
}
void PrintMouseState(const SEvent& event, irr::core::stringw &result)
/*
Helper function to print all the state information which get from a mouse-event into a stringw
*/
void PrintMouseState(const SEvent& event, stringw &result)
{
result += stringw(L"X: ");
result += stringw(event.MouseInput.X);
......@@ -167,6 +198,9 @@ void PrintMouseState(const SEvent& event, irr::core::stringw &result)
result += stringw(L"\n");
}
/*
A typical event receiver.
*/
class MyEventReceiver : public IEventReceiver
{
public:
......@@ -201,11 +235,22 @@ public:
}
else if ( event.GUIEvent.Caller == Context.ButtonChangeIcon )
{
irr::s32 selectedCursor = Context.CursorBox->getSelected();
irr::s32 selectedSprite = Context.SpriteBox->getSelected();
/*
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.
*/
s32 selectedCursor = Context.CursorBox->getSelected();
s32 selectedSprite = Context.SpriteBox->getSelected();
if ( selectedCursor >= 0 && selectedSprite >= 0 )
{
/*
This does replace the icon.
*/
Context.Device->getCursorControl()->changeIcon((ECURSOR_ICON)selectedCursor, Context.Sprites[selectedSprite] );
/*
Do also show the new icon.
*/
Context.Device->getCursorControl()->setActiveIcon( ECURSOR_ICON(selectedCursor) );
}
}
......@@ -216,11 +261,19 @@ public:
{
if ( event.GUIEvent.Caller == Context.CursorBox )
{
irr::s32 selected = Context.CursorBox->getSelected();
/*
Find out which cursor the user selected
*/
s32 selected = Context.CursorBox->getSelected();
if ( selected >= 0 )
{
/*
Here we set the new cursor icon which will now be used within our window.
*/
Context.Device->getCursorControl()->setActiveIcon( ECURSOR_ICON(selected) );
}
}
}
break;
default:
break;
......@@ -229,7 +282,7 @@ public:
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
irr::core::stringw infoText;
stringw infoText;
PrintMouseState(event, infoText);
Context.InfoStatic->setText(infoText.c_str());
if ( event.MouseInput.Event != EMIE_MOUSE_MOVED && event.MouseInput.Event != EMIE_MOUSE_WHEEL ) // no spam
......@@ -247,25 +300,27 @@ private:
SAppContext & Context;
};
// 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
irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, const irr::core::array< irr::io::path >& files, u32 frameTime )
/*
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
*/
s32 AddAnimatedIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, const array< io::path >& files, u32 frameTime )
{
if ( !spriteBank || !driver || !files.size() )
return -1;
irr::video::ITexture * tex = driver->getTexture( files[0] );
video::ITexture * tex = driver->getTexture( files[0] );
if ( tex )
{
core::array< core::rect<s32> >& spritePositions = spriteBank->getPositions();
array< rect<s32> >& spritePositions = spriteBank->getPositions();
u32 idxRect = spritePositions.size();
spritePositions.push_back( core::rect<s32>(0,0, tex->getSize().Width, tex->getSize().Height) );
spritePositions.push_back( rect<s32>(0,0, tex->getSize().Width, tex->getSize().Height) );
SGUISprite sprite;
sprite.frameTime = frameTime;
core::array< SGUISprite >& sprites = spriteBank->getSprites();
array< SGUISprite >& sprites = spriteBank->getSprites();
u32 startIdx = spriteBank->getTextureCount();
for ( u32 f=0; f < files.size(); ++f )
{
......@@ -273,7 +328,7 @@ irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDr
if ( tex )
{
spriteBank->addTexture( driver->getTexture(files[f]) );
irr::gui::SGUISpriteFrame frame;
gui::SGUISpriteFrame frame;
frame.rectNumber = idxRect;
frame.textureNumber = startIdx+f;
sprite.Frames.push_back( frame );
......@@ -287,18 +342,20 @@ irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDr
return -1;
}
// 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
irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, const irr::io::path& file, const irr::core::array< irr::core::rect<s32> >& rects, u32 frameTime )
/*
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
*/
s32 AddAnimatedIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, const io::path& file, const array< rect<s32> >& rects, u32 frameTime )
{
if ( !spriteBank || !driver || !rects.size() )
return -1;
irr::video::ITexture * tex = driver->getTexture( file );
video::ITexture * tex = driver->getTexture( file );
if ( tex )
{
core::array< core::rect<s32> >& spritePositions = spriteBank->getPositions();
array< rect<s32> >& spritePositions = spriteBank->getPositions();
u32 idxRect = spritePositions.size();
u32 idxTex = spriteBank->getTextureCount();
spriteBank->addTexture( tex );
......@@ -306,12 +363,12 @@ irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDr
SGUISprite sprite;
sprite.frameTime = frameTime;
core::array< SGUISprite >& sprites = spriteBank->getSprites();
array< SGUISprite >& sprites = spriteBank->getSprites();
for ( u32 i=0; i < rects.size(); ++i )
{
spritePositions.push_back( rects[i] );
irr::gui::SGUISpriteFrame frame;
gui::SGUISpriteFrame frame;
frame.rectNumber = idxRect+i;
frame.textureNumber = idxTex;
sprite.Frames.push_back( frame );
......@@ -324,11 +381,43 @@ irr::s32 AddAnimatedIcon( irr::gui::IGUISpriteBank * spriteBank, video::IVideoDr
return -1;
}
/*
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.
*/
s32 AddIconToSpriteBank( gui::IGUISpriteBank * spriteBank, video::IVideoDriver* driver, const io::path& file, const core::rect<s32>& rect )
{
if ( !spriteBank || !driver )
return -1;
video::ITexture * tex = driver->getTexture( file );
if ( tex )
{
core::array< core::rect<irr::s32> >& spritePositions = spriteBank->getPositions();
spritePositions.push_back( rect );
array< SGUISprite >& sprites = spriteBank->getSprites();
spriteBank->addTexture( tex );
gui::SGUISpriteFrame frame;
frame.rectNumber = spritePositions.size()-1;
frame.textureNumber = spriteBank->getTextureCount()-1;
SGUISprite sprite;
sprite.frameTime = 0;
sprite.Frames.push_back( frame );
sprites.push_back( sprite );
return sprites.size()-1;
}
return -1;
}
int main()
{
video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;
IrrlichtDevice * device = createDevice(driverType, core::dimension2d<u32>(640, 480));
IrrlichtDevice * device = createDevice(driverType, dimension2d<u32>(640, 480));
if (device == 0)
return 1; // could not create selected driver.
......@@ -336,77 +425,92 @@ int main()
video::IVideoDriver* driver = device->getVideoDriver();
IGUIEnvironment* env = device->getGUIEnvironment();
irr::gui::IGUISpriteBank * SpriteBankIcons;
gui::IGUISpriteBank * SpriteBankIcons;
SAppContext context;
context.Device = device;
core::rect< s32 > rectInfoStatic(10,10, 200, 200);
rect< s32 > rectInfoStatic(10,10, 200, 200);
env->addStaticText (L"Cursor state information", rectInfoStatic, true, true);
rectInfoStatic.UpperLeftCorner += core::dimension2di(0, 15);
rectInfoStatic.UpperLeftCorner += dimension2di(0, 15);
context.InfoStatic = env->addStaticText (L"", rectInfoStatic, true, true);
core::rect< s32 > rectEventBox(10,210, 200, 400);
rect< s32 > rectEventBox(10,210, 200, 400);
env->addStaticText (L"click events (new on top)", rectEventBox, true, true);
rectEventBox.UpperLeftCorner += core::dimension2di(0, 15);
rectEventBox.UpperLeftCorner += dimension2di(0, 15);
context.EventBox = env->addListBox(rectEventBox);
core::rect< s32 > rectCursorBox(210,10, 400, 250);
rect< s32 > rectCursorBox(210,10, 400, 250);
env->addStaticText (L"cursors, click to set the active one", rectCursorBox, true, true);
rectCursorBox.UpperLeftCorner += core::dimension2di(0, 15);
rectCursorBox.UpperLeftCorner += dimension2di(0, 15);
context.CursorBox = env->addListBox(rectCursorBox);
core::rect< s32 > rectSpriteBox(210,260, 400, 400);
rect< s32 > rectSpriteBox(210,260, 400, 400);
env->addStaticText (L"sprites", rectSpriteBox, true, true);
rectSpriteBox.UpperLeftCorner += core::dimension2di(0, 15);
rectSpriteBox.UpperLeftCorner += dimension2di(0, 15);
context.SpriteBox = env->addListBox(rectSpriteBox);
context.ButtonSetVisible = env->addButton( rect<s32>( 410, 20, 560, 40 ), 0, -1, L"set visible (delayed)" );
context.ButtonSetInvisible = env->addButton( rect<s32>( 410, 50, 560, 70 ), 0, -1, L"set invisible (delayed)" );
context.ButtonSimulateBadFps = env->addButton( rect<s32>( 410, 80, 560, 100 ), 0, -1, L"simulate bad FPS" );
context.ButtonSimulateBadFps->setIsPushButton(true);
context.ButtonChangeIcon = env->addButton( rect<s32>( 410, 140, 560, 160 ), 0, -1, L"change cursor icon\n(cursor+sprite must be selected)" );
// create sprites for cursor icons
SpriteBankIcons = env->addEmptySpriteBank(io::path("cursor_icons"));
context.SpriteBox->setSpriteBank(SpriteBankIcons);
// create an animated icon from several files
irr::core::array< irr::io::path > files;
files.push_back( irr::io::path("../../media/icon_crosshairs16x16bw1.png") );
files.push_back( irr::io::path("../../media/icon_crosshairs16x16bw2.png") );
files.push_back( irr::io::path("../../media/icon_crosshairs16x16bw3.png") );
files.push_back( irr::io::path("../../media/icon_crosshairs16x16bw3.png") );
files.push_back( irr::io::path("../../media/icon_crosshairs16x16bw2.png") );
irr::s32 SpriteIdxBw = AddAnimatedIcon( SpriteBankIcons, driver, files, 200 );
SCursorSprite spriteBw( SpriteBankIcons, SpriteIdxBw, core::position2d<s32>(7,7) );
context.SpriteBox->addItem(L"crosshair_bw", SpriteIdxBw);
context.Sprites.push_back(spriteBw);
// create an animated icon from one file
irr::core::array< irr::core::rect<s32> > iconRects;
iconRects.push_back( irr::core::rect<s32>(0,0, 16, 16) );
iconRects.push_back( irr::core::rect<s32>(16,0, 32, 16) );
iconRects.push_back( irr::core::rect<s32>(0,16, 16, 32) );
iconRects.push_back( irr::core::rect<s32>(0,16, 16, 32) );
iconRects.push_back( irr::core::rect<s32>(16,0, 32, 16) );
irr::s32 SpriteIdxCol = AddAnimatedIcon( SpriteBankIcons, driver, irr::io::path("../../media/icon_crosshairs16x16col.png"), iconRects, 200 );
SCursorSprite spriteCol( SpriteBankIcons, SpriteIdxCol, core::position2d<s32>(7,7) );
context.SpriteBox->addItem(L"up_icon_colored", SpriteIdxCol);
context.Sprites.push_back(spriteCol);
context.ButtonChangeIcon = env->addButton( rect<s32>( 410, 140, 560, 160 ), 0, -1, L"replace cursor icon\n(cursor+sprite must be selected)" );
// set the names for alll the system cursors
for ( int i=0; i < (int)gui::ECI_COUNT; ++i )
{
context.CursorBox->addItem(core::stringw( GUICursorIconNames[i] ).c_str());
context.CursorBox->addItem(stringw( GUICursorIconNames[i] ).c_str());
}
context.Device->getCursorControl()->addIcon(spriteBw);
context.CursorBox->addItem(L"black-white");
/*
Create sprites which then can be used as cursor icons.
*/
SpriteBankIcons = env->addEmptySpriteBank(io::path("cursor_icons"));
context.SpriteBox->setSpriteBank(SpriteBankIcons);
// create one animated icon from several files
array< io::path > files;
files.push_back( io::path("../../media/icon_crosshairs16x16bw1.png") );
files.push_back( io::path("../../media/icon_crosshairs16x16bw2.png") );
files.push_back( io::path("../../media/icon_crosshairs16x16bw3.png") );
files.push_back( io::path("../../media/icon_crosshairs16x16bw3.png") );
files.push_back( io::path("../../media/icon_crosshairs16x16bw2.png") );
SCursorSprite spriteBw; // the sprite + some additional information needed for cursors
spriteBw.SpriteId = AddAnimatedIconToSpriteBank( SpriteBankIcons, driver, files, 200 );
spriteBw.SpriteBank = SpriteBankIcons;
spriteBw.HotSpot = position2d<s32>(7,7);
context.addIcon(L"crosshair_bw", spriteBw);
// create one animated icon from one file
array< rect<s32> > iconRects;
iconRects.push_back( rect<s32>(0,0, 16, 16) );
iconRects.push_back( rect<s32>(16,0, 32, 16) );
iconRects.push_back( rect<s32>(0,16, 16, 32) );
iconRects.push_back( rect<s32>(0,16, 16, 32) );
iconRects.push_back( rect<s32>(16,0, 32, 16) );
SCursorSprite spriteCol; // the sprite + some additional information needed for cursors
spriteCol.SpriteId = AddAnimatedIconToSpriteBank( SpriteBankIcons, driver, io::path("../../media/icon_crosshairs16x16col.png"), iconRects, 200 );
spriteCol.HotSpot = position2d<s32>(7,7);
spriteCol.SpriteBank = SpriteBankIcons;
context.addIcon(L"crosshair_colored", spriteCol);
// Create some non-animated icons
rect<s32> rectIcon;
SCursorSprite spriteNonAnimated(SpriteBankIcons, 0, position2d<s32>(7,7));
rectIcon = rect<s32>(0,0, 16, 16);
spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path("../../media/icon_crosshairs16x16col.png"), rectIcon );
context.addIcon(L"crosshair_col1", spriteNonAnimated, false);
rectIcon = rect<s32>(16,0, 32, 16);
spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path("../../media/icon_crosshairs16x16col.png"), rectIcon );
context.addIcon(L"crosshair_col2", spriteNonAnimated, false);
rectIcon = rect<s32>(0,16, 16, 32);
spriteNonAnimated.SpriteId = AddIconToSpriteBank( SpriteBankIcons, driver, io::path("../../media/icon_crosshairs16x16col.png"), rectIcon );
context.addIcon(L"crosshair_col3", spriteNonAnimated, false);
context.Device->getCursorControl()->addIcon(spriteCol);
context.CursorBox->addItem(L"color");
MyEventReceiver receiver(context);
device->setEventReceiver(&receiver);
while(device->run() && driver)
{
// if (device->isWindowActive())
......@@ -419,13 +523,15 @@ int main()
env->drawAll();
// draw custom sprite with normal functions for comparison and to show the difference also for the hardware cursor.
if ( SpriteIdxCol >= 0 )
SpriteBankIcons->draw2DSprite(u32(SpriteIdxCol), core::position2di(30, 140), 0, video::SColor(255, 255, 255, 255), 0, realTimeNow);
if ( SpriteIdxBw >= 0 )
SpriteBankIcons->draw2DSprite(u32(SpriteIdxBw), core::position2di(80, 140), 0, video::SColor(255, 255, 255, 255), 0, realTimeNow);
// draw custom sprite with Irrlicht functions for comparison. It should usually look the same as the cursors.
if ( context.SpriteBox )
{
s32 selectedSprite = context.SpriteBox->getSelected();
if ( selectedSprite >= 0 && context.Sprites[selectedSprite].SpriteId >= 0 )
{
SpriteBankIcons->draw2DSprite(u32(context.Sprites[selectedSprite].SpriteId), position2di(580, 140), 0, video::SColor(255, 255, 255, 255), 0, realTimeNow);
}
}
driver->endScene();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment