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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
/* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/stark/ui/dialogbox.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/gfx/surfacerenderer.h"
#include "engines/stark/gfx/bitmap.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/fontprovider.h"
#include "engines/stark/ui/cursor.h"
#include "engines/stark/visual/text.h"
#include "common/memstream.h"
#include "common/stream.h"
#include "common/formats/winexe_pe.h"
#include "graphics/surface.h"
#include "image/bmp.h"
namespace Stark {
static const uint dialogMaxWidth = 450;
static const uint dialogHorizontalMargin = 10;
static const uint dialogVerticalMargin = 20;
static const uint buttonHorizontalMargin = 25;
static const uint buttonVerticalMargin = 5;
DialogBox::DialogBox(StarkEngine *vm, Gfx::Driver *gfx, Cursor *cursor) :
Window(gfx, cursor),
_background(nullptr),
_foreground(nullptr),
_confirmCallback(nullptr) {
_vm = vm;
_surfaceRenderer = gfx->createSurfaceRenderer();
_background = loadBackground(gfx);
if (_background) {
_background->setSamplingFilter(Gfx::Bitmap::kLinear);
}
_messageVisual = new VisualText(gfx);
_messageVisual->setColor(_textColor);
_messageVisual->setTargetWidth(dialogMaxWidth - 2 * dialogHorizontalMargin);
_messageVisual->setAlign(Graphics::kTextAlignCenter);
_confirmLabelVisual = new VisualText(gfx);
_confirmLabelVisual->setColor(_textColor);
_confirmLabelVisual->setTargetWidth(96);
_cancelLabelVisual = new VisualText(gfx);
_cancelLabelVisual->setColor(_textColor);
_cancelLabelVisual->setTargetWidth(96);
}
DialogBox::~DialogBox() {
close();
delete _messageVisual;
delete _confirmLabelVisual;
delete _cancelLabelVisual;
delete _background;
delete _surfaceRenderer;
}
void DialogBox::open(const Common::String &message, ConfirmCallback *confirmCallback,
const Common::String &confirmLabel, const Common::String &cancelLabel) {
assert(confirmCallback);
_visible = true;
_cursor->setCursorType(Cursor::kDefault);
_cursor->setMouseHint("");
_messageVisual->setText(message);
_confirmLabelVisual->setText(confirmLabel);
_cancelLabelVisual->setText(cancelLabel);
_confirmCallback = confirmCallback;
recomputeLayout();
}
void DialogBox::close() {
freeForeground();
delete _confirmCallback;
_confirmCallback = nullptr;
_visible = false;
}
void DialogBox::recomputeLayout() {
freeForeground();
_messageRect = _messageVisual->getRect();
uint buttonY = dialogVerticalMargin * 2 + _messageRect.height();
uint32 dialogWidth = _messageRect.width() + 2 * dialogHorizontalMargin;
// The button size is computed based on the largest label
Common::Rect labelSize = _confirmLabelVisual->getRect();
labelSize.extend(_cancelLabelVisual->getRect());
_confirmButtonRect = Common::Rect(
labelSize.width() + buttonHorizontalMargin * 2,
labelSize.height() + buttonVerticalMargin * 2);
_cancelButtonRect = Common::Rect(
labelSize.width() + buttonHorizontalMargin * 2,
labelSize.height() + buttonVerticalMargin * 2);
uint buttonSpacing;
if (dialogWidth > (uint32)(_confirmButtonRect.width() + _cancelButtonRect.width())) {
buttonSpacing = (dialogWidth - _confirmButtonRect.width() - _cancelButtonRect.width()) / 3;
} else {
buttonSpacing = buttonHorizontalMargin;
dialogWidth = buttonSpacing * 3 + _confirmButtonRect.width() + _cancelButtonRect.width();
}
_messageRect.translate((dialogWidth - _messageRect.width()) / 2, dialogVerticalMargin);
_confirmButtonRect.translate(buttonSpacing, buttonY);
_cancelButtonRect.translate(buttonSpacing * 2 + _confirmButtonRect.width(), buttonY);
uint32 dialogHeight = _confirmButtonRect.bottom + dialogVerticalMargin;
Graphics::Surface foreground;
foreground.create(dialogWidth, dialogHeight, Gfx::Driver::getRGBAPixelFormat());
drawBevel(&foreground, Common::Rect(0, 0, foreground.w, foreground.h));
uint32 buttonColor = foreground.format.RGBToColor(0, 0, 0);
foreground.fillRect(_confirmButtonRect, buttonColor);
foreground.fillRect(_cancelButtonRect, buttonColor);
drawBevel(&foreground, _confirmButtonRect);
drawBevel(&foreground, _cancelButtonRect);
_foreground = _gfx->createBitmap(&foreground);
_foreground->setSamplingFilter(Gfx::Bitmap::kLinear);
foreground.free();
Common::Point screenCenter(Gfx::Driver::kOriginalWidth / 2, Gfx::Driver::kOriginalHeight / 2);
_position = Common::Rect::center(screenCenter.x, screenCenter.y,
_foreground->width(), _foreground->height());
}
void DialogBox::freeForeground() {
delete _foreground;
_foreground = nullptr;
if (_messageVisual) {
_messageVisual->reset();
}
if (_confirmLabelVisual) {
_confirmLabelVisual->reset();
}
if (_cancelLabelVisual) {
_cancelLabelVisual->reset();
}
}
void DialogBox::onScreenChanged() {
recomputeLayout();
}
void DialogBox::onClick(const Common::Point &pos) {
if (_cancelButtonRect.contains(pos)) {
close();
} else if (_confirmButtonRect.contains(pos)) {
assert(_confirmCallback);
(*_confirmCallback)();
close();
}
}
void DialogBox::onKeyPress(const Common::CustomEventType customType) {
if (customType == kActionSkip) {
close();
}
}
Gfx::Bitmap *DialogBox::loadBackground(Gfx::Driver *gfx) {
Common::PEResources *executable = new Common::PEResources();
if (!executable->loadFromEXE("game.exe") && !executable->loadFromEXE("game.dll")) {
warning("Unable to load 'game.exe' to read the modal dialog background image");
delete executable;
return nullptr;
}
// As of Aug 2021:
// Steam version of The Longest Journey is 1.0.0.161 "Enno's two-CD Version"
// GOG version of The Longest Journey is 1.0.0.142 "RC1" (Special Build: "Paper Sun")
// Steam's game.exe does not contain a valid resource for the background bitmap id 147
// so we skip trying to retrieve it.
if (_vm->getGameFlags() & GF_MISSING_EXE_RESOURCES) {
warning("Steam version does not contain the modal dialog background bitmap in 'game.exe'. Using fallback color for dialog background...");
delete executable;
return nullptr;
}
Common::SeekableReadStream *stream = executable->getResource(Common::kWinBitmap, 147);
if (!stream) {
warning("Unable to find the modal dialog background bitmap in 'game.exe'");
delete executable;
return nullptr;
}
const uint32 bitmapWithHeaderLen = stream->size() + 14;
byte *bitmapWithHeader = new byte[bitmapWithHeaderLen];
Common::MemoryWriteStream bitmapWithHeaderWriteStream(bitmapWithHeader, bitmapWithHeaderLen);
bitmapWithHeaderWriteStream.write("BM", 2);
bitmapWithHeaderWriteStream.writeUint32LE(bitmapWithHeaderLen); // Filesize
bitmapWithHeaderWriteStream.writeUint32LE(0); // res1 & res2
bitmapWithHeaderWriteStream.writeUint32LE(0x436); // image offset
stream->read(bitmapWithHeader + 14, stream->size());
delete stream;
delete executable;
Common::MemoryReadStream bitmapWithHeaderReadStream(bitmapWithHeader, bitmapWithHeaderLen);
Image::BitmapDecoder decoder;
if (!decoder.loadStream(bitmapWithHeaderReadStream)) {
warning("Unable decode the modal dialog background bitmap from 'game.exe'");
return nullptr;
}
delete[] bitmapWithHeader;
return gfx->createBitmap(decoder.getSurface(), decoder.getPalette());
}
void DialogBox::onRender() {
if (_background) {
uint32 backgroundRepeatX = ceil(_foreground->width() / (float)_background->width());
for (uint i = 0; i < backgroundRepeatX; i++) {
_surfaceRenderer->render(_background, Common::Point(i * _background->width(), 0));
}
} else {
_surfaceRenderer->fill(_backgroundColor, Common::Point(0, 0), _foreground->width(), _foreground->height());
}
_surfaceRenderer->render(_foreground, Common::Point(0, 0));
_messageVisual->render(Common::Point(_messageRect.left, _messageRect.top));
Common::Rect confirmLabelRect = centerRect(_confirmButtonRect, _confirmLabelVisual->getRect());
Common::Rect cancelLabelRect = centerRect(_cancelButtonRect, _cancelLabelVisual->getRect());
_confirmLabelVisual->render(Common::Point(confirmLabelRect.left, confirmLabelRect.top));
_cancelLabelVisual->render(Common::Point(cancelLabelRect.left, cancelLabelRect.top));
}
void DialogBox::drawBevel(Graphics::Surface *surface, const Common::Rect &rect) {
uint32 topColor1 = surface->format.RGBToColor(191, 191, 191);
uint32 topColor2 = surface->format.RGBToColor(159, 159, 159);
uint32 bottomColor1 = surface->format.RGBToColor(64, 64, 64);
uint32 bottomColor2 = surface->format.RGBToColor(96, 96, 96);
// Top
surface->drawLine(rect.left, rect.top, rect.right - 1, rect.top, topColor1);
surface->drawLine(rect.left + 1, rect.top + 1, rect.right - 2, rect.top + 1, topColor2);
// Left
surface->drawLine(rect.left, rect.top, rect.left, rect.bottom - 1, topColor1);
surface->drawLine(rect.left + 1, rect.top + 1, rect.left + 1, rect.bottom - 2, topColor2);
// Right
surface->drawLine(rect.right - 1, rect.top, rect.right - 1, rect.bottom - 1, bottomColor2);
surface->drawLine(rect.right - 2, rect.top + 1, rect.right - 2, rect.bottom - 2, bottomColor1);
// Bottom
surface->drawLine(rect.left, rect.bottom - 1, rect.right - 1, rect.bottom - 1, bottomColor2);
surface->drawLine(rect.left + 1, rect.bottom - 2, rect.right - 2, rect.bottom - 2, bottomColor1);
}
Common::Rect DialogBox::centerRect(const Common::Rect &container, const Common::Rect &size) {
Common::Point center(
(container.left + container.right) / 2,
(container.top + container.bottom) / 2);
return Common::Rect::center(
center.x, center.y,
size.width(), size.height());
}
} // End of namespace Stark
|