scummvm/engines/wage/gui.cpp

598 lines
15 KiB
C++
Raw Normal View History

/* 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.
*
* MIT License:
*
* Copyright (c) 2009 Alexei Svitkine, Eugene Sandulenko
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
2016-01-03 21:59:57 +01:00
#include "common/timer.h"
2015-12-31 18:50:34 +01:00
#include "common/unzip.h"
#include "graphics/cursorman.h"
2015-12-31 18:50:34 +01:00
#include "graphics/fonts/bdf.h"
#include "graphics/palette.h"
#include "graphics/primitives.h"
2016-01-09 01:25:23 +01:00
#include "wage/wage.h"
#include "wage/design.h"
#include "wage/entities.h"
#include "wage/gui.h"
2016-04-14 12:08:07 +02:00
#include "wage/macwindow.h"
#include "wage/macwindowmanager.h"
#include "wage/menu.h"
2016-01-02 02:39:47 +01:00
#include "wage/world.h"
namespace Wage {
static const byte palette[] = {
0, 0, 0, // Black
0x80, 0x80, 0x80, // Gray
0xff, 0xff, 0xff, // White
2016-04-14 12:08:07 +02:00
0x00, 0xff, 0x00, // Green
2016-04-15 10:15:29 +02:00
0x00, 0xcf, 0x00 // Green2
};
static byte fillPatterns[][8] = { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, // kPatternSolid
{ 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }, // kPatternStripes
{ 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa } // kPatternCheckers2
};
static const byte macCursorArrow[] = {
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 0, 2, 3, 3, 3, 3, 3, 3, 3, 3,
2, 0, 0, 2, 3, 3, 3, 3, 3, 3, 3,
2, 0, 0, 0, 2, 3, 3, 3, 3, 3, 3,
2, 0, 0, 0, 0, 2, 3, 3, 3, 3, 3,
2, 0, 0, 0, 0, 0, 2, 3, 3, 3, 3,
2, 0, 0, 0, 0, 0, 0, 2, 3, 3, 3,
2, 0, 0, 0, 0, 0, 0, 0, 2, 3, 3,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3,
2, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2,
2, 0, 0, 2, 0, 0, 2, 3, 3, 3, 3,
2, 0, 2, 3, 2, 0, 0, 2, 3, 3, 3,
2, 2, 3, 3, 2, 0, 0, 2, 3, 3, 3,
2, 3, 3, 3, 3, 2, 0, 0, 2, 3, 3,
3, 3, 3, 3, 3, 2, 0, 0, 2, 3, 3,
3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3
};
static const byte macCursorBeam[] = {
0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 3,
3, 3, 0, 3, 0, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 0, 3, 3, 3, 3, 3, 3, 3,
3, 3, 0, 3, 0, 3, 3, 3, 3, 3, 3,
0, 0, 3, 3, 3, 0, 0, 3, 3, 3, 3,
};
2016-02-03 23:27:55 +01:00
static void cursorTimerHandler(void *refCon) {
2016-01-03 21:59:57 +01:00
Gui *gui = (Gui *)refCon;
int x = gui->_cursorX;
int y = gui->_cursorY;
if (x == 0 && y == 0)
return;
if (!gui->_screen.getPixels())
return;
2016-04-24 10:47:09 +02:00
x += gui->_consoleWindow->getInnerDimensions().left;
y += gui->_consoleWindow->getInnerDimensions().top;
2016-01-03 21:59:57 +01:00
2016-01-04 00:13:28 +01:00
gui->_screen.vLine(x, y, y + kCursorHeight, gui->_cursorState ? kColorBlack : kColorWhite);
2016-01-04 00:09:10 +01:00
if (!gui->_cursorOff)
gui->_cursorState = !gui->_cursorState;
2016-01-03 21:59:57 +01:00
gui->_cursorRect.left = x;
gui->_cursorRect.right = MIN<uint16>(x + 1, gui->_screen.w);
gui->_cursorRect.top = y;
gui->_cursorRect.bottom = MIN<uint16>(y + kCursorHeight, gui->_screen.h);
gui->_cursorDirty = true;
2016-01-03 21:59:57 +01:00
}
static bool sceneWindowCallback(WindowClick click, Common::Event &event, void *gui);
static bool consoleWindowCallback(WindowClick click, Common::Event &event, void *gui);
2016-01-02 02:39:47 +01:00
Gui::Gui(WageEngine *engine) {
_engine = engine;
_scene = NULL;
_sceneDirty = true;
2016-01-02 02:53:30 +01:00
_consoleDirty = true;
_menuDirty = true;
_cursorDirty = false;
2016-01-03 15:31:24 +01:00
_consoleFullRedraw = true;
_screen.create(g_system->getWidth(), g_system->getHeight(), Graphics::PixelFormat::createFormatCLUT8());
2016-04-18 10:36:09 +02:00
_wm.setScreen(&_screen);
2015-12-31 17:45:36 +01:00
_scrollPos = 0;
2016-01-03 15:31:24 +01:00
_consoleLineHeight = 8; // Dummy value which makes sense
_consoleNumLines = 24; // Dummy value
2015-12-31 18:50:34 +01:00
_builtInFonts = false;
2015-12-31 17:45:36 +01:00
2016-01-03 21:59:57 +01:00
_cursorX = 0;
_cursorY = 0;
_cursorState = false;
2016-01-04 00:09:10 +01:00
_cursorOff = false;
2016-01-03 21:59:57 +01:00
_inTextSelection = false;
_selectionStartX = _selectionStartY = -1;
_selectionEndX = _selectionEndY = -1;
2016-02-08 18:26:16 +01:00
_inputTextLineNum = 0;
2016-04-15 10:15:29 +02:00
g_system->getPaletteManager()->setPalette(palette, 0, ARRAYSIZE(palette) / 3);
CursorMan.replaceCursorPalette(palette, 0, 4);
CursorMan.replaceCursor(macCursorArrow, 11, 16, 1, 1, 3);
_cursorIsArrow = true;
CursorMan.showMouse(true);
for (int i = 0; i < ARRAYSIZE(fillPatterns); i++)
_patterns.push_back(fillPatterns[i]);
2015-12-31 18:50:34 +01:00
loadFonts();
2016-01-03 21:59:57 +01:00
2016-02-03 23:27:55 +01:00
g_system->getTimerManager()->installTimerProc(&cursorTimerHandler, 200000, this, "wageCursor");
_menu = new Menu(this);
2016-04-14 18:26:25 +02:00
2016-04-25 10:56:45 +02:00
_sceneWindow = _wm.addWindow(false, false);
_sceneWindow->setCallback(sceneWindowCallback, this);
2016-04-25 10:56:45 +02:00
_consoleWindow = _wm.addWindow(true, true);
_consoleWindow->setCallback(consoleWindowCallback, this);
}
Gui::~Gui() {
2016-01-06 23:40:41 +01:00
_screen.free();
2016-01-07 01:24:42 +01:00
_console.free();
2016-02-03 23:27:55 +01:00
g_system->getTimerManager()->removeTimerProc(&cursorTimerHandler);
delete _menu;
}
void Gui::undrawCursor() {
_cursorOff = true;
_cursorState = false;
cursorTimerHandler(this);
_cursorOff = false;
}
2016-01-03 12:39:29 +01:00
const Graphics::Font *Gui::getFont(const char *name, Graphics::FontManager::FontUsage fallback) {
2016-02-14 17:33:52 +01:00
const Graphics::Font *font = 0;
2016-01-03 12:39:29 +01:00
if (!_builtInFonts) {
font = FontMan.getFontByName(name);
if (!font)
warning("Cannot load font %s", name);
}
if (_builtInFonts || !font)
font = FontMan.getFontByUsage(fallback);
return font;
}
const Graphics::Font *Gui::getTitleFont() {
return getFont("Chicago-12", Graphics::FontManager::kBigGUIFont);
}
void Gui::draw() {
if (_engine->_isGameOver) {
if (_menuDirty) {
2016-04-24 13:51:17 +02:00
_wm.setFullRefresh(true);
_wm.draw();
_menu->render();
}
_menuDirty = false;
return;
}
2016-04-19 12:51:15 +02:00
if (!_engine->_world->_player->_currentScene)
return;
if (_scene != _engine->_world->_player->_currentScene) {
2016-04-05 18:18:25 +02:00
_sceneDirty = true;
2016-04-19 12:51:15 +02:00
_scene = _engine->_world->_player->_currentScene;
_sceneWindow->setDimensions(*_scene->_designBounds);
_sceneWindow->setTitle(_scene->_name);
_consoleWindow->setDimensions(*_scene->_textBounds);
2016-04-18 18:39:45 +02:00
_wm.setFullRefresh(true);
}
2016-04-18 18:39:45 +02:00
drawScene();
drawConsole();
_wm.draw();
2016-01-02 02:53:30 +01:00
if (_menuDirty)
_menu->render();
2015-12-30 01:58:42 +01:00
if (_cursorDirty && _cursorRect.left < _screen.w && _cursorRect.bottom < _screen.h) {
g_system->copyRectToScreen(_screen.getBasePtr(_cursorRect.left, _cursorRect.top), _screen.pitch,
_cursorRect.left, _cursorRect.top, _cursorRect.width(), _cursorRect.height());
_cursorDirty = false;
}
2016-01-02 02:53:30 +01:00
_sceneDirty = false;
_consoleDirty = false;
_menuDirty = false;
2016-01-03 15:31:24 +01:00
_consoleFullRedraw = false;
}
2016-04-05 09:39:16 +02:00
void Gui::drawScene() {
2016-04-21 21:37:31 +02:00
if (!_sceneDirty)
return;
_scene->paint(_sceneWindow->getSurface(), 0, 0);
_sceneWindow->setDirty(true);
2016-04-15 10:08:33 +02:00
2016-04-05 09:39:16 +02:00
_sceneDirty = true;
_consoleDirty = true;
_menuDirty = true;
_consoleFullRedraw = true;
}
static bool sceneWindowCallback(WindowClick click, Common::Event &event, void *g) {
Gui *gui = (Gui *)g;
return gui->processSceneEvents(click, event);
}
bool Gui::processSceneEvents(WindowClick click, Common::Event &event) {
if (_cursorIsArrow == false) {
CursorMan.replaceCursor(macCursorArrow, 11, 16, 1, 1, 3);
_cursorIsArrow = true;
}
if (click == kBorderInner && event.type == Common::EVENT_LBUTTONUP) {
Designed *obj = _scene->lookUpEntity(event.mouse.x - _sceneWindow->getDimensions().left,
event.mouse.y - _sceneWindow->getDimensions().top);
if (obj != nullptr)
_engine->processTurn(NULL, obj);
return true;
}
return false;
}
2016-04-05 09:39:16 +02:00
// Render console
void Gui::drawConsole() {
2016-04-21 21:37:31 +02:00
if (!_consoleDirty && !_consoleFullRedraw && !_sceneDirty)
2016-04-05 18:18:25 +02:00
return;
2016-04-05 09:39:16 +02:00
renderConsole(_consoleWindow->getSurface(), Common::Rect(kBorderWidth - 2, kBorderWidth - 2,
2016-04-24 10:47:09 +02:00
_consoleWindow->getDimensions().width(), _consoleWindow->getDimensions().height()));
_consoleWindow->setDirty(true);
2016-04-05 09:39:16 +02:00
}
static bool consoleWindowCallback(WindowClick click, Common::Event &event, void *g) {
Gui *gui = (Gui *)g;
return gui->processConsoleEvents(click, event);
}
bool Gui::processConsoleEvents(WindowClick click, Common::Event &event) {
if (click != kBorderInner && _cursorIsArrow == false) {
CursorMan.replaceCursor(macCursorArrow, 11, 16, 1, 1, 3);
_cursorIsArrow = true;
}
2016-04-20 00:56:04 +02:00
if (click == kBorderScrollUp || click == kBorderScrollDown) {
if (event.type == Common::EVENT_LBUTTONDOWN) {
2016-04-24 10:47:09 +02:00
int consoleHeight = _consoleWindow->getInnerDimensions().height();
int textFullSize = _lines.size() * _consoleLineHeight + consoleHeight;
2016-04-20 00:56:04 +02:00
float scrollPos = (float)_scrollPos / textFullSize;
2016-04-24 10:47:09 +02:00
float scrollSize = (float)consoleHeight / textFullSize;
2016-03-22 19:29:39 +01:00
2016-04-20 00:56:04 +02:00
_consoleWindow->setScroll(scrollPos, scrollSize);
return true;
2016-04-20 00:56:04 +02:00
} else if (event.type == Common::EVENT_LBUTTONUP) {
int oldScrollPos = _scrollPos;
2016-04-20 00:56:04 +02:00
switch (click) {
case kBorderScrollUp:
_scrollPos = MAX<int>(0, _scrollPos - _consoleLineHeight);
undrawCursor();
_cursorY -= (_scrollPos - oldScrollPos);
_consoleDirty = true;
2016-04-24 13:51:17 +02:00
_consoleFullRedraw = true;
2016-04-20 00:56:04 +02:00
break;
case kBorderScrollDown:
_scrollPos = MIN<int>((_lines.size() - 2) * _consoleLineHeight, _scrollPos + _consoleLineHeight);
undrawCursor();
_cursorY -= (_scrollPos - oldScrollPos);
_consoleDirty = true;
_consoleFullRedraw = true;
break;
default:
return false;
}
return true;
}
return false;
}
if (click == kBorderResizeButton) {
_consoleDirty = true;
_consoleFullRedraw = true;
return true;
}
if (click == kBorderInner) {
if (event.type == Common::EVENT_LBUTTONDOWN) {
startMarking(event.mouse.x, event.mouse.y);
return true;
} else if (event.type == Common::EVENT_LBUTTONUP) {
if (_inTextSelection) {
_inTextSelection = false;
if (_selectionEndY == -1 ||
(_selectionEndX == _selectionStartX && _selectionEndY == _selectionStartY)) {
_selectionStartY = _selectionEndY = -1;
_consoleFullRedraw = true;
_menu->enableCommand(kMenuEdit, kMenuActionCopy, false);
} else {
_menu->enableCommand(kMenuEdit, kMenuActionCopy, true);
bool cutAllowed = false;
if (_selectionStartY == _selectionEndY && _selectionStartY == (int)_lines.size() - 1)
cutAllowed = true;
_menu->enableCommand(kMenuEdit, kMenuActionCut, cutAllowed);
_menu->enableCommand(kMenuEdit, kMenuActionClear, cutAllowed);
}
}
return true;
} else if (event.type == Common::EVENT_MOUSEMOVE) {
if (_inTextSelection) {
updateTextSelection(event.mouse.x, event.mouse.y);
return true;
}
if (_cursorIsArrow) {
CursorMan.replaceCursor(macCursorBeam, 11, 16, 3, 8, 3);
_cursorIsArrow = false;
}
}
return false;
}
return false;
}
2015-12-31 18:50:34 +01:00
void Gui::loadFonts() {
Common::Archive *dat;
dat = Common::makeZipArchive("wage.dat");
if (!dat) {
warning("Could not find wage.dat. Falling back to built-in fonts");
_builtInFonts = true;
return;
2015-12-31 18:50:34 +01:00
}
Common::ArchiveMemberList list;
dat->listMembers(list);
for (Common::ArchiveMemberList::iterator it = list.begin(); it != list.end(); ++it) {
Common::SeekableReadStream *stream = dat->createReadStreamForMember((*it)->getName());
Graphics::BdfFont *font = Graphics::BdfFont::loadFont(*stream);
delete stream;
Common::String fontName = (*it)->getName();
// Trim the .bdf extension
for (int i = fontName.size() - 1; i >= 0; --i) {
if (fontName[i] == '.') {
while ((uint)i < fontName.size()) {
fontName.deleteLastChar();
}
break;
}
}
FontMan.assignFontToName(fontName, font);
debug(2, " %s", fontName.c_str());
}
_builtInFonts = false;
delete dat;
}
2016-01-29 18:31:19 +01:00
void Gui::regenCommandsMenu() {
_menu->regenCommandsMenu();
}
void Gui::regenWeaponsMenu() {
_menu->regenWeaponsMenu();
}
2016-02-02 20:21:19 +01:00
void Gui::processMenuShortCut(byte flags, uint16 ascii) {
_menu->processMenuShortCut(flags, ascii);
}
void Gui::mouseMove(int x, int y) {
if (_menu->_menuActivated) {
if (_menu->mouseMove(x, y))
_menuDirty = true;
return;
}
}
2016-02-04 18:59:36 +01:00
void Gui::pushArrowCursor() {
CursorMan.pushCursor(macCursorArrow, 11, 16, 1, 1, 3);
}
void Gui::popCursor() {
CursorMan.popCursor();
}
bool Gui::processEvent(Common::Event &event) {
if (_wm.processEvent(event))
return true;
switch (event.type) {
case Common::EVENT_MOUSEMOVE:
mouseMove(event.mouse.x, event.mouse.y);
break;
case Common::EVENT_LBUTTONDOWN:
mouseDown(event.mouse.x, event.mouse.y);
break;
case Common::EVENT_LBUTTONUP:
2016-04-20 00:59:21 +02:00
mouseUp(event.mouse.x, event.mouse.y);
break;
default:
return false;
}
return true;
}
2016-04-20 00:59:21 +02:00
void Gui::mouseUp(int x, int y) {
if (_menu->_menuActivated) {
2016-01-11 20:46:57 +01:00
if (_menu->mouseRelease(x, y)) {
_sceneDirty = true;
_consoleDirty = true;
_menuDirty = true;
2016-01-11 20:46:57 +01:00
}
2016-04-20 00:59:21 +02:00
return;
}
2016-04-20 00:59:21 +02:00
return;
2016-01-01 18:48:16 +01:00
}
void Gui::mouseDown(int x, int y) {
if (_menu->mouseClick(x, y)) {
2016-01-11 20:17:38 +01:00
_menuDirty = true;
}
}
int Gui::calcTextX(int x, int textLine) {
2016-02-03 11:24:50 +01:00
const Graphics::Font *font = getConsoleFont();
2016-02-14 15:25:44 +01:00
if ((uint)textLine >= _lines.size())
return 0;
2016-02-03 11:24:50 +01:00
Common::String str = _lines[textLine];
2016-04-24 10:47:09 +02:00
x -= _consoleWindow->getInnerDimensions().left;
2016-02-03 11:24:50 +01:00
for (int i = str.size(); i >= 0; i--) {
if (font->getStringWidth(str) < x) {
return i;
2016-02-03 11:24:50 +01:00
}
str.deleteLastChar();
}
return 0;
}
int Gui::calcTextY(int y) {
2016-04-24 10:47:09 +02:00
y -= _consoleWindow->getInnerDimensions().top;
if (y < 0)
y = 0;
const int firstLine = _scrollPos / _consoleLineHeight;
int textLine = (y - _scrollPos % _consoleLineHeight) / _consoleLineHeight + firstLine;
return textLine;
}
void Gui::startMarking(int x, int y) {
_selectionStartY = calcTextY(y);
_selectionStartX = calcTextX(x, _selectionStartY);
_selectionEndY = -1;
_inTextSelection = true;
}
void Gui::updateTextSelection(int x, int y) {
_selectionEndY = calcTextY(y);
2016-02-04 18:15:29 +01:00
_selectionEndX = calcTextX(x, _selectionEndY);
_consoleFullRedraw = true;
2016-01-11 20:17:38 +01:00
}
} // End of namespace Wage