aboutsummaryrefslogtreecommitdiff
path: root/engines/director/lingo/lingo-events.cpp
blob: fe64b80e2920bcbc0771e71acda41f81d03c41b3 (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
/* 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 "director/director.h"
#include "director/lingo/lingo.h"
#include "director/lingo/lingo-code.h"
#include "director/frame.h"
#include "director/score.h"
#include "director/sprite.h"

namespace Director {

struct EventHandlerType {
	LEvent handler;
	const char *name;
} static const eventHandlerDescs[] = {
	{ kEventPrepareMovie,		"prepareMovie" },
	{ kEventStartMovie,			"startMovie" },			//		D3?
	{ kEventStepMovie,			"stepMovie" },			//		D3?
	{ kEventStopMovie,			"stopMovie" },			//		D3?

	{ kEventNew,				"newSprite" },
	{ kEventBeginSprite,		"beginSprite" },
	{ kEventEndSprite,			"endSprite" },

	{ kEventEnterFrame,			"enterFrame" },			//			D4
	{ kEventPrepareFrame,		"prepareFrame" },
	{ kEventIdle,				"idle" },
	{ kEventStepFrame,			"stepFrame"},
	{ kEventExitFrame,			"exitFrame" },			//			D4

	{ kEventActivateWindow,		"activateWindow" },
	{ kEventDeactivateWindow,	"deactivateWindow" },
	{ kEventMoveWindow,			"moveWindow" },
	{ kEventResizeWindow,		"resizeWindow" },
	{ kEventOpenWindow,			"openWindow" },
	{ kEventCloseWindow,		"closeWindow" },
	{ kEventStart,				"start" },

	{ kEventKeyUp,				"keyUp" },				//			D4
	{ kEventKeyDown,			"keyDown" },			// D2 w		D4 (as when from D2)
	{ kEventMouseUp,			"mouseUp" },			// D2 w	D3?
	{ kEventMouseDown,			"mouseDown" },			// D2 w	D3?
	{ kEventRightMouseDown,		"rightMouseDown" },
	{ kEventRightMouseUp,		"rightMouseUp" },
	{ kEventMouseEnter,			"mouseEnter" },
	{ kEventMouseLeave,			"mouseLeave" },
	{ kEventMouseUpOutSide,		"mouseUpOutSide" },
	{ kEventMouseWithin,		"mouseWithin" },

	{ kEventTimeout,			"timeout" },			// D2 as when

	{ kEventNone,				0 },
};

void Lingo::initEventHandlerTypes() {
	for (const EventHandlerType *t = &eventHandlerDescs[0]; t->handler != kEventNone; ++t) {
		_eventHandlerTypeIds[t->name] = t->handler;
		_eventHandlerTypes[t->handler] = t->name;
	}
}

ScriptType Lingo::event2script(LEvent ev) {
	if (_vm->getVersion() < 4) {
		switch (ev) {
		//case kEventStartMovie: // We are precompiling it now
		//	return kMovieScript;
		case kEventEnterFrame:
			return kFrameScript;
		default:
			return kNoneScript;
		}
	}

	return kNoneScript;
}

Symbol *Lingo::getHandler(Common::String &name) {
	if (!_eventHandlerTypeIds.contains(name)) {
		if (_builtins.contains(name))
			return _builtins[name];

		return NULL;
	}

	uint32 entityIndex = ENTITY_INDEX(_eventHandlerTypeIds[name], _currentEntityId);
	if (!_handlers.contains(entityIndex))
		return NULL;

	return _handlers[entityIndex];
}

void Lingo::primaryEventHandler(LEvent event) {
	/* When an event occurs the message [...] is first sent to a
	 * primary event handler: [... if exists it is executed] and the
	 * event is passed on to other objects unless you explicitly stop
	 * the message by including the dontPassEventCommand in the script
	 * [D4 docs page 77]
	 */
	debugC(3, kDebugLingoExec, "STUB: primary event handler (%s) not implemented", _eventHandlerTypes[event]);
	switch (event) {
	case kEventMouseDown:
	case kEventMouseUp:
	case kEventKeyUp:
	case kEventKeyDown:
	case kEventTimeout:
		// TODO
		break;
	default:
		/* N.B.: No primary event handlers for events other than
		 * keyup, keydown, mouseup, mousedown, timeout
		 * [see: www.columbia.edu/itc/visualarts/r4110/s2001/handouts
		 * /03_03_Event_Hierarchy.pdf]
		 */
		warning("primaryEventHandler() on event other than mouseDown, mouseUp, keyUp, keyDown, timeout");
	}
#ifdef DEBUG_DONTPASSEVENT
	// #define DEBUG_DONTPASSEVENT to simulate raising of the dontPassEvent flag
	_dontPassEvent = true;
	debugC(3, kDebugLingoExec, "STUB: primaryEventHandler raising dontPassEvent");
#else
	debugC(3, kDebugLingoExec, "STUB: primaryEventHandler not raising dontPassEvent");
#endif
}

