1 |
#include "BGImagePageFinal.h" |
2 |
#include "ui_BGImagePageFinal.h" |
3 |
|
4 |
BGImagePageFinal::BGImagePageFinal(QString appDir, QString bgImagesLocation, QWidget *parent) : |
5 |
QWizardPage(parent), |
6 |
ui(new Ui::BGImagePageFinal) |
7 |
{ |
8 |
ui->setupUi(this); |
9 |
this->appDir = appDir; |
10 |
this->bgImagesLocation = bgImagesLocation; |
11 |
this->oniSplitCommands = new QStringList(); |
12 |
this->myOniSplitConverter = new Converter(this->appDir, this->oniSplitCommands); |
13 |
|
14 |
ui->lbComplete->setText("<html>The wizard is now complete. The images have been created. " |
15 |
"You can view all created files clicking <a href=' '>here.</a><br />" |
16 |
"<br />Click restart to create more background images from the wizard beggining, " |
17 |
"otherwise click finish.</html>"); // Don't use rich text in qtdesigner because it generates platform dependent code |
18 |
|
19 |
connectSlots(); |
20 |
} |
21 |
|
22 |
void BGImagePageFinal::initializePage(){ |
23 |
startProcessing(); |
24 |
} |
25 |
|
26 |
void BGImagePageFinal::startProcessing(){ |
27 |
// page 1 |
28 |
QString imagePath; |
29 |
QString imageType; |
30 |
// page 2 |
31 |
bool createTXMB; |
32 |
bool createTXMP; |
33 |
QString imageCustomName; |
34 |
QString txmbName; |
35 |
QString levelId; |
36 |
|
37 |
imagePath = field("leImageFullPath").toString(); |
38 |
imageType = field("lbImageType").toString(); |
39 |
createTXMB = field("cbCreateTXMB").toBool(); |
40 |
createTXMP = field("cbCreateTXMP").toBool(); |
41 |
imageCustomName = field("leImageName").toString(); |
42 |
txmbName = field("leTXMBName").toString(); |
43 |
levelId = field("leLevelId").toString(); |
44 |
|
45 |
|
46 |
QImage sourceImage(imagePath); |
47 |
QList<QString> imagesSplitted; |
48 |
|
49 |
// Check if images folder exists and create it if necessary |
50 |
QDir saveDir(this->bgImagesLocation); |
51 |
|
52 |
if(!saveDir.exists()) |
53 |
{ |
54 |
saveDir.mkpath("."); // http://stackoverflow.com/questions/2241808/checking-if-a-folder-exists-and-creating-folders-in-qt-c thanks Petrucio |
55 |
} |
56 |
|
57 |
imagesSplitted = splitIntoMultipleImages(sourceImage, imageCustomName, imageType); |
58 |
|
59 |
// Image divided with sucess |
60 |
if(imagesSplitted.size() > 0 && createTXMP){ |
61 |
|
62 |
// call creations of TXMP files (oni split command) |
63 |
this->oniSplitCommands->clear(); |
64 |
|
65 |
for(const QString ¤tFile : imagesSplitted){ |
66 |
this->oniSplitCommands->append("-create:txmp " + Util::String::insertQuotes(this->bgImagesLocation) + " -format:bgr32 " + Util::String::insertQuotes(currentFile)); |
67 |
} |
68 |
|
69 |
this->myOniSplitConverter->start(); // finally process the onisplit commands |
70 |
this->myOniSplitConverter->wait(); // wait for it to complete |
71 |
|
72 |
if(createTXMB){ |
73 |
|
74 |
QString txmbXmlFile = createTxmbXmlFile(imagesSplitted, txmbName, sourceImage.size(), levelId); |
75 |
|
76 |
if(txmbXmlFile.isEmpty()) |
77 |
{ |
78 |
UtilVago::showAndLogErrorPopUp("Couldn't create TXMB xml file!"); |
79 |
return; |
80 |
} |
81 |
|
82 |
// Create TXMB oni files |
83 |
this->oniSplitCommands->clear(); |
84 |
this->oniSplitCommands->append("-create " + Util::String::insertQuotes(this->bgImagesLocation) + " " + Util::String::insertQuotes(txmbXmlFile)); |
85 |
this->myOniSplitConverter->start(); |
86 |
this->myOniSplitConverter->wait(); |
87 |
} |
88 |
} |
89 |
} |
90 |
|
91 |
QString BGImagePageFinal::createTxmbXmlFile(QList<QString> imagesSplitted, QString fileName, const QSize &imageSize, QString levelId){ |
92 |
QString filePath = this->bgImagesLocation + "/" + fileName + ".xml"; |
93 |
|
94 |
// If it's empty assume zero |
95 |
if(levelId.trimmed().isEmpty()){ |
96 |
levelId = "0"; |
97 |
} |
98 |
|
99 |
pugi::xml_document doc; |
100 |
|
101 |
pugi::xml_node rootNode = doc.append_child("Oni"); |
102 |
pugi::xml_node txmbNode = rootNode.append_child("TXMB"); |
103 |
txmbNode.append_child("Width").append_child(pugi::xml_node_type::node_pcdata).set_value(QString::number(imageSize.width()).toLatin1().data()); |
104 |
txmbNode.append_child("Height").append_child(pugi::xml_node_type::node_pcdata).set_value(QString::number(imageSize.height()).toLatin1().data()); |
105 |
pugi::xml_node texturesNode = txmbNode.append_child("Textures"); |
106 |
|
107 |
for(const QString &currSplittedImage : imagesSplitted) |
108 |
{ |
109 |
QFileInfo currImageFile(currSplittedImage); |
110 |
texturesNode.append_child("Link").append_child(pugi::xml_node_type::node_pcdata).set_value(QSTR_TO_CSTR(currImageFile.baseName())); |
111 |
} |
112 |
|
113 |
txmbNode.append_attribute("id").set_value(QSTR_TO_CSTR(levelId)); |
114 |
|
115 |
if(!doc.save_file(QSTR_TO_CSTR(filePath))){ |
116 |
return ""; |
117 |
} |
118 |
|
119 |
return filePath; |
120 |
} |
121 |
|
122 |
// returns number of images created from the split |
123 |
QList<QString> BGImagePageFinal::splitIntoMultipleImages(QImage sourceImage, QString imageName, QString imageType){ |
124 |
|
125 |
QList<QString> splittedImages; |
126 |
|
127 |
QVector<int> horizontalSideSizes = getSplitSizes(sourceImage.width()); |
128 |
|
129 |
QVector<int> verticalSideSizes = getSplitSizes(sourceImage.height()); |
130 |
|
131 |
int currentVerticalPosition = 0; |
132 |
int currentHorizontalPosition = 0; |
133 |
|
134 |
for(const int currentVerticalSize : verticalSideSizes){ |
135 |
|
136 |
for(const int currentHorizontalSize : horizontalSideSizes){ |
137 |
|
138 |
QImage dividedImage = sourceImage.copy(currentHorizontalPosition,currentVerticalPosition,currentHorizontalSize,currentVerticalSize); |
139 |
QString currentImageId = QString::number(splittedImages.size()+1); |
140 |
|
141 |
// Not used. It added 0 when the number was less than 10 |
142 |
// if(currentImageId.length() == 1){ |
143 |
// currentImageId = "0" + currentImageId; |
144 |
// } |
145 |
|
146 |
QString imageDestinationPath = this->bgImagesLocation + "/" + imageName + currentImageId + "." + imageType; |
147 |
|
148 |
if(!dividedImage.save(imageDestinationPath)){ |
149 |
UtilVago::showAndLogErrorPopUp("Couldn't save image " + imageDestinationPath + "! Files weren't created correctly."); |
150 |
return QList<QString>(); |
151 |
} |
152 |
|
153 |
currentHorizontalPosition += currentHorizontalSize; |
154 |
|
155 |
splittedImages.append(imageDestinationPath); |
156 |
} |
157 |
|
158 |
currentHorizontalPosition = 0; |
159 |
currentVerticalPosition += currentVerticalSize; |
160 |
} |
161 |
|
162 |
return splittedImages; |
163 |
} |
164 |
|
165 |
QVector<int> BGImagePageFinal::getSplitSizes(int imageSideSize) |
166 |
{ |
167 |
int remainingSideSize = imageSideSize; |
168 |
constexpr int regularSize = 256; |
169 |
|
170 |
QVector<int> splitSizes; |
171 |
|
172 |
while(remainingSideSize > 0){ |
173 |
|
174 |
if(remainingSideSize-regularSize < 0){ |
175 |
splitSizes.append(remainingSideSize); |
176 |
remainingSideSize -= remainingSideSize; |
177 |
break; |
178 |
} |
179 |
|
180 |
splitSizes.append(regularSize); |
181 |
|
182 |
remainingSideSize -= regularSize; |
183 |
|
184 |
} |
185 |
|
186 |
if(remainingSideSize != 0) |
187 |
{ |
188 |
splitSizes.clear(); |
189 |
} |
190 |
|
191 |
return splitSizes; |
192 |
} |
193 |
|
194 |
void BGImagePageFinal::openBGImagesFolder(){ |
195 |
QDesktopServices::openUrl(QUrl("file:///"+this->bgImagesLocation)); |
196 |
} |
197 |
|
198 |
void BGImagePageFinal::catchOniSplitProcessingErrors(QString result, int numErrors){ |
199 |
|
200 |
if(numErrors!=0){ |
201 |
QString sNumErrors=QString::number(numErrors); |
202 |
bool isWarning = false; |
203 |
|
204 |
|
205 |
if(result.startsWith("Warning: Texture")){ |
206 |
isWarning = true; |
207 |
} |
208 |
|
209 |
if(numErrors>1){ |
210 |
|
211 |
result = result+"\n This is the last of " + sNumErrors; |
212 |
|
213 |
if(!isWarning){ |
214 |
result += " errors."; |
215 |
} |
216 |
else{ |
217 |
result += " messages."; |
218 |
} |
219 |
} |
220 |
|
221 |
if(isWarning){ |
222 |
UtilVago::showWarningPopUpLogButton(result); |
223 |
} |
224 |
else{ |
225 |
UtilVago::showErrorPopUpLogButton(result); |
226 |
} |
227 |
} |
228 |
} |
229 |
|
230 |
void BGImagePageFinal::connectSlots(){ |
231 |
connect(this->myOniSplitConverter, SIGNAL(resultConversion(QString, int)), this, SLOT(catchOniSplitProcessingErrors(QString, int))); |
232 |
connect(ui->lbComplete, SIGNAL(linkActivated(const QString & )), this, SLOT(openBGImagesFolder())); |
233 |
} |
234 |
|
235 |
BGImagePageFinal::~BGImagePageFinal() |
236 |
{ |
237 |
delete ui; |
238 |
} |