summaryrefslogtreecommitdiff
path: root/lyt/window.cpp
blob: 52d2db91a8b35402428ffc66f9aa1475ea7cd53a (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
/*******************************************************************************
  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 "window.h"
#include "layout.h"
#include "common.h"


LYTWindowFrame::LYTWindowFrame(LYTWindow &window) : m_window(window) {
}

void LYTWindowFrame::writeToDataStream(QDataStream &out) const {
	// calculate the material number
	int materialNum = m_window.m_layout.materials.getIndexOfName(materialName);
	out << (quint16)materialNum;

	out << (quint8)type;
	WritePadding(1, out);
}

void LYTWindowFrame::readFromDataStream(QDataStream &in) {
	// read the material name
	quint16 materialNum;
	in >> (quint16&)materialNum;

	materialName = m_window.m_layout.materials.getNameOfIndex(materialNum);

	in >> (quint8&)type;
	in.skipRawData(1); // padding
}

void LYTWindowFrame::dumpToDebug() const {
	qDebug() << "LYTWindowFrame @" << (void*)this << "- type:" << type << "- material:" << materialName;
}



LYTWindow::LYTWindow(LYTLayout &layout) : LYTPane(layout) {
}

LYTWindow::~LYTWindow() {
	foreach (LYTWindowFrame *frame, frames)
		delete frame;
}



Magic LYTWindow::magic() const {
	return Magic('wnd1');
}


void LYTWindow::dumpToDebug(bool showHeading) const {
	if (showHeading)
		qDebug() << "LYTWindow" << name << "@" << (void*)this;

	LYTPane::dumpToDebug(false);

	qDebug() << "- Content VtxColours:" << contentVtxColours[0] << "-" << contentVtxColours[1];
	qDebug() << "                     " << contentVtxColours[2] << "-" << contentVtxColours[3];
	qDebug() << "- Content Material:" << contentMaterialName;
	qDebug() << "- Content Tex Coords:" << contentTexCoords.count();

	foreach (LYTTexCoords texCoord, contentTexCoords) {
		qDebug() << "----" << texCoord.coord[0] << "-" << texCoord.coord[1] << "-" << texCoord.coord[2] << "-" << texCoord.coord[3];
	}

	qDebug() << "- Content Overflow: Left" << contentOverflowLeft << "- Right" << contentOverflowRight;
	qDebug() << "                    Top" << contentOverflowTop << "- Bottom" << contentOverflowBottom;

	qDebug() << "- Frames:" << frames.count();

	foreach (LYTWindowFrame *frame, frames) {
		frame->dumpToDebug();
	}
}



void LYTWindow::writeToDataStream(QDataStream &out) const {
	LYTPane::writeToDataStream(out);

	out << (float)contentOverflowLeft;
	out << (float)contentOverflowRight;
	out << (float)contentOverflowTop;
	out << (float)contentOverflowBottom;

	out << (quint8)frames.count();
	WritePadding(3, out);

	out << (quint32)0x68; // offset to content struct
	out << (quint32)(0x7C + (contentTexCoords.count()*0x20)); // offset to frame offset list

	for (int i = 0; i < 4; i++)
		WriteRGBA8Color(contentVtxColours[i], out);

	// calculate the material number
	int materialNum = m_layout.materials.getIndexOfName(contentMaterialName);
	out << (quint16)materialNum;

	// write texcoords
	out << (quint8)contentTexCoords.count();
	WritePadding(1, out);

	foreach (LYTTexCoords texCoord, contentTexCoords) {
		for (int i = 0; i < 4; i++)
			WritePointF(out, texCoord.coord[i]);
	}

	// write frame offsets
	quint32 frameOffset = 0x7C; // end of fixed-size part of content struct
	frameOffset += (contentTexCoords.count() * 0x20); // end of content struct
	frameOffset += (frames.count() * 4); // end of offset list

	for (int i = 0; i < frames.count(); i++) {
		out << (quint32)frameOffset;
		frameOffset += 4; // size of frame struct
	}

	// now write frames
	foreach (LYTWindowFrame *frame, frames) {
		frame->writeToDataStream(out);
	}
}


void LYTWindow::readFromDataStream(QDataStream &in) {
	qint64 startPos = in.device()->pos();

	LYTPane::readFromDataStream(in);

	in >> (float&)contentOverflowLeft;
	in >> (float&)contentOverflowRight;
	in >> (float&)contentOverflowTop;
	in >> (float&)contentOverflowBottom;

	quint8 frameCount;
	in >> (quint8&)frameCount;
	in.skipRawData(3); // padding

	quint32 contentOffset, frameListOffset;
	in >> (quint32&)contentOffset;
	in >> (quint32&)frameListOffset;

	// read content struct
	// subtract 8 from the offset because section.data doesn't contain
	// the nw4r::ut::BinaryBlockHeader whereas these offsets do count it
	in.device()->seek(startPos + contentOffset - 8);

	for (int i = 0; i < 4; i++)
		ReadRGBA8Color(contentVtxColours[i], in);

	// read the material name
	quint16 materialNum;
	in >> (quint16&)materialNum;

	contentMaterialName = m_layout.materials.getNameOfIndex(materialNum);

	// read texcoords
	quint8 texCoordNum;
	in >> (quint8&)texCoordNum;
	in.skipRawData(1); // padding

	contentTexCoords.resize(texCoordNum);

	for (int i = 0; i < texCoordNum; i++) {
		for (int j = 0; j < 4; j++)
			ReadPointF(in, contentTexCoords[i].coord[j]);
	}

	// read frame offset list
	// subtract 8 from the offset once again
	in.device()->seek(startPos + frameListOffset - 8);

	QVector<quint32> frameOffsets(frameCount);

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

	// now read each frame
	for (int i = 0; i < frameCount; i++) {
		in.device()->seek(frameOffsets[i] - 8);

		frames.append(new LYTWindowFrame(*this));
		frames.last()->readFromDataStream(in);
	}
}