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
|
/* 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.
*
*/
/*
* Based on
* WebVenture (c) 2010, Sean Kasun
* https://github.com/mrkite/webventure, http://seancode.com/webventure/
*
* Used with explicit permission from the author
*/
#include "common/system.h"
#include "macventure/dialog.h"
namespace MacVenture {
Dialog::Dialog(Gui *gui, Common::Point pos, uint width, uint height) :
_gui(gui), _bounds(Common::Rect(pos.x, pos.y, pos.x + width, pos.y + height)) {}
Dialog::Dialog(Gui *gui, PrebuiltDialogs prebuilt) {
_gui = gui;
const PrebuiltDialog &dialog = g_prebuiltDialogs[prebuilt];
calculateBoundsFromPrebuilt(dialog.bounds);
for (int i = 0; dialog.elements[i].type != kDEEnd; i++) {
addPrebuiltElement(dialog.elements[i]);
}
}
Dialog::~Dialog() {
for (Common::Array<DialogElement*>::iterator it = _elements.begin(); it != _elements.end(); it++) {
delete *it;
}
}
void Dialog::handleDialogAction(DialogElement *trigger, DialogAction action) {
switch(action) {
case kDACloseDialog:
_gui->closeDialog();
break;
case kDASubmit:
_gui->setTextInput(_userInput);
_gui->closeDialog();
break;
case kDASaveAs:
_gui->saveGame();
_gui->closeDialog();
break;
case kDALoadGame:
_gui->loadGame();
_gui->closeDialog();
break;
case kDANewGame:
_gui->newGame();
_gui->closeDialog();
break;
case kDAQuit:
_gui->quitGame();
_gui->closeDialog();
break;
default:
break;
}
}
const Graphics::Font &Dialog::getFont() {
return _gui->getCurrentFont();
}
bool Dialog::processEvent(Common::Event event) {
for (Common::Array<DialogElement*>::iterator it = _elements.begin(); it != _elements.end(); it++) {
if ((*it)->processEvent(this, event)) {
return true;
}
}
return false;
}
void Dialog::addButton(Common::String title, MacVenture::DialogAction action, Common::Point position, uint width, uint height) {
_elements.push_back(new DialogButton(this, title, action, position, width, height));
}
void Dialog::addText(Common::String content, Common::Point position) {
_elements.push_back(new DialogPlainText(this, content, position));
}
void Dialog::addTextInput(Common::Point position, int width, int height) {
_elements.push_back(new DialogTextInput(this, position, width, height));
}
void Dialog::draw() {
Graphics::ManagedSurface compose;
// Compose the surface
compose.create(_bounds.width(), _bounds.height());
Common::Rect base(0, 0, _bounds.width(), _bounds.height());
compose.fillRect(base, kColorWhite);
compose.frameRect(base, kColorBlack);
for (Common::Array<DialogElement*>::iterator it = _elements.begin(); it != _elements.end(); it++) {
(*it)->draw(this, compose);
}
g_system->copyRectToScreen(compose.getPixels(), compose.pitch,
_bounds.left, _bounds.top, _bounds.width(), _bounds.height());
}
void Dialog::localize(Common::Point &point) {
point.x -= _bounds.left;
point.y -= _bounds.top;
}
void Dialog::setUserInput(Common::String content) {
_userInput = content;
}
void Dialog::addPrebuiltElement(const MacVenture::PrebuiltDialogElement &element) {
Common::Point position(element.left, element.top);
switch(element.type) {
case kDEButton:
addButton(element.title, element.action, position, element.width, element.height);
break;
case kDEPlainText:
addText(element.title, position);
break;
case kDETextInput:
addTextInput(position, element.width, element.height);
break;
default:
break;
}
}
// Dialog Element
DialogElement::DialogElement(Dialog *dialog, Common::String title, DialogAction action, Common::Point position, uint width, uint height) :
_text(title), _action(action) {
if (width == 0) {
width = dialog->getFont().getStringWidth(title);
}
if (height == 0) {
height = dialog->getFont().getFontHeight();
}
_bounds = Common::Rect(position.x, position.y, position.x + width, position.y + height);
}
bool DialogElement::processEvent(MacVenture::Dialog *dialog, Common::Event event) {
return doProcessEvent(dialog, event);
}
void DialogElement::draw(MacVenture::Dialog *dialog, Graphics::ManagedSurface &target) {
doDraw(dialog, target);
}
const Common::String &DialogElement::getText() {
return doGetText();
}
const Common::String &DialogElement::doGetText() {
return _text;
}
// CONCRETE DIALOG ELEMENTS
DialogButton::DialogButton(Dialog *dialog, Common::String title, DialogAction action, Common::Point position, uint width, uint height):
DialogElement(dialog, title, action, position, width, height) {}
bool DialogButton::doProcessEvent(MacVenture::Dialog *dialog, Common::Event event) {
Common::Point mouse = event.mouse;
if (event.type == Common::EVENT_LBUTTONDOWN) {
dialog->localize(mouse);
if (_bounds.contains(mouse)) {
debugC(2, kMVDebugGUI, "Click! Button: %s", _text.c_str());
dialog->handleDialogAction(this, _action);
return true;
}
}
return false;
}
void DialogButton::doDraw(MacVenture::Dialog *dialog, Graphics::ManagedSurface &target) {
target.fillRect(_bounds, kColorWhite);
target.frameRect(_bounds, kColorBlack);
// Draw title
dialog->getFont().drawString(
&target, _text, _bounds.left, _bounds.top, _bounds.width(), kColorBlack, Graphics::kTextAlignCenter);
}
DialogPlainText::DialogPlainText(Dialog *dialog, Common::String content, Common::Point position) :
DialogElement(dialog, content, kDANone, position, 0, 0) { }
DialogPlainText::~DialogPlainText() {}
bool DialogPlainText::doProcessEvent(MacVenture::Dialog *dialog, Common::Event event) {
return false;
}
void DialogPlainText::doDraw(MacVenture::Dialog *dialog, Graphics::ManagedSurface &target) {
// Draw contents
dialog->getFont().drawString(
&target, _text, _bounds.left, _bounds.top, _bounds.width(), kColorBlack, Graphics::kTextAlignCenter);
}
DialogTextInput::DialogTextInput(Dialog *dialog, Common::Point position, uint width, uint height) :
DialogElement(dialog, "", kDANone, position, width, height) {}
DialogTextInput::~DialogTextInput() {}
bool DialogTextInput::doProcessEvent(Dialog *dialog, Common::Event event) {
if (event.type == Common::EVENT_KEYDOWN) {
switch (event.kbd.keycode) {
case Common::KEYCODE_BACKSPACE:
if (!_text.empty()) {
_text.deleteLastChar();
dialog->setUserInput(_text);
return true;
}
break;
default:
if (event.kbd.ascii >= 0x20 && event.kbd.ascii <= 0x7f) {
_text += (char)event.kbd.ascii;
dialog->setUserInput(_text);
return true;
}
break;
}
}
return false;
}
void DialogTextInput::doDraw(MacVenture::Dialog *dialog, Graphics::ManagedSurface &target) {
target.fillRect(_bounds, kColorWhite);
target.frameRect(_bounds, kColorBlack);
dialog->getFont().drawString(&target, _text, _bounds.left, _bounds.top, _bounds.width(), kColorBlack);
}
void Dialog::calculateBoundsFromPrebuilt(const PrebuiltDialogBounds &bounds) {
_bounds = Common::Rect(
bounds.left,
bounds.top,
bounds.right,
bounds.bottom);
}
} // End of namespace MacVenture
|