2008-07-03 22:38:19 +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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2008-07-07 15:06:43 +00:00
|
|
|
#include "backends/common/virtual-keyboard.h"
|
|
|
|
#include "backends/common/virtual-keyboard-parser.h"
|
2008-07-03 22:38:19 +00:00
|
|
|
#include "common/config-manager.h"
|
2008-07-07 14:30:11 +00:00
|
|
|
#include "common/events.h"
|
2008-07-03 22:38:19 +00:00
|
|
|
#include "graphics/imageman.h"
|
2008-07-07 21:10:58 +00:00
|
|
|
#include "graphics/surface-keycolored.h"
|
2008-07-03 22:38:19 +00:00
|
|
|
#include "common/unzip.h"
|
|
|
|
|
2008-07-07 15:42:26 +00:00
|
|
|
namespace Common {
|
2008-07-03 22:38:19 +00:00
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
VirtualKeyboard::VirtualKeyboard() : _currentMode(0), _keyDown(0) {
|
2008-07-03 22:38:19 +00:00
|
|
|
assert(g_system);
|
|
|
|
_system = g_system;
|
|
|
|
|
|
|
|
_parser = new VirtualKeyboardParser(this);
|
2008-07-08 15:03:39 +00:00
|
|
|
_loaded = _displaying = _drag = false;
|
2008-07-09 13:33:36 +00:00
|
|
|
_lastScreenChanged = _system->getScreenChangeID();
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualKeyboard::~VirtualKeyboard() {
|
2008-07-04 17:55:19 +00:00
|
|
|
// TODO: clean up event data pointers
|
2008-07-09 13:33:36 +00:00
|
|
|
deleteEventData();
|
2008-07-04 17:55:19 +00:00
|
|
|
delete _parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::reset() {
|
|
|
|
// TODO: clean up event data pointers
|
2008-07-09 13:33:36 +00:00
|
|
|
deleteEventData();
|
2008-07-04 17:55:19 +00:00
|
|
|
_modes.clear();
|
|
|
|
_initialMode = _currentMode = 0;
|
2008-07-08 15:03:39 +00:00
|
|
|
_kbdBound.left = _kbdBound.top
|
|
|
|
= _kbdBound.right = _kbdBound.bottom = 0;
|
2008-07-04 17:55:19 +00:00
|
|
|
_hAlignment = kAlignCentre;
|
|
|
|
_vAlignment = kAlignBottom;
|
|
|
|
_keyQueue.clear();
|
2008-07-07 14:30:11 +00:00
|
|
|
_keyDown = 0;
|
2008-07-08 15:03:39 +00:00
|
|
|
_displaying = _drag = false;
|
|
|
|
_firstRun = true;
|
2008-07-09 13:33:36 +00:00
|
|
|
_lastScreenChanged = _system->getScreenChangeID();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::deleteEventData() {
|
|
|
|
ModeMap::iterator it_m;
|
|
|
|
EventMap::iterator it_e;
|
|
|
|
for (it_m = _modes.begin(); it_m != _modes.end(); it_m++) {
|
|
|
|
EventMap *evt = &(it_m->_value.events);
|
|
|
|
for (it_e = evt->begin(); it_e != evt->end(); it_e++)
|
|
|
|
delete it_e->_value.data;
|
|
|
|
}
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualKeyboard::loadKeyboardPack(Common::String packName) {
|
2008-07-04 17:55:19 +00:00
|
|
|
|
2008-07-03 22:38:19 +00:00
|
|
|
if (ConfMan.hasKey("extrapath"))
|
|
|
|
Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
|
|
|
|
|
|
|
|
if (Common::File::exists(packName + ".xml")) {
|
|
|
|
// uncompressed keyboard pack
|
|
|
|
if (!_parser->loadFile(packName + ".xml"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} else if (Common::File::exists(packName + ".zip")) {
|
|
|
|
// compressed keyboard pack
|
|
|
|
#ifdef USE_ZLIB
|
|
|
|
unzFile zipFile = unzOpen((packName + ".zip").c_str());
|
|
|
|
if (zipFile && unzLocateFile(zipFile, (packName + ".xml").c_str(), 2) == UNZ_OK) {
|
|
|
|
unz_file_info fileInfo;
|
|
|
|
unzOpenCurrentFile(zipFile);
|
|
|
|
unzGetCurrentFileInfo(zipFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
|
|
|
|
byte *buffer = new byte[fileInfo.uncompressed_size+1];
|
|
|
|
assert(buffer);
|
2008-07-04 11:38:15 +00:00
|
|
|
memset(buffer, 0, (fileInfo.uncompressed_size+1)*sizeof(byte));
|
2008-07-03 22:38:19 +00:00
|
|
|
unzReadCurrentFile(zipFile, buffer, fileInfo.uncompressed_size);
|
|
|
|
unzCloseCurrentFile(zipFile);
|
2008-07-04 11:38:15 +00:00
|
|
|
if (!_parser->loadBuffer(buffer, fileInfo.uncompressed_size+1, true)) {
|
2008-07-03 22:38:19 +00:00
|
|
|
unzClose(zipFile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unzClose(zipFile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
unzClose(zipFile);
|
|
|
|
|
|
|
|
ImageMan.addArchive(packName + ".zip");
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
warning("Keyboard pack not found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-07-09 11:30:49 +00:00
|
|
|
_parser->setParseMode(kParseFull);
|
2008-07-07 21:10:58 +00:00
|
|
|
_loaded = _parser->parse();
|
|
|
|
if (_loaded)
|
|
|
|
printf("Keyboard pack '%s' loaded successfully!\n", packName.c_str());
|
2008-07-08 15:03:39 +00:00
|
|
|
|
2008-07-07 21:10:58 +00:00
|
|
|
return _loaded;
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-08 15:03:39 +00:00
|
|
|
void VirtualKeyboard::setDefaultPosition()
|
2008-07-04 17:55:19 +00:00
|
|
|
{
|
2008-07-09 13:33:36 +00:00
|
|
|
int16 scrW = _system->getOverlayWidth(), scrH = _system->getOverlayHeight();
|
2008-07-08 15:03:39 +00:00
|
|
|
int16 kbdW = _kbdBound.width(), kbdH = _kbdBound.height();
|
|
|
|
int16 posX = 0, posY = 0;
|
2008-07-09 13:33:36 +00:00
|
|
|
if (scrW != kbdW) {
|
2008-07-04 17:55:19 +00:00
|
|
|
switch (_hAlignment) {
|
2008-07-08 15:03:39 +00:00
|
|
|
case kAlignLeft:
|
|
|
|
posX = 0;
|
|
|
|
break;
|
2008-07-04 17:55:19 +00:00
|
|
|
case kAlignCentre:
|
2008-07-09 13:33:36 +00:00
|
|
|
posX = (scrW - kbdW) / 2;
|
2008-07-04 17:55:19 +00:00
|
|
|
break;
|
|
|
|
case kAlignRight:
|
2008-07-09 13:33:36 +00:00
|
|
|
posX = scrW - kbdW;
|
2008-07-07 15:45:48 +00:00
|
|
|
break;
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-09 13:33:36 +00:00
|
|
|
if (scrH != kbdH) {
|
2008-07-04 17:55:19 +00:00
|
|
|
switch (_vAlignment) {
|
2008-07-08 15:03:39 +00:00
|
|
|
case kAlignTop:
|
|
|
|
posY = 0;
|
|
|
|
break;
|
2008-07-04 17:55:19 +00:00
|
|
|
case kAlignMiddle:
|
2008-07-09 13:33:36 +00:00
|
|
|
posY = (scrH - kbdH) / 2;
|
2008-07-04 17:55:19 +00:00
|
|
|
break;
|
|
|
|
case kAlignBottom:
|
2008-07-09 13:33:36 +00:00
|
|
|
posY = scrH - kbdH;
|
2008-07-07 15:45:48 +00:00
|
|
|
break;
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-08 15:03:39 +00:00
|
|
|
_kbdBound.moveTo(posX, posY);
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-09 11:30:49 +00:00
|
|
|
bool VirtualKeyboard::checkModeResolutions()
|
2008-07-04 17:55:19 +00:00
|
|
|
{
|
2008-07-09 11:30:49 +00:00
|
|
|
_parser->setParseMode(kParseCheckResolutions);
|
|
|
|
_loaded = _parser->parse();
|
|
|
|
return _loaded;
|
2008-07-08 15:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::move(int16 x, int16 y) {
|
|
|
|
// snap to edge of screen
|
|
|
|
if (ABS(x) < SNAP_WIDTH)
|
|
|
|
x = 0;
|
|
|
|
int16 x2 = _system->getOverlayWidth() - _kbdBound.width();
|
|
|
|
if (ABS(x - x2) < SNAP_WIDTH)
|
|
|
|
x = x2;
|
|
|
|
if (ABS(y) < SNAP_WIDTH)
|
|
|
|
y = 0;
|
|
|
|
int16 y2 = _system->getOverlayHeight() - _kbdBound.height();
|
|
|
|
if (ABS(y - y2) < SNAP_WIDTH)
|
|
|
|
y = y2;
|
|
|
|
|
|
|
|
_kbdBound.moveTo(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::String VirtualKeyboard::findArea(int16 x, int16 y) {
|
|
|
|
x -= _kbdBound.left;
|
|
|
|
y -= _kbdBound.top;
|
|
|
|
if (x < 0 || x > _kbdBound.width()) return "";
|
|
|
|
if (y < 0 || y > _kbdBound.height()) return "";
|
|
|
|
return _currentMode->imageMap.findMapArea(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::processClick(const Common::String& area) {
|
2008-07-07 21:10:58 +00:00
|
|
|
if (!_currentMode->events.contains(area)) return;
|
|
|
|
Event evt = _currentMode->events[area];
|
2008-07-08 15:03:39 +00:00
|
|
|
|
2008-07-04 17:55:19 +00:00
|
|
|
switch (evt.type) {
|
|
|
|
case kEventKey:
|
|
|
|
// add virtual keypress to queue
|
2008-07-09 13:33:36 +00:00
|
|
|
_keyQueue.push(*(Common::KeyState*)evt.data);
|
2008-07-04 17:55:19 +00:00
|
|
|
break;
|
|
|
|
case kEventSwitchMode:
|
|
|
|
// switch to new mode
|
|
|
|
switchMode(*(Common::String *)evt.data);
|
|
|
|
break;
|
|
|
|
case kEventClose:
|
|
|
|
// close virtual keyboard
|
|
|
|
_displaying = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 22:38:19 +00:00
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::switchMode(Mode *newMode) {
|
|
|
|
_currentMode = newMode;
|
2008-07-08 15:03:39 +00:00
|
|
|
_kbdBound.setWidth(_currentMode->image->w);
|
|
|
|
_kbdBound.setHeight(_currentMode->image->h);
|
2008-07-07 14:30:11 +00:00
|
|
|
_needRedraw = true;
|
|
|
|
}
|
|
|
|
|
2008-07-04 17:55:19 +00:00
|
|
|
void VirtualKeyboard::switchMode(const Common::String& newMode) {
|
2008-07-07 14:30:11 +00:00
|
|
|
if (!_modes.contains(newMode)) {
|
|
|
|
warning("Keyboard mode '%s' unknown", newMode.c_str());
|
|
|
|
return;
|
|
|
|
}
|
2008-07-08 15:03:39 +00:00
|
|
|
switchMode(&_modes[newMode]);
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::show() {
|
2008-07-09 11:30:49 +00:00
|
|
|
if (!_loaded) {
|
|
|
|
warning("Keyboard not loaded therefore can't be shown");
|
|
|
|
return;
|
|
|
|
}
|
2008-07-09 13:33:36 +00:00
|
|
|
if (_lastScreenChanged != _system->getScreenChangeID())
|
|
|
|
screenChanged();
|
2008-07-07 14:30:11 +00:00
|
|
|
switchMode(_initialMode);
|
2008-07-04 17:55:19 +00:00
|
|
|
_displaying = true;
|
2008-07-08 15:03:39 +00:00
|
|
|
if (_firstRun) {
|
|
|
|
_firstRun = false;
|
|
|
|
setDefaultPosition();
|
|
|
|
}
|
2008-07-09 13:33:36 +00:00
|
|
|
_system->showOverlay();
|
2008-07-04 17:55:19 +00:00
|
|
|
runLoop();
|
2008-07-09 13:33:36 +00:00
|
|
|
_system->hideOverlay();
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::hide() {
|
|
|
|
_displaying = false;
|
|
|
|
}
|
2008-07-03 22:38:19 +00:00
|
|
|
|
2008-07-09 13:33:36 +00:00
|
|
|
void VirtualKeyboard::screenChanged() {
|
|
|
|
_lastScreenChanged = _system->getScreenChangeID();
|
|
|
|
if (!checkModeResolutions())
|
|
|
|
_displaying = false;
|
|
|
|
}
|
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::runLoop() {
|
|
|
|
Common::EventManager *eventMan = _system->getEventManager();
|
2008-07-08 15:03:39 +00:00
|
|
|
|
2008-07-04 17:55:19 +00:00
|
|
|
while (_displaying) {
|
2008-07-07 14:30:11 +00:00
|
|
|
if (_needRedraw) redraw();
|
2008-07-04 17:55:19 +00:00
|
|
|
|
2008-07-07 21:10:58 +00:00
|
|
|
_system->updateScreen();
|
2008-07-07 14:30:11 +00:00
|
|
|
Common::Event event;
|
|
|
|
while (eventMan->pollEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case Common::EVENT_LBUTTONDOWN:
|
2008-07-08 15:03:39 +00:00
|
|
|
if (_kbdBound.contains(event.mouse)) {
|
|
|
|
_areaDown = findArea(event.mouse.x, event.mouse.y);
|
|
|
|
if (_areaDown.empty()) {
|
|
|
|
_drag = true;
|
|
|
|
_dragPoint.x = event.mouse.x - _kbdBound.left;
|
|
|
|
_dragPoint.y = event.mouse.y - _kbdBound.top;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
_areaDown.clear();
|
2008-07-07 14:30:11 +00:00
|
|
|
break;
|
|
|
|
case Common::EVENT_LBUTTONUP:
|
2008-07-08 15:03:39 +00:00
|
|
|
if (_drag) _drag = false;
|
|
|
|
if (!_areaDown.empty() && _areaDown == findArea(event.mouse.x, event.mouse.y)) {
|
|
|
|
processClick(_areaDown);
|
|
|
|
_areaDown.clear();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Common::EVENT_MOUSEMOVE:
|
|
|
|
if (_drag) {
|
|
|
|
move(event.mouse.x - _dragPoint.x,
|
|
|
|
event.mouse.y - _dragPoint.y);
|
|
|
|
_needRedraw = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Common::EVENT_SCREEN_CHANGED:
|
2008-07-09 13:33:36 +00:00
|
|
|
screenChanged();
|
2008-07-07 14:30:11 +00:00
|
|
|
break;
|
|
|
|
case Common::EVENT_QUIT:
|
|
|
|
_system->quit();
|
|
|
|
return;
|
2008-07-07 15:45:48 +00:00
|
|
|
default:
|
|
|
|
break;
|
2008-07-07 14:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-09 23:05:47 +00:00
|
|
|
// Delay for a moment
|
|
|
|
_system->delayMillis(10);
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
2008-07-09 13:33:36 +00:00
|
|
|
|
|
|
|
// push keydown & keyup events into the event manager
|
|
|
|
Common::Event evt;
|
|
|
|
evt.synthetic = false;
|
|
|
|
while (!_keyQueue.empty()) {
|
|
|
|
evt.kbd = _keyQueue.pop();
|
|
|
|
evt.type = Common::EVENT_KEYDOWN;
|
|
|
|
eventMan->pushEvent(evt);
|
|
|
|
evt.type = Common::EVENT_KEYUP;
|
|
|
|
eventMan->pushEvent(evt);
|
|
|
|
}
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::redraw() {
|
2008-07-07 21:10:58 +00:00
|
|
|
Graphics::SurfaceKeyColored surf;
|
|
|
|
|
|
|
|
surf.create(_system->getOverlayWidth(), _system->getOverlayHeight(), sizeof(OverlayColor));
|
|
|
|
|
2008-07-08 15:03:39 +00:00
|
|
|
_system->clearOverlay();
|
2008-07-07 21:10:58 +00:00
|
|
|
_system->grabOverlay((OverlayColor*)surf.pixels, surf.w);
|
2008-07-08 15:03:39 +00:00
|
|
|
surf.blit(_currentMode->image, _kbdBound.left, _kbdBound.top, _system->RGBToColor(0xff, 0, 0xff));
|
2008-07-09 13:33:36 +00:00
|
|
|
|
2008-07-07 21:10:58 +00:00
|
|
|
_system->copyRectToOverlay((OverlayColor*)surf.pixels, surf.w, 0, 0, surf.w, surf.h);
|
|
|
|
|
|
|
|
surf.free();
|
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
_needRedraw = false;
|
|
|
|
}
|
|
|
|
|
2008-07-03 22:38:19 +00:00
|
|
|
} // end of namespace GUI
|