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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/* 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 "common/system.h"
#include "graphics/screen.h"
#include "mm/mm1/events.h"
#include "mm/mm1/mm1.h"
#include "mm/mm1/gfx/gfx.h"
#include "mm/mm1/views/dialogs.h"
#include "mm/mm1/views_enh/dialogs.h"
namespace MM {
namespace MM1 {
Events *g_events;
Events::Events(bool enhancedMode) : UIElement("Root", nullptr),
_enhancedMode(enhancedMode) {
g_events = this;
}
Events::~Events() {
g_events = nullptr;
}
void Events::runGame() {
UIElement *allViews = _enhancedMode ?
(UIElement *)new ViewsEnh::Dialogs() :
(UIElement *)new Views::Dialogs();
uint currTime, nextFrameTime = 0;
_screen = new Graphics::Screen();
// Run the game
int saveSlot = ConfMan.getInt("save_slot");
if (saveSlot == -1 ||
g_engine->loadGameState(saveSlot).getCode() != Common::kNoError) {
addView("Title");
}
Common::Event e;
bool quitFlag = false;
while (!quitFlag) {
while (g_system->getEventManager()->pollEvent(e)) {
if (e.type == Common::EVENT_QUIT ||
e.type == Common::EVENT_RETURN_TO_LAUNCHER) {
quitFlag = true;
break;
} else {
processEvent(e);
}
}
g_system->delayMillis(10);
if ((currTime = g_system->getMillis()) >= nextFrameTime) {
nextFrameTime = currTime + FRAME_DELAY;
tick();
drawElements();
_screen->update();
}
quitFlag |= shouldQuit();
}
delete _screen;
delete allViews;
}
void Events::processEvent(Common::Event &ev) {
switch (ev.type) {
case Common::EVENT_KEYDOWN:
if (ev.kbd.keycode < Common::KEYCODE_NUMLOCK)
msgKeypress(KeypressMessage(ev.kbd));
break;
case Common::EVENT_CUSTOM_ENGINE_ACTION_START:
msgAction(ActionMessage((KeybindingAction)ev.customType));
break;
case Common::EVENT_LBUTTONDOWN:
case Common::EVENT_RBUTTONDOWN:
//case Common::EVENT_MBUTTONDOWN:
msgMouseDown(MouseDownMessage(ev.type, ev.mouse));
break;
case Common::EVENT_LBUTTONUP:
case Common::EVENT_RBUTTONUP:
//case Common::EVENT_MBUTTONUP:
msgMouseUp(MouseUpMessage(ev.type, ev.mouse));
break;
default:
break;
}
}
void Events::replaceView(UIElement *ui, bool replaceAllViews) {
assert(ui);
UIElement *priorView = focusedView();
if (replaceAllViews) {
clearViews();
} else if (!_views.empty()) {
priorView->msgUnfocus(UnfocusMessage());
_views.pop();
}
_views.push(ui);
ui->redraw();
ui->msgFocus(FocusMessage(priorView));
}
void Events::replaceView(const Common::String &name, bool replaceAllViews) {
replaceView(findView(name));
}
void Events::addView(UIElement *ui) {
assert(ui);
UIElement *priorView = focusedView();
if (!_views.empty())
priorView->msgUnfocus(UnfocusMessage());
_views.push(ui);
ui->redraw();
ui->msgFocus(FocusMessage(priorView));
}
void Events::addView(const Common::String &name) {
addView(findView(name));
}
void Events::popView() {
UIElement *priorView = focusedView();
priorView->msgUnfocus(UnfocusMessage());
_views.pop();
for (int i = 0; i < (int)_views.size() - 1; ++i) {
_views[i]->redraw();
_views[i]->draw();
}
if (!_views.empty()) {
UIElement *view = focusedView();
view->msgFocus(FocusMessage(priorView));
view->redraw();
view->draw();
}
}
void Events::redrawViews() {
for (uint i = 0; i < _views.size(); ++i) {
_views[i]->redraw();
_views[i]->draw();
}
}
bool Events::isPresent(const Common::String &name) const {
for (uint i = 0; i < _views.size(); ++i) {
if (_views[i]->_name == name)
return true;
}
return false;
}
void Events::clearViews() {
if (!_views.empty())
focusedView()->msgUnfocus(UnfocusMessage());
_views.clear();
}
void Events::addKeypress(const Common::KeyCode kc) {
Common::KeyState ks;
ks.keycode = kc;
if (kc >= Common::KEYCODE_SPACE && kc <= Common::KEYCODE_TILDE)
ks.ascii = kc;
focusedView()->msgKeypress(KeypressMessage(ks));
}
void Events::addAction(KeybindingAction action) {
focusedView()->msgAction(ActionMessage(action));
}
bool Events::isKeypressPending() const {
// TODO: Currently the engine doesn't cache keypresses, but rather
// processes them immediately after each is pulled from the
// SDL event queue. So for this to work, we'd need to rework
// the event handler code
return false;
}
/*------------------------------------------------------------------------*/
Bounds::Bounds(Common::Rect &innerBounds) :
_bounds(0, 0, 320, 200),
_innerBounds(innerBounds),
left(_bounds.left), top(_bounds.top),
right(_bounds.right), bottom(_bounds.bottom) {
}
Bounds &Bounds::operator=(const Common::Rect &r) {
_bounds = r;
_innerBounds = r;
_innerBounds.grow(-_borderSize);
return *this;
}
void Bounds::setBorderSize(size_t borderSize) {
_borderSize = borderSize;
_innerBounds = *this;
_innerBounds.grow(-_borderSize);
}
/*------------------------------------------------------------------------*/
UIElement::UIElement(const Common::String &name, UIElement *uiParent) :
_name(name), _parent(uiParent),
_bounds(_innerBounds) {
if (_parent)
_parent->_children.push_back(this);
}
void UIElement::redraw() {
_needsRedraw = true;
for (size_t i = 0; i < _children.size(); ++i)
_children[i]->redraw();
}
void UIElement::drawElements() {
if (_needsRedraw) {
draw();
_needsRedraw = false;
}
for (size_t i = 0; i < _children.size(); ++i)
_children[i]->drawElements();
}
UIElement *UIElement::findViewGlobally(const Common::String &name) {
return g_events->findView(name);
}
void UIElement::focus() {
g_engine->replaceView(this);
}
void UIElement::close() {
assert(g_engine->focusedView() == this);
g_engine->popView();
}
bool UIElement::isFocused() const {
return g_events->focusedView() == this;
}
void UIElement::clearSurface() {
Graphics::ManagedSurface s = getSurface();
s.fillRect(Common::Rect(s.w, s.h), 0);
}
void UIElement::draw() {
for (size_t i = 0; i < _children.size(); ++i) {
_children[i]->draw();
}
}
bool UIElement::tick() {
if (_timeoutCtr && --_timeoutCtr == 0) {
timeout();
}
for (size_t i = 0; i < _children.size(); ++i) {
if (_children[i]->tick())
return true;
}
return false;
}
UIElement *UIElement::findView(const Common::String &name) {
if (_name.equalsIgnoreCase(name))
return this;
UIElement *result;
for (size_t i = 0; i < _children.size(); ++i) {
if ((result = _children[i]->findView(name)) != nullptr)
return result;
}
return nullptr;
}
void UIElement::replaceView(UIElement *ui, bool replaceAllViews) {
g_events->replaceView(ui, replaceAllViews);
}
void UIElement::replaceView(const Common::String &name, bool replaceAllViews) {
g_events->replaceView(name, replaceAllViews);
}
void UIElement::addView(UIElement *ui) {
g_events->addView(ui);
}
void UIElement::addView(const Common::String &name) {
g_events->addView(name);
}
void UIElement::addView() {
g_events->addView(this);
}
Graphics::ManagedSurface UIElement::getSurface() const {
return Graphics::ManagedSurface(*g_events->getScreen(), _bounds);
}
int UIElement::getRandomNumber(int minNumber, int maxNumber) {
return g_engine->getRandomNumber(maxNumber - minNumber + 1) + minNumber;
}
int UIElement::getRandomNumber(int maxNumber) {
return g_engine->getRandomNumber(maxNumber);
}
void UIElement::delaySeconds(uint seconds) {
_timeoutCtr = seconds * FRAME_RATE;
}
void UIElement::delayFrames(uint frames) {
_timeoutCtr = frames;
}
bool UIElement::endDelay() {
if (_timeoutCtr) {
_timeoutCtr = 0;
timeout();
return true;
} else {
return false;
}
}
void UIElement::timeout() {
redraw();
}
} // namespace MM1
} // namespace MM
|