2007-05-30 21:56:52 +00:00
|
|
|
/* 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.
|
2006-05-25 22:51:42 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2011-06-27 23:06:23 +00:00
|
|
|
namespace Common {
|
2010-11-16 08:23:13 +00:00
|
|
|
DECLARE_SINGLETON(Graphics::CursorManager);
|
2011-06-27 23:06:23 +00:00
|
|
|
}
|
2007-05-27 11:40:03 +00:00
|
|
|
|
2006-05-25 22:51:42 +00:00
|
|
|
namespace Graphics {
|
|
|
|
|
2009-12-09 23:05:15 +00:00
|
|
|
CursorManager::~CursorManager() {
|
2012-02-22 19:20:55 +00:00
|
|
|
for (Common::Stack<Cursor *>::size_type i = 0; i < _cursorStack.size(); ++i)
|
2009-12-09 23:05:15 +00:00
|
|
|
delete _cursorStack[i];
|
|
|
|
_cursorStack.clear();
|
2012-02-22 19:20:55 +00:00
|
|
|
for (Common::Stack<Palette *>::size_type i = 0; i < _cursorPaletteStack.size(); ++i)
|
2009-12-09 23:05:15 +00:00
|
|
|
delete _cursorPaletteStack[i];
|
|
|
|
_cursorPaletteStack.clear();
|
|
|
|
}
|
|
|
|
|
2006-05-25 22:51:42 +00:00
|
|
|
bool CursorManager::isVisible() {
|
|
|
|
if (_cursorStack.empty())
|
|
|
|
return false;
|
|
|
|
return _cursorStack.top()->_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CursorManager::showMouse(bool visible) {
|
|
|
|
if (_cursorStack.empty())
|
|
|
|
return false;
|
2013-05-16 21:18:09 +00:00
|
|
|
if (_locked) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-05-25 22:51:42 +00:00
|
|
|
|
|
|
|
_cursorStack.top()->_visible = visible;
|
2006-05-25 23:20:35 +00:00
|
|
|
|
|
|
|
// Should work, even if there's just a dummy cursor on the stack.
|
2006-05-25 22:51:42 +00:00
|
|
|
return g_system->showMouse(visible);
|
|
|
|
}
|
2009-06-05 23:59:40 +00:00
|
|
|
|
2012-06-16 01:14:07 +00:00
|
|
|
void CursorManager::pushCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
|
2012-06-03 00:02:57 +00:00
|
|
|
Cursor *cur = new Cursor(buf, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
|
2006-05-25 22:51:42 +00:00
|
|
|
|
|
|
|
cur->_visible = isVisible();
|
|
|
|
_cursorStack.push(cur);
|
|
|
|
|
|
|
|
if (buf) {
|
2012-06-03 00:02:57 +00:00
|
|
|
g_system->setMouseCursor(cur->_data, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
|
2006-05-25 22:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CursorManager::popCursor() {
|
|
|
|
if (_cursorStack.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Cursor *cur = _cursorStack.pop();
|
|
|
|
delete cur;
|
|
|
|
|
|
|
|
if (!_cursorStack.empty()) {
|
|
|
|
cur = _cursorStack.top();
|
2012-06-03 00:02:57 +00:00
|
|
|
g_system->setMouseCursor(cur->_data, cur->_width, cur->_height, cur->_hotspotX, cur->_hotspotY, cur->_keycolor, cur->_dontScale, &cur->_format);
|
2006-05-25 22:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_system->showMouse(isVisible());
|
|
|
|
}
|
|
|
|
|
2008-06-09 23:15:17 +00:00
|
|
|
|
|
|
|
void CursorManager::popAllCursors() {
|
|
|
|
while (!_cursorStack.empty()) {
|
|
|
|
Cursor *cur = _cursorStack.pop();
|
|
|
|
delete cur;
|
|
|
|
}
|
|
|
|
|
2011-06-03 21:30:55 +00:00
|
|
|
if (g_system->hasFeature(OSystem::kFeatureCursorPalette)) {
|
2008-06-09 23:15:17 +00:00
|
|
|
while (!_cursorPaletteStack.empty()) {
|
|
|
|
Palette *pal = _cursorPaletteStack.pop();
|
|
|
|
delete pal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_system->showMouse(isVisible());
|
|
|
|
}
|
|
|
|
|
2012-06-16 01:14:07 +00:00
|
|
|
void CursorManager::replaceCursor(const void *buf, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
|
2009-06-11 05:56:00 +00:00
|
|
|
|
2006-05-25 22:51:42 +00:00
|
|
|
if (_cursorStack.empty()) {
|
2012-06-03 00:02:57 +00:00
|
|
|
pushCursor(buf, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
|
2006-05-25 22:51:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor *cur = _cursorStack.top();
|
2009-06-05 08:09:37 +00:00
|
|
|
|
2009-08-21 18:16:37 +00:00
|
|
|
#ifdef USE_RGB_COLOR
|
2009-06-30 07:30:57 +00:00
|
|
|
uint size;
|
2009-06-26 08:50:11 +00:00
|
|
|
if (!format)
|
2009-06-30 07:30:57 +00:00
|
|
|
size = w * h;
|
2009-06-30 08:25:08 +00:00
|
|
|
else
|
|
|
|
size = w * h * format->bytesPerPixel;
|
2009-06-06 01:16:04 +00:00
|
|
|
#else
|
2006-05-25 22:51:42 +00:00
|
|
|
uint size = w * h;
|
2009-06-06 01:16:04 +00:00
|
|
|
#endif
|
2006-05-25 22:51:42 +00:00
|
|
|
|
|
|
|
if (cur->_size < size) {
|
2006-05-27 11:47:44 +00:00
|
|
|
delete[] cur->_data;
|
2006-05-25 22:51:42 +00:00
|
|
|
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;
|
2012-06-03 00:02:57 +00:00
|
|
|
cur->_dontScale = dontScale;
|
2009-08-21 18:16:37 +00:00
|
|
|
#ifdef USE_RGB_COLOR
|
2009-07-04 04:13:10 +00:00
|
|
|
if (format)
|
|
|
|
cur->_format = *format;
|
|
|
|
else
|
2009-07-08 16:07:58 +00:00
|
|
|
cur->_format = Graphics::PixelFormat::createFormatCLUT8();
|
2009-06-24 06:44:30 +00:00
|
|
|
#endif
|
2006-05-25 22:51:42 +00:00
|
|
|
|
2012-06-03 00:02:57 +00:00
|
|
|
g_system->setMouseCursor(cur->_data, w, h, hotspotX, hotspotY, keycolor, dontScale, format);
|
2006-05-25 22:51:42 +00:00
|
|
|
}
|
|
|
|
|
2009-06-10 15:20:52 +00:00
|
|
|
bool CursorManager::supportsCursorPalettes() {
|
2011-06-03 21:30:55 +00:00
|
|
|
return g_system->hasFeature(OSystem::kFeatureCursorPalette);
|
2009-06-10 15:20:52 +00:00
|
|
|
}
|
|
|
|
|
2007-02-12 00:04:56 +00:00
|
|
|
void CursorManager::disableCursorPalette(bool disable) {
|
2011-06-03 21:30:55 +00:00
|
|
|
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
|
2007-02-12 00:04:56 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_cursorPaletteStack.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Palette *pal = _cursorPaletteStack.top();
|
|
|
|
pal->_disabled = disable;
|
|
|
|
|
2011-06-03 22:14:09 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureCursorPalette, !disable);
|
2007-02-12 00:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorManager::pushCursorPalette(const byte *colors, uint start, uint num) {
|
2011-06-03 21:30:55 +00:00
|
|
|
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
|
2007-02-12 00:04:56 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
Palette *pal = new Palette(colors, start, num);
|
|
|
|
_cursorPaletteStack.push(pal);
|
|
|
|
|
|
|
|
if (num)
|
|
|
|
g_system->setCursorPalette(colors, start, num);
|
|
|
|
else
|
2011-06-03 22:14:09 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
|
2007-02-12 00:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorManager::popCursorPalette() {
|
2011-06-03 21:30:55 +00:00
|
|
|
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
|
2007-02-12 00:04:56 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_cursorPaletteStack.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Palette *pal = _cursorPaletteStack.pop();
|
|
|
|
delete pal;
|
|
|
|
|
|
|
|
if (_cursorPaletteStack.empty()) {
|
2011-06-03 22:14:09 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
|
2007-02-12 00:04:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pal = _cursorPaletteStack.top();
|
|
|
|
|
|
|
|
if (pal->_num && !pal->_disabled)
|
|
|
|
g_system->setCursorPalette(pal->_data, pal->_start, pal->_num);
|
|
|
|
else
|
2011-06-03 22:14:09 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
|
2007-02-12 00:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CursorManager::replaceCursorPalette(const byte *colors, uint start, uint num) {
|
2011-06-03 21:30:55 +00:00
|
|
|
if (!g_system->hasFeature(OSystem::kFeatureCursorPalette))
|
2007-02-12 00:04:56 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_cursorPaletteStack.empty()) {
|
|
|
|
pushCursorPalette(colors, start, num);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Palette *pal = _cursorPaletteStack.top();
|
2011-02-20 04:43:04 +00:00
|
|
|
uint size = 3 * num;
|
2007-02-12 00:04:56 +00:00
|
|
|
|
|
|
|
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) {
|
2011-02-20 04:43:04 +00:00
|
|
|
memcpy(pal->_data, colors, size);
|
2007-02-12 00:04:56 +00:00
|
|
|
g_system->setCursorPalette(pal->_data, pal->_start, pal->_num);
|
|
|
|
} else {
|
2011-06-03 22:14:09 +00:00
|
|
|
g_system->setFeatureState(OSystem::kFeatureCursorPalette, false);
|
2007-02-12 00:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 21:18:09 +00:00
|
|
|
void CursorManager::lock(bool locked) {
|
|
|
|
_locked = locked;
|
|
|
|
}
|
|
|
|
|
2012-06-16 01:14:07 +00:00
|
|
|
CursorManager::Cursor::Cursor(const void *data, uint w, uint h, int hotspotX, int hotspotY, uint32 keycolor, bool dontScale, const Graphics::PixelFormat *format) {
|
2009-12-09 23:04:54 +00:00
|
|
|
#ifdef USE_RGB_COLOR
|
|
|
|
if (!format)
|
|
|
|
_format = Graphics::PixelFormat::createFormatCLUT8();
|
2010-01-25 01:39:44 +00:00
|
|
|
else
|
2009-12-09 23:04:54 +00:00
|
|
|
_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;
|
2012-06-03 00:02:57 +00:00
|
|
|
_dontScale = dontScale;
|
2009-12-09 23:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CursorManager::Cursor::~Cursor() {
|
|
|
|
delete[] _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
CursorManager::Palette::Palette(const byte *colors, uint start, uint num) {
|
|
|
|
_start = start;
|
|
|
|
_num = num;
|
2011-02-20 04:43:04 +00:00
|
|
|
_size = 3 * num;
|
2009-12-09 23:04:54 +00:00
|
|
|
|
|
|
|
if (num) {
|
|
|
|
_data = new byte[_size];
|
|
|
|
memcpy(_data, colors, _size);
|
|
|
|
} else {
|
|
|
|
_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CursorManager::Palette::~Palette() {
|
|
|
|
delete[] _data;
|
|
|
|
}
|
|
|
|
|
2006-05-25 22:51:42 +00:00
|
|
|
} // End of namespace Graphics
|