aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/events.cpp
blob: b476d08c9c2f21e2aa6cbe4a5daa903d6bdfbac3 (plain)
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
/* 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.
 *
 */

// TODO: There is a 'please_hyperwalk' variable that gets accessed that is meant to be global, but
// at the moment it's implemented as a local variable

#include "graphics/cursorman.h"

#include "m4/events.h"
#include "m4/graphics.h"
#include "m4/scene.h"
#include "m4/viewmgr.h"
#include "m4/m4.h"

namespace M4 {

bool please_hyperwalk = false;

/*--------------------------------------------------------------------------*
 * Events																*
 *																			*
 * Implements an interface to the event system								*
 *--------------------------------------------------------------------------*/

Events::Events(MadsM4Engine *vm) : _vm(vm) {
	_mouseState = MSTATE_NO_EVENT;
	quitFlag = false;
	_keyCode = 0;
	_mouseButtons = 0;
	_ctrlFlag = false;

	if (_vm->isM4())
		_console = new M4Console(_m4Vm);
	else
		_console = new MadsConsole(_madsVm);
}

Events::~Events() {
	delete _console;
}

M4EventType Events::handleEvents() {
	static int oldX = -1, oldY = -1;
	static uint32 dclickTime = 0;

	// Handle event types
	while (g_system->getEventManager()->pollEvent(_event)) {
		switch (_event.type) {
		case Common::EVENT_QUIT:
			quitFlag = true;
			break;
		case Common::EVENT_KEYDOWN:
			// Note: The Ctrl-D ScummVM shortcut has been specialised so it will only activate the debugger
			// if it's the first key pressed after the Ctrl key is held down
			if ((_event.kbd.keycode == Common::KEYCODE_LCTRL) || (_event.kbd.keycode == Common::KEYCODE_RCTRL))
				_ctrlFlag = true;

			else if (_event.kbd.hasFlags(Common::KBD_CTRL)) {
				if ((_event.kbd.keycode == Common::KEYCODE_d) && _ctrlFlag) {
					_console->attach();
					_console->onFrame();
				}
				_ctrlFlag = false;
			}
			_keyCode = (int32)_event.kbd.keycode | ((_event.kbd.flags & (Common::KBD_CTRL | Common::KBD_ALT | Common::KBD_SHIFT)) << 24);


			break;
		case Common::EVENT_LBUTTONDOWN:
		case Common::EVENT_LBUTTONUP:
		case Common::EVENT_RBUTTONDOWN:
		case Common::EVENT_RBUTTONUP:
		case Common::EVENT_MBUTTONDOWN:
		case Common::EVENT_MBUTTONUP:
		case Common::EVENT_MOUSEMOVE:
		case Common::EVENT_WHEELUP:
		case Common::EVENT_WHEELDOWN:
			_vm->_mouse->handleEvent(_event);
			break;
		default:
			break;
		}
	}

	_mouseButtons = g_system->getEventManager()->getButtonState();

	// State machine for moving between states
	switch (_mouseState) {
	case MSTATE_NO_EVENT:
		if (_mouseButtons & LEFT_BUTTON_DOWN) {
			if ((dclickTime != 0) && (g_system->getMillis() < dclickTime)) {
				_mouseState = MSTATE_DOUBLECLICK_DOWN;
				dclickTime = 0;
				return MEVENT_DOUBLECLICK;
			}
			dclickTime = 0;
			_mouseState = MSTATE_LEFT_CLICK_DOWN;
			return MEVENT_LEFT_CLICK;
		}
		if (_mouseButtons & RIGHT_BUTTON_DOWN) {
			_mouseState = MSTATE_RIGHT_CLICK_DOWN;
			return MEVENT_RIGHT_CLICK;
		}
		if ((_event.mouse.x != oldX) || (_event.mouse.y != oldY)) {
			oldX = _event.mouse.x; oldY = _event.mouse.y;
			return MEVENT_MOVE;
		}
		return MEVENT_NO_EVENT;

	case MSTATE_LEFT_CLICK_DOWN:
		if (!(_mouseButtons & LEFT_BUTTON_DOWN)) {
			dclickTime = g_system->getMillis() + 1000 * 15 / 60;
			_mouseState = MSTATE_NO_EVENT;
			return MEVENT_LEFT_RELEASE;
		}
		if ((_event.mouse.x != oldX) || (_event.mouse.y != oldY)) {
			oldX = _event.mouse.x; oldY = _event.mouse.y;
			return MEVENT_LEFT_DRAG;
		}
		return MEVENT_LEFT_HOLD;

	case MSTATE_RIGHT_CLICK_DOWN:
		if (!(_mouseButtons & RIGHT_BUTTON_DOWN)) {
			_mouseState = MSTATE_NO_EVENT;
			please_hyperwalk = true;
			return MEVENT_RIGHT_RELEASE;
		}
		if ((_event.mouse.x != oldX) || (_event.mouse.y != oldY)) {
			oldX = _event.mouse.x; oldY = _event.mouse.y;
			return MEVENT_RIGHT_DRAG;
		}
		return MEVENT_RIGHT_HOLD;

	case MSTATE_DOUBLECLICK_DOWN:
		if (!(_mouseButtons & LEFT_BUTTON_DOWN)) {
			_mouseState = MSTATE_NO_EVENT;
			return MEVENT_DOUBLECLICK_RELEASE;
		}
		if ((_event.mouse.x != oldX) || (_event.mouse.y != oldY)) {
			oldX = _event.mouse.x; oldY = _event.mouse.y;
			return MEVENT_DOUBLECLICK_DRAG;
		}
		return MEVENT_DOUBLECLICK_HOLD;

	default:
		return MEVENT_NO_EVENT;
	}
}

bool Events::kbdCheck(uint32 &keyCode) {
	if (_keyCode == 0)
		return false;

	keyCode = _keyCode;
	_keyCode = 0;
	return true;
}


/*--------------------------------------------------------------------------*
 * Mouse																*
 *																			*
 * Implements an interface to the mouse										*
 *--------------------------------------------------------------------------*/

Mouse::Mouse(MadsM4Engine *vm) : _vm(vm) {
	_locked = false;
	_cursorOn = false;
	_cursor = NULL;
	_cursorSprites = NULL;
	resetMouse();
}

Mouse::~Mouse() {
	delete _cursorSprites;
}

bool Mouse::init(const char *seriesName, RGB8 *palette) {
	Common::SeekableReadStream *stream = _vm->res()->get(seriesName);
	int colorCount = 0;
	RGB8* cursorPalette;

	_cursorSprites = new SpriteAsset(_vm, stream, stream->size(), seriesName);

	// Remove cursor special pixels and set the mouse cursor hotspot in MADS games
	if (!_vm->isM4()) {
		byte *data = NULL;
		for (int i = 0; i < _cursorSprites->getCount(); i++) {
			bool hotSpotSet = false;

			for (int x = 0; x < _cursorSprites->getFrame(i)->width(); x++) {
				for (int y = 0; y < _cursorSprites->getFrame(i)->height(); y++) {
					data = _cursorSprites->getFrame(i)->getBasePtr(x, y);
					if (*data == 1) {
						// It seems that some cursors have more than one hotspot
						// In such a case, the first hotspot seems to set the x and
						// the second one the y hotspot offset
						if (!hotSpotSet) {
							_cursorSprites->getFrame(i)->xOffset = x;
							_cursorSprites->getFrame(i)->yOffset = y;
							hotSpotSet = true;
						} else {
							_cursorSprites->getFrame(i)->yOffset = y;
						}
						*data = 0;
					}
				}	// for y
			}	// for x
		}	// for i
	}

	colorCount = _cursorSprites->getColorCount();
	cursorPalette = _cursorSprites->getPalette();
	_vm->_palette->setPalette(cursorPalette, 0, colorCount);

	//debugCN(kDebugCore, "Cursor count: %d\n", _cursorSprites->getCount());

	_vm->res()->toss(seriesName);

	_currentCursor = -1;
	return true;
}

bool Mouse::setCursorNum(int cursorIndex) {
	if ((cursorIndex < 0) || (cursorIndex >= (int)_cursorSprites->getCount()))
		return false;

	_lockedCursor = cursorIndex;
	if (_locked)
		// Cursor is locked, so don't go ahead with changing cursor
		return true;

	_currentCursor = _lockedCursor;
	_cursor = _cursorSprites->getFrame(cursorIndex);

	// Set the cursor to the sprite
	CursorMan.replaceCursor((const byte *)_cursor->getBasePtr(), _cursor->width(), _cursor->height(),
		_cursor->xOffset, _cursor->yOffset, TRANSPARENT_COLOR_INDEX);

	return true;
}

int Mouse::cursorCount() {
	return _cursorSprites->getCount();
}

void Mouse::cursorOn() {
	_cursorOn = true;
	CursorMan.showMouse(!inHideArea());
}

void Mouse::cursorOff() {
	_cursorOn = false;
	CursorMan.showMouse(false);
}

void Mouse::lockCursor(int cursorIndex) {
	_locked = false;
	setCursorNum(cursorIndex);
	_locked = true;
}

void Mouse::unlockCursor() {
	_locked = false;
	setCursorNum(_lockedCursor);
}

const char *Mouse::getVerb() {
	switch (_vm->_mouse->getCursorNum()) {
	case CURSOR_LOOK:
		return "LOOK AT";
	case CURSOR_TAKE:
		return "TAKE";
	case CURSOR_USE:
		return "GEAR";
	default:
		return NULL;
	}
}

void Mouse::resetMouse() {
	_hideRect.left = -1;
	_hideRect.top = -1;
	_hideRect.right = -1;
	_hideRect.bottom = -1;
	_showRect.left = -1;
	_showRect.top = -1;
	_showRect.right = -1;
	_showRect.bottom = -1;
}

void Mouse::setHideRect(Common::Rect &r) {
	_hideRect = r;
}

void Mouse::setShowRect(Common::Rect &r) {
	_showRect = r;
}

const Common::Rect *Mouse::getHideRect() {
	return &_hideRect;
}

const Common::Rect *Mouse::getShowRect() {
	if ((_showRect.top == -1) || (_showRect.left == -1)) {
		// Show rectangle uninitialised - set it to current screen dimensions
		_showRect.top = 0;
		_showRect.left = 0;
		_showRect.right = _vm->_screen->width() - 1;
		_showRect.bottom = _vm->_screen->height() -1;
	}

	return &_showRect;
}

void Mouse::handleEvent(Common::Event &event) {
	_currentPos.x = event.mouse.x;
	_currentPos.y = event.mouse.y;

	// If mouse is turned on, check to see if the position is in the hide rect, or outside the show rect.
	// If so, handle toggling the visibility of the mouse
	bool showFlag = !inHideArea();
	if (_cursorOn && (CursorMan.isVisible() != showFlag)) {
		CursorMan.showMouse(showFlag);
	}
}

bool Mouse::inHideArea() {
	// Returns true if the mouse is inside a specified hide rect, or if a show rect is specified and
	// the mouse is currently outside it
	if ((_currentPos.x >= _hideRect.left) && (_currentPos.x <= _hideRect.right) &&
		(_currentPos.y >= _hideRect.top) && (_currentPos.y <= _hideRect.bottom))
		// Inside a hide area
		return true;


	if ((_showRect.top == -1) && (_showRect.left == -1))
		// No show rect defined
		return false;

	// Return true if the mouse is outside the show area
	return (_currentPos.x < _showRect.left) || (_currentPos.x > _showRect.right) ||
		(_currentPos.y < _showRect.top) || (_currentPos.y > _showRect.bottom);
}

} // End of namespace M4