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
|
2023-01-19 18:16:11 +02:00
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "CursorHandler.h"
|
|
|
|
|
2008-08-01 21:13:33 +03:00
|
|
|
#include "SDL_Extensions.h"
|
2012-12-15 12:04:55 +03:00
|
|
|
#include "CGuiHandler.h"
|
2023-01-19 18:16:11 +02:00
|
|
|
#include "CAnimation.h"
|
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
|
2023-01-21 15:07:35 +02:00
|
|
|
#ifdef VCMI_APPLE
|
|
|
|
#include <dispatch/dispatch.h>
|
|
|
|
#endif
|
|
|
|
|
2023-01-19 18:16:11 +02:00
|
|
|
std::unique_ptr<ICursor> CursorHandler::createCursor()
|
|
|
|
{
|
|
|
|
if (settings["video"]["cursor"].String() == "auto")
|
|
|
|
{
|
|
|
|
#if defined(VCMI_ANDROID) || defined(VCMI_IOS)
|
|
|
|
return std::make_unique<CursorSoftware>();
|
|
|
|
#else
|
|
|
|
return std::make_unique<CursorHardware>();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings["video"]["cursor"].String() == "hardware")
|
|
|
|
return std::make_unique<CursorHardware>();
|
|
|
|
|
|
|
|
assert(settings["video"]["cursor"].String() == "software");
|
|
|
|
return std::make_unique<CursorSoftware>();
|
|
|
|
}
|
|
|
|
|
|
|
|
CursorHandler::CursorHandler()
|
2023-01-05 22:38:52 +02:00
|
|
|
: cursor(createCursor())
|
2022-12-18 22:32:07 +02:00
|
|
|
, frameTime(0.f)
|
|
|
|
, showing(false)
|
2023-01-05 19:03:27 +02:00
|
|
|
, pos(0,0)
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2018-07-25 00:36:48 +02:00
|
|
|
|
2022-12-18 22:32:07 +02:00
|
|
|
type = Cursor::Type::DEFAULT;
|
2012-12-14 18:32:53 +03:00
|
|
|
dndObject = nullptr;
|
2016-10-28 13:03:26 +02:00
|
|
|
|
|
|
|
cursors =
|
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
std::make_unique<CAnimation>("CRADVNTR"),
|
|
|
|
std::make_unique<CAnimation>("CRCOMBAT"),
|
|
|
|
std::make_unique<CAnimation>("CRDEFLT"),
|
|
|
|
std::make_unique<CAnimation>("CRSPELL")
|
2016-10-28 13:03:26 +02:00
|
|
|
};
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
for (auto & cursor : cursors)
|
|
|
|
cursor->preload();
|
2012-12-14 18:32:53 +03:00
|
|
|
|
2022-12-18 22:32:07 +02:00
|
|
|
set(Cursor::Map::POINTER);
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::position() const
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
2023-01-05 19:03:27 +02:00
|
|
|
return pos;
|
2022-12-18 22:32:07 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::changeGraphic(Cursor::Type type, size_t index)
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2022-12-18 14:34:38 +02:00
|
|
|
assert(dndObject == nullptr);
|
|
|
|
|
2023-01-05 22:13:24 +02:00
|
|
|
if (type == this->type && index == this->frame)
|
|
|
|
return;
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
this->type = type;
|
|
|
|
this->frame = index;
|
2018-07-25 00:36:48 +02:00
|
|
|
|
2023-01-05 22:38:52 +02:00
|
|
|
cursor->setImage(getCurrentImage(), getPivotOffset());
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::set(Cursor::Default index)
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
|
|
|
changeGraphic(Cursor::Type::DEFAULT, static_cast<size_t>(index));
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::set(Cursor::Map index)
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
|
|
|
changeGraphic(Cursor::Type::ADVENTURE, static_cast<size_t>(index));
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::set(Cursor::Combat index)
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
|
|
|
changeGraphic(Cursor::Type::COMBAT, static_cast<size_t>(index));
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::set(Cursor::Spellcast index)
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
|
|
|
//Note: this is animated cursor, ignore specified frame and only change type
|
|
|
|
changeGraphic(Cursor::Type::SPELLBOOK, frame);
|
|
|
|
}
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
void CursorHandler::dragAndDropCursor(std::shared_ptr<IImage> image)
|
|
|
|
{
|
|
|
|
dndObject = image;
|
2023-01-05 22:38:52 +02:00
|
|
|
cursor->setImage(getCurrentImage(), getPivotOffset());
|
2023-01-05 21:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorHandler::dragAndDropCursor (std::string path, size_t index)
|
2009-11-28 21:55:40 +02:00
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
CAnimation anim(path);
|
|
|
|
anim.load(index);
|
|
|
|
dragAndDropCursor(anim.getImage(index));
|
2009-11-28 21:55:40 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::cursorMove(const int & x, const int & y)
|
2007-08-21 16:48:18 +03:00
|
|
|
{
|
2023-01-05 19:03:27 +02:00
|
|
|
pos.x = x;
|
|
|
|
pos.y = y;
|
2023-01-05 21:30:54 +02:00
|
|
|
|
2023-01-05 22:38:52 +02:00
|
|
|
cursor->setCursorPosition(pos);
|
2007-08-21 16:48:18 +03:00
|
|
|
}
|
2012-12-14 18:32:53 +03:00
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::getPivotOffsetDefault(size_t index)
|
2009-08-30 15:47:40 +03:00
|
|
|
{
|
2023-01-05 19:03:27 +02:00
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::getPivotOffsetMap(size_t index)
|
2023-01-05 19:03:27 +02:00
|
|
|
{
|
|
|
|
static const std::array<Point, 43> offsets = {{
|
|
|
|
{ 0, 0}, // POINTER = 0,
|
|
|
|
{ 0, 0}, // HOURGLASS = 1,
|
|
|
|
{ 12, 10}, // HERO = 2,
|
|
|
|
{ 12, 12}, // TOWN = 3,
|
|
|
|
|
|
|
|
{ 15, 13}, // T1_MOVE = 4,
|
|
|
|
{ 13, 13}, // T1_ATTACK = 5,
|
2023-01-11 13:41:44 +02:00
|
|
|
{ 16, 32}, // T1_SAIL = 6,
|
|
|
|
{ 13, 20}, // T1_DISEMBARK = 7,
|
2023-01-05 19:03:27 +02:00
|
|
|
{ 8, 9}, // T1_EXCHANGE = 8,
|
|
|
|
{ 14, 16}, // T1_VISIT = 9,
|
|
|
|
|
|
|
|
{ 15, 13}, // T2_MOVE = 10,
|
|
|
|
{ 13, 13}, // T2_ATTACK = 11,
|
2023-01-11 13:41:44 +02:00
|
|
|
{ 16, 32}, // T2_SAIL = 12,
|
|
|
|
{ 13, 20}, // T2_DISEMBARK = 13,
|
2023-01-05 19:03:27 +02:00
|
|
|
{ 8, 9}, // T2_EXCHANGE = 14,
|
|
|
|
{ 14, 16}, // T2_VISIT = 15,
|
|
|
|
|
|
|
|
{ 15, 13}, // T3_MOVE = 16,
|
|
|
|
{ 13, 13}, // T3_ATTACK = 17,
|
2023-01-11 13:41:44 +02:00
|
|
|
{ 16, 32}, // T3_SAIL = 18,
|
|
|
|
{ 13, 20}, // T3_DISEMBARK = 19,
|
2023-01-05 19:03:27 +02:00
|
|
|
{ 8, 9}, // T3_EXCHANGE = 20,
|
|
|
|
{ 14, 16}, // T3_VISIT = 21,
|
|
|
|
|
|
|
|
{ 15, 13}, // T4_MOVE = 22,
|
|
|
|
{ 13, 13}, // T4_ATTACK = 23,
|
2023-01-11 13:41:44 +02:00
|
|
|
{ 16, 32}, // T4_SAIL = 24,
|
|
|
|
{ 13, 20}, // T4_DISEMBARK = 25,
|
2023-01-05 19:03:27 +02:00
|
|
|
{ 8, 9}, // T4_EXCHANGE = 26,
|
|
|
|
{ 14, 16}, // T4_VISIT = 27,
|
|
|
|
|
2023-01-11 13:41:44 +02:00
|
|
|
{ 16, 32}, // T1_SAIL_VISIT = 28,
|
|
|
|
{ 16, 32}, // T2_SAIL_VISIT = 29,
|
|
|
|
{ 16, 32}, // T3_SAIL_VISIT = 30,
|
|
|
|
{ 16, 32}, // T4_SAIL_VISIT = 31,
|
2023-01-05 19:03:27 +02:00
|
|
|
|
|
|
|
{ 6, 1}, // SCROLL_NORTH = 32,
|
|
|
|
{ 16, 2}, // SCROLL_NORTHEAST = 33,
|
|
|
|
{ 21, 6}, // SCROLL_EAST = 34,
|
|
|
|
{ 16, 16}, // SCROLL_SOUTHEAST = 35,
|
|
|
|
{ 6, 21}, // SCROLL_SOUTH = 36,
|
|
|
|
{ 1, 16}, // SCROLL_SOUTHWEST = 37,
|
|
|
|
{ 1, 5}, // SCROLL_WEST = 38,
|
|
|
|
{ 2, 1}, // SCROLL_NORTHWEST = 39,
|
|
|
|
|
|
|
|
{ 0, 0}, // POINTER_COPY = 40,
|
|
|
|
{ 14, 16}, // TELEPORT = 41,
|
|
|
|
{ 20, 20}, // SCUTTLE_BOAT = 42
|
|
|
|
}};
|
|
|
|
|
2023-01-05 23:14:52 +02:00
|
|
|
assert(offsets.size() == size_t(Cursor::Map::COUNT)); //Invalid number of pivot offsets for cursor
|
2023-01-05 19:03:27 +02:00
|
|
|
assert(index < offsets.size());
|
|
|
|
return offsets[index];
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::getPivotOffsetCombat(size_t index)
|
2023-01-05 19:03:27 +02:00
|
|
|
{
|
|
|
|
static const std::array<Point, 20> offsets = {{
|
|
|
|
{ 12, 12 }, // BLOCKED = 0,
|
|
|
|
{ 10, 14 }, // MOVE = 1,
|
|
|
|
{ 14, 14 }, // FLY = 2,
|
|
|
|
{ 12, 12 }, // SHOOT = 3,
|
|
|
|
{ 12, 12 }, // HERO = 4,
|
|
|
|
{ 8, 12 }, // QUERY = 5,
|
|
|
|
{ 0, 0 }, // POINTER = 6,
|
|
|
|
{ 21, 0 }, // HIT_NORTHEAST = 7,
|
|
|
|
{ 31, 5 }, // HIT_EAST = 8,
|
|
|
|
{ 21, 21 }, // HIT_SOUTHEAST = 9,
|
|
|
|
{ 0, 21 }, // HIT_SOUTHWEST = 10,
|
|
|
|
{ 0, 5 }, // HIT_WEST = 11,
|
|
|
|
{ 0, 0 }, // HIT_NORTHWEST = 12,
|
|
|
|
{ 6, 0 }, // HIT_NORTH = 13,
|
|
|
|
{ 6, 31 }, // HIT_SOUTH = 14,
|
|
|
|
{ 14, 0 }, // SHOOT_PENALTY = 15,
|
|
|
|
{ 12, 12 }, // SHOOT_CATAPULT = 16,
|
|
|
|
{ 12, 12 }, // HEAL = 17,
|
|
|
|
{ 12, 12 }, // SACRIFICE = 18,
|
|
|
|
{ 14, 20 }, // TELEPORT = 19
|
|
|
|
}};
|
|
|
|
|
2023-01-05 23:14:52 +02:00
|
|
|
assert(offsets.size() == size_t(Cursor::Combat::COUNT)); //Invalid number of pivot offsets for cursor
|
2023-01-05 19:03:27 +02:00
|
|
|
assert(index < offsets.size());
|
|
|
|
return offsets[index];
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::getPivotOffsetSpellcast()
|
2023-01-05 19:03:27 +02:00
|
|
|
{
|
|
|
|
return { 18, 28};
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
Point CursorHandler::getPivotOffset()
|
2023-01-05 19:03:27 +02:00
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
if (dndObject)
|
2023-01-05 22:38:52 +02:00
|
|
|
return dndObject->dimensions() / 2;
|
2023-01-05 21:30:54 +02:00
|
|
|
|
2023-01-05 19:03:27 +02:00
|
|
|
switch (type) {
|
|
|
|
case Cursor::Type::ADVENTURE: return getPivotOffsetMap(frame);
|
|
|
|
case Cursor::Type::COMBAT: return getPivotOffsetCombat(frame);
|
|
|
|
case Cursor::Type::DEFAULT: return getPivotOffsetDefault(frame);
|
|
|
|
case Cursor::Type::SPELLBOOK: return getPivotOffsetSpellcast();
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return {0, 0};
|
2009-10-25 16:36:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
std::shared_ptr<IImage> CursorHandler::getCurrentImage()
|
|
|
|
{
|
|
|
|
if (dndObject)
|
|
|
|
return dndObject;
|
|
|
|
|
|
|
|
return cursors[static_cast<size_t>(type)]->getImage(frame);
|
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::centerCursor()
|
2011-04-05 20:38:24 +03:00
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
Point screenSize {screen->w, screen->h};
|
|
|
|
pos = screenSize / 2 - getPivotOffset();
|
|
|
|
|
2012-12-14 18:32:53 +03:00
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
|
2023-01-17 22:01:35 +02:00
|
|
|
CSDL_Ext::warpMouse(pos.x, pos.y);
|
2011-04-26 16:30:29 +03:00
|
|
|
SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
|
2023-01-05 21:30:54 +02:00
|
|
|
|
2023-01-05 22:38:52 +02:00
|
|
|
cursor->setCursorPosition(pos);
|
2011-04-05 20:38:24 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::updateSpellcastCursor()
|
2014-07-03 14:10:01 +03:00
|
|
|
{
|
2023-01-27 15:01:43 +02:00
|
|
|
static const float frameDisplayDuration = 5.f / 60.f; // H3 uses ~83 ms per image = 5 / 60 second
|
2018-07-25 00:36:48 +02:00
|
|
|
|
2023-01-05 19:03:27 +02:00
|
|
|
frameTime += GH.mainFPSmng->getElapsedMilliseconds() / 1000.f;
|
|
|
|
size_t newFrame = frame;
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
while (frameTime >= frameDisplayDuration)
|
2022-12-18 22:32:07 +02:00
|
|
|
{
|
2023-01-05 19:03:27 +02:00
|
|
|
frameTime -= frameDisplayDuration;
|
|
|
|
newFrame++;
|
|
|
|
}
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-05 19:03:27 +02:00
|
|
|
auto & animation = cursors.at(static_cast<size_t>(type));
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
while (newFrame >= animation->size())
|
2023-01-05 19:03:27 +02:00
|
|
|
newFrame -= animation->size();
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-05 19:03:27 +02:00
|
|
|
changeGraphic(Cursor::Type::SPELLBOOK, newFrame);
|
|
|
|
}
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-05 19:34:37 +02:00
|
|
|
void CursorHandler::render()
|
2023-01-05 19:03:27 +02:00
|
|
|
{
|
|
|
|
if(!showing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (type == Cursor::Type::SPELLBOOK)
|
|
|
|
updateSpellcastCursor();
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-05 22:38:52 +02:00
|
|
|
cursor->render();
|
2023-01-05 21:30:54 +02:00
|
|
|
}
|
2022-12-18 22:32:07 +02:00
|
|
|
|
2023-01-19 18:16:11 +02:00
|
|
|
void CursorHandler::hide()
|
|
|
|
{
|
|
|
|
if (!showing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
showing = false;
|
|
|
|
cursor->setVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorHandler::show()
|
|
|
|
{
|
|
|
|
if (showing)
|
|
|
|
return;
|
|
|
|
|
|
|
|
showing = true;
|
|
|
|
cursor->setVisible(true);
|
|
|
|
}
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
void CursorSoftware::render()
|
|
|
|
{
|
|
|
|
//texture must be updated in the main (renderer) thread, but changes to cursor type may come from other threads
|
|
|
|
if (needUpdate)
|
|
|
|
updateTexture();
|
2018-07-25 00:36:48 +02:00
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
Point renderPos = pos - pivot;
|
2018-07-25 00:36:48 +02:00
|
|
|
|
|
|
|
SDL_Rect destRect;
|
2023-01-05 19:03:27 +02:00
|
|
|
destRect.x = renderPos.x;
|
|
|
|
destRect.y = renderPos.y;
|
2018-07-25 00:36:48 +02:00
|
|
|
destRect.w = 40;
|
|
|
|
destRect.h = 40;
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
SDL_RenderCopy(mainRenderer, cursorTexture, nullptr, &destRect);
|
2018-07-25 00:36:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
void CursorSoftware::createTexture(const Point & dimensions)
|
2018-07-25 00:36:48 +02:00
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
if(cursorTexture)
|
|
|
|
SDL_DestroyTexture(cursorTexture);
|
|
|
|
|
|
|
|
if (cursorSurface)
|
|
|
|
SDL_FreeSurface(cursorSurface);
|
|
|
|
|
|
|
|
cursorSurface = CSDL_Ext::newSurface(dimensions.x, dimensions.y);
|
|
|
|
cursorTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, dimensions.x, dimensions.y);
|
|
|
|
|
|
|
|
SDL_SetSurfaceBlendMode(cursorSurface, SDL_BLENDMODE_NONE);
|
|
|
|
SDL_SetTextureBlendMode(cursorTexture, SDL_BLENDMODE_BLEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorSoftware::updateTexture()
|
|
|
|
{
|
|
|
|
Point dimensions(-1, -1);
|
|
|
|
|
|
|
|
if (!cursorSurface || Point(cursorSurface->w, cursorSurface->h) != cursorImage->dimensions())
|
|
|
|
createTexture(cursorImage->dimensions());
|
|
|
|
|
2023-01-18 16:54:53 +02:00
|
|
|
CSDL_Ext::fillSurface(cursorSurface, Colors::TRANSPARENCY);
|
2023-01-05 21:30:54 +02:00
|
|
|
|
|
|
|
cursorImage->draw(cursorSurface);
|
|
|
|
SDL_UpdateTexture(cursorTexture, NULL, cursorSurface->pixels, cursorSurface->pitch);
|
|
|
|
needUpdate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorSoftware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
|
|
|
|
{
|
|
|
|
assert(image != nullptr);
|
|
|
|
cursorImage = image;
|
|
|
|
pivot = pivotOffset;
|
|
|
|
needUpdate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorSoftware::setCursorPosition( const Point & newPos )
|
|
|
|
{
|
|
|
|
pos = newPos;
|
2014-07-03 14:10:01 +03:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:16:11 +02:00
|
|
|
void CursorSoftware::setVisible(bool on)
|
|
|
|
{
|
|
|
|
visible = on;
|
|
|
|
}
|
|
|
|
|
2023-01-05 21:30:54 +02:00
|
|
|
CursorSoftware::CursorSoftware():
|
|
|
|
cursorTexture(nullptr),
|
|
|
|
cursorSurface(nullptr),
|
|
|
|
needUpdate(false),
|
2023-01-19 18:16:11 +02:00
|
|
|
visible(false),
|
2023-01-05 21:30:54 +02:00
|
|
|
pivot(0,0)
|
2009-10-25 16:36:11 +02:00
|
|
|
{
|
2023-01-05 21:30:54 +02:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
CursorSoftware::~CursorSoftware()
|
|
|
|
{
|
|
|
|
if(cursorTexture)
|
|
|
|
SDL_DestroyTexture(cursorTexture);
|
|
|
|
|
|
|
|
if (cursorSurface)
|
|
|
|
SDL_FreeSurface(cursorSurface);
|
2009-10-25 16:36:11 +02:00
|
|
|
}
|
2023-01-05 22:13:24 +02:00
|
|
|
|
|
|
|
CursorHardware::CursorHardware():
|
|
|
|
cursor(nullptr)
|
|
|
|
{
|
2023-01-19 18:16:11 +02:00
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2023-01-05 22:13:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CursorHardware::~CursorHardware()
|
|
|
|
{
|
|
|
|
if(cursor)
|
|
|
|
SDL_FreeCursor(cursor);
|
|
|
|
}
|
|
|
|
|
2023-01-19 18:16:11 +02:00
|
|
|
void CursorHardware::setVisible(bool on)
|
|
|
|
{
|
2023-01-21 15:07:35 +02:00
|
|
|
#ifdef VCMI_APPLE
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
#endif
|
2023-01-19 18:16:11 +02:00
|
|
|
if (on)
|
|
|
|
SDL_ShowCursor(SDL_ENABLE);
|
|
|
|
else
|
|
|
|
SDL_ShowCursor(SDL_DISABLE);
|
2023-01-21 15:07:35 +02:00
|
|
|
#ifdef VCMI_APPLE
|
|
|
|
});
|
|
|
|
#endif
|
2023-01-19 18:16:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 22:13:24 +02:00
|
|
|
void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
|
|
|
|
{
|
|
|
|
auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);
|
|
|
|
|
2023-01-18 16:54:53 +02:00
|
|
|
CSDL_Ext::fillSurface(cursorSurface, Colors::TRANSPARENCY);
|
2023-01-05 23:24:49 +02:00
|
|
|
|
2023-01-05 22:13:24 +02:00
|
|
|
image->draw(cursorSurface);
|
|
|
|
|
2023-01-05 22:38:52 +02:00
|
|
|
auto oldCursor = cursor;
|
2023-01-05 22:13:24 +02:00
|
|
|
cursor = SDL_CreateColorCursor(cursorSurface, pivotOffset.x, pivotOffset.y);
|
|
|
|
|
|
|
|
if (!cursor)
|
|
|
|
logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
|
|
|
|
|
|
|
|
SDL_FreeSurface(cursorSurface);
|
2023-01-21 15:07:35 +02:00
|
|
|
#ifdef VCMI_APPLE
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
#endif
|
2023-01-05 22:13:24 +02:00
|
|
|
SDL_SetCursor(cursor);
|
2023-01-05 22:38:52 +02:00
|
|
|
|
|
|
|
if (oldCursor)
|
|
|
|
SDL_FreeCursor(oldCursor);
|
2023-01-21 15:07:35 +02:00
|
|
|
#ifdef VCMI_APPLE
|
|
|
|
});
|
|
|
|
#endif
|
2023-01-05 22:13:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorHardware::setCursorPosition( const Point & newPos )
|
|
|
|
{
|
|
|
|
//no-op
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorHardware::render()
|
|
|
|
{
|
|
|
|
//no-op
|
|
|
|
}
|