2011-12-13 21:23:17 +00:00
|
|
|
#include "StdInc.h"
|
2013-03-03 19:03:45 +00:00
|
|
|
#include <SDL.h>
|
2007-08-21 13:48:18 +00:00
|
|
|
#include "CCursorHandler.h"
|
2013-03-03 19:03:45 +00:00
|
|
|
#include "GL2D.h"
|
|
|
|
#include "../Gfx/Animations.h"
|
|
|
|
#include "../Gfx/Images.h"
|
2011-12-13 21:23:17 +00:00
|
|
|
|
2012-12-15 09:04:55 +00:00
|
|
|
#include "../CAnimation.h"
|
|
|
|
#include "CGuiHandler.h"
|
2007-08-21 13:48:18 +00:00
|
|
|
|
2009-04-15 14:03:31 +00:00
|
|
|
/*
|
|
|
|
* CCursorHandler.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
CCursorHandler::CCursorHandler()
|
2007-08-21 13:48:18 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
xpos = ypos = 0;
|
|
|
|
type = ECursor::DEFAULT;
|
|
|
|
dndObject = nullptr;
|
2012-12-15 08:47:02 +00:00
|
|
|
currentCursor = nullptr;
|
2012-12-14 15:32:53 +00:00
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
//* help = CSDL_Ext::newSurface(40,40);
|
2008-08-01 18:13:33 +00:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2012-12-14 15:32:53 +00:00
|
|
|
|
|
|
|
changeGraphic(ECursor::ADVENTURE, 0);
|
2007-08-21 13:48:18 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
|
2012-12-14 15:32:53 +00:00
|
|
|
void CCursorHandler::changeGraphic(ECursor::ECursorTypes type, int index)
|
2007-08-21 13:48:18 +00:00
|
|
|
{
|
2013-03-03 19:03:45 +00:00
|
|
|
const std::string cursorDefs[4] = { "CRADVNTR", "CRCOMBAT", "CRDEFLT", "CRSPELL" };
|
2012-12-14 15:32:53 +00:00
|
|
|
|
|
|
|
if (type != this->type)
|
|
|
|
{
|
|
|
|
BLOCK_CAPTURING; // not used here
|
|
|
|
|
|
|
|
this->type = type;
|
|
|
|
this->frame = index;
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
currentCursor = Gfx::CManager::getAnimation(cursorDefs[type]);
|
2012-12-14 15:32:53 +00:00
|
|
|
}
|
2013-03-03 19:03:45 +00:00
|
|
|
frame = index;
|
2007-08-21 13:48:18 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
|
2012-12-14 15:32:53 +00:00
|
|
|
void CCursorHandler::dragAndDropCursor(CAnimImage * object)
|
2009-11-28 19:55:40 +00:00
|
|
|
{
|
2013-03-03 19:03:45 +00:00
|
|
|
if (dndObject) delete dndObject;
|
2012-12-14 15:32:53 +00:00
|
|
|
|
|
|
|
dndObject = object;
|
2009-11-28 19:55:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
void CCursorHandler::cursorMove(int x, int y)
|
2007-08-21 13:48:18 +00:00
|
|
|
{
|
2008-08-01 18:13:33 +00:00
|
|
|
xpos = x;
|
|
|
|
ypos = y;
|
2007-08-21 13:48:18 +00:00
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
|
|
|
|
void CCursorHandler::drawWithScreenRestore()
|
2007-08-21 13:48:18 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
if(!showing) return;
|
2008-08-28 17:36:34 +00:00
|
|
|
int x = xpos, y = ypos;
|
2009-08-30 12:47:40 +00:00
|
|
|
shiftPos(x, y);
|
2011-06-11 04:54:41 +00:00
|
|
|
|
2012-12-14 15:32:53 +00:00
|
|
|
if (dndObject)
|
|
|
|
{
|
|
|
|
dndObject->moveTo(Point(x - dndObject->pos.w/2, y - dndObject->pos.h/2));
|
2013-03-03 19:03:45 +00:00
|
|
|
dndObject->showAll();
|
2012-12-14 15:32:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-03-03 19:03:45 +00:00
|
|
|
currentCursor->getFrame(frame)->putAt(Gfx::Point(x, y));
|
2011-06-11 04:54:41 +00:00
|
|
|
}
|
2007-08-21 13:48:18 +00:00
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
|
|
|
|
void CCursorHandler::drawRestored()
|
2007-08-21 13:48:18 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
if(!showing)
|
|
|
|
return;
|
|
|
|
|
2008-08-28 17:36:34 +00:00
|
|
|
int x = xpos, y = ypos;
|
2009-08-30 12:47:40 +00:00
|
|
|
shiftPos(x, y);
|
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
void CCursorHandler::draw()
|
2011-04-17 18:02:13 +00:00
|
|
|
{
|
2013-03-03 19:03:45 +00:00
|
|
|
currentCursor->getFrame(frame)->putAt(Gfx::Point(xpos, ypos));
|
2011-04-17 18:02:13 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
|
2009-08-30 12:47:40 +00:00
|
|
|
void CCursorHandler::shiftPos( int &x, int &y )
|
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
|
2008-08-28 17:36:34 +00:00
|
|
|
{
|
|
|
|
x-=16;
|
|
|
|
y-=16;
|
2009-08-30 12:47:40 +00:00
|
|
|
|
|
|
|
// Properly align the melee attack cursors.
|
2013-01-16 14:28:49 +00:00
|
|
|
if (type == ECursor::COMBAT)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
switch (frame)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
|
|
|
case 7: // Bottom left
|
|
|
|
x -= 6;
|
|
|
|
y += 16;
|
|
|
|
break;
|
|
|
|
case 8: // Left
|
|
|
|
x -= 16;
|
|
|
|
y += 10;
|
|
|
|
break;
|
|
|
|
case 9: // Top left
|
|
|
|
x -= 6;
|
|
|
|
y -= 6;
|
|
|
|
break;
|
|
|
|
case 10: // Top right
|
|
|
|
x += 16;
|
|
|
|
y -= 6;
|
|
|
|
break;
|
|
|
|
case 11: // Right
|
|
|
|
x += 16;
|
|
|
|
y += 11;
|
|
|
|
break;
|
|
|
|
case 12: // Bottom right
|
|
|
|
x += 16;
|
|
|
|
y += 16;
|
|
|
|
break;
|
|
|
|
case 13: // Below
|
|
|
|
x += 9;
|
|
|
|
y += 16;
|
|
|
|
break;
|
|
|
|
case 14: // Above
|
|
|
|
x += 9;
|
|
|
|
y -= 15;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-08-28 17:36:34 +00:00
|
|
|
}
|
2013-01-16 14:28:49 +00:00
|
|
|
else if(type == ECursor::ADVENTURE)
|
2008-08-28 17:36:34 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
if (frame == 0); //to exclude
|
|
|
|
else if(frame == 2)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
|
|
|
x -= 12;
|
|
|
|
y -= 10;
|
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
else if(frame == 3)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
|
|
|
x -= 12;
|
|
|
|
y -= 12;
|
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
else if(frame < 27)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
int hlpNum = (frame - 4)%6;
|
2009-08-30 12:47:40 +00:00
|
|
|
if(hlpNum == 0)
|
|
|
|
{
|
|
|
|
x -= 15;
|
|
|
|
y -= 13;
|
|
|
|
}
|
|
|
|
else if(hlpNum == 1)
|
|
|
|
{
|
|
|
|
x -= 13;
|
|
|
|
y -= 13;
|
|
|
|
}
|
|
|
|
else if(hlpNum == 2)
|
|
|
|
{
|
|
|
|
x -= 20;
|
|
|
|
y -= 20;
|
|
|
|
}
|
|
|
|
else if(hlpNum == 3)
|
|
|
|
{
|
|
|
|
x -= 13;
|
|
|
|
y -= 16;
|
|
|
|
}
|
|
|
|
else if(hlpNum == 4)
|
|
|
|
{
|
|
|
|
x -= 8;
|
|
|
|
y -= 9;
|
|
|
|
}
|
|
|
|
else if(hlpNum == 5)
|
|
|
|
{
|
|
|
|
x -= 14;
|
|
|
|
y -= 16;
|
|
|
|
}
|
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
else if(frame == 41)
|
2010-03-20 22:17:19 +00:00
|
|
|
{
|
|
|
|
x -= 14;
|
|
|
|
y -= 16;
|
|
|
|
}
|
2012-12-14 15:32:53 +00:00
|
|
|
else if(frame < 31 || frame == 42)
|
2009-08-30 12:47:40 +00:00
|
|
|
{
|
|
|
|
x -= 20;
|
|
|
|
y -= 20;
|
|
|
|
}
|
2008-08-28 17:36:34 +00:00
|
|
|
}
|
2009-10-25 14:36:11 +00:00
|
|
|
}
|
|
|
|
|
2013-03-03 19:03:45 +00:00
|
|
|
|
2011-04-05 17:38:24 +00:00
|
|
|
void CCursorHandler::centerCursor()
|
|
|
|
{
|
2013-03-03 19:03:45 +00:00
|
|
|
this->xpos = (GL2D::getScreenWidth() - currentCursor->getWidth()) / 2;
|
|
|
|
this->ypos = (GL2D::getScreenHeight() - currentCursor->getHeight()) / 2;
|
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
|
2011-04-05 17:38:24 +00:00
|
|
|
SDL_WarpMouse(this->xpos, this->ypos);
|
2011-04-26 13:30:29 +00:00
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
|
2011-04-05 17:38:24 +00:00
|
|
|
}
|
|
|
|
|
2009-10-25 14:36:11 +00:00
|
|
|
CCursorHandler::~CCursorHandler()
|
|
|
|
{
|
2012-12-14 15:32:53 +00:00
|
|
|
delete dndObject;
|
2009-10-25 14:36:11 +00:00
|
|
|
}
|