aboutsummaryrefslogtreecommitdiff
path: root/engines/m4/viewmgr.cpp
blob: 46b4b5af9d4f31a5d0c9700c0ac03f3f0bffc4f3 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* 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$
 *
 */

// TODO: Views have a _coords rect, so I'm not sure if x/y is needed in the onRefresh

#include "m4/m4.h"
#include "m4/viewmgr.h"
#include "m4/mads_anim.h"

namespace M4 {

void returnToMainMenuFn(MadsM4Engine *vm) {
	vm->_palette->resetColorCounts();
	vm->_palette->setMadsSystemPalette();

	vm->loadMenu(MAIN_MENU);
}

RectList::RectList() {
}

RectList::~RectList() {
}

void RectList::addRect(int x1, int y1, int x2, int y2) {
	addRect(Common::Rect(x1, y1, x2, y2));
}

void RectList::addRect(const Common::Rect &rect) {
	/* TODO:
		Implement the following:
			- Don't add the Rect if it's contained in any Rect in the list
			- Split up the Rect if it intersects any Rect in the list
			  and add the resulting partial Rects instead
	*/
	push_back(rect);
}

int RectList::find(const Common::Point &pt) {
	for (uint idx = 0; idx < size(); ++idx) {
		if (this->operator [](idx).contains(pt.x, pt.y))
			return idx;
	}
	return -1;
}

//--------------------------------------------------------------------------

HotkeyList::HotkeyList(View *owner) : _view(owner) {
}

HotkeyList::~HotkeyList() {
	for (uint32 i = 0; i < _hotkeys.size(); i++)
		delete _hotkeys[i];
}

void HotkeyList::add(uint32 key, Hotkey::Callback callback) {
	_hotkeys.push_back(new Hotkey(key, callback));
}

void HotkeyList::remove(uint32 key) {
	for (uint32 i = 0; i < _hotkeys.size(); i++) {
		if (_hotkeys[i]->key == key) {
			delete _hotkeys[i];
			_hotkeys.remove_at(i);
			break;
		}
	}
}

bool HotkeyList::call(uint32 key) {
	for (uint32 i = 0; i < _hotkeys.size(); i++) {
		if (_hotkeys[i]->key == key) {
			if (_hotkeys[i]->callback)
				(_hotkeys[i]->callback)(_vm, _view, key);
			return true;
		}
	}
	return false;
}

//--------------------------------------------------------------------------

// View constructor

View::View(MadsM4Engine *vm, const Common::Rect &viewBounds, bool transparent)
	: M4Surface(viewBounds.width(), viewBounds.height()), _hotkeys(this), _vm(vm) {
	SCREEN_FLAGS_DEFAULT;
	_coords = viewBounds;
	_transparent = transparent;
}

View::View(MadsM4Engine *vm, int x, int y, bool transparent)
	: M4Surface(), _hotkeys(this), _vm(vm) {
	SCREEN_FLAGS_DEFAULT;
	_coords.left = x;
	_coords.top = y;
	_coords.right = _vm->_screen->width();
	_coords.bottom = _vm->_screen->height();
	_transparent = transparent;
}

void View::getCoordinates(Common::Rect &rect) {
	rect = _coords;
}

void View::extract(int *status) {
}

void View::show() {
	_screenFlags.visible = true;
	_vm->_viewManager->moveToFront(this);
	_vm->_viewManager->restore(_coords);
}

void View::hide() {
	_screenFlags.visible = false;
	_vm->_viewManager->restore(_coords);
}

void View::moveToBack() {
	_vm->_viewManager->moveToBack(this);
}

void View::moveAbsolute(int x, int y) {
	// TODO: Handle clipping and offscreen
	Common::Rect oldCoords = _coords;
	_coords.moveTo(x, y);
	_vm->_viewManager->restore(oldCoords);
	_vm->_viewManager->restore(_coords);
}

void View::moveRelative(int x, int y) {
	// TODO: Handle clipping and offscreen
	Common::Rect oldCoords = _coords;
	_coords.translate(x, y);
	_vm->_viewManager->restore(oldCoords);
	_vm->_viewManager->restore(_coords);
}

void View::resize(int newWidth, int newHeight) {
	Common::Rect oldCoords = _coords;
	if (newWidth >= 0)
		_coords.setWidth(newWidth);
	if (newHeight >= 0)
		_coords.setHeight(newHeight);
	_vm->_viewManager->restore(oldCoords);
	_vm->_viewManager->restore(_coords);
}

void View::restore(int x1, int y1, int x2, int y2) {
	_vm->_viewManager->restore(_coords.left + x1, _coords.top + y1, _coords.left + x2, _coords.top + y2);
}

void View::onRefresh(RectList *rects, M4Surface *destSurface) {
	assert(destSurface);

	if (rects == NULL)
		// No rect list specified, so copy entire surface
		copyTo(destSurface, _coords.left, _coords.top, _transparent ? 0 : -1);
	else {
		// Loop through the set of specified rectangles
		RectList::iterator i;
		for (i = rects->begin(); i != rects->end(); ++i) {
			Common::Rect &destRect = *i;
			Common::Rect srcBounds(destRect.left - _coords.left, destRect.top - _coords.top,
				destRect.right - _coords.left, destRect.bottom - _coords.top);
			copyTo(destSurface, srcBounds, destRect.left, destRect.top, _transparent ? 0 : -1);
		}
	}
}

//--------------------------------------------------------------------------

ViewManager::ViewManager(MadsM4Engine *vm): _systemHotkeys(HotkeyList(NULL)), _vm(vm) {
	_captureScreen = NULL;
	_captureEvents = false;
}

ViewManager::~ViewManager() {
	// Delete any remaining active views
	ListIterator i;
	for (i = _views.begin(); i != _views.end(); ++i)
		delete (*i);
}

void ViewManager::addView(View *view) {
	_views.push_back(view);
	moveToFront(view);
}

// Warning: After calling this method, the passed view object will no longer be valid

void ViewManager::deleteView(View *view) {
	_views.remove(view);
	delete view;
}

void ViewManager::handleEvents(const Common::Event &event) {
}

void ViewManager::handleKeyboardEvents(uint32 keycode) {
	Common::Point mousePos = _vm->_mouse->currentPos();
	View *view;
	bool blockedFlag;
	bool foundFlag;
	bool handledFlag;

	// Scan view list for one which accepts or blocks keyboard events. If one is found,
	// then the event is passed to it

	view = NULL;
	handledFlag = false;
	foundFlag = false;
	blockedFlag = false;

	// Loop from the front to back view
	ListIterator i;
	for (i = _views.reverse_begin(); (i != _views.end()) && !foundFlag; --i) {
		view = *i;
		if (!view->isVisible()) continue;

		if (view->screenFlags().blocks & SCREVENT_KEY)
			blockedFlag = true;
		if (view->screenFlags().get & SCREVENT_KEY) {
			foundFlag = true;
			handledFlag = (view->onEvent)(KEVENT_KEY, keycode, mousePos.x, mousePos.y, _captureEvents);
			if (_captureEvents)
				_captureScreen = view;
		}
	}

	// Scan view list for one with a hotkey list, aborting if a view is found that either
	// blocks keyboard events, or has a hotkey list that includes the keycode

	blockedFlag = false;
	for (i = _views.reverse_begin(); (i != _views.end()) && !foundFlag && !blockedFlag; --i) {
		view = *i;
		if (!view->isVisible()) continue;

		if (view->screenFlags().blocks & SCREVENT_KEY)
			blockedFlag = true;
		if (view->screenFlags().get & SCREVENT_KEY) {
			if (view->hotkeys().call(keycode)) {
				handledFlag = true;
				_captureEvents = false;
				//_vm->_dialogs->keyMouseCollision();	// TODO
			}
		}
	}

	// Final check: if no view handled or blocked the key, check against the system hotkey list

	if (!handledFlag && !blockedFlag) {
		handledFlag = _systemHotkeys.call(keycode);
		if (handledFlag) {
			_captureEvents = false;
			//_vm->_dialogs->keyMouseCollision();	// TODO
		}
	}
}

void ViewManager::handleMouseEvents(M4EventType event) {
	Common::Point mousePos = _vm->_mouse->currentPos();
	ListIterator i;
	View *view;
	bool blockedFlag;
	bool foundFlag;

	// If a window sets the _captureEvents flag to true, it will receive all events until
	// it sets it to false, even if it's not the top window
	if (_captureEvents) {
		assert(_captureScreen);
		if (_captureScreen->screenFlags().get & SCREVENT_MOUSE)
			(_captureScreen->onEvent)(event, 0, mousePos.x, mousePos.y, _captureEvents);

	} else {
		blockedFlag = false;
		foundFlag = false;
		view = NULL;

		// Loop from the front to back view
		for (i = _views.reverse_begin(); (i != _views.end()) && !foundFlag && !blockedFlag; --i) {
			view = *i;
			if (!view->isVisible()) continue;

			if (view->screenFlags().blocks & SCREVENT_MOUSE)
				blockedFlag = true;
			if ((view->screenFlags().get & SCREVENT_MOUSE) && view->isInside(mousePos.x, mousePos.y))
				foundFlag = true;
		}

		if (foundFlag)
			view->onEvent(event, 0, mousePos.x, mousePos.y, _captureEvents);
		else
			_captureEvents = false;
		if (_captureEvents)
			_captureScreen = view;
	}
}

void ViewManager::restore(int x1, int y1, int x2, int y2) {
	RectList *rl = new RectList();
	Common::Rect redrawBounds(x1, y1, x2, y2);
	rl->addRect(x1, y1, x2, y2);

	for (ListIterator i = _views.begin(); i != _views.end(); ++i) {
		View *v = *i;

		if (v->isVisible() && v->bounds().intersects(redrawBounds))
			v->onRefresh(rl, _vm->_screen);
	}

	_vm->_screen->update();

}

void ViewManager::restore(const Common::Rect &rect) {
	restore(rect.left, rect.top, rect.right, rect.bottom);
}

void ViewManager::moveToFront(View *view) {
	if (_views.size() < 2)
		return;

	_views.remove(view);

	ListIterator i = _views.begin();
	while ((i != _views.end()) && ((*i)->layer() <= view->layer()))
		++i;

	_views.insert(i, view);
}

void ViewManager::moveToBack(View *view) {
	if (_views.size() < 2)
		return;

	_views.remove(view);

	ListIterator i = _views.begin();
	while ((i != _views.end()) && ((*i)->layer() < view->layer()))
		++i;

	_views.insert(i, view);
}

View *ViewManager::getView(int screenType) {
	ListIterator i = _views.begin();
	while (i != _views.end()) {
		if ((*i)->screenType() == screenType)
			return *i;
		++i;
	}

	return NULL;
}

void ViewManager::updateState() {
	Common::List<View *> viewList = _views;

	for (ListIterator i = viewList.begin(); i != viewList.end(); ++i) {
		if (_vm->_events->quitFlag)
			return;

		View *v = *i;
		v->updateState();
	}
}

void ViewManager::refreshAll() {
	_vm->_screen->clear();

	for (ListIterator i = _views.begin(); i != _views.end(); ++i) {
		View *v = *i;

		if (v->isVisible())
			v->onRefresh(NULL, _vm->_screen);
	}

	_vm->_screen->update();
}

void ViewManager::showTextView(const char *textViewName, bool returnToMainMenu) {
	// Deactivate the scene if it's currently active
	View *view = _vm->_viewManager->getView(VIEWID_SCENE);
	if (view != NULL)
		_vm->_viewManager->deleteView(view);

	// Deactivate the main menu if it's currently active
	view = _vm->_viewManager->getView(VIEWID_MAINMENU);
	if (view != NULL)
		_vm->_viewManager->deleteView(view);

	// Activate the textview view
	_vm->_font->setFont(FONT_CONVERSATION_MADS);
	TextviewView *textView = new TextviewView(_vm);
	_vm->_viewManager->addView(textView);
	if (returnToMainMenu)
		textView->setScript(textViewName, returnToMainMenuFn);
	else
		textView->setScript(textViewName, NULL);
}

void ViewManager::showAnimView(const char *animViewName, bool returnToMainMenu) {
	// Deactivate the scene if it's currently active
	View *view = _vm->_viewManager->getView(VIEWID_SCENE);
	if (view != NULL)
		_vm->_viewManager->deleteView(view);

	// Deactivate the main menu if it's currently active
	view = _vm->_viewManager->getView(VIEWID_MAINMENU);
	if (view != NULL)
		_vm->_viewManager->deleteView(view);

	// Activate the animview view
	AnimviewView *animView = new AnimviewView(_vm);
	_vm->_viewManager->addView(animView);
	if (returnToMainMenu)
		animView->setScript(animViewName, returnToMainMenuFn);
	else
		animView->setScript(animViewName, NULL);
}

} // End of namespace M4