1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "graphics/cursorman.h"
#include "common/system.h"
#include "common/stack.h"
namespace Common {
DECLARE_SINGLETON(Graphics::CursorManager);
}
namespace Graphics {
CursorManager::~CursorManager() {
for (Common::Stack<Cursor *>::size_type i = 0; i < _cursorStack.size(); ++i)
delete _cursorStack[i];
_cursorStack.clear();
for (Common::Stack<Palette *>::size_type i = 0; i < _cursorPaletteStack.size(); ++i)
delete _cursorPaletteStack[i];
_cursorPaletteStack.clear();
}
bool CursorManager::isVisible() {
if (_cursorStack.empty())
return false;
return _cursorStack.top()->_visible;
}
bool CursorManager::showMouse(bool visible) {
if (_cursorStack.empty())
return false;
if (_locked) {
return false;
}
_cursorStack.top()->_visible = visible;
// Should work, even if there's just a dummy cursor on the stack.
return g_system->showMouse(visible);
}
void CursorManager::pushCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
Cursor *cur = new Cursor(buf, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
cur->_visible = isVisible();
_cursorStack.push(cur);
g_system->setMouseCursor(cur->_data, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
}
void CursorManager::popCursor() {
if (_cursorStack.empty())
return;
Cursor *cur = _cursorStack.pop();
delete cur;
if (!_cursorStack.empty()) {
cur = _cursorStack.top();
g_system->setMouseCursor(cur->_data, cur->_width, cur->_height, cur->_hotspotX, cur->_hotspotY, cur->_keycolor, cur->_dontScale, &cur->_format);
} else {
g_system->setMouseCursor(nullptr, 0, 0, 0, 0, 0);
}
g_system->showMouse(isVisible());
}
void CursorManager::popAllCursors() {
while (!_cursorStack.empty()) {
Cursor *cur = _cursorStack.pop();
delete cur;
}
if (g_system->hasFeature(OSystem::kFeatureCursorPalette)) {
while (!_cursorPaletteStack.empty()) {
Palette *pal = _cursorPaletteStack.pop();
delete pal;
}
}
g_system->setMouseCursor(nullptr, 0, 0, 0, 0, 0);
g_system->showMouse(isVisible());
}
void CursorManager::replaceCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
if (_cursorStack.empty()) {
pushCursor(buf, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
return;
}
Cursor *cur = _cursorStack.top();
#ifdef USE_RGB_COLOR
uint size;
if (!format)
size = w * h;
else
size = w * h * format->bytesPerPixel;
#else
uint size = w * h;
#endif
if (cur->_size < size) {
delete[] cur->_data;
cur->_data = new byte[size];
cur->_size = size;
}
if (buf && cur->_data)
memcpy(cur->_data, buf, size);
cur->_width = w;
cur->_height = h;
cur->_hotspotX = hotspotX;
cur->_hotspotY = hotspotY;
cur->_keycolor = keycolor;
cur->_dontScale = dontScale;
#ifdef USE_RGB_COLOR
if (format)
cur->_format = *format;
else
cur->_format = Graphics::PixelFormat::createFormatCLUT8();
#endif
g_system->setMouseCursor(cur->_data, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
}
bool CursorManager::supportsCursorPalettes() {
return g_system->hasFeature(OSystem::kFeatureCursorPalette);
}
void CursorManager::disableCursorPalette(bool disable) {
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
return;
if (_cursorPaletteStack.empty())
return;
Palette *pal = _cursorPaletteStack.top();
pal->_disabled = disable;
g_system->setFeatureState(OSystem::kFeatureCursorPalette, !disable);
}
void CursorManager::pushCursorPalette(const byte *colors, uint start, uint num) {
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
return;
Palette *pal = new Palette(colors, start, num);
_cursorPaletteStack.push(pal);
if (num)
g_system->setCursorPalette(colors, start, num);
else
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
}
void CursorManager::popCursorPalette() {
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
return;
if (_cursorPaletteStack.empty())
return;
Palette *pal = _cursorPaletteStack.pop();
delete pal;
if (_cursorPaletteStack.empty()) {
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
return;
}
pal = _cursorPaletteStack.top();
if (pal->_num && !pal->_disabled)
g_system->setCursorPalette(pal->_data, pal->_start, pal->_num);
else
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
}
void CursorManager::replaceCursorPalette(const byte *colors, uint start, uint num) {
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
return;
if (_cursorPaletteStack.empty()) {
pushCursorPalette(colors, start, num);
return;
}
Palette *pal = _cursorPaletteStack.top();
uint size = 3 * num;
if (pal->_size < size) {
// Could not re-use the old buffer. Create a new one.
delete[] pal->_data;
pal->_data = new byte[size];
pal->_size = size;
}
pal->_start = start;
pal->_num = num;
if (num) {
memcpy(pal->_data, colors, size);
g_system->setCursorPalette(pal->_data, pal->_start, pal->_num);
} else {
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
}
}
void CursorManager::lock(bool locked) {
_locked = locked;
}
CursorManager::Cursor::Cursor(const void *data, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
#ifdef USE_RGB_COLOR
if (!format)
_format = Graphics::PixelFormat::createFormatCLUT8();
else
_format = *format;
_size = w * h * _format.bytesPerPixel;
_keycolor = keycolor & ((1 << (_format.bytesPerPixel << 3)) - 1);
#else
_format = Graphics::PixelFormat::createFormatCLUT8();
_size = w * h;
_keycolor = keycolor & 0xFF;
#endif
_data = new byte[_size];
if (data && _data)
memcpy(_data, data, _size);
_width = w;
_height = h;
_hotspotX = hotspotX;
_hotspotY = hotspotY;
_dontScale = dontScale;
_visible = false;
}
CursorManager::Cursor::~Cursor() {
delete[] _data;
}
CursorManager::Palette::Palette(const byte *colors, uint start, uint num) {
_start = start;
_num = num;
_size = 3 * num;
if (num) {
_data = new byte[_size];
memcpy(_data, colors, _size);
} else {
_data = NULL;
}
_disabled = false;
}
CursorManager::Palette::~Palette() {
delete[] _data;
}
} // End of namespace Graphics
|