aboutsummaryrefslogtreecommitdiff
path: root/engines/cryomni3d/versailles/toolbar.cpp
blob: f6884bd035cfdea62e3958fb3680d6973a38c387 (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
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* 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/system.h"

#include "cryomni3d/cryomni3d.h"

#include "cryomni3d/versailles/toolbar.h"

namespace CryOmni3D {
namespace Versailles {

void Toolbar::init(const Sprites *sprites, FontManager *fontManager,
                   const Common::Array<Common::String> *messages, Inventory *inventory,
                   CryOmni3DEngine *engine) {
	_sprites = sprites;
	_fontManager = fontManager;
	_messages = messages;
	_inventory = inventory;
	_engine = engine;

	_bgSurface.create(640, 60, Graphics::PixelFormat::createFormatCLUT8());
	_destSurface.create(640, 60, Graphics::PixelFormat::createFormatCLUT8());

	// Inventory
	addZone(51, 56, Common::Point(211, 8), &Toolbar::callbackInventory<0>);
	addZone(51, 56, Common::Point(258, 8), &Toolbar::callbackInventory<1>);
	addZone(51, 56, Common::Point(305, 8), &Toolbar::callbackInventory<2>);
	addZone(51, 56, Common::Point(352, 8), &Toolbar::callbackInventory<3>);
	addZone(51, 56, Common::Point(399, 8), &Toolbar::callbackInventory<4>);
	addZone(51, 56, Common::Point(446, 8), &Toolbar::callbackInventory<5>);
	addZone(51, 56, Common::Point(493, 8), &Toolbar::callbackInventory<6>);
	addZone(51, 56, Common::Point(540, 8), &Toolbar::callbackInventory<7>);

	// Documentation
	const Graphics::Cursor &cursorDoc = _sprites->getCursor(133);
	Common::Point docPos(627 - cursorDoc.getWidth(), 42 - cursorDoc.getHeight());
	addZone(133, 137, docPos, &Toolbar::callbackDocumentation);

	// Options
	const Graphics::Cursor &cursorOpt = _sprites->getCursor(225);
	Common::Point optPos(0, 60 - cursorOpt.getHeight());
	addZone(225, 225, optPos, &Toolbar::callbackOptions);

	// Previous or next
	addZone(183, -1, Common::Point(190, 18), &Toolbar::callbackInventoryPrev);
	addZone(240, -1, Common::Point(574, 18), &Toolbar::callbackInventoryNext);
	// View
	addZone(142, -1, Common::Point(158, 12), &Toolbar::callbackViewObject);
}

Toolbar::~Toolbar() {
	_bgSurface.free();
	_destSurface.free();
}

void Toolbar::inventoryChanged(uint newPosition) {
	if (newPosition != -1u && newPosition > _inventoryOffset) {
		_inventoryOffset = newPosition - 7;
	}
	// Refresh
	updateZones();
}

void Toolbar::addZone(uint16 cursorMainId, uint16 cursorSecondaryId, Common::Point position,
                      ZoneCallback callback) {
	const Graphics::Cursor &cursorMain = _sprites->getCursor(cursorMainId);
	Common::Rect rct(cursorMain.getWidth(), cursorMain.getHeight());
	rct.moveTo(position);

	// By default it's the secondary image
	Zone zone = { rct, cursorMainId, cursorSecondaryId, callback, true, false };
	_zones.push_back(zone);
}

Common::Array<Toolbar::Zone>::const_iterator Toolbar::hitTestZones(const Common::Point &mousePos)
const {
	Common::Array<Zone>::const_iterator it;
	for (it = _zones.begin(); it != _zones.end(); it++) {
		if (!it->hidden && it->rect.contains(mousePos) && it->callback) {
			break;
		}
	}
	return it;
}

uint Toolbar::captureEvent(const Common::Point &mousePos, uint dragStatus) {
	uint result = 0;
	Common::Array<Zone>::const_iterator it = hitTestZones(mousePos);
	if (it != _zones.end()) {
		result = (this->*(it->callback))(dragStatus);
	}
	return result;
}

void Toolbar::updateZones() {
	_zones[8].secondary = !_engine->hasPlaceDocumentation();

	Inventory::const_iterator inventoryIt, inventorySelectedIt;
	if (!_inventoryEnabled) {
		_inventoryMaxOffset = 0;
		_inventoryOffset = 0;
		_zones[10].secondary = true;
		_zones[11].secondary = true;
	} else {
		_inventoryMaxOffset = 0;
		// Find an object in inventory after the 8 first
		for (inventoryIt = _inventory->begin() + 8; inventoryIt != _inventory->end(); inventoryIt++) {
			if (*inventoryIt != nullptr) {
				_inventoryMaxOffset = (inventoryIt - _inventory->begin()) - 7;
			}
		}
		_zones[10].secondary = !_inventoryMaxOffset;
		_zones[11].secondary = !_inventoryMaxOffset;
		if (_inventoryOffset > _inventoryMaxOffset) {
			// Clamp inventory offset to its max
			_inventoryOffset = _inventoryMaxOffset;
		}
		inventoryIt = _inventory->begin() + _inventoryOffset;
		inventorySelectedIt = _inventory->begin() + _inventorySelected;
	}
	// Inventory zones are from 0 to 7
	for (Common::Array<Zone>::iterator zoneIt = _zones.begin(); zoneIt != _zones.begin() + 8;
	        zoneIt++, inventoryIt++) {
		if (!_inventoryEnabled) {
			zoneIt->hidden = true;
			zoneIt->imageMain = 0;
			zoneIt->imageSecondary = 0;
			zoneIt->secondary = false;
		} else if (inventoryIt >= _inventory->end() || *inventoryIt == nullptr) {
			// Nothing in inventory at this position
			zoneIt->hidden = false;
			zoneIt->imageMain = 51;
			zoneIt->imageSecondary = 56;
			zoneIt->secondary = true;
		} else {
			// Setup inventory icon
			zoneIt->hidden = false;
			zoneIt->imageMain = (*inventoryIt)->idCA();
			zoneIt->imageSecondary = (*inventoryIt)->idCl();
			zoneIt->secondary = (inventorySelectedIt != inventoryIt);
		}
	}
}

uint Toolbar::callbackInventory(uint invId, uint dragStatus) {
	if (!_inventoryEnabled) {
		return 0;
	}

	invId += _inventoryOffset;
	Object *obj = nullptr;
	if (invId < _inventory->size()) {
		obj = (*_inventory)[invId];
	}
	if (obj == nullptr) {
		return 0;
	}

	if (!obj->valid()) {
		return 0;
	}

	switch (dragStatus) {
	case kDragStatus_Pressed:
		_inventorySelected = invId;
		_engine->setCursor(181);
		_zones[12].secondary = (obj->viewCallback() == nullptr);
		_inventory_button_dragging = true;
		return 1;
	case kDragStatus_Dragging:
		if (_inventorySelected == invId) {
			return 0;
		}
		_inventorySelected = invId;
		_zones[12].secondary = (obj->viewCallback() == nullptr);
		_inventory_button_dragging = true;
		return 1;
	case kDragStatus_Finished:
		_engine->setCursor(obj->idSl());
		_inventory->setSelectedObject(obj);
		_inventorySelected = invId;
		return 1;
	default:
		return 0;
	}

}

uint Toolbar::callbackInventoryPrev(uint dragStatus) {
	if (!_inventoryEnabled) {
		return 0;
	}

	if (dragStatus == kDragStatus_Pressed && _inventoryOffset > 0) {
		// Restart auto repeat only if there could be something
		_engine->setAutoRepeatClick(150);
		_inventoryOffset--;
		return 1;
	}
	// In any other case we didn't do anything
	return 0;
}

uint Toolbar::callbackInventoryNext(uint dragStatus) {
	if (!_inventoryEnabled) {
		return 0;
	}

	if (dragStatus == kDragStatus_Pressed && _inventoryOffset < _inventoryMaxOffset) {
		_engine->setAutoRepeatClick(150);
		_inventoryOffset++;
		return 1;
	}
	// In any other case we didn't do anything
	return 0;
}

uint Toolbar::callbackViewObject(uint dragStatus) {
	if (!_inventoryEnabled) {
		return 0;
	}

	_mouse_in_view_object = true;

	if (_inventorySelected == -1u) {
		// Nothing selected in toolbar
		return 0;
	}
	Inventory::const_iterator inventorySelectedIt = _inventory->begin() + _inventorySelected;
	Object *selectedObject = *inventorySelectedIt;
	if (selectedObject == nullptr || selectedObject->viewCallback() == nullptr) {
		// Nothing to view, the sprite isn't even displayed
		return 0;
	}

	switch (dragStatus) {
	case kDragStatus_NoDrag:
		_backup_selected_object = selectedObject;
		_engine->setCursor(181);
		return 0;
	case kDragStatus_Pressed:
	case kDragStatus_Dragging:
		return 1;
	case kDragStatus_Finished:
		// Just clicked
		g_system->showMouse(false);
		(*selectedObject->viewCallback())();
		g_system->showMouse(true);
		_parentMustRedraw = true;
		_shortExit = true;
		return 1;
	default:
		return 0;
	}
}

uint Toolbar::callbackOptions(uint dragStatus) {
	_mouse_in_options = true;

	switch (dragStatus) {
	case kDragStatus_NoDrag:
		_backup_selected_object = _inventory->selectedObject();
		_engine->setCursor(181);
		return 0;
	case kDragStatus_Pressed:
	case kDragStatus_Dragging:
		// Nothing to do, we wait release
		return 0;
	case kDragStatus_Finished:
		// Just clicked
		_engine->displayOptions();
		_parentMustRedraw = true;
		_shortExit = true;
		_engine->setMousePos(Common::Point(320, 240)); // Center of screen
		// Displaying options hides the mouse
		g_system->showMouse(true);
		return 0;
	default:
		return 0;
	}
}

uint Toolbar::callbackDocumentation(uint dragStatus) {
	_mouse_in_options = true;

	switch (dragStatus) {
	case kDragStatus_NoDrag:
	case kDragStatus_Pressed:
	case kDragStatus_Dragging:
		// Nothing to do, we wait release
		return 0;
	case kDragStatus_Finished:
		// Just clicked
		if (_engine->displayPlaceDocumentation()) {
			_parentMustRedraw = true;
			_shortExit = true;
			_engine->setMousePos(Common::Point(320, 240)); // Center of screen
		}
		return 0;
	default:
		return 0;
	}
}

void Toolbar::drawToolbar(const Graphics::Surface *original) {
	if (_position > 60) {
		_position = 60;
	}

	if (_position != 0) {
		// Not entirely drawn, we must copy a part of the original image
		Common::Rect rct(0, 420, 640, 420 + _position);
		_destSurface.copyRectToSurface(*original, 0, 0, rct);
	}

	if (_position == 60) {
		// Entirely hidden, just stop there, we have nothing to draw
		return;
	}

	// Not entirely hidden, we must display the transparent background prepared for us
	Common::Rect rct(0, _position, 640, 60);
	_destSurface.copyRectToSurface(_bgSurface, 0, _position, rct);

	// Now draw the various zones on the surface
	for (Common::Array<Zone>::const_iterator it = _zones.begin(); it != _zones.end(); it++) {
		if (it->hidden) {
			continue;
		}

		uint16 spriteId = it->secondary ? it->imageSecondary : it->imageMain;
		if (spriteId == uint16(-1)) {
			continue;
		}

		Common::Rect dst = it->rect;
		dst.translate(0, _position);

		// Clip the rectangle to fit inside the surface
		dst.clip(Common::Rect(_destSurface.w, _destSurface.h));

		if (dst.isEmpty()) {
			continue;
		}

		const Graphics::Surface &sprite = _sprites->getSurface(spriteId);
		_destSurface.transBlitFrom(sprite, Common::Rect(dst.width(), dst.height()), dst,
		                           _sprites->getKeyColor(spriteId));
	}

	// And now draw the object description if needed
	if (_inventoryEnabled && _inventoryHovered != -1u) {
		Object *obj = (*_inventory)[_inventoryHovered];

		uint zoneId = _inventoryHovered - _inventoryOffset;
		if (zoneId >= 8) {
			// The object is hidden: huh?
			return;
		}

		_fontManager->setSurface(&_destSurface);
		_fontManager->setForeColor(243);
		_fontManager->setCurrentFont(5);
		_fontManager->setTransparentBackground(true);
		const Common::String &objName = (*_messages)[obj->idOBJ()];
		uint x = 195 - _fontManager->getStrWidth(objName);
		uint startX = _zones[zoneId].rect.left + kTextOffset;
		_fontManager->displayStr(x, 38 + _position, objName);
		_destSurface.hLine(x, 54 + _position, startX - 1, 243); // minus 1 because hLine draws inclusive
		_destSurface.vLine(startX, 42 + _position, 54 + _position, 243);
	}
}

bool Toolbar::displayToolbar(const Graphics::Surface *original) {
	/**
	 * In game there are 2 functions to handle toolbar: one in warp and one in fixed images
	 * This one is the warp one and fixed images have a more asynchronous one during pop-up/down phases
	 * Let's make it simple for now
	 */

	// WORKAROUND: Set cursor here to be more consistent: it's thumb cursor just before showing until just after showed
	_engine->setCursor(181);

	_parentMustRedraw = false;
	_shortExit = false;

	// Prepare the background of the toolbar by making it translucent
	// Get the lowest part of the image
	const Graphics::Surface subset = original->getSubArea(Common::Rect(0, original->h - _bgSurface.h,
	                                 _bgSurface.w, original->h));
	_engine->makeTranslucent(_bgSurface, subset);

	// WORKAROUND: Reset the inventory status at init to let sprites highlighted until toolbar is hidden
	_inventorySelected = -1;
	_inventoryHovered = -1;
	_zones[12].secondary = true;

	updateZones();

	for (_position = 60; _position > 0; _position--) {
		// Make the toolbar go up
		drawToolbar(original);
		g_system->copyRectToScreen(_destSurface.getPixels(), _destSurface.pitch, 0,
		                           original->h - _destSurface.h, _destSurface.w, _destSurface.h);
		g_system->updateScreen();
		// pollEvents will slow down the animation because it waits 10ms
		_engine->pollEvents();
		if (g_engine->shouldQuit()) {
			return false;
		}
	}

	// Flush events
	_engine->clearKeys();
	_engine->waitMouseRelease();

	handleToolbarEvents(original);
	if (g_engine->shouldQuit()) {
		return false;
	}

	if (_shortExit) {
		return _parentMustRedraw;
	}

	for (_position = 0; _position <= 60; _position++) {
		// Make the toolbar go up
		drawToolbar(original);
		g_system->copyRectToScreen(_destSurface.getPixels(), _destSurface.pitch, 0,
		                           original->h - _destSurface.h, _destSurface.w, _destSurface.h);
		g_system->updateScreen();
		// pollEvents will slow down the animation because it waits 10ms
		_engine->pollEvents();
		if (g_engine->shouldQuit()) {
			return false;
		}
	}

	return _parentMustRedraw;
}

void Toolbar::handleToolbarEvents(const Graphics::Surface *original) {
	bool mouseInsideToolbar;
	bool exitToolbar = false;
	bool redrawToolbar;

	// Don't have anything hovered for now
	_inventoryHovered = -1;
	_inventorySelected = -1;
	_inventory->setSelectedObject(nullptr);
	_backup_selected_object = nullptr;

	// Refresh zones because we erased selected object
	updateZones();

	// No need of original surface because the toolbar is fully displayed
	drawToolbar(original);

	g_system->copyRectToScreen(_destSurface.getPixels(), _destSurface.pitch, 0,
	                           original->h - _destSurface.h, _destSurface.w, _destSurface.h);
	g_system->updateScreen();

	_engine->setCursor(181);

	mouseInsideToolbar = (_engine->getMousePos().y > 388);

	while (!exitToolbar) {
		_mouse_in_options = false;
		_mouse_in_view_object = false;

		_engine->pollEvents();
		if (g_engine->shouldQuit()) {
			exitToolbar = true;
			break;
		}

		redrawToolbar = false;
		if (_engine->checkKeysPressed(2, Common::KEYCODE_ESCAPE, Common::KEYCODE_SPACE) ||
		        _engine->getCurrentMouseButton() == 2) {
			_engine->waitMouseRelease();
			exitToolbar = true;
			break;
		}

		Common::Point mousePosInToolbar = _engine->getMousePos();
		mousePosInToolbar -= Common::Point(0, 420);

		if (captureEvent(mousePosInToolbar, _engine->getDragStatus())) {
			// Something has changed with the zones handling, update zones
			updateZones();
			redrawToolbar = true;
		} else if (_engine->getDragStatus() == kDragStatus_Pressed) {
			// A click happened and wasn't handled, deselect object
			_inventorySelected = -1;
			_inventory->setSelectedObject(nullptr);
			_engine->setCursor(181);
			// Reset view object
			_zones[12].secondary = true;
			updateZones();
			redrawToolbar = true;
		}

		if (!mouseInsideToolbar) {
			mouseInsideToolbar = (_engine->getMousePos().y > 388);
		} else if (_engine->getMousePos().y <= 388) {
			// mouseInsideToolbar is true and the mouse is outside the toolbar
			exitToolbar = true;
			break;
		}

		if (_engine->getCurrentMouseButton() == 1) {
			// When the mouse button is down, nothing is selected
			// It's selected on release
			_inventory->setSelectedObject(nullptr);
		}

		if (_backup_selected_object != nullptr && !(_mouse_in_options || _mouse_in_view_object) &&
		        !_engine->getCurrentMouseButton()) {
			_inventory->setSelectedObject(_backup_selected_object);
			_engine->setCursor(_backup_selected_object->idSl());
			_backup_selected_object = nullptr;
		}

		// Hover the inventory objects
		if (_inventory->selectedObject() == nullptr /* || _inventory_button_dragging */) {
			// The 2nd above condition is maybe useless because when the mouse button is down the selected object is always null
			bool shouldHover = false;
			Common::Array<Zone>::const_iterator zoneIt = hitTestZones(mousePosInToolbar);
			uint zoneId = zoneIt - _zones.begin();
			uint inventoryId = zoneId + _inventoryOffset;
			if (zoneId < 8 && inventoryId < _inventory->size() && (*_inventory)[inventoryId] != nullptr) {
				// It's the inventory
				shouldHover = true;
				if (_inventoryHovered != inventoryId && (*_inventory)[inventoryId]->valid()) {
					// It's not the one currently hovered and it's a valid object
					_inventoryHovered = inventoryId;
					redrawToolbar = true;
				}
			}
			if (!shouldHover && _inventoryHovered != -1u && !_mouse_in_view_object)  {
				// Remove hovering
				_inventoryHovered = -1;
				_inventorySelected = -1;
				updateZones();
				if (!_inventory->selectedObject()) {
					// Reset back the cursor if nothing is selected
					_engine->setCursor(181);
				}
				// Remove view
				_zones[12].secondary = true;
				redrawToolbar = true;
			}
			_inventory_button_dragging = false;
		}

		if (_parentMustRedraw) {
			break;
		}

		if (redrawToolbar) {
			drawToolbar(original);
			g_system->copyRectToScreen(_destSurface.getPixels(), _destSurface.pitch, 0,
			                           original->h - _destSurface.h, _destSurface.w, _destSurface.h);
		}

		g_system->updateScreen();
	}

	// Hide description when finished and selected object
	// WORKAROUND: moved to the start to keep the selected object hilighted until the toolbar disappearance
}

} // End of namespace Versailles
} // End of namespace CryOmni3D