1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-06-27 00:41:08 +02:00

Hardware cursor can now be hidden

This commit is contained in:
Ivan Savenko
2023-01-19 18:16:11 +02:00
parent 5484efde90
commit fd05036f3b
2 changed files with 68 additions and 32 deletions

View File

@ -4,39 +4,39 @@
* Authors: listed in file AUTHORS in main folder * Authors: listed in file AUTHORS in main folder
* *
* License: GNU General Public License v2.0 or later * License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder * Full text of license available in license.txt file, in main folder
* *
*/ */
#include "StdInc.h" #include "StdInc.h"
#include "CursorHandler.h" #include "CursorHandler.h"
#include <SDL.h> #include <SDL.h>
#include "SDL_Extensions.h" #include "SDL_Extensions.h"
#include "CGuiHandler.h" #include "CGuiHandler.h"
#include "CAnimation.h" #include "CAnimation.h"
#include "../../lib/CConfigHandler.h" #include "../../lib/CConfigHandler.h"
std::unique_ptr<ICursor> CursorHandler::createCursor() std::unique_ptr<ICursor> CursorHandler::createCursor()
{ {
if (settings["video"]["cursor"].String() == "auto") if (settings["video"]["cursor"].String() == "auto")
{ {
#if defined(VCMI_ANDROID) || defined(VCMI_IOS) #if defined(VCMI_ANDROID) || defined(VCMI_IOS)
return std::make_unique<CursorSoftware>(); return std::make_unique<CursorSoftware>();
#else #else
return std::make_unique<CursorHardware>(); return std::make_unique<CursorHardware>();
#endif #endif
} }
if (settings["video"]["cursor"].String() == "hardware") if (settings["video"]["cursor"].String() == "hardware")
return std::make_unique<CursorHardware>(); return std::make_unique<CursorHardware>();
assert(settings["video"]["cursor"].String() == "software"); assert(settings["video"]["cursor"].String() == "software");
return std::make_unique<CursorSoftware>(); return std::make_unique<CursorSoftware>();
} }
CursorHandler::CursorHandler() CursorHandler::CursorHandler()
: cursor(createCursor()) : cursor(createCursor())
, frameTime(0.f) , frameTime(0.f)
, showing(false) , showing(false)
@ -288,6 +288,24 @@ void CursorHandler::render()
cursor->render(); cursor->render();
} }
void CursorHandler::hide()
{
if (!showing)
return;
showing = false;
cursor->setVisible(false);
}
void CursorHandler::show()
{
if (showing)
return;
showing = true;
cursor->setVisible(true);
}
void CursorSoftware::render() void CursorSoftware::render()
{ {
//texture must be updated in the main (renderer) thread, but changes to cursor type may come from other threads //texture must be updated in the main (renderer) thread, but changes to cursor type may come from other threads
@ -348,10 +366,16 @@ void CursorSoftware::setCursorPosition( const Point & newPos )
pos = newPos; pos = newPos;
} }
void CursorSoftware::setVisible(bool on)
{
visible = on;
}
CursorSoftware::CursorSoftware(): CursorSoftware::CursorSoftware():
cursorTexture(nullptr), cursorTexture(nullptr),
cursorSurface(nullptr), cursorSurface(nullptr),
needUpdate(false), needUpdate(false),
visible(false),
pivot(0,0) pivot(0,0)
{ {
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
@ -364,12 +388,12 @@ CursorSoftware::~CursorSoftware()
if (cursorSurface) if (cursorSurface)
SDL_FreeSurface(cursorSurface); SDL_FreeSurface(cursorSurface);
} }
CursorHardware::CursorHardware(): CursorHardware::CursorHardware():
cursor(nullptr) cursor(nullptr)
{ {
SDL_ShowCursor(SDL_DISABLE);
} }
CursorHardware::~CursorHardware() CursorHardware::~CursorHardware()
@ -378,6 +402,14 @@ CursorHardware::~CursorHardware()
SDL_FreeCursor(cursor); SDL_FreeCursor(cursor);
} }
void CursorHardware::setVisible(bool on)
{
if (on)
SDL_ShowCursor(SDL_ENABLE);
else
SDL_ShowCursor(SDL_DISABLE);
}
void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
{ {
auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y); auto cursorSurface = CSDL_Ext::newSurface(image->dimensions().x, image->dimensions().y);

View File

@ -120,6 +120,7 @@ public:
virtual void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) = 0; virtual void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) = 0;
virtual void setCursorPosition( const Point & newPos ) = 0; virtual void setCursorPosition( const Point & newPos ) = 0;
virtual void render() = 0; virtual void render() = 0;
virtual void setVisible( bool on) = 0;
}; };
class CursorHardware : public ICursor class CursorHardware : public ICursor
@ -135,6 +136,7 @@ public:
void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) override; void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) override;
void setCursorPosition( const Point & newPos ) override; void setCursorPosition( const Point & newPos ) override;
void render() override; void render() override;
void setVisible( bool on) override;
}; };
class CursorSoftware : public ICursor class CursorSoftware : public ICursor
@ -147,6 +149,7 @@ class CursorSoftware : public ICursor
Point pos; Point pos;
Point pivot; Point pivot;
bool needUpdate; bool needUpdate;
bool visible;
void createTexture(const Point & dimensions); void createTexture(const Point & dimensions);
void updateTexture(); void updateTexture();
@ -157,6 +160,7 @@ public:
void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) override; void setImage(std::shared_ptr<IImage> image, const Point & pivotOffset) override;
void setCursorPosition( const Point & newPos ) override; void setCursorPosition( const Point & newPos ) override;
void render() override; void render() override;
void setVisible( bool on) override;
}; };
/// handles mouse cursor /// handles mouse cursor
@ -222,8 +226,8 @@ public:
void render(); void render();
void hide() { showing=false; }; void hide();
void show() { showing=true; }; void show();
/// change cursor's positions to (x, y) /// change cursor's positions to (x, y)
void cursorMove(const int & x, const int & y); void cursorMove(const int & x, const int & y);