mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-21 05:53:02 +01:00
Add cursor scaling
This commit is contained in:
committed by
Michael Steenbeek
parent
2b2e617086
commit
0aa515355a
@@ -14,41 +14,27 @@
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <openrct2/common.h>
|
||||
#include <SDL.h>
|
||||
#include <openrct2/config/Config.h>
|
||||
#include <openrct2/core/Guard.hpp>
|
||||
#include <openrct2/interface/Cursors.h>
|
||||
|
||||
#include "CursorRepository.h"
|
||||
|
||||
using namespace OpenRCT2::Ui;
|
||||
|
||||
CursorRepository::~CursorRepository()
|
||||
{
|
||||
for (size_t i = 0; i < CURSOR_COUNT; i++)
|
||||
{
|
||||
SDL_FreeCursor(_loadedCursors[i]);
|
||||
_loadedCursors[i] = nullptr;
|
||||
}
|
||||
_scaledCursors.clear();
|
||||
_currentCursor = CURSOR_UNDEFINED;
|
||||
_currentCursorScale = 1;
|
||||
}
|
||||
|
||||
void CursorRepository::LoadCursors()
|
||||
{
|
||||
// Using system cursors
|
||||
_loadedCursors[CURSOR_ARROW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||||
_loadedCursors[CURSOR_HAND_POINT] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
||||
|
||||
// Using custom cursors
|
||||
for (size_t i = 0; i < CURSOR_COUNT; i++)
|
||||
{
|
||||
const CursorData * cursorData = GetCursorData((CURSOR_ID)i);
|
||||
if (cursorData != nullptr)
|
||||
{
|
||||
_loadedCursors[i] = Create(cursorData);
|
||||
}
|
||||
}
|
||||
|
||||
_currentCursor = CURSOR_UNDEFINED;
|
||||
SetCursorScale(static_cast<uint8>(round(gConfigGeneral.window_scale)));
|
||||
SetCurrentCursor(CURSOR_ARROW);
|
||||
}
|
||||
|
||||
@@ -61,20 +47,104 @@ void CursorRepository::SetCurrentCursor(CURSOR_ID cursorId)
|
||||
{
|
||||
if (_currentCursor != cursorId)
|
||||
{
|
||||
SDL_Cursor * cursor = _loadedCursors[cursorId];
|
||||
SDL_Cursor * cursor = _scaledCursors.at(_currentCursorScale).getScaledCursor(cursorId);
|
||||
SDL_SetCursor(cursor);
|
||||
_currentCursor = cursorId;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Cursor * CursorRepository::Create(const CursorData * cursorInfo)
|
||||
static bool getBit(const uint8 * data, size_t x, size_t y, size_t width)
|
||||
{
|
||||
SDL_Cursor * cursor = SDL_CreateCursor(
|
||||
cursorInfo->Data,
|
||||
cursorInfo->Mask,
|
||||
CURSOR_WIDTH,
|
||||
CURSOR_HEIGHT,
|
||||
cursorInfo->HotSpot.X,
|
||||
cursorInfo->HotSpot.Y);
|
||||
size_t position = y * width + x;
|
||||
return (data[position / 8] & (1 << (7 - (x % 8)))) != 0;
|
||||
}
|
||||
|
||||
static void setBit(uint8 * data, size_t x, size_t y, size_t width)
|
||||
{
|
||||
size_t position = y * width + x;
|
||||
data[position / 8] |= (1 << (7 - (position % 8)));
|
||||
}
|
||||
|
||||
static void drawRect(uint8 * data, size_t x, size_t y, size_t width, size_t scale)
|
||||
{
|
||||
for (size_t outY = (y * scale); outY < ((1 + y) * scale); outY++)
|
||||
{
|
||||
for (size_t outX = (x * scale); outX < ((1 + x) * scale); outX++)
|
||||
{
|
||||
setBit(data, outX, outY, width * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint8 * scaleDataArray(const uint8 data[], size_t width, size_t height, size_t scale)
|
||||
{
|
||||
size_t length = width * height;
|
||||
auto * ret = static_cast<uint8 *>(calloc(sizeof(uint8), length * scale * scale));
|
||||
|
||||
for (size_t y = 0; y < height * 8; y++)
|
||||
{
|
||||
for (size_t x = 0; x < width; x++)
|
||||
{
|
||||
bool value = getBit(data, x, y, width);
|
||||
if (!value) continue;
|
||||
|
||||
drawRect(ret, x, y, width, scale);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
SDL_Cursor * CursorRepository::Create(const CursorData * cursorInfo, uint8 scale)
|
||||
{
|
||||
SDL_Cursor * cursor;
|
||||
|
||||
auto integer_scale = (int) round(scale);
|
||||
|
||||
auto data = scaleDataArray(cursorInfo->Data, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast<size_t>(integer_scale));
|
||||
auto mask = scaleDataArray(cursorInfo->Mask, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast<size_t>(integer_scale));
|
||||
|
||||
cursor = SDL_CreateCursor(
|
||||
data,
|
||||
mask,
|
||||
BASE_CURSOR_WIDTH * integer_scale,
|
||||
BASE_CURSOR_HEIGHT * integer_scale,
|
||||
cursorInfo->HotSpot.X * integer_scale,
|
||||
cursorInfo->HotSpot.Y * integer_scale);
|
||||
|
||||
free(data);
|
||||
free(mask);
|
||||
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
void CursorRepository::SetCursorScale(uint8 cursorScale)
|
||||
{
|
||||
if (cursorScale > 0.0)
|
||||
{
|
||||
_currentCursorScale = cursorScale;
|
||||
GenerateScaledCursorSetHolder(_currentCursorScale);
|
||||
}
|
||||
}
|
||||
|
||||
void CursorRepository::GenerateScaledCursorSetHolder(uint8 scale)
|
||||
{
|
||||
if (_scaledCursors.find(scale) == _scaledCursors.end())
|
||||
{
|
||||
std::function<SDL_Cursor *(CURSOR_ID)> cursorGenerator = [this, scale](CURSOR_ID cursorId)
|
||||
{
|
||||
switch (cursorId)
|
||||
{
|
||||
// We can't scale the system cursors, but they should be appropriately scaled anyway
|
||||
case CURSOR_ARROW:
|
||||
return SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
||||
case CURSOR_HAND_POINT:
|
||||
return SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
||||
default:
|
||||
return this->Create(GetCursorData(cursorId), scale);
|
||||
}
|
||||
};
|
||||
_scaledCursors.emplace(scale, cursorGenerator);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@
|
||||
*****************************************************************************/
|
||||
#pragma endregion
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
#include <SDL.h>
|
||||
|
||||
#include <openrct2/interface/Cursors.h>
|
||||
|
||||
struct SDL_Cursor;
|
||||
@@ -25,20 +30,51 @@ namespace OpenRCT2
|
||||
class CursorRepository
|
||||
{
|
||||
private:
|
||||
constexpr static sint32 CURSOR_WIDTH = 32;
|
||||
constexpr static sint32 CURSOR_HEIGHT = 32;
|
||||
class CursorSetHolder
|
||||
{
|
||||
private:
|
||||
SDL_Cursor * _cursors[CURSOR_COUNT] = {nullptr};
|
||||
public:
|
||||
CursorSetHolder(const std::function<SDL_Cursor *(CURSOR_ID)> & getCursor)
|
||||
{
|
||||
for (size_t i = 0; i < CURSOR_COUNT; i++)
|
||||
{
|
||||
_cursors[i] = getCursor(static_cast<CURSOR_ID>(i));
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Cursor * _loadedCursors[CURSOR_COUNT] = { nullptr };
|
||||
CURSOR_ID _currentCursor = CURSOR_UNDEFINED;
|
||||
~CursorSetHolder()
|
||||
{
|
||||
for (size_t i = 0; i < CURSOR_COUNT; i++)
|
||||
{
|
||||
SDL_FreeCursor(_cursors[i]);
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Cursor * getScaledCursor(CURSOR_ID cursorId)
|
||||
{
|
||||
return _cursors[cursorId];
|
||||
}
|
||||
};
|
||||
|
||||
constexpr static sint32 BASE_CURSOR_WIDTH = 32;
|
||||
constexpr static sint32 BASE_CURSOR_HEIGHT = 32;
|
||||
|
||||
CURSOR_ID _currentCursor = CURSOR_UNDEFINED;
|
||||
uint8 _currentCursorScale = 1;
|
||||
|
||||
std::map<uint8, CursorSetHolder> _scaledCursors;
|
||||
|
||||
public:
|
||||
~CursorRepository();
|
||||
void LoadCursors();
|
||||
CURSOR_ID GetCurrentCursor();
|
||||
void SetCurrentCursor(CURSOR_ID cursorId);
|
||||
void SetCursorScale(uint8 cursorScale);
|
||||
|
||||
private:
|
||||
SDL_Cursor * Create(const CursorData * cursorInfo);
|
||||
SDL_Cursor * Create(const CursorData * cursorInfo, uint8 scale);
|
||||
void GenerateScaledCursorSetHolder(uint8 scale);
|
||||
static const CursorData * GetCursorData(CURSOR_ID cursorId);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -212,6 +212,11 @@ public:
|
||||
_cursorRepository.SetCurrentCursor(cursor);
|
||||
}
|
||||
|
||||
void SetCursorScale(uint8 scale) override
|
||||
{
|
||||
_cursorRepository.SetCursorScale(scale);
|
||||
}
|
||||
|
||||
void SetCursorVisible(bool value) override
|
||||
{
|
||||
SDL_ShowCursor(value ? SDL_ENABLE : SDL_DISABLE);
|
||||
|
||||
@@ -970,6 +970,7 @@ static void window_options_mousedown(rct_window *w, rct_widgetindex widgetIndex,
|
||||
config_save_default();
|
||||
gfx_invalidate_screen();
|
||||
context_trigger_resize();
|
||||
context_update_cursor_scale();
|
||||
break;
|
||||
case WIDX_SCALE_DOWN:
|
||||
gConfigGeneral.window_scale -= 0.25f;
|
||||
@@ -977,6 +978,7 @@ static void window_options_mousedown(rct_window *w, rct_widgetindex widgetIndex,
|
||||
config_save_default();
|
||||
gfx_invalidate_screen();
|
||||
context_trigger_resize();
|
||||
context_update_cursor_scale();
|
||||
break;
|
||||
case WIDX_SCALE_QUALITY_DROPDOWN:
|
||||
gDropdownItemsFormat[0] = STR_DROPDOWN_MENU_LABEL;
|
||||
|
||||
@@ -907,6 +907,11 @@ extern "C"
|
||||
GetContext()->GetUiContext()->SetCursor((CURSOR_ID)cursor);
|
||||
}
|
||||
|
||||
void context_update_cursor_scale()
|
||||
{
|
||||
GetContext()->GetUiContext()->SetCursorScale(static_cast<uint8>(round(gConfigGeneral.window_scale)));
|
||||
}
|
||||
|
||||
void context_hide_cursor()
|
||||
{
|
||||
GetContext()->GetUiContext()->SetCursorVisible(false);
|
||||
|
||||
@@ -195,6 +195,7 @@ extern "C"
|
||||
#endif
|
||||
void context_init();
|
||||
void context_setcurrentcursor(sint32 cursor);
|
||||
void context_update_cursor_scale();
|
||||
void context_hide_cursor();
|
||||
void context_show_cursor();
|
||||
void context_get_cursor_position(sint32 * x, sint32 * y);
|
||||
|
||||
@@ -55,6 +55,8 @@ enum CURSOR_ID
|
||||
|
||||
namespace OpenRCT2 { namespace Ui
|
||||
{
|
||||
constexpr size_t CURSOR_BIT_WIDTH = 32;
|
||||
constexpr size_t CURSOR_HEIGHT = 4;
|
||||
struct CursorData
|
||||
{
|
||||
struct HotSpot
|
||||
@@ -62,8 +64,8 @@ namespace OpenRCT2 { namespace Ui
|
||||
sint16 X;
|
||||
sint16 Y;
|
||||
} HotSpot;
|
||||
uint8 Data[32 * 4];
|
||||
uint8 Mask[32 * 4];
|
||||
uint8 Data[CURSOR_BIT_WIDTH * CURSOR_HEIGHT];
|
||||
uint8 Mask[CURSOR_BIT_WIDTH * CURSOR_HEIGHT];
|
||||
};
|
||||
} }
|
||||
|
||||
|
||||
@@ -1038,6 +1038,7 @@ static sint32 cc_set(const utf8 **argv, sint32 argc)
|
||||
config_save_default();
|
||||
gfx_invalidate_screen();
|
||||
context_trigger_resize();
|
||||
context_update_cursor_scale();
|
||||
console_execute_silent("get window_scale");
|
||||
}
|
||||
else if (strcmp(argv[0], "window_limit") == 0 && invalidArguments(&invalidArgs, int_valid[0])) {
|
||||
|
||||
@@ -54,6 +54,7 @@ namespace OpenRCT2 { namespace Ui
|
||||
const CursorState * GetCursorState() override { return nullptr; }
|
||||
CURSOR_ID GetCursor() override { return CURSOR_ARROW; }
|
||||
void SetCursor(CURSOR_ID cursor) override { }
|
||||
void SetCursorScale(uint8 scale) override { }
|
||||
void SetCursorVisible(bool value) override { }
|
||||
void GetCursorPosition(sint32 * x, sint32 * y) override { }
|
||||
void SetCursorPosition(sint32 x, sint32 y) override { }
|
||||
|
||||
@@ -117,6 +117,7 @@ namespace OpenRCT2
|
||||
virtual const CursorState * GetCursorState() abstract;
|
||||
virtual CURSOR_ID GetCursor() abstract;
|
||||
virtual void SetCursor(CURSOR_ID cursor) abstract;
|
||||
virtual void SetCursorScale(uint8 scale) abstract;
|
||||
virtual void SetCursorVisible(bool value) abstract;
|
||||
virtual void GetCursorPosition(sint32 * x, sint32 * y) abstract;
|
||||
virtual void SetCursorPosition(sint32 x, sint32 y) abstract;
|
||||
|
||||
Reference in New Issue
Block a user