void Lingo::processInputEvent(LEvent event) {
	/* When the mouseDown or mouseUp occurs over a sprite, the message
	 * goes first to the sprite script, then to the script of the cast
	 * member, to the frame script and finally to the movie scripts.
	 *
	 * When the mouseDown or mouseUp doesn't occur over a sprite, the
	 * message goes to the frame script and then to the movie script.
	 *
	 * When more than one movie script [...]
	 * [D4 docs] */

	Score *score = _vm->getCurrentScore();
	Frame *currentFrame = score->_frames[score->getCurrentFrame()];
	assert(currentFrame != nullptr);
	uint16 spriteId = score->_currentMouseDownSpriteId;

	primaryEventHandler(event);

	if (_dontPassEvent) {
		_dontPassEvent = false;

		return;
	}

	if (_vm->getVersion() > 3) {
		if (true) {
			// TODO: Check whether occurring over a sprite
			processEvent(event, kSpriteScript, currentFrame->_sprites[spriteId]->_scriptId);
		}
		processEvent(event, kCastScript, currentFrame->_sprites[spriteId]->_castId);
		processEvent(event, kFrameScript, score->_frames[score->getCurrentFrame()]->_actionId);
		// TODO: Is the kFrameScript call above correct?
	} else if (event == kEventMouseUp) {
		// Frame script overrides sprite script
		if (!currentFrame->_sprites[spriteId]->_scriptId) {
			processEvent(kEventNone, kSpriteScript, currentFrame->_sprites[spriteId]->_castId + score->_castIDoffset);
			processEvent(event, kSpriteScript, currentFrame->_sprites[spriteId]->_castId + score->_castIDoffset);
		} else {
			processEvent(kEventNone, kFrameScript, currentFrame->_sprites[spriteId]->_scriptId);
		}
	}
	if (event == kEventKeyDown) {
		// TODO: is the above condition necessary or useful?
		processEvent(event, kGlobalScript, 0);
	}

	runMovieScript(event);
}

void Lingo::runMovieScript(LEvent event) {
	/* If more than one movie script handles the same message, Lingo
	 * searches the movie scripts according to their order in the cast
	 * window [p.81 of D4 docs]
	 */

	if (_dontPassEvent)
		return;

	for (uint i = 0; i < _scriptContexts[kMovieScript].size(); i++) {
		processEvent(event, kMovieScript, i);
		// TODO: How do know which script handles the message?
	}
	debugC(9, kDebugEvents, "STUB: processEvent(event, kMovieScript, ?)");
}

void Lingo::processFrameEvent(LEvent event) {
	/* [in D4] the enterFrame, exitFrame, idle and timeout messages
	 * are sent to a frame script and then a movie script.	If the
	 * current frame has no frame script when the event occurs, the
	 * message goes to movie scripts.
	 * [p.81 of D4 docs]
	 */
	// TODO: Same for D2-3 or not?
	Score *score = _vm->getCurrentScore();

	if (event == kEventTimeout) {
		primaryEventHandler(event);
	}

	if (_dontPassEvent) {
		_dontPassEvent = false;

		return;
	}

	int entity;

	if (event == kEventPrepareFrame || event == kEventIdle) {
		entity = score->getCurrentFrame();
	} else {
		assert(score->_frames[score->getCurrentFrame()] != nullptr);
		entity = score->_frames[score->getCurrentFrame()]->_actionId;
	}
	processEvent(event, kFrameScript, entity);

	runMovieScript(event);
}

void Lingo::processGenericEvent(LEvent event) {
	// Movie Script
	int id = -1;
	if (event == kEventStart || event == kEventPrepareMovie)
		id = 0;
	else
		warning("STUB: processGenericEvent called for something else than kEventStart or kEventPrepareMovie, additional logic probably needed");

	processEvent(event, kMovieScript, id);
}

void Lingo::processSpriteEvent(LEvent event) {
	Score *score = _vm->getCurrentScore();
	Frame *currentFrame = score->_frames[score->getCurrentFrame()];
	if (event == kEventBeginSprite) {
		// TODO: Check if this is also possibly a kSpriteScript?
		for (uint16 i = 0; i < CHANNEL_COUNT; i++)
			if (currentFrame->_sprites[i]->_enabled)
				processEvent(event, kCastScript, currentFrame->_sprites[i]->_scriptId);

	} else {
		warning("STUB: processSpriteEvent called for something else than kEventBeginSprite, additional logic probably needed");
	}

}

void Lingo::processEvent(LEvent event) {
	switch (event) {
		case kEventKeyUp:
		case kEventKeyDown:
		case kEventMouseUp:
		case kEventMouseDown:
			processInputEvent(event);
			break;

		case kEventIdle:
		case kEventEnterFrame:
		case kEventExitFrame:
		case kEventNone:
			processFrameEvent(event);
			break;

		case kEventStart:
		case kEventStartMovie:
		case kEventStopMovie:
		case kEventTimeout:
		case kEventPrepareMovie:
			processGenericEvent(event);
			break;
		case kEventBeginSprite:
			processSpriteEvent(event);
			break;

		default:
			warning("processEvent: Unhandled event %s", _eventHandlerTypes[event]);
	}

	_dontPassEvent = false;
}

void Lingo::processEvent(LEvent event, ScriptType st, int entityId) {
	if (entityId < 0)
		return;

	if (_dontPassEvent)
		return;

	debugC(9, kDebugEvents, "Lingo::processEvent(%s, %s, %d)", _eventHandlerTypes[event], scriptType2str(st), entityId);

	_currentEntityId = entityId;

	if (!_eventHandlerTypes.contains(event))
		error("processEvent: Unknown event %d for entity %d", event, entityId);

	if (_handlers.contains(ENTITY_INDEX(event, entityId))) {
		debugC(1, kDebugEvents, "Lingo::processEvent(%s, %s, %d), _eventHandler", _eventHandlerTypes[event], scriptType2str(st), entityId);
		executeHandler(_eventHandlerTypes[event]); // D4+ Events
	} else if (event == kEventNone && _scriptContexts[st].contains(entityId)) {
		debugC(1, kDebugEvents, "Lingo::processEvent(%s, %s, %d), script", _eventHandlerTypes[event], scriptType2str(st), entityId);

		executeScript(st, entityId, 0); // D3 list of scripts.
	} else {
		debugC(9, kDebugEvents, "STUB: processEvent(%s, %s, %d)", _eventHandlerTypes[event], scriptType2str(st), entityId);
	}
}

} // End of namespace Director