/
FileWidget.cpp
98 lines (77 loc) · 2.72 KB
/
FileWidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "FileWidget.h"
#include "SceneManager.h"
#include "SaveChunk.h"
FileWidget* FileWidget::m_pInstance = 0;
FileWidget::FileWidget()
: QWidget(0)
{
newSceneBtn = new QPushButton("New Scene");
chunkCmb = new QComboBox();
addChunkToSceneBtn = new QPushButton("Add Scene");
tileCmb = new QComboBox();
addTileToSceneBtn = new QPushButton("Add Tile");
saveAsChunkBtn = new QPushButton("Save as Scene");
saveAsTileBtn = new QPushButton("Save as Tile");
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *chunkLayout = new QHBoxLayout;
QHBoxLayout *tileLayout = new QHBoxLayout;
QHBoxLayout *saveLayout = new QHBoxLayout;
chunkLayout->addWidget(chunkCmb);
chunkLayout->addWidget(addChunkToSceneBtn);
saveLayout->addWidget(saveAsChunkBtn);
mainLayout->addWidget(newSceneBtn);
mainLayout->addLayout(chunkLayout);
mainLayout->addLayout(saveLayout);
mainLayout->setContentsMargins(0,0,0,0);
setLayout(mainLayout);
connect(newSceneBtn, SIGNAL(clicked()),this, SLOT(newScene()));
connect(addChunkToSceneBtn, SIGNAL(clicked()),this, SLOT(addChunk()));
connect(addTileToSceneBtn, SIGNAL(clicked()),this, SLOT(addTile()));
connect(saveAsChunkBtn, SIGNAL(clicked()),this, SLOT(saveChunk()));
connect(saveAsTileBtn, SIGNAL(clicked()),this, SLOT(saveTile()));
setFixedHeight(sizeHint().height());
setFixedWidth(400);
}
FileWidget *FileWidget::getInstance() {
if (m_pInstance == 0) {
m_pInstance = new FileWidget();
}
return m_pInstance;
}
void FileWidget::refresh() {
tileCmb->clear();
std::map<std::string, Tile*> *tiles = SceneManager::getInstance()->getTileManager()->getTiles();
for (map<std::string, Tile*>::iterator it = tiles->begin(); it != tiles->end(); it++) {
tileCmb->addItem((*it).first.c_str());
}
chunkCmb->clear();
std::map<std::string, Chunk*> *chunks = SceneManager::getInstance()->getChunkManager()->getChunks();
for (map<std::string, Chunk*>::iterator it = chunks->begin(); it != chunks->end(); it++) {
chunkCmb->addItem((*it).first.c_str());
}
}
void FileWidget::newScene() {
SceneManager::getInstance()->clear();
}
void FileWidget::addChunk() {
QString value = chunkCmb->currentText();
SceneManager::getInstance()->addChunk(value.toStdString());
}
void FileWidget::addTile() {
QString value = tileCmb->currentText();
SceneManager::getInstance()->addTile(value.toStdString());
}
void FileWidget::saveChunk() {
QString filename = QFileDialog::getSaveFileName(this, "Save Tile","Data/Chunks",tr("*.achk"));
if (!filename.isEmpty()){
SaveChunk save;
save.save(qPrintable(filename));
}
}
void FileWidget::saveTile() {
QString filename = QFileDialog::getSaveFileName(this, "Save Tile","Data/Tiles",tr("*.atl"));
if (!filename.isEmpty()){
SaveChunk save;
save.save(qPrintable(filename));
}
}