1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* 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.
*
*/
#include "common/events.h"
#include "common/stream.h"
#include "common/system.h"
#include "engines/stark/services/userinterface.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/staticprovider.h"
#include "engines/stark/ui/actionmenu.h"
#include "engines/stark/ui/cursor.h"
#include "engines/stark/ui/dialogpanel.h"
#include "engines/stark/ui/fmvplayer.h"
#include "engines/stark/ui/gamewindow.h"
#include "engines/stark/ui/inventorywindow.h"
#include "engines/stark/ui/topmenu.h"
namespace Stark {
UserInterface::UserInterface(Gfx::Driver *gfx) :
_gfx(gfx),
_cursor(nullptr),
_topMenu(nullptr),
_dialogPanel(nullptr),
_inventoryWindow(nullptr),
_exitGame(false),
_fmvPlayer(nullptr),
_actionMenu(nullptr),
_gameWindow(nullptr),
_interactive(true),
_interactionAttemptDenied(false),
_currentScreen(kScreenGame),
_gameWindowThumbnail(nullptr) {
}
UserInterface::~UserInterface() {
freeGameScreenThumbnail();
delete _gameWindow;
delete _actionMenu;
delete _topMenu;
delete _dialogPanel;
delete _inventoryWindow;
delete _fmvPlayer;
delete _cursor;
}
void UserInterface::init() {
// Game screen windows
_cursor = new Cursor(_gfx);
_topMenu = new TopMenu(_gfx, _cursor);
_dialogPanel = new DialogPanel(_gfx, _cursor);
_actionMenu = new ActionMenu(_gfx, _cursor);
_inventoryWindow = new InventoryWindow(_gfx, _cursor, _actionMenu);
_actionMenu->setInventory(_inventoryWindow);
_gameWindow = new GameWindow(_gfx, _cursor, _actionMenu, _inventoryWindow);
_gameScreenWindows.push_back(_actionMenu);
_gameScreenWindows.push_back(_inventoryWindow);
_gameScreenWindows.push_back(_gameWindow);
_gameScreenWindows.push_back(_topMenu);
_gameScreenWindows.push_back(_dialogPanel);
// FMV Screen window
_fmvPlayer = new FMVPlayer(_gfx, _cursor);
}
void UserInterface::update() {
StarkStaticProvider->onGameLoop();
// Check for UI mouse overs
dispatchEvent(&Window::handleMouseMove);
}
void UserInterface::handleMouseMove(const Common::Point &pos) {
_cursor->setMousePosition(pos);
}
void UserInterface::handleClick() {
dispatchEvent(&Window::handleClick);
}
void UserInterface::handleRightClick() {
dispatchEvent(&Window::handleRightClick);
}
void UserInterface::handleDoubleClick() {
dispatchEvent(&Window::handleDoubleClick);
}
void UserInterface::dispatchEvent(WindowHandler handler) {
switch (_currentScreen) {
case kScreenGame:
for (uint i = 0; i < _gameScreenWindows.size(); i++) {
if (_gameScreenWindows[i]->isMouseInside()) {
(*_gameScreenWindows[i].*handler)();
return;
}
}
break;
case kScreenFMV:
if (_fmvPlayer->isMouseInside()) {
(*_fmvPlayer.*handler)();
return;
}
break;
default: // Nothing goes here
break;
}
}
void UserInterface::inventoryOpen(bool open) {
// Make the inventory update its contents.
if (open) {
_inventoryWindow->open();
} else {
_inventoryWindow->close();
}
}
int16 UserInterface::getSelectedInventoryItem() const {
if (_inventoryWindow) {
return _inventoryWindow->getSelectedInventoryItem();
} else {
return -1;
}
}
void UserInterface::selectInventoryItem(int16 itemIndex) {
_inventoryWindow->setSelectedInventoryItem(itemIndex);
}
void UserInterface::requestFMVPlayback(const Common::String &name) {
// TODO: Save the current screen so that it can be restored when the playback ends
changeScreen(kScreenFMV);
_fmvPlayer->play(name);
}
void UserInterface::onFMVStopped() {
// TODO: Restore the previous screen
changeScreen(kScreenGame);
}
void UserInterface::changeScreen(Screen screen) {
_currentScreen = screen;
}
bool UserInterface::isInGameScreen() const {
return _currentScreen == kScreenGame;
}
bool UserInterface::isInventoryOpen() const {
return _inventoryWindow->isVisible();
}
bool UserInterface::skipFMV() {
if (_currentScreen == kScreenFMV) {
_fmvPlayer->stop();
return true;
}
return false;
}
void UserInterface::render() {
switch (_currentScreen) {
case kScreenGame:
for (int i = _gameScreenWindows.size() - 1; i >= 0; i--) {
_gameScreenWindows[i]->render();
}
break;
case kScreenFMV:
_fmvPlayer->render();
break;
default: // Nothing goes here
break;
}
// The cursor depends on the UI being done.
_cursor->render();
}
bool UserInterface::isInteractive() const {
return _interactive;
}
void UserInterface::setInteractive(bool interactive) {
if (interactive && !_interactive) {
StarkGlobal->setNormalSpeed();
} else if (!interactive && _interactive) {
_interactionAttemptDenied = false;
}
_interactive = interactive;
}
void UserInterface::markInteractionDenied() {
if (!_interactive) {
_interactionAttemptDenied = true;
}
}
bool UserInterface::wasInteractionDenied() const {
return !_interactive && _interactionAttemptDenied;
}
void UserInterface::clearLocationDependentState() {
_dialogPanel->reset();
_gameWindow->reset();
_inventoryWindow->reset();
}
void UserInterface::optionsOpen() {
// TODO: Open the TLJ menu instead of the ResidualVM one
Common::Event event;
event.type = Common::EVENT_MAINMENU;
g_system->getEventManager()->pushEvent(event);
}
void UserInterface::saveGameScreenThumbnail() {
freeGameScreenThumbnail();
Graphics::Surface *big = _gameWindow->getScreenshot();
assert(big->format.bytesPerPixel == 4);
_gameWindowThumbnail = new Graphics::Surface();
_gameWindowThumbnail->create(kThumbnailWidth, kThumbnailHeight, big->format);
uint32 *dst = (uint32 *)_gameWindowThumbnail->getPixels();
for (uint i = 0; i < _gameWindowThumbnail->h; i++) {
for (uint j = 0; j < _gameWindowThumbnail->w; j++) {
uint32 srcX = big->w * j / _gameWindowThumbnail->w;
uint32 srcY = big->h * i / _gameWindowThumbnail->h;
uint32 *src = (uint32 *)big->getBasePtr(srcX, srcY);
// Copy RGBA pixel
*dst++ = *src;
}
}
big->free();
delete big;
}
void UserInterface::freeGameScreenThumbnail() {
if (_gameWindowThumbnail) {
_gameWindowThumbnail->free();
delete _gameWindowThumbnail;
_gameWindowThumbnail = nullptr;
}
}
const Graphics::Surface *UserInterface::getGameWindowThumbnail() const {
return _gameWindowThumbnail;
}
void UserInterface::onScreenChanged() {
_dialogPanel->onScreenChanged();
}
} // End of namespace Stark
|