summaryrefslogtreecommitdiff
path: root/lyt/layout.cpp
blob: afe8a67aa826780f2e8b481a96c0cae176073ceb (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
/*******************************************************************************
  This file is part of LayoutStudio (http://github.com/Treeki/LayoutStudio)
  Copyright (c) 2010 Treeki (treeki@gmail.com)

  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, version 2.0.

  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 2.0 for more details.

  You should have received a copy of the GNU General Public License 2.0
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/

#include "layout.h"
#include <QtCore/QStack>
#include <QtCore/QVector>


LYTLayout::LYTLayout(LYTPackageBase &package) : rootPane(0), m_package(package) {
	this->clear();
}

LYTLayout::LYTLayout(LYTPackageBase &package, QString name) : rootPane(0), m_package(package) {
	this->loadLayoutFromPackage(name);
}

LYTLayout::~LYTLayout() {
	this->clear();
}


void LYTLayout::clear() {
	this->width = 608.0;
	this->height = 456.0;

	this->m_fontRefs.clear();
	this->m_textureRefs.clear();

	foreach (LYTMaterialContainerEntry materialEntry, materials.list)
		delete materialEntry.second;

	this->materials.clear();

	if (this->rootPane != 0)
		delete this->rootPane;

	foreach (LYTGroup *group, groups)
		delete group;
}


LYTPackageBase &LYTLayout::package() const {
	return m_package;
}


bool LYTLayout::loadLayoutFromPackage(QString name) {
	qDebug() << "Loading layout: " << name;
	qDebug() << "From package: " << m_package.description();

	QStack<LYTPane *> paneStack;
	LYTPane *lastPane, *newPane;

	bool hasGroupContainer = false;
	int groupStackID = 0;

	QByteArray layoutData = m_package.getLayout(name);
	LYTBinaryFile file(layoutData);

	this->clear();

	foreach (LYTBinaryFileSection section, file.sections) {
		// Handle every section in the file

		qDebug("Handling %c%c%c%c", section.magic.str[3], section.magic.str[2], section.magic.str[1], section.magic.str[0]);

		switch (section.magic.value) {
		case 'lyt1':
			this->readLyt1(section);
			break;

		case 'txl1':
			this->readTxl1(section);
			break;

		case 'fnl1':
			this->readFnl1(section);
			break;

		case 'mat1':
			this->readMat1(section);
			break;

		case 'pan1':
		case 'txt1':
		case 'pic1':
		case 'wnd1':
		case 'bnd1':
			newPane = this->createPaneObj(section);
			newPane->dumpToDebug();

			if (rootPane == 0)
				rootPane = newPane;

			if (!paneStack.empty()) {
				newPane->parent = paneStack.last();
				paneStack.last()->children.append(newPane);
			}

			lastPane = newPane;

			break;

		case 'pas1':
			paneStack.push(lastPane);
			break;

		case 'pae1':
			lastPane = paneStack.pop();
			break;

		case 'grp1':
			if (!hasGroupContainer) {
				// nw4r::lyt::Layout::Build actually creates a GroupContainer
				// but we just use a QList so we don't need to do that
				hasGroupContainer = true;
			} else {
				if (groupStackID == 1) {
					// create a group and add it to the list
					QDataStream in(section.data);
					InitDataStream(in);
					groups.append(new LYTGroup());
					groups.last()->readFromDataStream(in, *this->rootPane);
				}
			}

			break;

		case 'grs1':
			groupStackID++;
			break;

		case 'gre1':
			groupStackID--;
			break;
		}
	}

	// todo: usd1

	return true;
}

void LYTLayout::readLyt1(LYTBinaryFileSection &section) {
	// initial info block containing width + height

	QDataStream in(section.data);
	InitDataStream(in);

	in >> (quint8&)flags; // seems to be unused in newer nw4r::lyt versions (but still in file)?
	in.skipRawData(3); // padding
	in >> (float&)width;
	in >> (float&)height;

	qDebug() << "Read lyt1 block: layout size:" << width << "x" << height;
}

void LYTLayout::readTxl1(LYTBinaryFileSection &section) {
	// list of texture references for this layout

	QDataStream in(section.data);
	InitDataStream(in);
	m_textureRefs = ReadStringList(in);
}

void LYTLayout::readFnl1(LYTBinaryFileSection &section) {
	// list of font references for this layout

	QDataStream in(section.data);
	InitDataStream(in);
	m_fontRefs = ReadStringList(in);
}

void LYTLayout::readMat1(LYTBinaryFileSection &section) {
	// the material section for the layout
	// not fun.

	QDataStream in(section.data);
	InitDataStream(in);

	quint16 count;
	in >> (quint16&)count;
	in.skipRawData(2); // padding

	// create a list of every offset
	// these offsets are from the start of the section, not from
	// the first offset entry like StringLists are
	QVector<quint32> materialOffsets(count);

	for (int i = 0; i < count; i++) {
		quint32 offset;
		in >> (quint32&)offset;
		materialOffsets[i] = offset;
	}

	// now read each material
	for (int i = 0; i < count; i++) {
		// subtract 8 from the offset because section.data doesn't contain
		// the nw4r::ut::BinaryBlockHeader whereas these offsets do count it
		in.device()->seek(materialOffsets[i] - 8);

		LYTMaterial *material = new LYTMaterial(*this);
		material->readFromDataStream(in);
		material->dumpToDebug();

		materials.addMaterial(material->name, material);
	}
}

LYTPane *LYTLayout::createPaneObj(LYTBinaryFileSection &section) {
	LYTPane *pane;

	if (section.magic.value == 'pan1')
		pane = new LYTPane(*this);
	else if (section.magic.value == 'txt1')
		pane = new LYTTextBox(*this);
	else if (section.magic.value == 'pic1')
		pane = new LYTPicture(*this);
	else if (section.magic.value == 'wnd1')
		pane = new LYTWindow(*this);
	else if (section.magic.value == 'bnd1')
		pane = new LYTBounding(*this);

	QDataStream in(section.data);
	InitDataStream(in);

	pane->readFromDataStream(in);

	return pane;
}



QByteArray LYTLayout::pack() {
	qDebug() << "Writing layout";

	// before we start writing the file, find out a bunch of info
	qDebug() << "Compiling texture ref list";
	m_textureRefs = this->generateTextureRefs();

	qDebug() << "Compiling font ref list";
	m_fontRefs = this->generateFontRefs();

	// now start writing the file
	Magic magic('RLYT');
	Version version(0x000A);

	LYTBinaryFile file(magic, version);

	QDataStream *stream;

	// write lyt1
	file.sections.append(LYTBinaryFileSection(Magic('lyt1')));
	stream = file.sections.last().createWriteStream();
	(*stream) << (quint8)flags;
	WritePadding(3, *stream);
	(*stream) << (float)this->width;
	(*stream) << (float)this->height;
	delete stream;

	// write txl1
	file.sections.append(LYTBinaryFileSection(Magic('txl1')));
	stream = file.sections.last().createWriteStream();
	WriteStringList(*stream, this->m_textureRefs);
	delete stream;

	// write fnl1
	file.sections.append(LYTBinaryFileSection(Magic('fnl1')));
	stream = file.sections.last().createWriteStream();
	WriteStringList(*stream, this->m_fontRefs);
	delete stream;

	// write mat1
	file.sections.append(LYTBinaryFileSection(Magic('mat1')));
	this->writeMat1(file.sections.last());

	// next up: write the pane tree
	this->writePane(file, this->rootPane);

	// now write groups
	this->writeGroups(file);

	// and we're done!
	return file.pack();
}



QStringList LYTLayout::generateTextureRefs() const {
	QStringList out;

	foreach (LYTMaterialContainerEntry matEntry, materials.list) {
		foreach (LYTTexMap texMap, matEntry.second->texMaps) {
			if (!out.contains(texMap.textureName))
				out.append(texMap.textureName);
		}
	}

	out.sort();

	return out;
}


QStringList LYTLayout::generateFontRefs() const {
	QStringList out;

	this->rootPane->addFontRefsToList(out);

	out.sort();

	return out;
}



void LYTLayout::writeMat1(LYTBinaryFileSection &section) const {
	QDataStream *stream = section.createWriteStream();

	(*stream) << (quint16)this->materials.count();
	WritePadding(2, *stream);

	// now, assign space for the mat1 offsets, we'll set them later
	qint64 matOffsetPos = stream->device()->pos();

	WritePadding(4 * this->materials.count(), *stream);
	QVector<quint32> materialOffsets(this->materials.count());

	// write them
	for (int i = 0; i < this->materials.count(); i++) {
		// add 8 to the offset to account for nw4r::ut::BinaryBlockHeader
		materialOffsets[i] = stream->device()->pos() + 8;
		this->materials.getMaterialByIndex(i)->writeToDataStream(*stream);

		// pad it if needed
		qint64 checkPadding = stream->device()->pos();
		int padding = AlignUp(checkPadding, 4) - checkPadding;
		WritePadding(padding, *stream);
	}

	// and now write the offsets
	qint64 endMatPos = stream->device()->pos();
	stream->device()->seek(matOffsetPos);

	for (int i = 0; i < materialOffsets.count(); i++) {
		(*stream) << (quint32)materialOffsets[i];
	}

	stream->device()->seek(endMatPos);

	delete stream;
}


void LYTLayout::writePane(LYTBinaryFile &file, LYTPane *pane) const {
	file.sections.append(LYTBinaryFileSection(pane->magic()));

	QDataStream *stream = file.sections.last().createWriteStream();
	pane->writeToDataStream(*stream);
	delete stream;


	if (!pane->children.isEmpty()) {
		file.sections.append(LYTBinaryFileSection(Magic('pas1')));

		foreach (LYTPane *p, pane->children) {
			this->writePane(file, p);
		}

		file.sections.append(LYTBinaryFileSection(Magic('pae1')));
	}
}


void LYTLayout::writeGroups(LYTBinaryFile &file) const {
	QDataStream *stream;

	// first, write a dummy group for the root
	file.sections.append(LYTBinaryFileSection(Magic('grp1')));
	stream = file.sections.last().createWriteStream();

	WriteFixedLengthASCII(*stream, "RootGroup", 0x10);
	WritePadding(4, *stream);

	delete stream;

	// now write the children
	if (!this->groups.isEmpty()) {
		file.sections.append(LYTBinaryFileSection(Magic('grs1')));

		foreach (LYTGroup *p, this->groups) {
			file.sections.append(LYTBinaryFileSection(Magic('grp1')));

			stream = file.sections.last().createWriteStream();
			p->writeToDataStream(*stream);
			delete stream;
		}

		file.sections.append(LYTBinaryFileSection('gre1'));
	}
}