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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/events.h"
#include "common/system.h"
#include "common/textconsole.h"
#include "graphics/cursorman.h"
#include "parallaction/exec.h"
#include "parallaction/input.h"
#include "parallaction/parallaction.h"
#include "parallaction/debug.h"
namespace Parallaction {
#define MOUSEARROW_WIDTH_NS 16
#define MOUSEARROW_HEIGHT_NS 16
#define MOUSECOMBO_WIDTH_NS 32 // sizes for cursor + selected inventory item
#define MOUSECOMBO_HEIGHT_NS 32
struct MouseComboProperties {
int _xOffset;
int _yOffset;
int _width;
int _height;
};
/*
// TODO: improve NS's handling of normal cursor before merging cursor code.
MouseComboProperties _mouseComboProps_NS = {
7, // combo x offset (the icon from the inventory will be rendered from here)
7, // combo y offset (ditto)
32, // combo (arrow + icon) width
32 // combo (arrow + icon) height
};
*/
MouseComboProperties _mouseComboProps_BR = {
8, // combo x offset (the icon from the inventory will be rendered from here)
8, // combo y offset (ditto)
68, // combo (arrow + icon) width
68 // combo (arrow + icon) height
};
Input::Input(Parallaction *vm) : _vm(vm) {
_gameType = _vm->getGameType();
_transCurrentHoverItem = 0;
_hasDelayedAction = false; // actived when the character needs to move before taking an action
_mouseState = MOUSE_DISABLED;
_activeItem._index = 0;
_activeItem._id = 0;
_mouseButtons = 0;
_delayedActionZone.reset();
_dinoCursor = 0;
_dougCursor = 0;
_donnaCursor = 0;
_comboArrow = 0;
initCursors();
}
Input::~Input() {
if (_gameType == GType_Nippon) {
delete _mouseArrow;
}
delete _comboArrow;
delete _dinoCursor;
delete _dougCursor;
delete _donnaCursor;
}
// FIXME: the engine has 3 event loops. The following routine hosts the main one,
// and it's called from 8 different places in the code. There exist 2 more specialised
// loops which could possibly be merged into this one with some effort in changing
// caller code, i.e. adding condition checks.
//
void Input::readInput() {
bool updateMousePos = false;
Common::Event e;
_mouseButtons = kMouseNone;
_hasKeyPressEvent = false;
Common::EventManager *eventMan = _vm->_system->getEventManager();
while (eventMan->pollEvent(e)) {
updateMousePos = true;
switch (e.type) {
case Common::EVENT_KEYDOWN:
_hasKeyPressEvent = true;
_keyPressed = e.kbd;
if (e.kbd.hasFlags(Common::KBD_CTRL) && e.kbd.keycode == Common::KEYCODE_d)
_vm->_debugger->attach();
updateMousePos = false;
break;
case Common::EVENT_LBUTTONDOWN:
_mouseButtons = kMouseLeftDown;
break;
case Common::EVENT_LBUTTONUP:
_mouseButtons = kMouseLeftUp;
break;
case Common::EVENT_RBUTTONDOWN:
_mouseButtons = kMouseRightDown;
break;
case Common::EVENT_RBUTTONUP:
_mouseButtons = kMouseRightUp;
break;
case Common::EVENT_RTL:
case Common::EVENT_QUIT:
return;
default:
break;
}
}
if (updateMousePos) {
setCursorPos(e.mouse);
}
_vm->_debugger->onFrame();
return;
}
bool Input::getLastKeyDown(uint16 &ascii) {
ascii = _keyPressed.ascii;
return (_hasKeyPressEvent);
}
// FIXME: see comment for readInput()
void Input::waitForButtonEvent(uint32 buttonEventMask, int32 timeout) {
if (buttonEventMask == kMouseNone) {
_mouseButtons = kMouseNone; // don't wait on nothing
return;
}
const int32 LOOP_RESOLUTION = 30;
if (timeout <= 0) {
do {
readInput();
_vm->_system->delayMillis(LOOP_RESOLUTION);
} while ((_mouseButtons & buttonEventMask) == 0);
} else {
do {
readInput();
_vm->_system->delayMillis(LOOP_RESOLUTION);
timeout -= LOOP_RESOLUTION;
} while ((timeout > 0) && (_mouseButtons & buttonEventMask) == 0);
}
}
int Input::updateGameInput() {
int event = kEvNone;
if (!isMouseEnabled() ||
(_engineFlags & kEngineBlockInput) ||
(_engineFlags & kEngineWalking) ||
(_engineFlags & kEngineChangeLocation)) {
debugC(3, kDebugInput, "updateGameInput: input flags (mouse: %i, block: %i, walking: %i, changeloc: %i)",
isMouseEnabled(),
(_engineFlags & kEngineBlockInput) == 0,
(_engineFlags & kEngineWalking) == 0,
(_engineFlags & kEngineChangeLocation) == 0
);
return event;
}
if (_gameType == GType_Nippon) {
if (_hasKeyPressEvent && (_vm->getFeatures() & GF_DEMO) == 0) {
if (_keyPressed.keycode == Common::KEYCODE_l) event = kEvLoadGame;
if (_keyPressed.keycode == Common::KEYCODE_s) event = kEvSaveGame;
}
} else
if (_gameType == GType_BRA) {
if (_hasKeyPressEvent && (_vm->getFeatures() & GF_DEMO) == 0) {
if (_keyPressed.keycode == Common::KEYCODE_F5) event = kEvIngameMenu;
}
} else {
error("unsupported gametype in updateGameInput");
}
if (event == kEvNone) {
translateGameInput();
}
return event;
}
int Input::updateInput() {
int oldMode = _inputMode;
int event = kEvNone;
readInput();
switch (_inputMode) {
case kInputModeGame:
event = updateGameInput();
break;
case kInputModeInventory:
updateInventoryInput();
break;
}
// when mode changes, then consider any input consumed
// for the current frame
if (oldMode != _inputMode) {
_mouseButtons = kEvNone;
_hasKeyPressEvent = false;
}
return event;
}
void Input::trackMouse(ZonePtr z) {
if ((z != _hoverZone) && (_hoverZone)) {
stopHovering();
return;
}
if (!z) {
return;
}
if ((!_hoverZone) && ((z->_flags & kFlagsNoName) == 0)) {
_hoverZone = z;
_vm->_gfx->showFloatingLabel(_hoverZone->_label);
return;
}
}
void Input::stopHovering() {
_hoverZone.reset();
_vm->_gfx->hideFloatingLabel();
}
void Input::takeAction(ZonePtr z) {
stopHovering();
_vm->pauseJobs();
_vm->runZone(z);
_vm->resumeJobs();
}
void Input::walkTo(const Common::Point &dest) {
stopHovering();
setArrowCursor();
_vm->scheduleWalk(dest.x, dest.y, true);
}
bool Input::translateGameInput() {
if (_engineFlags & kEnginePauseJobs) {
return false;
}
if (_hasDelayedAction) {
// if walking is over, then take programmed action
takeAction(_delayedActionZone);
_hasDelayedAction = false;
_delayedActionZone.reset();
return true;
}
if (_mouseButtons == kMouseRightDown) {
// right button down shows inventory
enterInventoryMode();
return true;
}
Common::Point mousePos;
getAbsoluteCursorPos(mousePos);
// test if mouse is hovering on an interactive zone for the currently selected inventory item
ZonePtr z = _vm->hitZone(_activeItem._id, mousePos.x, mousePos.y);
if (((_mouseButtons == kMouseLeftUp) && (_activeItem._id == 0) && ((_engineFlags & kEngineWalking) == 0)) && ((!z) || (ACTIONTYPE(z) != kZoneCommand))) {
walkTo(mousePos);
return true;
}
trackMouse(z);
if (!z) {
return true;
}
if ((_mouseButtons == kMouseLeftUp) && ((_activeItem._id != 0) || (ACTIONTYPE(z) == kZoneCommand))) {
bool noWalk = z->_flags & kFlagsNoWalk; // check the explicit no-walk flag
if (_gameType == GType_BRA) {
// action performed on object marked for self-use do not need walk in BRA
noWalk |= ((z->_flags & kFlagsYourself) != 0);
}
if (noWalk) {
takeAction(z);
} else {
// action delayed: if Zone defined a moveto position the character is programmed to move there,
// else it will move to the mouse position
_delayedActionZone = z;
_hasDelayedAction = true;
if (z->_moveTo.y != 0) {
mousePos = z->_moveTo;
}
walkTo(mousePos);
}
_vm->beep();
setArrowCursor();
return true;
}
return true;
}
void Input::enterInventoryMode() {
Common::Point mousePos;
getAbsoluteCursorPos(mousePos);
bool hitCharacter = _vm->hitZone(kZoneYou, mousePos.x, mousePos.y);
if (hitCharacter) {
if (_activeItem._id != 0) {
_activeItem._index = (_activeItem._id >> 16) & 0xFFFF;
_engineFlags |= kEngineDragging;
} else {
setArrowCursor();
}
}
stopHovering();
_vm->pauseJobs();
_vm->openInventory();
_transCurrentHoverItem = -1;
_inputMode = kInputModeInventory;
}
void Input::exitInventoryMode() {
// right up hides inventory
Common::Point mousePos;
getCursorPos(mousePos);
int pos = _vm->getHoverInventoryItem(mousePos.x, mousePos.y);
_vm->highlightInventoryItem(-1); // disable
if ((_engineFlags & kEngineDragging)) {
_engineFlags &= ~kEngineDragging;
ZonePtr z = _vm->hitZone(kZoneMerge, _activeItem._index, _vm->getInventoryItemIndex(pos));
if (z) {
_vm->dropItem(z->u._mergeObj1);
_vm->dropItem(z->u._mergeObj2);
_vm->addInventoryItem(z->u._mergeObj3);
_vm->_cmdExec->run(z->_commands); // commands might set a new _inputMode
}
}
_vm->closeInventory();
if (pos == -1) {
setArrowCursor();
} else {
const InventoryItem *item = _vm->getInventoryItem(pos);
if (item->_index != 0) {
_activeItem._id = item->_id;
setInventoryCursor(item->_index);
}
}
_vm->resumeJobs();
// in case the input mode was not changed by the code above (especially by the commands
// executed in case of a merge), then assume we are going back to game mode
if (_inputMode == kInputModeInventory) {
_inputMode = kInputModeGame;
}
}
bool Input::updateInventoryInput() {
if (_mouseButtons == kMouseRightUp) {
exitInventoryMode();
return true;
}
Common::Point mousePos;
getCursorPos(mousePos);
int16 _si = _vm->getHoverInventoryItem(mousePos.x, mousePos.y);
if (_si != _transCurrentHoverItem) {
_transCurrentHoverItem = _si;
_vm->highlightInventoryItem(_si); // enable
}
return true;
}
void Input::setMouseState(MouseTriState state) {
assert(state == MOUSE_ENABLED_SHOW || state == MOUSE_ENABLED_HIDE || state == MOUSE_DISABLED);
_mouseState = state;
switch (_mouseState) {
case MOUSE_ENABLED_HIDE:
case MOUSE_DISABLED:
CursorMan.showMouse(false);
break;
case MOUSE_ENABLED_SHOW:
CursorMan.showMouse(true);
break;
}
}
MouseTriState Input::getMouseState() {
return _mouseState;
}
bool Input::isMouseEnabled() {
return (_mouseState == MOUSE_ENABLED_SHOW) || (_mouseState == MOUSE_ENABLED_HIDE);
}
void Input::getAbsoluteCursorPos(Common::Point& p) const {
_vm->_gfx->getScrollPos(p);
p.x += _mousePos.x;
p.y += _mousePos.y;
}
void Input::initCursors() {
_dinoCursor = _donnaCursor = _dougCursor = 0;
switch (_gameType) {
case GType_Nippon:
_comboArrow = _vm->_disk->loadPointer("pointer");
_mouseArrow = new Cnv(1, MOUSEARROW_WIDTH_NS, MOUSEARROW_HEIGHT_NS, _resMouseArrow_NS, false);
break;
case GType_BRA:
if (_vm->getPlatform() == Common::kPlatformPC) {
_dinoCursor = _vm->_disk->loadPointer("pointer1");
_dougCursor = _vm->_disk->loadPointer("pointer2");
_donnaCursor = _vm->_disk->loadPointer("pointer3");
Graphics::Surface *surf = new Graphics::Surface;
surf->create(_mouseComboProps_BR._width, _mouseComboProps_BR._height, Graphics::PixelFormat::createFormatCLUT8());
_comboArrow = new SurfaceToFrames(surf);
// TODO: choose the pointer depending on the active character
// For now, we pick Donna's
_mouseArrow = _donnaCursor;
} else {
// TODO: Where are the Amiga cursors?
Graphics::Surface *surf1 = new Graphics::Surface;
surf1->create(_mouseComboProps_BR._width, _mouseComboProps_BR._height, Graphics::PixelFormat::createFormatCLUT8());
_comboArrow = new SurfaceToFrames(surf1);
// TODO: scale mouse cursor (see staticres.cpp)
Graphics::Surface *surf2 = new Graphics::Surface;
surf2->create(32, 16, Graphics::PixelFormat::createFormatCLUT8());
memcpy(surf2->pixels, _resMouseArrow_BR_Amiga, 32*16);
_mouseArrow = new SurfaceToFrames(surf2);
}
break;
default:
warning("Input::initCursors: unknown gametype");
}
}
void Input::setArrowCursor() {
switch (_gameType) {
case GType_Nippon:
debugC(1, kDebugInput, "setting mouse cursor to arrow");
// this stuff is needed to avoid artifacts with labels and selected items when switching cursors
stopHovering();
_activeItem._id = 0;
CursorMan.replaceCursor(_mouseArrow->getData(0), MOUSEARROW_WIDTH_NS, MOUSEARROW_HEIGHT_NS, 0, 0, 0);
break;
case GType_BRA: {
Common::Rect r;
_mouseArrow->getRect(0, r);
CursorMan.replaceCursor(_mouseArrow->getData(0), r.width(), r.height(), 0, 0, 0);
CursorMan.showMouse(true);
_activeItem._id = 0;
break;
}
default:
warning("Input::setArrowCursor: unknown gametype");
}
}
void Input::setInventoryCursor(ItemName name) {
assert(name > 0);
switch (_gameType) {
case GType_Nippon: {
byte *v8 = _comboArrow->getData(0);
// FIXME: destination offseting is not clear
_vm->_inventoryRenderer->drawItem(name, v8 + 7 * MOUSECOMBO_WIDTH_NS + 7, MOUSECOMBO_WIDTH_NS);
CursorMan.replaceCursor(v8, MOUSECOMBO_WIDTH_NS, MOUSECOMBO_HEIGHT_NS, 0, 0, 0);
break;
}
case GType_BRA: {
byte *src = _mouseArrow->getData(0);
byte *dst = _comboArrow->getData(0);
memcpy(dst, src, _comboArrow->getSize(0));
// FIXME: destination offseting is not clear
_vm->_inventoryRenderer->drawItem(name, dst + _mouseComboProps_BR._yOffset * _mouseComboProps_BR._width + _mouseComboProps_BR._xOffset, _mouseComboProps_BR._width);
CursorMan.replaceCursor(dst, _mouseComboProps_BR._width, _mouseComboProps_BR._height, 0, 0, 0);
break;
}
default:
warning("Input::setInventoryCursor: unknown gametype");
}
}
} // namespace Parallaction
|