2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2007-08-21 16:48:18 +03:00
|
|
|
#include "CCursorHandler.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
#include <SDL.h>
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2008-08-01 21:13:33 +03:00
|
|
|
#include "SDL_Extensions.h"
|
2012-12-15 12:04:55 +03:00
|
|
|
#include "CGuiHandler.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "widgets/Images.h"
|
2014-07-13 20:53:37 +03:00
|
|
|
|
2013-04-04 17:58:54 +03:00
|
|
|
#include "../CMT.h"
|
2007-08-21 16:48:18 +03:00
|
|
|
|
2009-04-15 17:03:31 +03: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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-08-21 16:48:18 +03:00
|
|
|
void CCursorHandler::initCursor()
|
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
xpos = ypos = 0;
|
|
|
|
type = ECursor::DEFAULT;
|
|
|
|
dndObject = nullptr;
|
2012-12-15 11:47:02 +03:00
|
|
|
currentCursor = nullptr;
|
2012-12-14 18:32:53 +03:00
|
|
|
|
2009-08-30 15:47:40 +03:00
|
|
|
help = CSDL_Ext::newSurface(40,40);
|
2014-07-03 17:05:07 +03:00
|
|
|
#ifndef VCMI_SDL1
|
|
|
|
//No blending. Ensure, that we are copying pixels during "screen restore draw"
|
|
|
|
SDL_SetSurfaceBlendMode(help,SDL_BLENDMODE_NONE);
|
|
|
|
#endif // VCMI_SDL1
|
2008-08-01 21:13:33 +03:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2012-12-14 18:32:53 +03:00
|
|
|
|
|
|
|
changeGraphic(ECursor::ADVENTURE, 0);
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
void CCursorHandler::changeGraphic(ECursor::ECursorTypes type, int index)
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
std::string cursorDefs[4] = { "CRADVNTR.DEF", "CRCOMBAT.DEF", "CRDEFLT.DEF", "CRSPELL.DEF" };
|
|
|
|
|
|
|
|
if (type != this->type)
|
|
|
|
{
|
|
|
|
BLOCK_CAPTURING; // not used here
|
|
|
|
|
|
|
|
this->type = type;
|
|
|
|
this->frame = index;
|
|
|
|
|
|
|
|
delete currentCursor;
|
|
|
|
currentCursor = new CAnimImage(cursorDefs[int(type)], index);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame != index)
|
|
|
|
{
|
|
|
|
frame = index;
|
|
|
|
currentCursor->setFrame(index);
|
|
|
|
}
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
void CCursorHandler::dragAndDropCursor(CAnimImage * object)
|
2009-11-28 21:55:40 +02:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
if (dndObject)
|
|
|
|
delete dndObject;
|
|
|
|
|
|
|
|
dndObject = object;
|
2009-11-28 21:55:40 +02:00
|
|
|
}
|
|
|
|
|
2008-09-25 17:09:31 +03:00
|
|
|
void CCursorHandler::cursorMove(const int & x, const int & y)
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2008-08-01 21:13:33 +03:00
|
|
|
xpos = x;
|
|
|
|
ypos = y;
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
|
|
|
|
void CCursorHandler::drawWithScreenRestore()
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
if(!showing) return;
|
2008-08-28 20:36:34 +03:00
|
|
|
int x = xpos, y = ypos;
|
2009-08-30 15:47:40 +03:00
|
|
|
shiftPos(x, y);
|
2011-06-11 07:54:41 +03:00
|
|
|
|
|
|
|
SDL_Rect temp_rect1 = genRect(40,40,x,y);
|
|
|
|
SDL_Rect temp_rect2 = genRect(40,40,0,0);
|
|
|
|
SDL_BlitSurface(screen, &temp_rect1, help, &temp_rect2);
|
2012-12-14 18:32:53 +03:00
|
|
|
|
|
|
|
if (dndObject)
|
|
|
|
{
|
|
|
|
dndObject->moveTo(Point(x - dndObject->pos.w/2, y - dndObject->pos.h/2));
|
|
|
|
dndObject->showAll(screen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentCursor->moveTo(Point(x,y));
|
|
|
|
currentCursor->showAll(screen);
|
2011-06-11 07:54:41 +03:00
|
|
|
}
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
|
|
|
|
void CCursorHandler::drawRestored()
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
if(!showing)
|
|
|
|
return;
|
|
|
|
|
2008-08-28 20:36:34 +03:00
|
|
|
int x = xpos, y = ypos;
|
2009-08-30 15:47:40 +03:00
|
|
|
shiftPos(x, y);
|
2011-06-11 07:54:41 +03:00
|
|
|
|
|
|
|
SDL_Rect temp_rect = genRect(40, 40, x, y);
|
2013-06-26 14:18:27 +03:00
|
|
|
SDL_BlitSurface(help, nullptr, screen, &temp_rect);
|
2010-08-23 18:16:40 +03:00
|
|
|
//blitAt(help,x,y);
|
2009-08-30 15:47:40 +03:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:02:13 +03:00
|
|
|
void CCursorHandler::draw(SDL_Surface *to)
|
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
currentCursor->moveTo(Point(xpos, ypos));
|
|
|
|
currentCursor->showAll(screen);
|
2011-04-17 21:02:13 +03:00
|
|
|
}
|
|
|
|
|
2009-08-30 15:47:40 +03:00
|
|
|
void CCursorHandler::shiftPos( int &x, int &y )
|
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
|
|
|
x-=16;
|
|
|
|
y-=16;
|
2009-08-30 15:47:40 +03:00
|
|
|
|
|
|
|
// Properly align the melee attack cursors.
|
2013-01-16 17:28:49 +03:00
|
|
|
if (type == ECursor::COMBAT)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
switch (frame)
|
2009-08-30 15:47:40 +03: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 20:36:34 +03:00
|
|
|
}
|
2013-01-16 17:28:49 +03:00
|
|
|
else if(type == ECursor::ADVENTURE)
|
2008-08-28 20:36:34 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
if (frame == 0); //to exclude
|
|
|
|
else if(frame == 2)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
|
|
|
x -= 12;
|
|
|
|
y -= 10;
|
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
else if(frame == 3)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
|
|
|
x -= 12;
|
|
|
|
y -= 12;
|
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
else if(frame < 27)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
int hlpNum = (frame - 4)%6;
|
2009-08-30 15:47:40 +03: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 18:32:53 +03:00
|
|
|
else if(frame == 41)
|
2010-03-21 00:17:19 +02:00
|
|
|
{
|
|
|
|
x -= 14;
|
|
|
|
y -= 16;
|
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
else if(frame < 31 || frame == 42)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
|
|
|
x -= 20;
|
|
|
|
y -= 20;
|
|
|
|
}
|
2008-08-28 20:36:34 +03:00
|
|
|
}
|
2009-10-25 16:36:11 +02:00
|
|
|
}
|
|
|
|
|
2011-04-05 20:38:24 +03:00
|
|
|
void CCursorHandler::centerCursor()
|
|
|
|
{
|
2012-12-14 18:32:53 +03:00
|
|
|
this->xpos = (screen->w / 2.) - (currentCursor->pos.w / 2.);
|
|
|
|
this->ypos = (screen->h / 2.) - (currentCursor->pos.h / 2.);
|
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
|
2011-04-05 20:38:24 +03:00
|
|
|
SDL_WarpMouse(this->xpos, this->ypos);
|
2011-04-26 16:30:29 +03:00
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
|
2011-04-05 20:38:24 +03:00
|
|
|
}
|
|
|
|
|
2014-07-03 14:10:01 +03:00
|
|
|
void CCursorHandler::render()
|
|
|
|
{
|
|
|
|
drawWithScreenRestore();
|
|
|
|
CSDL_Ext::update(screen);
|
|
|
|
drawRestored();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-25 16:36:11 +02:00
|
|
|
CCursorHandler::~CCursorHandler()
|
|
|
|
{
|
|
|
|
if(help)
|
|
|
|
SDL_FreeSurface(help);
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
delete currentCursor;
|
|
|
|
delete dndObject;
|
2009-10-25 16:36:11 +02:00
|
|
|
}
|