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