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"
|
|
|
|
#include "common/unzip.h"
|
|
|
|
|
|
|
|
namespace GUI {
|
|
|
|
|
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-07 14:52:30 +00:00
|
|
|
_loaded = _displaying = false;
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VirtualKeyboard::~VirtualKeyboard() {
|
2008-07-04 17:55:19 +00:00
|
|
|
// TODO: clean up event data pointers
|
|
|
|
delete _parser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::reset() {
|
|
|
|
// TODO: clean up event data pointers
|
|
|
|
_modes.clear();
|
|
|
|
_initialMode = _currentMode = 0;
|
|
|
|
_pos.x = _pos.y = 0;
|
|
|
|
_hAlignment = kAlignCentre;
|
|
|
|
_vAlignment = kAlignBottom;
|
|
|
|
_keyQueue.clear();
|
2008-07-07 14:30:11 +00:00
|
|
|
_keyDown = 0;
|
2008-07-04 17:55:19 +00:00
|
|
|
_displaying = false;
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualKeyboard::loadKeyboardPack(Common::String packName) {
|
2008-07-04 17:55:19 +00:00
|
|
|
// reset to default settings
|
|
|
|
reset();
|
|
|
|
|
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-04 17:55:19 +00:00
|
|
|
if (!_parser->parse())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!_initialMode)
|
|
|
|
warning("Initial mode of keyboard pack not defined");
|
|
|
|
|
|
|
|
ModeMap::iterator it;
|
|
|
|
for (it = _modes.begin(); it != _modes.end(); it++) {
|
|
|
|
// if no image then it means layout tag for the
|
|
|
|
// required resolution was missing from the mode tag.
|
|
|
|
if (!it->_value.image) {
|
|
|
|
warning("'%s' layout missing from '%s' mode", it->_value.resolution, it->_value.name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-07 14:52:30 +00:00
|
|
|
_loaded = true;
|
2008-07-04 17:55:19 +00:00
|
|
|
return true;
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-04 17:55:19 +00:00
|
|
|
void VirtualKeyboard::reposition()
|
|
|
|
{
|
|
|
|
// calculate keyboard co-ordinates
|
|
|
|
int16 scrW = _system->getOverlayWidth(), scrH = _system->getOverlayHeight();
|
|
|
|
int16 keyW = _currentMode->image->w, keyH = _currentMode->image->h;
|
|
|
|
if (scrW != keyW) {
|
|
|
|
switch (_hAlignment) {
|
|
|
|
case kAlignCentre:
|
|
|
|
_pos.x = (scrW - keyW) / 2;
|
|
|
|
break;
|
|
|
|
case kAlignRight:
|
|
|
|
_pos.x = scrW - keyW;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (scrH != keyH) {
|
|
|
|
switch (_vAlignment) {
|
|
|
|
case kAlignMiddle:
|
|
|
|
_pos.y = (scrH - keyH) / 2;
|
|
|
|
break;
|
|
|
|
case kAlignBottom:
|
|
|
|
_pos.y = scrH - keyH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::processClick(int16 x, int16 y)
|
|
|
|
{
|
|
|
|
x -= _pos.x;
|
|
|
|
y -= _pos.y;
|
|
|
|
if (x < 0 || x > _currentMode->image->w) return;
|
|
|
|
if (y < 0 || y > _currentMode->image->h) return;
|
|
|
|
|
|
|
|
Common::MapArea *area = _currentMode->imageMap.findMapArea(x, y);
|
|
|
|
if (!area) return;
|
|
|
|
if (!_currentMode->events.contains(area->getTarget())) return;
|
|
|
|
Event evt = _currentMode->events[area->getTarget()];
|
|
|
|
|
|
|
|
switch (evt.type) {
|
|
|
|
case kEventKey:
|
|
|
|
// add virtual keypress to queue
|
|
|
|
_keyQueue.push_back(*(Common::KeyState*)evt.data);
|
|
|
|
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;
|
|
|
|
reposition();
|
|
|
|
_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-04 17:55:19 +00:00
|
|
|
_currentMode = &_modes[newMode];
|
|
|
|
reposition();
|
2008-07-07 14:30:11 +00:00
|
|
|
_needRedraw = true;
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualKeyboard::show() {
|
2008-07-07 14:30:11 +00:00
|
|
|
switchMode(_initialMode);
|
2008-07-04 17:55:19 +00:00
|
|
|
_displaying = true;
|
|
|
|
runLoop();
|
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-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::runLoop() {
|
|
|
|
Common::EventManager *eventMan = _system->getEventManager();
|
|
|
|
|
|
|
|
_system->showOverlay();
|
|
|
|
// capture mouse clicks
|
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 14:30:11 +00:00
|
|
|
Common::Event event;
|
|
|
|
while (eventMan->pollEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case Common::EVENT_LBUTTONDOWN:
|
|
|
|
_mouseDown = event.mouse;
|
|
|
|
break;
|
|
|
|
case Common::EVENT_LBUTTONUP:
|
|
|
|
if (ABS(_mouseDown.x - event.mouse.x) < 5
|
|
|
|
&& ABS(_mouseDown.y - event.mouse.y) < 5)
|
|
|
|
processClick(event.mouse.x, event.mouse.y);
|
|
|
|
break;
|
|
|
|
case Common::EVENT_QUIT:
|
|
|
|
_system->quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-07-04 17:55:19 +00:00
|
|
|
}
|
2008-07-07 14:30:11 +00:00
|
|
|
// clear keyboard from overlay
|
|
|
|
_system->hideOverlay();
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
2008-07-07 14:30:11 +00:00
|
|
|
void VirtualKeyboard::redraw() {
|
|
|
|
_needRedraw = false;
|
|
|
|
_system->clearOverlay();
|
2008-07-04 17:55:19 +00:00
|
|
|
_system->copyRectToOverlay((OverlayColor*)_currentMode->image->pixels,
|
2008-07-07 14:30:11 +00:00
|
|
|
_currentMode->image->w, _pos.x, _pos.y,
|
2008-07-04 17:55:19 +00:00
|
|
|
_currentMode->image->w, _currentMode->image->h);
|
2008-07-07 14:30:11 +00:00
|
|
|
_system->updateScreen();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VirtualKeyboard::pollEvent(Common::Event &event) {
|
|
|
|
if (_displaying || (_keyQueue.empty() && !_keyDown))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
event.synthetic = false; // ???
|
|
|
|
if (_keyDown) {
|
|
|
|
event.type = Common::EVENT_KEYUP;
|
|
|
|
event.kbd = *_keyDown;
|
|
|
|
_keyQueue.remove_at(0);
|
|
|
|
_keyDown = 0;
|
|
|
|
} else {
|
|
|
|
_keyDown = _keyQueue.begin();
|
|
|
|
event.type = Common::EVENT_KEYDOWN;
|
|
|
|
event.kbd = *_keyDown;
|
|
|
|
}
|
|
|
|
return true;
|
2008-07-03 22:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end of namespace GUI
|