2017-07-13 10:26:03 +02: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
|
|
|
|
*
|
|
|
|
*/
|
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"
|
2017-06-11 08:01:41 +02: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
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
void CCursorHandler::clearBuffer()
|
|
|
|
{
|
|
|
|
Uint32 fillColor = SDL_MapRGBA(buffer->format, 0, 0, 0, 0);
|
|
|
|
CSDL_Ext::fillRect(buffer, nullptr, fillColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCursorHandler::updateBuffer(CIntObject * payload)
|
|
|
|
{
|
|
|
|
payload->moveTo(Point(0,0));
|
|
|
|
payload->showAll(buffer);
|
|
|
|
|
|
|
|
needUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCursorHandler::replaceBuffer(CIntObject * payload)
|
|
|
|
{
|
|
|
|
clearBuffer();
|
|
|
|
updateBuffer(payload);
|
|
|
|
}
|
|
|
|
|
2007-08-21 16:48:18 +03:00
|
|
|
void CCursorHandler::initCursor()
|
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
cursorLayer = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 40, 40);
|
|
|
|
SDL_SetTextureBlendMode(cursorLayer, SDL_BLENDMODE_BLEND);
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
xpos = ypos = 0;
|
|
|
|
type = ECursor::DEFAULT;
|
|
|
|
dndObject = nullptr;
|
2016-10-28 13:03:26 +02:00
|
|
|
|
|
|
|
cursors =
|
|
|
|
{
|
|
|
|
make_unique<CAnimImage>("CRADVNTR", 0),
|
|
|
|
make_unique<CAnimImage>("CRCOMBAT", 0),
|
|
|
|
make_unique<CAnimImage>("CRDEFLT", 0),
|
|
|
|
make_unique<CAnimImage>("CRSPELL", 0)
|
|
|
|
};
|
|
|
|
|
|
|
|
currentCursor = cursors.at(int(ECursor::DEFAULT)).get();
|
2012-12-14 18:32:53 +03:00
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
buffer = CSDL_Ext::newSurface(40,40);
|
|
|
|
|
|
|
|
SDL_SetSurfaceBlendMode(buffer, SDL_BLENDMODE_NONE);
|
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
|
|
|
{
|
2016-10-28 13:03:26 +02:00
|
|
|
if(type != this->type)
|
2012-12-14 18:32:53 +03:00
|
|
|
{
|
|
|
|
this->type = type;
|
|
|
|
this->frame = index;
|
2016-10-28 13:03:26 +02:00
|
|
|
currentCursor = cursors.at(int(type)).get();
|
|
|
|
currentCursor->setFrame(index);
|
2012-12-14 18:32:53 +03:00
|
|
|
}
|
2016-10-28 13:03:26 +02:00
|
|
|
else if(index != this->frame)
|
2012-12-14 18:32:53 +03:00
|
|
|
{
|
2016-10-28 13:03:26 +02:00
|
|
|
this->frame = index;
|
2012-12-14 18:32:53 +03:00
|
|
|
currentCursor->setFrame(index);
|
|
|
|
}
|
2018-07-25 00:36:48 +02:00
|
|
|
|
|
|
|
replaceBuffer(currentCursor);
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:39:16 +02:00
|
|
|
void CCursorHandler::dragAndDropCursor(std::unique_ptr<CAnimImage> object)
|
2009-11-28 21:55:40 +02:00
|
|
|
{
|
2016-10-28 12:39:16 +02:00
|
|
|
dndObject = std::move(object);
|
2018-07-25 00:36:48 +02:00
|
|
|
if(dndObject)
|
|
|
|
replaceBuffer(dndObject.get());
|
|
|
|
else
|
|
|
|
replaceBuffer(currentCursor);
|
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
|
|
|
|
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()
|
|
|
|
{
|
2020-10-01 10:38:06 +02:00
|
|
|
this->xpos = static_cast<int>((screen->w / 2.) - (currentCursor->pos.w / 2.));
|
|
|
|
this->ypos = static_cast<int>((screen->h / 2.) - (currentCursor->pos.h / 2.));
|
2012-12-14 18:32:53 +03:00
|
|
|
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()
|
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
if(!showing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//the must update texture in the main (renderer) thread, but changes to cursor type may come from other threads
|
|
|
|
updateTexture();
|
|
|
|
|
|
|
|
int x = xpos;
|
|
|
|
int y = ypos;
|
|
|
|
shiftPos(x, y);
|
|
|
|
|
|
|
|
if(dndObject)
|
|
|
|
{
|
|
|
|
x -= dndObject->pos.w/2;
|
|
|
|
y -= dndObject->pos.h/2;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Rect destRect;
|
|
|
|
destRect.x = x;
|
|
|
|
destRect.y = y;
|
|
|
|
destRect.w = 40;
|
|
|
|
destRect.h = 40;
|
|
|
|
|
|
|
|
SDL_RenderCopy(mainRenderer, cursorLayer, nullptr, &destRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCursorHandler::updateTexture()
|
|
|
|
{
|
|
|
|
if(needUpdate)
|
|
|
|
{
|
|
|
|
SDL_UpdateTexture(cursorLayer, nullptr, buffer->pixels, buffer->pitch);
|
|
|
|
needUpdate = false;
|
|
|
|
}
|
2014-07-03 14:10:01 +03:00
|
|
|
}
|
|
|
|
|
2018-07-25 00:36:48 +02:00
|
|
|
CCursorHandler::CCursorHandler()
|
|
|
|
: needUpdate(true),
|
|
|
|
buffer(nullptr),
|
|
|
|
cursorLayer(nullptr),
|
|
|
|
showing(false)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2016-10-28 13:03:26 +02:00
|
|
|
|
2009-10-25 16:36:11 +02:00
|
|
|
CCursorHandler::~CCursorHandler()
|
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
if(buffer)
|
|
|
|
SDL_FreeSurface(buffer);
|
|
|
|
|
|
|
|
if(cursorLayer)
|
|
|
|
SDL_DestroyTexture(cursorLayer);
|
2009-10-25 16:36:11 +02:00
|
|
|
}
|