scummvm/engines/wage/gui.cpp

367 lines
9.9 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 20:59:57 +00:00
#include "common/timer.h"
#include "common/system.h"
#include "graphics/cursorman.h"
#include "graphics/primitives.h"
2016-07-29 09:43:28 +00:00
#include "graphics/macgui/macwindowmanager.h"
2016-07-29 09:45:52 +00:00
#include "graphics/macgui/macwindow.h"
2016-07-29 09:18:10 +00:00
#include "graphics/macgui/macmenu.h"
2016-01-09 00:25:23 +00:00
#include "wage/wage.h"
#include "wage/design.h"
#include "wage/entities.h"
#include "wage/gui.h"
2016-01-02 01:39:47 +00:00
#include "wage/world.h"
namespace Wage {
static const Graphics::MenuData menuSubItems[] = {
{ kMenuHighLevel, "File", 0, 0, false },
{ kMenuHighLevel, "Edit", 0, 0, false },
{ kMenuFile, "New", kMenuActionNew, 0, false },
{ kMenuFile, "Open...", kMenuActionOpen, 0, true },
{ kMenuFile, "Close", kMenuActionClose, 0, true },
{ kMenuFile, "Save", kMenuActionSave, 0, true },
{ kMenuFile, "Save as...", kMenuActionSaveAs, 0, true },
{ kMenuFile, "Revert", kMenuActionRevert, 0, false },
{ kMenuFile, "Quit", kMenuActionQuit, 0, true },
{ kMenuEdit, "Undo", kMenuActionUndo, 'Z', false },
{ kMenuEdit, NULL, 0, 0, false },
{ kMenuEdit, "Cut", kMenuActionCut, 'K', false },
{ kMenuEdit, "Copy", kMenuActionCopy, 'C', false },
{ kMenuEdit, "Paste", kMenuActionPaste, 'V', false },
{ kMenuEdit, "Clear", kMenuActionClear, 'B', false },
{ 0, NULL, 0, 0, false }
};
2016-02-03 22:27:55 +00:00
static void cursorTimerHandler(void *refCon) {
2016-07-20 20:09:58 +00:00
Gui *gui = (Gui *)refCon;
2016-01-03 20:59:57 +00:00
int x = gui->_cursorX;
int y = gui->_cursorY;
if (x == 0 && y == 0)
return;
if (!gui->_screen.getPixels())
return;
2016-04-24 08:47:09 +00:00
x += gui->_consoleWindow->getInnerDimensions().left;
y += gui->_consoleWindow->getInnerDimensions().top;
2016-01-03 20:59:57 +00:00
2016-01-03 23:13:28 +00:00
gui->_screen.vLine(x, y, y + kCursorHeight, gui->_cursorState ? kColorBlack : kColorWhite);
2016-01-03 23:09:10 +00:00
if (!gui->_cursorOff)
gui->_cursorState = !gui->_cursorState;
2016-01-03 20:59:57 +00: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 20:59:57 +00:00
}
2016-07-29 09:44:38 +00:00
static bool sceneWindowCallback(Graphics::WindowClick click, Common::Event &event, void *gui);
static bool consoleWindowCallback(Graphics::WindowClick click, Common::Event &event, void *gui);
static void menuCommandsCallback(int action, Common::String &text, void *data);
2016-01-02 01:39:47 +00:00
Gui::Gui(WageEngine *engine) {
_engine = engine;
_scene = NULL;
_sceneDirty = true;
2016-01-02 01:53:30 +00:00
_consoleDirty = true;
_cursorDirty = false;
2016-01-03 14:31:24 +00:00
_consoleFullRedraw = true;
_screen.create(g_system->getWidth(), g_system->getHeight(), Graphics::PixelFormat::createFormatCLUT8());
2016-04-18 08:36:09 +00:00
_wm.setScreen(&_screen);
2015-12-31 16:45:36 +00:00
_scrollPos = 0;
2016-01-03 14:31:24 +00:00
_consoleLineHeight = 8; // Dummy value which makes sense
_consoleNumLines = 24; // Dummy value
2015-12-31 16:45:36 +00:00
2016-01-03 20:59:57 +00:00
_cursorX = 0;
_cursorY = 0;
_cursorState = false;
2016-01-03 23:09:10 +00:00
_cursorOff = false;
2016-01-03 20:59:57 +00:00
_inTextSelection = false;
_selectionStartX = _selectionStartY = -1;
_selectionEndX = _selectionEndY = -1;
2016-02-08 17:26:16 +00:00
_inputTextLineNum = 0;
2016-02-03 22:27:55 +00:00
g_system->getTimerManager()->installTimerProc(&cursorTimerHandler, 200000, this, "wageCursor");
_menu = _wm.addMenu();
2016-04-14 16:26:25 +00:00
_menu->setCommandsCallback(menuCommandsCallback, this);
_menu->addStaticMenus(menuSubItems);
_menu->addMenuSubItem(kMenuAbout, _engine->_world->getAboutMenuItemName(), kMenuActionAbout);
_commandsMenuId = _menu->addMenuItem(_engine->_world->_commandsMenuName.c_str());
regenCommandsMenu();
if (!_engine->_world->_weaponMenuDisabled) {
_weaponsMenuId = _menu->addMenuItem(_engine->_world->_weaponsMenuName.c_str());
regenWeaponsMenu();
} else {
_weaponsMenuId = -1;
}
_menu->calcDimensions();
2016-04-28 10:09:08 +00:00
_sceneWindow = _wm.addWindow(false, false, false);
_sceneWindow->setCallback(sceneWindowCallback, this);
2016-04-28 10:09:08 +00:00
_consoleWindow = _wm.addWindow(true, true, true);
_consoleWindow->setCallback(consoleWindowCallback, this);
}
Gui::~Gui() {
2016-01-06 22:40:41 +00:00
_screen.free();
2016-01-07 00:24:42 +00:00
_console.free();
2016-02-03 22:27:55 +00:00
g_system->getTimerManager()->removeTimerProc(&cursorTimerHandler);
}
void Gui::undrawCursor() {
_cursorOff = true;
_cursorState = false;
cursorTimerHandler(this);
_cursorOff = false;
}
void Gui::draw() {
if (_engine->_isGameOver) {
_wm.draw();
return;
}
2016-04-19 10:51:15 +00:00
if (!_engine->_world->_player->_currentScene)
return;
if (_scene != _engine->_world->_player->_currentScene) {
2016-04-05 16:18:25 +00:00
_sceneDirty = true;
2016-04-19 10:51:15 +00:00
_scene = _engine->_world->_player->_currentScene;
_sceneWindow->setDimensions(*_scene->_designBounds);
_sceneWindow->setTitle(_scene->_name);
_consoleWindow->setDimensions(*_scene->_textBounds);
2016-04-18 16:39:45 +00:00
_wm.setFullRefresh(true);
}
2016-04-18 16:39:45 +00:00
drawScene();
drawConsole();
_wm.draw();
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 01:53:30 +00:00
_sceneDirty = false;
_consoleDirty = false;
2016-01-03 14:31:24 +00:00
_consoleFullRedraw = false;
}
2016-04-05 07:39:16 +00:00
void Gui::drawScene() {
2016-04-21 19:37:31 +00:00
if (!_sceneDirty)
return;
_scene->paint(_sceneWindow->getSurface(), 0, 0);
_sceneWindow->setDirty(true);
2016-04-15 08:08:33 +00:00
2016-04-05 07:39:16 +00:00
_sceneDirty = true;
_consoleDirty = true;
_menu->setDirty(true);
2016-04-05 07:39:16 +00:00
_consoleFullRedraw = true;
}
2016-07-29 09:44:38 +00:00
static bool sceneWindowCallback(Graphics::WindowClick click, Common::Event &event, void *g) {
Gui *gui = (Gui *)g;
return gui->processSceneEvents(click, event);
}
2016-07-29 09:44:38 +00:00
bool Gui::processSceneEvents(Graphics::WindowClick click, Common::Event &event) {
if (click == Graphics::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 07:39:16 +00:00
// Render console
void Gui::drawConsole() {
2016-04-21 19:37:31 +00:00
if (!_consoleDirty && !_consoleFullRedraw && !_sceneDirty)
2016-04-05 16:18:25 +00:00
return;
2016-04-05 07:39:16 +00:00
2016-07-29 09:44:38 +00:00
renderConsole(_consoleWindow->getSurface(), Common::Rect(Graphics::kBorderWidth - 2, Graphics::kBorderWidth - 2,
2016-04-24 08:47:09 +00:00
_consoleWindow->getDimensions().width(), _consoleWindow->getDimensions().height()));
_consoleWindow->setDirty(true);
2016-04-05 07:39:16 +00:00
}
2016-07-29 09:44:38 +00:00
static bool consoleWindowCallback(Graphics::WindowClick click, Common::Event &event, void *g) {
Gui *gui = (Gui *)g;
return gui->processConsoleEvents(click, event);
}
////////////////
// Menu stuff
////////////////
2016-01-29 17:31:19 +00:00
void Gui::regenCommandsMenu() {
_menu->createSubMenuFromString(_commandsMenuId, _engine->_world->_commandsMenu.c_str());
2016-01-29 17:31:19 +00:00
}
void Gui::regenWeaponsMenu() {
if (_engine->_world->_weaponMenuDisabled)
return;
_menu->clearSubMenu(_weaponsMenuId);
Chr *player = _engine->_world->_player;
ObjArray *weapons = player->getWeapons(true);
bool empty = true;
for (uint i = 0; i < weapons->size(); i++) {
Obj *obj = (*weapons)[i];
if (obj->_type == Obj::REGULAR_WEAPON ||
obj->_type == Obj::THROW_WEAPON ||
obj->_type == Obj::MAGICAL_OBJECT) {
Common::String command(obj->_operativeVerb);
command += " ";
command += obj->_name;
_menu->addMenuSubItem(_weaponsMenuId, command.c_str(), kMenuActionCommand, 0, 0, true);
empty = false;
}
}
delete weapons;
if (empty)
_menu->addMenuSubItem(_weaponsMenuId, "You have no weapons", 0, 0, 0, false);
}
bool Gui::processEvent(Common::Event &event) {
return _wm.processEvent(event);
}
void menuCommandsCallback(int action, Common::String &text, void *data) {
Gui *g = (Gui *)data;
g->executeMenuCommand(action, text);
}
void Gui::executeMenuCommand(int action, Common::String &text) {
switch(action) {
case kMenuActionAbout:
case kMenuActionNew:
case kMenuActionClose:
case kMenuActionRevert:
case kMenuActionQuit:
break;
case kMenuActionOpen:
_engine->scummVMSaveLoadDialog(false);
break;
case kMenuActionSave:
case kMenuActionSaveAs:
_engine->scummVMSaveLoadDialog(true);
break;
case kMenuActionUndo:
actionUndo();
break;
case kMenuActionCut:
actionCut();
break;
case kMenuActionCopy:
actionCopy();
break;
case kMenuActionPaste:
actionPaste();
break;
case kMenuActionClear:
actionClear();
break;
case kMenuActionCommand:
_engine->processTurn(&text, NULL);
break;
default:
warning("Unknown action: %d", action);
}
}
} // End of namespace Wage