aboutsummaryrefslogtreecommitdiff
path: root/engines/tsage/events.cpp
blob: 1fa17022de098e74e4d489d18355ced90fd31cdd (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
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
/* 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.
 *
 */

#include "common/events.h"
#include "common/singleton.h"
#include "graphics/cursorman.h"
#include "common/system.h"

#include "tsage/events.h"
#include "tsage/core.h"
#include "tsage/staticres.h"
#include "tsage/tsage.h"
#include "tsage/globals.h"

namespace TsAGE {

EventsClass::EventsClass() {
	_currentCursor = CURSOR_NONE;
	_lastCursor = CURSOR_NONE;
	_frameNumber = 0;
	_priorFrameTime = 0;
	_prevDelayFrame = 0;
	g_saver->addListener(this);
	g_saver->addLoadNotifier(&EventsClass::loadNotifierProc);
}

bool EventsClass::pollEvent() {
	uint32 milli = g_system->getMillis();
	if ((milli - _priorFrameTime) >= GAME_FRAME_TIME) {
		_priorFrameTime = milli;
		++_frameNumber;

		// Update screen
		GLOBALS._screen.update();
	}

	if (!g_system->getEventManager()->pollEvent(_event)) return false;

	// Handle keypress
	switch (_event.type) {
	case Common::EVENT_QUIT:
	case Common::EVENT_RTL:
		break;

	case Common::EVENT_MOUSEMOVE:
	case Common::EVENT_LBUTTONDOWN:
	case Common::EVENT_LBUTTONUP:
	case Common::EVENT_RBUTTONDOWN:
	case Common::EVENT_RBUTTONUP:
		// Keep a copy of the current mouse position
		_mousePos = _event.mouse;
		break;

	default:
		break;
	}

	return true;
}

void EventsClass::waitForPress(int eventMask) {
	Event evt;
	while (!g_vm->shouldQuit() && !getEvent(evt, eventMask))
		g_system->delayMillis(10);
}

/**
 * Standard event retrieval, which only returns keyboard and mouse clicks
 */
bool EventsClass::getEvent(Event &evt, int eventMask) {
	while (pollEvent() && !g_vm->shouldQuit()) {
		evt.handled = false;
		evt.eventType = EVENT_NONE;
		evt.mousePos = _event.mouse;
		evt.kbd = _event.kbd;

		switch (_event.type) {
		case Common::EVENT_MOUSEMOVE:
			evt.eventType = EVENT_MOUSE_MOVE;
			break;
		case Common::EVENT_LBUTTONDOWN:
			evt.eventType = EVENT_BUTTON_DOWN;
			evt.btnState = BTNSHIFT_LEFT;
			break;
		case Common::EVENT_RBUTTONDOWN:
			evt.eventType = EVENT_BUTTON_DOWN;
			evt.btnState = BTNSHIFT_RIGHT;
			break;
		case Common::EVENT_MBUTTONDOWN:
			evt.eventType = EVENT_BUTTON_DOWN;
			evt.btnState = BTNSHIFT_MIDDLE;
			break;
		case Common::EVENT_LBUTTONUP:
		case Common::EVENT_RBUTTONUP:
		case Common::EVENT_MBUTTONUP:
			evt.eventType = EVENT_BUTTON_UP;
			evt.btnState = BTNSHIFT_LEFT;
			break;
		case Common::EVENT_KEYDOWN:
			evt.eventType = EVENT_KEYPRESS;
			evt.kbd = _event.kbd;
			break;
		default:
			break;
		}

		if (evt.eventType & eventMask)
			return true;
	}

	evt.handled = false;
	evt.eventType = EVENT_NONE;

	return false;
}

/**
 * Sets the specified cursor
 *
 * @cursorType Specified cursor number
 */
void EventsClass::setCursor(CursorType cursorType) {
	if (cursorType == _lastCursor)
		return;

	_lastCursor = cursorType;
	g_globals->clearFlag(122);
	CursorMan.showMouse(true);

	const byte *cursor;
	bool delFlag = true;
	uint size;
	bool questionEnabled = false;

	switch (cursorType) {
	case CURSOR_NONE:
		// No cursor
		g_globals->setFlag(122);

		if ((g_vm->getGameID() != GType_Ringworld) || ((g_vm->getGameID() == GType_Ringworld) && (g_vm->getFeatures() & GF_DEMO)))  {
			CursorMan.showMouse(false);
			return;
		}
		cursor = g_resourceManager->getSubResource(4, 1, 6, &size);
		break;

	case CURSOR_LOOK:
		// Look cursor
		if (g_vm->getGameID() == GType_BlueForce) {
			cursor = g_resourceManager->getSubResource(1, 5, 3, &size);
		} else if (g_vm->getGameID() == GType_Ringworld2) {
			cursor = g_resourceManager->getSubResource(5, 1, 5, &size);
		} else {
			cursor = g_resourceManager->getSubResource(4, 1, 5, &size);
		}
		_currentCursor = CURSOR_LOOK;
		break;

	case CURSOR_USE:
		// Use cursor
		if (g_vm->getGameID() == GType_BlueForce) {
			cursor = g_resourceManager->getSubResource(1, 5, 2, &size);
		} else if (g_vm->getGameID() == GType_Ringworld2) {
			cursor = g_resourceManager->getSubResource(5, 1, 4, &size);
		} else {
			cursor = g_resourceManager->getSubResource(4, 1, 4, &size);
		}
		_currentCursor = CURSOR_USE;
		break;

	case CURSOR_TALK:
		// Talk cursor
		if (g_vm->getGameID() == GType_BlueForce) {
			cursor = g_resourceManager->getSubResource(1, 5, 4, &size);
		} else if (g_vm->getGameID() == GType_Ringworld2) {
			cursor = g_resourceManager->getSubResource(5, 1, 6, &size);
		} else {
			cursor = g_resourceManager->getSubResource(4, 1, 3, &size);
		}
		_currentCursor = CURSOR_TALK;
		break;

	case CURSOR_EXIT:
		// Exit cursor (Blue Force)
		assert(g_vm->getGameID() == GType_BlueForce);
		cursor = g_resourceManager->getSubResource(1, 5, 7, &size);
		_currentCursor = CURSOR_EXIT;
		break;

	case CURSOR_PRINTER:
		// Printer cursor (Blue Force)
		assert(g_vm->getGameID() == GType_BlueForce);
		cursor = g_resourceManager->getSubResource(1, 7, 6, &size);
		_currentCursor = CURSOR_PRINTER;
		break;

	case CURSOR_ARROW:
		// Arrow cursor
		cursor = CURSOR_ARROW_DATA;
		delFlag = false;
		break;

	case CURSOR_WALK:
	default:
		switch (g_vm->getGameID()) {
		case GType_BlueForce:
			if (cursorType == CURSOR_WALK) {
				cursor = g_resourceManager->getSubResource(1, 5, 1, &size);
			} else {
				// Inventory icon
				cursor = g_resourceManager->getSubResource(10, ((int)cursorType - 1) / 20 + 1,
					((int)cursorType - 1) % 20 + 1, &size);
				questionEnabled = true;
			}
			_currentCursor = cursorType;
			break;
		case GType_Ringworld2:
			if (cursorType == CURSOR_WALK) {
				cursor = CURSOR_WALK_DATA;
				delFlag = false;
			} else {
				// Inventory icon
				InvObject *invObject = g_globals->_inventory->getItem((int)cursorType);
				cursor = g_resourceManager->getSubResource(6, invObject->_strip, invObject->_frame, &size);
				questionEnabled = true;
			}
			_currentCursor = cursorType;
			break;
		default:
			// For Ringworld, always treat as the walk cursor
			cursor = CURSOR_WALK_DATA;
			_currentCursor = CURSOR_WALK;
			delFlag = false;
			break;
		}
		break;

	// Ringworld 2 specific cursors
	case EXITCURSOR_N:
	case EXITCURSOR_S:
	case EXITCURSOR_W:
	case EXITCURSOR_E:
	case EXITCURSOR_LEFT_HAND:
	case CURSOR_INVALID:
	case EXITCURSOR_NE:
	case EXITCURSOR_SE:
	case EXITCURSOR_SW:
	case EXITCURSOR_NW:
	case SHADECURSOR_UP:
	case SHADECURSOR_DOWN:
	case SHADECURSOR_HAND:
		_currentCursor = cursorType;
		cursor = g_resourceManager->getSubResource(5, 1, cursorType - R2CURSORS_START, &size);
		break;

	case R2_CURSOR_ROPE:
		_currentCursor = cursorType;
		cursor = g_resourceManager->getSubResource(5, 4, 1, &size);
		break;
	}

	// Decode the cursor
	GfxSurface s = surfaceFromRes(cursor);

	Graphics::Surface surface = s.lockSurface();
	const byte *cursorData = (const byte *)surface.getPixels();
	CursorMan.replaceCursor(cursorData, surface.w, surface.h, s._centroid.x, s._centroid.y, s._transColor);
	s.unlockSurface();

	if (delFlag)
		DEALLOCATE(cursor);

	// For Blue Force and Return to Ringworld, enable the question button when an inventory icon is selected
	if (g_vm->getGameID() != GType_Ringworld)
		T2_GLOBALS._uiElements._question.setEnabled(questionEnabled);
}

void EventsClass::pushCursor(CursorType cursorType) {
	const byte *cursor;
	bool delFlag = true;
	uint size;

	switch (cursorType) {
	case CURSOR_NONE:
		// No cursor
		cursor = g_resourceManager->getSubResource(4, 1, 6, &size);
		break;

	case CURSOR_LOOK:
		// Look cursor
		cursor = g_resourceManager->getSubResource(4, 1, 5, &size);
		break;

	case CURSOR_USE:
		// Use cursor
		cursor = g_resourceManager->getSubResource(4, 1, 4, &size);
		break;

	case CURSOR_TALK:
		// Talk cursor
		cursor = g_resourceManager->getSubResource(4, 1, 3, &size);
		break;

	case CURSOR_ARROW:
		// Arrow cursor
		cursor = CURSOR_ARROW_DATA;
		delFlag = false;
		break;

	case CURSOR_WALK:
	default:
		// Walk cursor
		cursor = CURSOR_WALK_DATA;
		delFlag = false;
		break;
	}

	// Decode the cursor
	GfxSurface s = surfaceFromRes(cursor);

	Graphics::Surface surface = s.lockSurface();
	const byte *cursorData = (const byte *)surface.getPixels();
	CursorMan.pushCursor(cursorData, surface.w, surface.h, s._centroid.x, s._centroid.y, s._transColor);
	s.unlockSurface();

	if (delFlag)
		DEALLOCATE(cursor);
}

void EventsClass::popCursor() {
	CursorMan.popCursor();
}

void EventsClass::setCursor(Graphics::Surface &cursor, int transColor, const Common::Point &hotspot, CursorType cursorId) {
	const byte *cursorData = (const byte *)cursor.getPixels();
	CursorMan.replaceCursor(cursorData, cursor.w, cursor.h, hotspot.x, hotspot.y, transColor);

	_currentCursor = cursorId;
}

void EventsClass::setCursor(GfxSurface &cursor) {
	Graphics::Surface s = cursor.lockSurface();

	const byte *cursorData = (const byte *)s.getPixels();
	CursorMan.replaceCursor(cursorData, cursor.getBounds().width(), cursor.getBounds().height(),
		cursor._centroid.x, cursor._centroid.y, cursor._transColor);

	_lastCursor = CURSOR_NONE;
}

void EventsClass::setCursorFromFlag() {
	setCursor(isCursorVisible() ? _currentCursor : CURSOR_NONE);
}

void EventsClass::showCursor() {
	setCursor(_currentCursor);
}

CursorType EventsClass::hideCursor() {
	CursorType oldCursor = _currentCursor;
	setCursor(CURSOR_NONE);
	return oldCursor;
}

bool EventsClass::isCursorVisible() const {
	return !g_globals->getFlag(122);
}

/**
 * Delays the game for the specified number of frames, if necessary, from the
 * previous time the delay method was called
 */
void EventsClass::delay(int numFrames) {
	while (_frameNumber < (_prevDelayFrame + numFrames)) {
		uint32 delayAmount = CLIP(_priorFrameTime + GAME_SCRIPT_TIME - g_system->getMillis(),
			(uint32)0, (uint32)GAME_FRAME_TIME);
		if (delayAmount > 0)
			g_system->delayMillis(delayAmount);

		++_frameNumber;
		_priorFrameTime = g_system->getMillis();
	}

	GLOBALS._screen.update();
	_prevDelayFrame = _frameNumber;
	_priorFrameTime = g_system->getMillis();
}

void EventsClass::listenerSynchronize(Serializer &s) {
	s.syncAsUint32LE(_frameNumber);
	s.syncAsUint32LE(_prevDelayFrame);

	if (s.getVersion() >= 5) {
		s.syncAsSint16LE(_currentCursor);
		s.syncAsSint16LE(_lastCursor);
	}
}

void EventsClass::loadNotifierProc(bool postFlag) {
	if (postFlag) {
		if (g_globals->_events._lastCursor == CURSOR_NONE)
			g_globals->_events._lastCursor = g_globals->_events._currentCursor;
		else
			g_globals->_events._lastCursor = CURSOR_NONE;
	}
}

} // end of namespace TsAGE