15 #include <QtCore/qiodevice.h>
16 #include <QtCore/qfile.h>
30 QGridLayout*
layout =
new QGridLayout(
this);
33 layout->addWidget(
new QLabel(tr(
"Settings:"),
this),
line++, 0);
39 QDialogButtonBox*
box =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal,
this);
41 connect(
box, SIGNAL(rejected()),
this, SLOT(reject()));
43 box->button(QDialogButtonBox::Ok)->setText(
"Generate");
47 setWindowTitle(tr(
"Face variant maker"));
49 mySettings->setPlainText(tr(R
"RaW(dest: %1/arch/wall/light_blue_cave/
51 faces: cave_15.111, cave1.111, cave2.111, cave3.111, cave4.111, cave5.111
52 faces: cave6.111, cave7.111, cave8.111, cave9.111, cave10.111, cave11.111
53 faces: cave12.111, cave13.111, cave14.111, cave15.111, cave16.111, cave17.111
54 faces: cave18.111, cave19.111, cave20.111, cave21.111, cave22.111, cave23.111
55 faces: cave24.111, cave25.111
58 119, 119, 119:14, 229, 197; 67, 151, 229
77 if (
split.size() != 3) {
78 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid color %1").arg(
color));
81 return QColor::fromRgb(
split[0].trimmed().toInt(),
split[1].trimmed().toInt(),
split[2].trimmed().toInt());
86 QMap<QRgb, QList<QColor> > colors;
87 QString
dest,
name, archContent, faceContent;
91 bool inArch =
false, inFace =
false;
92 auto lines =
mySettings->toPlainText().split(
'\n');
94 foreach (QString
line, lines) {
96 if (
line ==
"archend") {
99 archContent +=
line +
"\n";
104 if (
line ==
"faceend") {
107 faceContent +=
line +
"\n";
112 if (
line.isEmpty()) {
116 if (
line.startsWith(
"dest:")) {
118 }
else if (
line.startsWith(
"name:")) {
120 }
else if (
line.startsWith(
"faces:")) {
121 QStringList add(
line.mid(6).split(
','));
122 foreach (QString face, add) {
123 faces.append(face.trimmed());
125 }
else if (
line.startsWith(
"variants:")) {
126 variants =
line.mid(9).trimmed().toInt();
127 }
else if (
line ==
"arch") {
129 }
else if (
line ==
"face") {
133 if (
split.size() != 2) {
134 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid line %1").arg(
line));
139 foreach (QString d,
dest) {
140 colors[source.rgba()].append(
parse(d));
147 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'dest'!"));
152 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'name'!"));
157 QMessageBox::critical(
this, tr(
"Error"), tr(
"No face to process!"));
163 QMessageBox::critical(
this, tr(
"Error"), tr(
"At least one variant required!"));
167 QFile arc(
dest + QDir::separator() +
name +
".arc");
168 if (!archContent.isEmpty() && !arc.open(QFile::Truncate | QFile::WriteOnly))
170 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the archetype file %1!").arg(arc.fileName()));
173 QFile fface(
dest + QDir::separator() +
name +
".face");
174 if ((!faceContent.isEmpty() || variants > 1) && !fface.open(QFile::Truncate | QFile::WriteOnly))
176 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the face file %1!").arg(fface.fileName()));
181 foreach(QString face,
faces)
183 QString archName(
name +
"_" + QString::number(i));
184 if (!archContent.isEmpty()) {
185 QString
item(tr(
"Object %1\nface %1.111\n").arg(archName));
187 item += tr(
"animation %1\n").arg(archName);
191 arc.write(
item.toLocal8Bit());
194 QString
anim(tr(
"animation %1\n").arg(archName));
198 for (
int var = 1; var <= variants; var++) {
206 image = face.toImage();
210 for (
int x = 0;
x < image.width();
x++) {
211 for (
int y = 0;
y < image.height();
y++) {
212 auto rgba = image.pixel(
x,
y);
213 auto subs = colors.find(rgba);
214 if (subs != colors.end()) {
215 image.setPixelColor(
x,
y, QColor(subs.value()[
rndm(0, subs.value().length() - 1)]));
220 QString base =
dest + QDir::separator() +
name +
"_" + QString::number(i) +
".base.11" + QString::number(var) +
".png";
221 image.save(base,
"PNG");
223 if (!faceContent.isEmpty()) {
224 QString fc(tr(
"face %1\n%2end\n").arg(archName +
".11" + QString::number(var), faceContent));
225 fface.write(fc.toLocal8Bit());
227 anim += tr(
"%1.11%2\n").arg(archName).arg(var);
231 fface.write(
anim.toLocal8Bit());
236 QMessageBox::information(
this, tr(
"Completed"), tr(
"Generation completed"));