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


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

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

	out << (quint8)type;
	out.skipRawData(1); // padding
}

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

	materialName = m_window.m_layout.materials.keys().at(materialNum);

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

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



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

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



// all of this needs to be done

void LYTWindow::dumpToDebug(bool showHeading) {
	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) {
	LYTPane::writeToDataStream(out);

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

	out << (quint8)frames.count();
	out.skipRawData(3); // padding

	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.keys().indexOf(contentMaterialName);
	out << (quint16)materialNum;

	// write texcoords
	out << (quint8)contentTexCoords.count();
	out.skipRawData(1); // padding

	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.keys().at(materialNum);

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

	contentTexCoords.resize(texCoordNum);

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

	// 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);
	}
}