2017-03-25 15:57:02 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2018-06-15 14:07:34 +02:00
|
|
|
*
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
*****************************************************************************/
|
2017-03-25 15:57:02 +01:00
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
#include "CursorRepository.h"
|
2017-12-09 17:08:38 +01:00
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
#include <cmath>
|
2017-03-25 15:57:02 +01:00
|
|
|
#include <openrct2/common.h>
|
2017-12-09 17:08:38 +01:00
|
|
|
#include <openrct2/config/Config.h>
|
2017-03-25 15:57:02 +01:00
|
|
|
#include <openrct2/core/Guard.hpp>
|
|
|
|
#include <openrct2/interface/Cursors.h>
|
2017-12-09 17:08:38 +01:00
|
|
|
|
2017-03-25 15:57:02 +01:00
|
|
|
using namespace OpenRCT2::Ui;
|
|
|
|
|
2017-03-26 22:42:07 +02:00
|
|
|
CursorRepository::~CursorRepository()
|
|
|
|
{
|
2017-12-09 17:08:38 +01:00
|
|
|
_scaledCursors.clear();
|
2020-10-11 18:01:14 +02:00
|
|
|
_currentCursor = CursorID::Undefined;
|
2017-12-09 17:08:38 +01:00
|
|
|
_currentCursorScale = 1;
|
2017-03-26 22:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorRepository::LoadCursors()
|
2017-03-25 15:57:02 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
SetCursorScale(static_cast<uint8_t>(round(gConfigGeneral.window_scale)));
|
2020-10-11 18:01:14 +02:00
|
|
|
SetCurrentCursor(CursorID::Arrow);
|
2017-03-25 15:57:02 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 18:01:14 +02:00
|
|
|
CursorID CursorRepository::GetCurrentCursor()
|
2017-03-25 15:57:02 +01:00
|
|
|
{
|
|
|
|
return _currentCursor;
|
|
|
|
}
|
|
|
|
|
2020-10-11 18:01:14 +02:00
|
|
|
void CursorRepository::SetCurrentCursor(CursorID cursorId)
|
2017-03-25 15:57:02 +01:00
|
|
|
{
|
|
|
|
if (_currentCursor != cursorId)
|
|
|
|
{
|
2018-06-22 23:22:29 +02:00
|
|
|
SDL_Cursor* cursor = _scaledCursors.at(_currentCursorScale).getScaledCursor(cursorId);
|
2017-03-25 15:57:02 +01:00
|
|
|
SDL_SetCursor(cursor);
|
|
|
|
_currentCursor = cursorId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
static bool getBit(const uint8_t* data, size_t x, size_t y, size_t width)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
|
|
|
size_t position = y * width + x;
|
|
|
|
return (data[position / 8] & (1 << (7 - (x % 8)))) != 0;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
static void setBit(uint8_t* data, size_t x, size_t y, size_t width)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
|
|
|
size_t position = y * width + x;
|
|
|
|
data[position / 8] |= (1 << (7 - (position % 8)));
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
static void drawRect(uint8_t* data, size_t x, size_t y, size_t width, size_t scale)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
static uint8_t* scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale)
|
2017-03-25 15:57:02 +01:00
|
|
|
{
|
2017-12-09 17:08:38 +01:00
|
|
|
size_t length = width * height;
|
2018-06-22 23:22:29 +02:00
|
|
|
auto* ret = static_cast<uint8_t*>(calloc(sizeof(uint8_t), length * scale * scale));
|
2017-12-09 17:08:38 +01:00
|
|
|
|
|
|
|
for (size_t y = 0; y < height * 8; y++)
|
|
|
|
{
|
|
|
|
for (size_t x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
bool value = getBit(data, x, y, width);
|
2018-06-22 23:22:29 +02:00
|
|
|
if (!value)
|
|
|
|
continue;
|
2017-12-09 17:08:38 +01:00
|
|
|
|
|
|
|
drawRect(ret, x, y, width, scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:22:29 +02:00
|
|
|
SDL_Cursor* CursorRepository::Create(const CursorData* cursorInfo, uint8_t scale)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
2018-06-22 23:22:29 +02:00
|
|
|
SDL_Cursor* cursor;
|
2017-12-09 17:08:38 +01:00
|
|
|
|
2020-04-18 12:15:07 +02:00
|
|
|
auto integer_scale = static_cast<int>(round(scale));
|
2017-12-09 17:08:38 +01:00
|
|
|
|
|
|
|
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(
|
2018-07-21 13:51:54 +02:00
|
|
|
data, mask, BASE_CURSOR_WIDTH * integer_scale, BASE_CURSOR_HEIGHT * integer_scale,
|
|
|
|
cursorInfo->HotSpot.X * integer_scale, cursorInfo->HotSpot.Y * integer_scale);
|
2017-12-09 17:08:38 +01:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
free(mask);
|
|
|
|
|
2017-03-25 15:57:02 +01:00
|
|
|
return cursor;
|
|
|
|
}
|
2017-12-09 17:08:38 +01:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void CursorRepository::SetCursorScale(uint8_t cursorScale)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
|
|
|
if (cursorScale > 0.0)
|
|
|
|
{
|
|
|
|
_currentCursorScale = cursorScale;
|
|
|
|
GenerateScaledCursorSetHolder(_currentCursorScale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
void CursorRepository::GenerateScaledCursorSetHolder(uint8_t scale)
|
2017-12-09 17:08:38 +01:00
|
|
|
{
|
|
|
|
if (_scaledCursors.find(scale) == _scaledCursors.end())
|
|
|
|
{
|
2020-10-11 18:01:14 +02:00
|
|
|
std::function<SDL_Cursor*(CursorID)> cursorGenerator = [this, scale](CursorID cursorId) {
|
2017-12-09 17:08:38 +01:00
|
|
|
switch (cursorId)
|
|
|
|
{
|
|
|
|
// We can't scale the system cursors, but they should be appropriately scaled anyway
|
2020-10-11 18:01:14 +02:00
|
|
|
case CursorID::Arrow:
|
2017-12-09 17:08:38 +01:00
|
|
|
return SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
|
2020-10-11 18:01:14 +02:00
|
|
|
case CursorID::HandPoint:
|
2017-12-09 17:08:38 +01:00
|
|
|
return SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
|
|
|
|
default:
|
|
|
|
return this->Create(GetCursorData(cursorId), scale);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
_scaledCursors.emplace(scale, cursorGenerator);
|
|
|
|
}
|
|
|
|
}
|