15 #include <QtCore/qiodevice.h>
16 #include <QtCore/qfile.h>
28 QGridLayout*
layout =
new QGridLayout(
this);
31 layout->addWidget(
new QLabel(tr(
"Settings:"),
this),
line++, 0);
37 QDialogButtonBox*
box =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal,
this);
39 connect(
box, SIGNAL(rejected()),
this, SLOT(reject()));
41 box->button(QDialogButtonBox::Ok)->setText(
"Generate");
45 setWindowTitle(tr(
"Face variant maker"));
47 mySettings->setPlainText(QString(R
"RaW(dest: %1/arch/wall/light_blue_cave/
49 faces: cave_15.111, cave1.111, cave2.111, cave3.111, cave4.111, cave5.111
50 faces: cave6.111, cave7.111, cave8.111, cave9.111, cave10.111, cave11.111
51 faces: cave12.111, cave13.111, cave14.111, cave15.111, cave16.111, cave17.111
52 faces: cave18.111, cave19.111, cave20.111, cave21.111, cave22.111, cave23.111
53 faces: cave24.111, cave25.111
56 119, 119, 119:14, 229, 197; 67, 151, 229
75 if (
split.size() != 3) {
76 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid color %1").arg(
color));
79 return QColor::fromRgb(
split[0].trimmed().toInt(),
split[1].trimmed().toInt(),
split[2].trimmed().toInt());
84 QMap<QRgb, QList<QColor> > colors;
85 QString
dest,
name, archContent, faceContent;
89 bool inArch =
false, inFace =
false;
90 auto lines =
mySettings->toPlainText().split(
'\n');
92 foreach (QString
line, lines) {
94 if (
line ==
"archend") {
97 archContent +=
line +
"\n";
102 if (
line ==
"faceend") {
105 faceContent +=
line +
"\n";
110 if (
line.isEmpty()) {
114 if (
line.startsWith(
"dest:")) {
116 }
else if (
line.startsWith(
"name:")) {
118 }
else if (
line.startsWith(
"faces:")) {
119 QStringList add(
line.mid(6).split(
','));
120 foreach (QString face, add) {
121 faces.append(face.trimmed());
123 }
else if (
line.startsWith(
"variants:")) {
124 variants =
line.mid(9).trimmed().toInt();
125 }
else if (
line ==
"arch") {
127 }
else if (
line ==
"face") {
131 if (
split.size() != 2) {
132 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid line %1").arg(
line));
137 foreach (QString d,
dest) {
138 colors[source.rgba()].append(
parse(d));
145 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'dest'!"));
150 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'name'!"));
155 QMessageBox::critical(
this, tr(
"Error"), tr(
"No face to process!"));
161 QMessageBox::critical(
this, tr(
"Error"), tr(
"At least one variant required!"));
165 QFile arc(
dest + QDir::separator() +
name +
".arc");
166 if (!archContent.isEmpty() && !arc.open(QFile::Truncate | QFile::WriteOnly))
168 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the archetype file %1!").arg(arc.fileName()));
171 QFile fface(
dest + QDir::separator() +
name +
".face");
172 if ((!faceContent.isEmpty() || variants > 1) && !fface.open(QFile::Truncate | QFile::WriteOnly))
174 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the face file %1!").arg(fface.fileName()));
179 foreach(QString face,
faces)
181 QString archName(
name +
"_" + QString::number(i));
182 if (!archContent.isEmpty()) {
183 QString
item(tr(
"Object %1\nface %1.111\n").arg(archName));
185 item += tr(
"animation %1\n").arg(archName);
189 arc.write(
item.toLocal8Bit());
192 QString
anim(tr(
"animation %1\n").arg(archName));
196 for (
int var = 1; var <= variants; var++) {
204 image = face.toImage();
208 for (
int x = 0;
x < image.width();
x++) {
209 for (
int y = 0;
y < image.height();
y++) {
210 auto rgba = image.pixel(
x,
y);
211 auto subs = colors.find(rgba);
212 if (subs != colors.end()) {
213 image.setPixelColor(
x,
y, QColor(subs.value()[
rndm(0, subs.value().length() - 1)]));
218 QString base =
dest + QDir::separator() +
name +
"_" + QString::number(i) +
".base.11" + QString::number(var) +
".png";
219 image.save(base,
"PNG");
221 if (!faceContent.isEmpty()) {
222 QString fc(tr(
"face %1\n%2end\n").arg(archName +
".11" + QString::number(var), faceContent));
223 fface.write(fc.toLocal8Bit());
225 anim += tr(
"%1.11%2\n").arg(archName).arg(var);
229 fface.write(
anim.toLocal8Bit());
234 QMessageBox::information(
this, tr(
"Completed"), tr(
"Generation completed"));