-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
256 lines (221 loc) · 8.56 KB
/
mainwindow.cpp
File metadata and controls
256 lines (221 loc) · 8.56 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "mainwindow.h"
#include "aboutdialog.h"
#include "levelsettingsdialog.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDir>
#include <QDirIterator>
#include <QDebug>
#include <QMessageBox>
#include <QVariantMap>
#include <QSettings>
#include <cassert>
#include "CScene.h"
#include "CStaticGeometryEntity.h"
#include "CAnimatedGeometryEntity.h"
#include "Globals.h"
#include "Maths.h"
#include "CResourceManager.h"
#include "CResource.h"
#include "CEntity.h"
//-----------------------------------------------------
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
mSelectedEntity(0), mCurrentScenePath("")
{
ui->setupUi(this);
// Connect all actions from UI
connect(ui->actionA_propos, SIGNAL(triggered()), this, SLOT(onClick_APropos()));
connect(ui->actionNouveau, SIGNAL(triggered()), this, SLOT(onClick_Nouveau()));
connect(ui->actionSauvegarder, SIGNAL(triggered()), this, SLOT(onClick_Sauvegarder()));
connect(ui->actionOuvrir, SIGNAL(triggered()), this, SLOT(onClick_Ouvrir()));
connect(ui->actionD_finir_le_chemin_du_projet, SIGNAL(triggered()), this, SLOT(onClick_SetProjectPath()));
connect(ui->actionQuitter, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionConfiguration_du_niveau, SIGNAL(triggered()), this, SLOT(onClick_ConfigNiveau()));
connect(ui->tree_EntitiesAvailable, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(onDoubleClick_EntityTree(QTreeWidgetItem*,int)));
connect(ui->tableProperties, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(onChange_Property(QTableWidgetItem*)));
connect(ui->renderWidget, SIGNAL(itemSelected(QGraphicsItem*)), this, SLOT(onSelect_RenderItem(QGraphicsItem*)));
connect(ui->renderWidget, SIGNAL(selectionCleared()), this, SLOT(onClear_RenderSelection()));
connect(ui->renderWidget, SIGNAL(itemChanged()), this, SLOT(onUpdate_Properties()));
// Reload the project path (if any)
QSettings settings("Jirachi", "Nyuu");
setProjectPath(settings.value("editor/ProjectPath", ".").toString());
}
//-----------------------------------------------------
MainWindow::~MainWindow()
{
delete ui;
}
//-----------------------------------------------------
void MainWindow::onClick_APropos()
{
static AboutDialog* dlg = 0;
if (!dlg)
dlg = new AboutDialog();
dlg->show();
}
//-----------------------------------------------------
void MainWindow::onClick_Nouveau()
{
// Create a new scene and use it as current
CScene* scene = new CScene();
Globals::setCurrentScene(scene);
ui->renderWidget->notifySceneChanged();
ui->dock_Toolset->setEnabled(true);
/* scene->createStaticGeometry("D:\\Projets\\Jirachi\\test-rsrc\\tree_0_med.png")->setPosition(Vector2D(50, 50));
scene->createStaticGeometry("D:\\Projets\\Jirachi\\test-rsrc\\tree_0_med.png")->setPosition(Vector2D(150, 150));
scene->createStaticGeometry("D:\\Projets\\Jirachi\\test-rsrc\\turret.svg")->setPosition(Vector2D(350, 150));*/
}
//-----------------------------------------------------
void MainWindow::setProjectPath(const QString &directory)
{
Globals::setProjectPath(directory);
QSettings settings("Jirachi", "Nyuu");
settings.setValue("editor/ProjectPath", directory);
// Load resources in that directory and update side tree
ui->tree_EntitiesAvailable->clear();
QDir dir(directory);
QDirIterator iterator(dir.absolutePath(), QDirIterator::Subdirectories);
while (iterator.hasNext()) {
iterator.next();
QStringList strs;
strs.push_back(iterator.fileName());
strs.push_back(iterator.filePath().right(iterator.filePath().size() - directory.size() - 1));
// We filter only .rs files
if ((!iterator.fileInfo().isDir() && !iterator.fileName().endsWith(".rs")) || iterator.fileName().startsWith("."))
continue;
// Folders are added as topLevelItems, files are children of them
if (!iterator.fileInfo().isDir())
{
QString topElement = strs.at(1).left(strs.at(1).indexOf("/"));
ui->tree_EntitiesAvailable->findItems(topElement, Qt::MatchExactly).first()->addChild(new QTreeWidgetItem(strs));
}
else
{
ui->tree_EntitiesAvailable->addTopLevelItem(new QTreeWidgetItem(strs));
}
}
}
//-----------------------------------------------------
void MainWindow::onClick_SetProjectPath()
{
QString directory = QFileDialog::getExistingDirectory(this, "Répertoire projet...", ".");
setProjectPath(directory);
}
//-----------------------------------------------------
void MainWindow::onDoubleClick_EntityTree(QTreeWidgetItem* item, int column)
{
Q_UNUSED(column);
// We grab a vector of the middle of the screen projected into scene-space
QPoint midScene(ui->renderWidget->width()/2, ui->renderWidget->height()/2);
QPointF projectedMidScene = ui->renderWidget->mapToScene(midScene);
Vector2D midSceneVec(projectedMidScene.x(), projectedMidScene.y());
CResource* resource = CResourceManager::getInstance()->get(item->text(1));
// We make the right entity based on its type
if (resource->getProperty("type").toString() == "animated")
{
Globals::getCurrentScene()->createAnimatedGeometry(resource)->setPosition(midSceneVec);
}
else if (resource->getProperty("type").toString() == "static")
{
Globals::getCurrentScene()->createStaticGeometry(resource)->setPosition(midSceneVec);
}
else
{
QMessageBox::critical(this, "Erreur de ressource", "Le type n'est pas renseigné ou invalide (" + resource->getProperty("type").toString() + ")");
}
}
//-----------------------------------------------------
void MainWindow::onClear_RenderSelection()
{
// Disable the right panel
ui->tab_2->setEnabled(false);
}
//-----------------------------------------------------
void MainWindow::onSelect_RenderItem(QGraphicsItem *item)
{
CEntity* entity = Globals::getCurrentScene()->getEntityFromGraphicsView(item);
onSelect_RenderItem(entity);
}
//-----------------------------------------------------
void MainWindow::onSelect_RenderItem(CEntity *item)
{
if (!item)
{
onClear_RenderSelection();
return;
}
// Enable the tab panel
ui->tab_2->setEnabled(true);
// Read properties and fill in the table
ui->tableProperties->clearContents();
mSelectedEntity = item;
QVariantMap properties = item->getProperties();
QList<QString> keys = properties.keys();
ui->tableProperties->setRowCount(keys.size());
int i = 0;
for (QList<QString>::iterator it = keys.begin(); it != keys.end(); ++it)
{
QTableWidgetItem* propName = new QTableWidgetItem(*it);
QTableWidgetItem* propValue = new QTableWidgetItem(properties[(*it)].toString());
propName->setFlags(Qt::ItemIsEnabled);
ui->tableProperties->setItem(i, 0, propName);
ui->tableProperties->setItem(i, 1, propValue);
i++;
}
}
//-----------------------------------------------------
void MainWindow::onChange_Property(QTableWidgetItem *item)
{
QString propName = ui->tableProperties->item(item->row(), 0)->text();
assert(mSelectedEntity);
if (item->text() != propName)
mSelectedEntity->setProperty(propName, item->text());
}
//-----------------------------------------------------
void MainWindow::onUpdate_Properties()
{
onSelect_RenderItem(mSelectedEntity);
}
//-----------------------------------------------------
void MainWindow::onClick_Sauvegarder()
{
if (Globals::getCurrentScene())
{
if (mCurrentScenePath == "")
{
mCurrentScenePath = QFileDialog::getSaveFileName(this, "Fichier final", ".", "*.scn");
}
Globals::getCurrentScene()->save(mCurrentScenePath);
}
}
//-----------------------------------------------------
void MainWindow::onClick_Ouvrir()
{
QString path = QFileDialog::getOpenFileName(this, "Fichier scène", ".", "*.scn");
if (path != "")
{
CScene* scene = new CScene();
Globals::setCurrentScene(scene);
ui->renderWidget->notifySceneChanged();
scene->load(path);
ui->dock_Toolset->setEnabled(true);
}
}
//-----------------------------------------------------
void MainWindow::onClick_ConfigNiveau()
{
if (!Globals::getCurrentScene())
{
QMessageBox::critical(this, "Impossible !", "Veuillez ouvrir un niveau.");
return;
}
static LevelSettingsDialog* dlg = 0;
if (!dlg)
{
dlg = new LevelSettingsDialog(this);
}
dlg->show();
}
//-----------------------------------------------------