ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/s10k/Vago/bgImageWizard/bgimagepagefinal.cpp
Revision: 1054
Committed: Wed Oct 12 21:35:13 2016 UTC (9 years ago) by s10k
Content type: text/x-c++src
Original Path: Vago/trunk/Vago/bgImageWizard/bgimagepagefinal.cpp
File size: 8781 byte(s)
Log Message:
Vago 1.1

File Contents

# Content
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 &currentFile : 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 /*
167 QVector<int> BGImagePageFinal::getSplitSizes(int imageSideSize)
168 {
169 int currNumber = 256;
170 int remainingSideSize = imageSideSize;
171
172 QVector<int> splitSizes;
173
174 while(currNumber > 8){
175 if(remainingSideSize-currNumber >= 0){
176 splitSizes.append(currNumber);
177 remainingSideSize -= currNumber;
178 }
179 else{
180 currNumber /= 2;
181 }
182 }
183
184 if(remainingSideSize != 0)
185 {
186 splitSizes.clear();
187 }
188
189 return splitSizes;
190 }
191 */
192
193 QVector<int> BGImagePageFinal::getSplitSizes(int imageSideSize)
194 {
195 int remainingSideSize = imageSideSize;
196 constexpr int regularSize = 256;
197
198 QVector<int> splitSizes;
199
200 while(remainingSideSize > 0){
201
202 if(remainingSideSize-regularSize < 0){
203 splitSizes.append(remainingSideSize);
204 remainingSideSize -= remainingSideSize;
205 break;
206 }
207
208 splitSizes.append(regularSize);
209
210 remainingSideSize -= regularSize;
211
212 }
213
214 if(remainingSideSize != 0)
215 {
216 splitSizes.clear();
217 }
218
219 return splitSizes;
220 }
221
222 void BGImagePageFinal::openBGImagesFolder(){
223 QDesktopServices::openUrl(QUrl("file:///"+this->bgImagesLocation));
224 }
225
226 void BGImagePageFinal::catchOniSplitProcessingErrors(QString result, int numErrors){
227
228 if(numErrors!=0){
229 QString sNumErrors=QString::number(numErrors);
230 bool isWarning = false;
231
232
233 if(result.startsWith("Warning: Texture")){
234 isWarning = true;
235 }
236
237 if(numErrors>1){
238
239 result = result+"\n This is the last of " + sNumErrors;
240
241 if(!isWarning){
242 result += " errors.";
243 }
244 else{
245 result += " messages.";
246 }
247 }
248
249 if(isWarning){
250 UtilVago::showWarningPopUpLogButton(result);
251 }
252 else{
253 UtilVago::showErrorPopUpLogButton(result);
254 }
255 }
256 }
257
258 void BGImagePageFinal::connectSlots(){
259 connect(this->myOniSplitConverter, SIGNAL(resultConversion(QString, int)), this, SLOT(catchOniSplitProcessingErrors(QString, int)));
260 connect(ui->lbComplete, SIGNAL(linkActivated(const QString & )), this, SLOT(openBGImagesFolder()));
261 }
262
263 BGImagePageFinal::~BGImagePageFinal()
264 {
265 delete ui;
266 }