File: maciconbar.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (343 lines) | stat: -rw-r--r-- 10,362 bytes parent folder | download | duplicates (2)
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
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "sci/sci.h"
#include "sci/engine/kernel.h"
#include "sci/engine/selector.h"
#include "sci/engine/state.h"
#include "sci/graphics/gfxdrivers.h"
#include "sci/graphics/maciconbar.h"
#include "sci/graphics/palette.h"
#include "sci/graphics/screen.h"

#include "common/memstream.h"
#include "common/system.h"
#include "graphics/surface.h"
#include "image/pict.h"

namespace Sci {

GfxMacIconBar::GfxMacIconBar(ResourceManager *resMan, EventManager *eventMan, SegManager *segMan, GfxScreen *screen, GfxPalette *palette) :
	_resMan(resMan), _eventMan(eventMan), _segMan(segMan), _screen(screen), _palette(palette)  {
	if (g_sci->getGameId() == GID_FREDDYPHARKAS)
		_inventoryIndex = 5;
	else
		_inventoryIndex = 4;

	_inventoryIcon = nullptr;
	_allDisabled = true;
	
	_isUpscaled = (_screen->getUpscaledHires() == GFX_SCREEN_UPSCALED_640x400);
}

GfxMacIconBar::~GfxMacIconBar() {
	freeIcons();
}

void GfxMacIconBar::initIcons(uint16 count, reg_t *objs) {
	// free icons and reset state in case game is restarting
	freeIcons();
	_iconBarItems.clear();
	_inventoryIcon = nullptr;
	_allDisabled = true;

	for (uint16 i = 0; i < count; i++) {
		addIcon(objs[i]);
	}
}

void GfxMacIconBar::freeIcons() {
	if (_inventoryIcon) {
		_inventoryIcon->free();
		delete _inventoryIcon;
	}

	for (uint32 i = 0; i < _iconBarItems.size(); i++) {
		if (_iconBarItems[i].nonSelectedImage) {
			_iconBarItems[i].nonSelectedImage->free();
			delete _iconBarItems[i].nonSelectedImage;
		}

		if (_iconBarItems[i].selectedImage) {
			_iconBarItems[i].selectedImage->free();
			delete _iconBarItems[i].selectedImage;
		}
	}
}

void GfxMacIconBar::addIcon(reg_t obj) {
	IconBarItem item;
	uint32 iconIndex = readSelectorValue(_segMan, obj, SELECTOR(iconIndex));

	item.object = obj;
	item.nonSelectedImage = createImage(iconIndex, false);

	if (iconIndex != _inventoryIndex)
		item.selectedImage = createImage(iconIndex, true);
	else
		item.selectedImage = nullptr;

	item.enabled = true;

	// Start after last icon
	uint16 x = _iconBarItems.empty() ? 0 : _iconBarItems.back().rect.right;

	// Start below the main viewing window and add a two pixel buffer
	uint16 y = _screen->getHeight() + 2;

	if (item.nonSelectedImage)
		item.rect = Common::Rect(x, y, MIN<uint32>(x + item.nonSelectedImage->w, 320), y + item.nonSelectedImage->h);
	else
		error("Could not find a non-selected image for icon %d", iconIndex);

	_iconBarItems.push_back(item);
}

void GfxMacIconBar::drawIcons() {
	// Draw the icons to the bottom of the screen

	for (uint32 i = 0; i < _iconBarItems.size(); i++)
		drawIcon(i, false);
}

void GfxMacIconBar::drawIcon(uint16 iconIndex, bool selected) {
	if (iconIndex >= _iconBarItems.size())
		return;

	Common::Rect rect = _iconBarItems[iconIndex].rect;

	if (isIconEnabled(iconIndex)) {
		if (selected)
			drawImage(_iconBarItems[iconIndex].selectedImage, rect, true);
		else
			drawImage(_iconBarItems[iconIndex].nonSelectedImage, rect, true);
	} else
		drawImage(_iconBarItems[iconIndex].nonSelectedImage, rect, false);

	if ((iconIndex == _inventoryIndex) && _inventoryIcon) {
		Common::Rect invRect = Common::Rect(0, 0, _inventoryIcon->w, _inventoryIcon->h);
		invRect.moveTo(rect.left, rect.top);
		invRect.translate((rect.width() - invRect.width()) / 2, (rect.height() - invRect.height()) / 2);

		drawImage(_inventoryIcon, invRect, isIconEnabled(iconIndex));
	}
}

// Add a black checkerboard pattern to an image before copying it to the screen.
// The pattern is to be applied to the image after any upscaling occurs, so rect
// must be the final screen coordinates.
void GfxMacIconBar::drawDisabledPattern(Graphics::Surface &surface, const Common::Rect &rect) {
	for (int y = 0; y < surface.h; y++) {
		// Start at the next four byte boundary
		int startX = 3 - ((rect.left + 3) & 3);

		// Start odd rows at two bytes past that (also properly aligned)
		if ((y + rect.top) & 1) {
			startX = (startX + 2) & 3;
		}

		// Set every fourth pixel to black
		for (int x = startX; x < surface.w; x += 4) {
			surface.setPixel(x, y, 0);
		}
	}
}

void GfxMacIconBar::drawImage(Graphics::Surface *surface, const Common::Rect &rect, bool enable) {
	if (surface == nullptr) {
		return;
	}

	if (_isUpscaled) {
		Common::Rect dstRect(rect.left * 2, rect.top * 2, rect.right * 2, rect.bottom * 2);

		// increase _upscaleBuffer if needed
		const uint32 upscaleSize = dstRect.width() * dstRect.height();
		if (upscaleSize > _upscaleBuffer->size()) {
			_upscaleBuffer.clear();
			_upscaleBuffer->allocate(upscaleSize);
		}

		// scale2x
		const int srcWidth = rect.width();
		const int srcHeight = rect.height();
		const int srcPitch = surface->pitch;
		const byte *srcPtr = (byte *)surface->getPixels();
		byte *dstPtr = _upscaleBuffer->getUnsafeDataAt(0, upscaleSize);
		for (int y = 0; y < srcHeight; y++) {
			for (int x = 0; x < srcWidth; x++) {
				const byte color = *srcPtr++;
				dstPtr[0] = color;
				dstPtr[1] = color;
				dstPtr[dstRect.width() + 0] = color;
				dstPtr[dstRect.width() + 1] = color;
				dstPtr += 2;
			}
			srcPtr += (srcPitch - srcWidth);
			dstPtr += dstRect.width();
		}

		if (!enable) {
			Graphics::Surface upscaleSurface;
			upscaleSurface.init(dstRect.width(), dstRect.height(), dstRect.width(), _upscaleBuffer->getUnsafeDataAt(0, upscaleSize), surface->format);
			drawDisabledPattern(upscaleSurface, dstRect);
		}
		_screen->gfxDriver()->copyRectToScreen(_upscaleBuffer->getUnsafeDataAt(0, upscaleSize), 0, 0, dstRect.width(), dstRect.left, dstRect.top, dstRect.width(), dstRect.height(), nullptr, nullptr);
	} else {
		if (!enable) {
			Graphics::Surface disableSurface;
			disableSurface.copyFrom(*surface);
			drawDisabledPattern(disableSurface, rect);
			_screen->gfxDriver()->copyRectToScreen((const byte*)disableSurface.getPixels(), 0, 0, disableSurface.pitch, rect.left, rect.top, rect.width(), rect.height(), nullptr, nullptr);
		} else {
			_screen->gfxDriver()->copyRectToScreen((const byte*)surface->getPixels(), 0, 0, surface->pitch, rect.left, rect.top, rect.width(), rect.height(), nullptr, nullptr);
		}
	}
}

bool GfxMacIconBar::isIconEnabled(uint16 iconIndex) const {
	if (iconIndex >= _iconBarItems.size())
		return false;

	return !_allDisabled && _iconBarItems[iconIndex].enabled;
}

void GfxMacIconBar::setIconEnabled(int16 iconIndex, bool enabled) {
	if (iconIndex < 0)
		_allDisabled = !enabled;
	else if (iconIndex < (int)_iconBarItems.size()) {
		_iconBarItems[iconIndex].enabled = enabled;
	}
}

void GfxMacIconBar::setInventoryIcon(int16 icon) {
	Graphics::Surface *surface = nullptr;

	if (icon >= 0)
		surface = loadPict(ResourceId(kResourceTypeMacPict, icon));

	if (_inventoryIcon) {
		// Free old inventory icon if we're removing the inventory icon
		// or setting a new one.
		if ((icon < 0) || surface) {
			_inventoryIcon->free();
			delete _inventoryIcon;
			_inventoryIcon = nullptr;
		}
	}

	if (surface)
		_inventoryIcon = surface;

	drawIcon(_inventoryIndex, false);
}

Graphics::Surface *GfxMacIconBar::loadPict(ResourceId id) {
	Resource *res = _resMan->findResource(id, false);

	if (!res || res->size() == 0)
		return nullptr;

	Image::PICTDecoder pictDecoder;
	Common::MemoryReadStream stream(res->toStream());
	if (!pictDecoder.loadStream(stream))
		return nullptr;

	Graphics::Surface *surface = new Graphics::Surface();
	surface->copyFrom(*pictDecoder.getSurface());
	remapColors(surface, pictDecoder.getPalette());

	return surface;
}

Graphics::Surface *GfxMacIconBar::createImage(uint32 iconIndex, bool isSelected) {
	ResourceType type = isSelected ? kResourceTypeMacIconBarPictS : kResourceTypeMacIconBarPictN;
	return loadPict(ResourceId(type, iconIndex + 1));
}

void GfxMacIconBar::remapColors(Graphics::Surface *surf, const byte *palette) {
	byte *pixels = (byte *)surf->getPixels();

	// Remap to the screen palette
	for (uint16 i = 0; i < surf->w * surf->h; i++) {
		byte color = *pixels;

		byte r = palette[color * 3];
		byte g = palette[color * 3 + 1];
		byte b = palette[color * 3 + 2];

		*pixels++ = _palette->findMacIconBarColor(r, g, b);
	}
}

bool GfxMacIconBar::pointOnIcon(uint32 iconIndex, Common::Point point) {
	return _iconBarItems[iconIndex].rect.contains(point);
}

bool GfxMacIconBar::handleEvents(SciEvent evt, reg_t &iconObj) {
	iconObj = NULL_REG;

	// Not a mouse press
	if (evt.type != kSciEventMousePress)
		return false;

	// If the mouse is not over the icon bar, return
	if (evt.mousePos.y < _screen->getHeight())
		return false;

	// Mouse press on the icon bar, check the icon rectangles
	uint iconNr;
	for (iconNr = 0; iconNr < _iconBarItems.size(); iconNr++) {
		if (pointOnIcon(iconNr, evt.mousePos) && isIconEnabled(iconNr))
			break;
	}

	// Mouse press on the icon bar but not on an enabled icon,
	// return true to indicate that this mouse press was handled
	if (iconNr == _iconBarItems.size())
		return true;

	drawIcon(iconNr, true);
	bool isSelected = true;

	// Wait for mouse release
	while (evt.type != kSciEventMouseRelease) {
		// Mimic behavior of SSCI when moving mouse with button held down
		if (isSelected != pointOnIcon(iconNr, evt.mousePos)) {
			isSelected = !isSelected;
			drawIcon(iconNr, isSelected);
		}

		evt = _eventMan->getSciEvent(kSciEventMouseRelease);
		g_system->delayMillis(10);
	}

	drawIcon(iconNr, false);

	// If user moved away from the icon, we do nothing
	if (pointOnIcon(iconNr, evt.mousePos))
		iconObj = _iconBarItems[iconNr].object;

	// The mouse press was handled
	return true;
}

} // End of namespace Sci