aboutsummaryrefslogtreecommitdiff
path: root/engines/wintermute/ui/ui_tiled_image.cpp
blob: e895477a368c7605d5fc12c9e2ff9ad1a62a2f41 (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
/* 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.
 *
 */

/*
 * This file is based on WME Lite.
 * http://dead-code.org/redir.php?target=wmelite
 * Copyright (c) 2011 Jan Nedoma
 */

#include "engines/wintermute/ui/ui_tiled_image.h"
#include "engines/wintermute/base/gfx/base_surface.h"
#include "engines/wintermute/base/base_dynamic_buffer.h"
#include "engines/wintermute/base/base_parser.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/base/base_sub_frame.h"
#include "engines/wintermute/base/base_file_manager.h"
#include "engines/wintermute/base/gfx/base_renderer.h"
#include "engines/wintermute/platform_osystem.h"

namespace Wintermute {

IMPLEMENT_PERSISTENT(UITiledImage, false)

//////////////////////////////////////////////////////////////////////////
UITiledImage::UITiledImage(BaseGame *inGame) : BaseObject(inGame) {
	_image = nullptr;

	_upLeft.setEmpty();
	_upMiddle.setEmpty();
	_upRight.setEmpty();
	_middleLeft.setEmpty();
	_middleMiddle.setEmpty();
	_middleRight.setEmpty();
	_downLeft.setEmpty();
	_downMiddle.setEmpty();
	_downRight.setEmpty();
}


//////////////////////////////////////////////////////////////////////////
UITiledImage::~UITiledImage() {
	delete _image;
	_image = nullptr;
}


//////////////////////////////////////////////////////////////////////////
bool UITiledImage::display(int x, int y, int width, int height) {
	if (!_image) {
		return STATUS_FAILED;
	}

	int tileWidth = _middleMiddle.right - _middleMiddle.left;
	int tileHeight = _middleMiddle.bottom - _middleMiddle.top;

	int nuColumns = (width - (_middleLeft.right - _middleLeft.left) - (_middleRight.right - _middleRight.left)) / tileWidth;
	int nuRows = (height - (_upMiddle.bottom - _upMiddle.top) - (_downMiddle.bottom - _downMiddle.top)) / tileHeight;

	_gameRef->_renderer->startSpriteBatch();

	// top left/right
	_image->_surface->displayTrans(x,                                                       y, _upLeft);
	_image->_surface->displayTrans(x + (_upLeft.right - _upLeft.left) + nuColumns * tileWidth, y, _upRight);

	// bottom left/right
	_image->_surface->displayTrans(x,                                                       y + (_upMiddle.bottom - _upMiddle.top) + nuRows * tileHeight, _downLeft);
	_image->_surface->displayTrans(x + (_upLeft.right - _upLeft.left) + nuColumns * tileWidth, y + (_upMiddle.bottom - _upMiddle.top) + nuRows * tileHeight, _downRight);

	// left/right
	if (nuRows > 0) {
		int yyy = y + (_upMiddle.bottom - _upMiddle.top);
		_image->_surface->displayTiled(x, yyy, _middleLeft, 1, nuRows);
		_image->_surface->displayTiled(x + (_middleLeft.right - _middleLeft.left) + nuColumns * tileWidth, yyy, _middleRight, 1, nuRows);
	}

	// top/bottom
	if (nuColumns > 0) {
		int xxx = x + (_upLeft.right - _upLeft.left);
		_image->_surface->displayTiled(xxx, y, _upMiddle, nuColumns, 1);
		_image->_surface->displayTiled(xxx, y + (_upMiddle.bottom - _upMiddle.top) + nuRows * tileHeight, _downMiddle, nuColumns, 1);
	}

	// tiles
	if (nuRows > 0 && nuColumns > 0) {
		int yyy = y + (_upMiddle.bottom - _upMiddle.top);
		int xxx = x + (_upLeft.right - _upLeft.left);
		_image->_surface->displayTiled(xxx, yyy, _middleMiddle, nuColumns, nuRows);
	}

	_gameRef->_renderer->endSpriteBatch();

	return STATUS_OK;
}


//////////////////////////////////////////////////////////////////////////
bool UITiledImage::loadFile(const char *filename) {
	char *buffer = (char *)BaseFileManager::getEngineInstance()->readWholeFile(filename);
	if (buffer == nullptr) {
		_gameRef->LOG(0, "UITiledImage::LoadFile failed for file '%s'", filename);
		return STATUS_FAILED;
	}

	bool ret;

	setFilename(filename);

	if (DID_FAIL(ret = loadBuffer(buffer, true))) {
		_gameRef->LOG(0, "Error parsing TILED_IMAGE file '%s'", filename);
	}


	delete[] buffer;

	return ret;
}


TOKEN_DEF_START
TOKEN_DEF(TILED_IMAGE)
TOKEN_DEF(TEMPLATE)
TOKEN_DEF(IMAGE)
TOKEN_DEF(UP_LEFT)
TOKEN_DEF(UP_RIGHT)
TOKEN_DEF(UP_MIDDLE)
TOKEN_DEF(DOWN_LEFT)
TOKEN_DEF(DOWN_RIGHT)
TOKEN_DEF(DOWN_MIDDLE)
TOKEN_DEF(MIDDLE_LEFT)
TOKEN_DEF(MIDDLE_RIGHT)
TOKEN_DEF(MIDDLE_MIDDLE)
TOKEN_DEF(VERTICAL_TILES)
TOKEN_DEF(HORIZONTAL_TILES)
TOKEN_DEF(EDITOR_PROPERTY)
TOKEN_DEF_END
//////////////////////////////////////////////////////////////////////////
bool UITiledImage::loadBuffer(char *buffer, bool complete) {
	TOKEN_TABLE_START(commands)
	TOKEN_TABLE(TILED_IMAGE)
	TOKEN_TABLE(TEMPLATE)
	TOKEN_TABLE(IMAGE)
	TOKEN_TABLE(UP_LEFT)
	TOKEN_TABLE(UP_RIGHT)
	TOKEN_TABLE(UP_MIDDLE)
	TOKEN_TABLE(DOWN_LEFT)
	TOKEN_TABLE(DOWN_RIGHT)
	TOKEN_TABLE(DOWN_MIDDLE)
	TOKEN_TABLE(MIDDLE_LEFT)
	TOKEN_TABLE(MIDDLE_RIGHT)
	TOKEN_TABLE(MIDDLE_MIDDLE)
	TOKEN_TABLE(VERTICAL_TILES)
	TOKEN_TABLE(HORIZONTAL_TILES)
	TOKEN_TABLE(EDITOR_PROPERTY)
	TOKEN_TABLE_END

	char *params;
	int cmd;
	BaseParser parser;
	bool hTiles = false, vTiles = false;
	int h1 = 0, h2 = 0, h3 = 0;
	int v1 = 0, v2 = 0, v3 = 0;

	if (complete) {
		if (parser.getCommand(&buffer, commands, &params) != TOKEN_TILED_IMAGE) {
			_gameRef->LOG(0, "'TILED_IMAGE' keyword expected.");
			return STATUS_FAILED;
		}
		buffer = params;
	}

	while ((cmd = parser.getCommand(&buffer, commands, &params)) > 0) {
		switch (cmd) {
		case TOKEN_TEMPLATE:
			if (DID_FAIL(loadFile(params))) {
				cmd = PARSERR_GENERIC;
			}
			break;

		case TOKEN_IMAGE:
			delete _image;
			_image = new BaseSubFrame(_gameRef);
			if (!_image || DID_FAIL(_image->setSurface(params))) {
				delete _image;
				_image = nullptr;
				cmd = PARSERR_GENERIC;
			}
			break;

		case TOKEN_UP_LEFT:
			parser.scanStr(params, "%d,%d,%d,%d", &_upLeft.left, &_upLeft.top, &_upLeft.right, &_upLeft.bottom);
			break;

		case TOKEN_UP_RIGHT:
			parser.scanStr(params, "%d,%d,%d,%d", &_upRight.left, &_upRight.top, &_upRight.right, &_upRight.bottom);
			break;

		case TOKEN_UP_MIDDLE:
			parser.scanStr(params, "%d,%d,%d,%d", &_upMiddle.left, &_upMiddle.top, &_upMiddle.right, &_upMiddle.bottom);
			break;

		case TOKEN_DOWN_LEFT:
			parser.scanStr(params, "%d,%d,%d,%d", &_downLeft.left, &_downLeft.top, &_downLeft.right, &_downLeft.bottom);
			break;

		case TOKEN_DOWN_RIGHT:
			parser.scanStr(params, "%d,%d,%d,%d", &_downRight.left, &_downRight.top, &_downRight.right, &_downRight.bottom);
			break;

		case TOKEN_DOWN_MIDDLE:
			parser.scanStr(params, "%d,%d,%d,%d", &_downMiddle.left, &_downMiddle.top, &_downMiddle.right, &_downMiddle.bottom);
			break;

		case TOKEN_MIDDLE_LEFT:
			parser.scanStr(params, "%d,%d,%d,%d", &_middleLeft.left, &_middleLeft.top, &_middleLeft.right, &_middleLeft.bottom);
			break;

		case TOKEN_MIDDLE_RIGHT:
			parser.scanStr(params, "%d,%d,%d,%d", &_middleRight.left, &_middleRight.top, &_middleRight.right, &_middleRight.bottom);
			break;

		case TOKEN_MIDDLE_MIDDLE:
			parser.scanStr(params, "%d,%d,%d,%d", &_middleMiddle.left, &_middleMiddle.top, &_middleMiddle.right, &_middleMiddle.bottom);
			break;

		case TOKEN_HORIZONTAL_TILES:
			parser.scanStr(params, "%d,%d,%d", &h1, &h2, &h3);
			hTiles = true;
			break;

		case TOKEN_VERTICAL_TILES:
			parser.scanStr(params, "%d,%d,%d", &v1, &v2, &v3);
			vTiles = true;
			break;

		case TOKEN_EDITOR_PROPERTY:
			parseEditorProperty(params, false);
			break;
		}
	}
	if (cmd == PARSERR_TOKENNOTFOUND) {
		_gameRef->LOG(0, "Syntax error in TILED_IMAGE definition");
		return STATUS_FAILED;
	}
	if (cmd == PARSERR_GENERIC) {
		_gameRef->LOG(0, "Error loading TILED_IMAGE definition");
		return STATUS_FAILED;
	}

	if (vTiles && hTiles) {
		// up row
		_upLeft.setRect(0, 0, h1, v1);
		_upMiddle.setRect(h1, 0, h1 + h2, v1);
		_upRight.setRect(h1 + h2, 0, h1 + h2 + h3, v1);

		// middle row
		_middleLeft.setRect(0, v1, h1, v1 + v2);
		_middleMiddle.setRect(h1, v1, h1 + h2, v1 + v2);
		_middleRight.setRect(h1 + h2, v1, h1 + h2 + h3, v1 + v2);

		// down row
		_downLeft.setRect(0, v1 + v2, h1, v1 + v2 + v3);
		_downMiddle.setRect(h1, v1 + v2, h1 + h2, v1 + v2 + v3);
		_downRight.setRect(h1 + h2, v1 + v2, h1 + h2 + h3, v1 + v2 + v3);
	}

	// default
	if (_image && _image->_surface) {
		int width = _image->_surface->getWidth() / 3;
		int height = _image->_surface->getHeight() / 3;

		if (_upLeft.isRectEmpty()) {
			_upLeft.setRect(0, 0, width, height);
		}
		if (_upMiddle.isRectEmpty()) {
			_upMiddle.setRect(width, 0, 2 * width, height);
		}
		if (_upRight.isRectEmpty()) {
			_upRight.setRect(2 * width, 0, 3 * width, height);
		}

		if (_middleLeft.isRectEmpty()) {
			_middleLeft.setRect(0, height, width, 2 * height);
		}
		if (_middleMiddle.isRectEmpty()) {
			_middleMiddle.setRect(width, height, 2 * width, 2 * height);
		}
		if (_middleRight.isRectEmpty()) {
			_middleRight.setRect(2 * width, height, 3 * width, 2 * height);
		}

		if (_downLeft.isRectEmpty()) {
			_downLeft.setRect(0, 2 * height, width, 3 * height);
		}
		if (_downMiddle.isRectEmpty()) {
			_downMiddle.setRect(width, 2 * height, 2 * width, 3 * height);
		}
		if (_downRight.isRectEmpty()) {
			_downRight.setRect(2 * width, 2 * height, 3 * width, 3 * height);
		}
	}

	return STATUS_OK;
}

//////////////////////////////////////////////////////////////////////////
bool UITiledImage::saveAsText(BaseDynamicBuffer *buffer, int indent) {
	buffer->putTextIndent(indent, "TILED_IMAGE\n");
	buffer->putTextIndent(indent, "{\n");

	if (_image && _image->getSurfaceFilename()) {
		buffer->putTextIndent(indent + 2, "IMAGE=\"%s\"\n", _image->getSurfaceFilename());
	}

	int h1, h2, h3;
	int v1, v2, v3;

	h1 = _upLeft.right;
	h2 = _upMiddle.right - _upMiddle.left;
	h3 = _upRight.right - _upRight.left;

	v1 = _upLeft.bottom;
	v2 = _middleLeft.bottom - _middleLeft.top;
	v3 = _downLeft.bottom - _downLeft.top;


	buffer->putTextIndent(indent + 2, "VERTICAL_TILES { %d, %d, %d }\n", v1, v2, v3);
	buffer->putTextIndent(indent + 2, "HORIZONTAL_TILES { %d, %d, %d }\n", h1, h2, h3);

	// editor properties
	BaseClass::saveAsText(buffer, indent + 2);

	buffer->putTextIndent(indent, "}\n");
	return STATUS_OK;
}

//////////////////////////////////////////////////////////////////////////
void UITiledImage::correctSize(int32 *width, int32 *height) {
	int tileWidth = _middleMiddle.right - _middleMiddle.left;
	int tileHeight = _middleMiddle.bottom - _middleMiddle.top;

	int nuColumns = (*width - (_middleLeft.right - _middleLeft.left) - (_middleRight.right - _middleRight.left)) / tileWidth;
	int nuRows = (*height - (_upMiddle.bottom - _upMiddle.top) - (_downMiddle.bottom - _downMiddle.top)) / tileHeight;

	*width  = (_middleLeft.right - _middleLeft.left) + (_middleRight.right - _middleRight.left) + nuColumns * tileWidth;
	*height = (_upMiddle.bottom - _upMiddle.top) + (_downMiddle.bottom - _downMiddle.top) + nuRows * tileHeight;
}


//////////////////////////////////////////////////////////////////////////
bool UITiledImage::persist(BasePersistenceManager *persistMgr) {
	BaseObject::persist(persistMgr);

	persistMgr->transferRect32(TMEMBER(_downLeft));
	persistMgr->transferRect32(TMEMBER(_downMiddle));
	persistMgr->transferRect32(TMEMBER(_downRight));
	persistMgr->transferPtr(TMEMBER_PTR(_image));
	persistMgr->transferRect32(TMEMBER(_middleLeft));
	persistMgr->transferRect32(TMEMBER(_middleMiddle));
	persistMgr->transferRect32(TMEMBER(_middleRight));
	persistMgr->transferRect32(TMEMBER(_upLeft));
	persistMgr->transferRect32(TMEMBER(_upMiddle));
	persistMgr->transferRect32(TMEMBER(_upRight));

	return STATUS_OK;
}

} // End of namespace Wintermute