3 #include <QtCore/qiodevice.h>
4 #include <QtCore/qfile.h>
18 QGridLayout*
layout =
new QGridLayout(
this);
21 layout->addWidget(
new QLabel(tr(
"Settings:"),
this),
line++, 0);
27 QDialogButtonBox*
box =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Close, Qt::Horizontal,
this);
29 connect(
box, SIGNAL(rejected()),
this, SLOT(reject()));
31 box->button(QDialogButtonBox::Ok)->setText(
"Generate");
35 setWindowTitle(tr(
"Face variant maker"));
37 mySettings->setPlainText(tr(R
"RaW(dest: %1/arch/wall/light_blue_cave/
39 faces: cave_15.111, cave1.111, cave2.111, cave3.111, cave4.111, cave5.111
40 faces: cave6.111, cave7.111, cave8.111, cave9.111, cave10.111, cave11.111
41 faces: cave12.111, cave13.111, cave14.111, cave15.111, cave16.111, cave17.111
42 faces: cave18.111, cave19.111, cave20.111, cave21.111, cave22.111, cave23.111
43 faces: cave24.111, cave25.111
46 119, 119, 119:14, 229, 197; 67, 151, 229
65 if (
split.size() != 3) {
66 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid color %1").arg(
color));
69 return QColor::fromRgb(
split[0].trimmed().toInt(),
split[1].trimmed().toInt(),
split[2].trimmed().toInt());
74 QMap<QRgb, QList<QColor> > colors;
75 QString
dest,
name, archContent, faceContent;
79 bool inArch =
false, inFace =
false;
80 auto lines =
mySettings->toPlainText().split(
'\n');
82 foreach (QString
line, lines) {
84 if (
line ==
"archend") {
87 archContent +=
line +
"\n";
92 if (
line ==
"faceend") {
95 faceContent +=
line +
"\n";
100 if (
line.isEmpty()) {
104 if (
line.startsWith(
"dest:")) {
106 }
else if (
line.startsWith(
"name:")) {
108 }
else if (
line.startsWith(
"faces:")) {
109 QStringList add(
line.mid(6).split(
','));
110 foreach (QString face, add) {
111 faces.append(face.trimmed());
113 }
else if (
line.startsWith(
"variants:")) {
114 variants =
line.mid(9).trimmed().toInt();
115 }
else if (
line ==
"arch") {
117 }
else if (
line ==
"face") {
121 if (
split.size() != 2) {
122 QMessageBox::critical(
this, tr(
"Error"), tr(
"Invalid line %1").arg(
line));
127 foreach (QString d,
dest) {
128 colors[source.rgba()].append(
parse(d));
135 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'dest'!"));
140 QMessageBox::critical(
this, tr(
"Error"), tr(
"Missing 'name'!"));
145 QMessageBox::critical(
this, tr(
"Error"), tr(
"No face to process!"));
151 QMessageBox::critical(
this, tr(
"Error"), tr(
"At least one variant required!"));
155 QFile arc(
dest + QDir::separator() +
name +
".arc");
156 if (!archContent.isEmpty() && !arc.open(QFile::Truncate | QFile::WriteOnly))
158 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the archetype file %1!").arg(arc.fileName()));
161 QFile fface(
dest + QDir::separator() +
name +
".face");
162 if ((!faceContent.isEmpty() || variants > 1) && !fface.open(QFile::Truncate | QFile::WriteOnly))
164 QMessageBox::critical(
this, tr(
"Error"), tr(
"Error while opening the face file %1!").arg(fface.fileName()));
169 foreach(QString face,
faces)
171 QString archName(
name +
"_" + QString::number(i));
172 if (!archContent.isEmpty()) {
173 QString
item(tr(
"Object %1\nface %1.111\n").arg(archName));
175 item += tr(
"animation %1\n").arg(archName);
179 arc.write(
item.toLocal8Bit());
182 QString
anim(tr(
"animation %1\n").arg(archName));
186 for (
int var = 1; var <= variants; var++) {
194 image = face.toImage();
198 for (
int x = 0;
x < image.width();
x++) {
199 for (
int y = 0;
y < image.height();
y++) {
200 auto rgba = image.pixel(
x,
y);
201 auto subs = colors.find(rgba);
202 if (subs != colors.end()) {
203 image.setPixelColor(
x,
y, QColor(subs.value()[
rndm(0, subs.value().length() - 1)]));
208 QString base =
dest + QDir::separator() +
name +
"_" + QString::number(i) +
".base.11" + QString::number(var) +
".png";
209 image.save(base,
"PNG");
211 if (!faceContent.isEmpty()) {
212 QString fc(tr(
"face %1\n%2end\n").arg(archName +
".11" + QString::number(var), faceContent));
213 fface.write(fc.toLocal8Bit());
215 anim += tr(
"%1.11%2\n").arg(archName).arg(var);
219 fface.write(
anim.toLocal8Bit());
224 QMessageBox::information(
this, tr(
"Completed"), tr(
"Generation completed"));