-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
329 lines (248 loc) · 10.1 KB
/
mainwindow.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "exportmapdialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QScrollBar>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
settings = new QSettings("Blarg City", "Goombatlas");
ui->setupUi(this);
setMapName();
QString iconsPath(QCoreApplication::applicationDirPath() + "/goombatlas_data/icons/");
// Setup Mode Selection Box
modeBox = new QComboBox(this);
ui->toolBar->insertWidget(ui->actionModePlaceholder, modeBox);
ui->toolBar->removeAction(ui->actionModePlaceholder);
connect(modeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(modeChanged(int)));
modes.append(Mode("Edition"));
modes.append(Mode("Animation"));
// Load icons
ui->actionNew->setIcon(QIcon(iconsPath + "new.png"));
ui->actionOpen->setIcon(QIcon(iconsPath + "open.png"));
ui->actionSave->setIcon(QIcon(iconsPath + "save.png"));
ui->actionSaveAs->setIcon(QIcon(iconsPath + "save_as.png"));
ui->actionZoomIn->setIcon(QIcon(iconsPath + "zoom_in.png"));
ui->actionZoomOut->setIcon(QIcon(iconsPath + "zoom_out.png"));
ui->actionZoom100->setIcon(QIcon(iconsPath + "zoom_100.png"));
ui->actionMaximumZoom->setIcon(QIcon(iconsPath + "zoom_max.png"));
ui->actionMinimumZoom->setIcon(QIcon(iconsPath + "zoom_min.png"));
ui->actionGrid->setIcon(QIcon(iconsPath + "grid.png"));
ui->actionExportC->setIcon(QIcon(iconsPath + "export_c"));
// Setup Map View
map = new Map();
mapViewArea = new QScrollAreaRE();
ui->mainLayout->addWidget(mapViewArea);
mapView = new MapView(this, map);
connect(mapView, SIGNAL(scrollTo(int,int)), this, SLOT(mapScrollTo(int,int)));
connect(mapView, SIGNAL(scrollTo(QPoint)), this, SLOT(mapScrollTo(QPoint)));
connect(mapView, SIGNAL(scrollTo_(int,int)), this, SLOT(mapScrollTo_(int,int)));
mapViewArea->setWidget(mapView);
connect(mapViewArea, SIGNAL(resized(QResizeEvent*)), mapView, SLOT(viewAreaResized(QResizeEvent*)));
// Setup Node Editor
nodeEditor = new NodeEditorWidget(map, mapView, this);
connect(nodeEditor, SIGNAL(redrawMap()), mapView, SLOT(repaint()));
connect(mapView, SIGNAL(changeSelectedNode(Node*)), nodeEditor, SLOT(select(Node*)));
connect(mapView, SIGNAL(changeDeselectedNode()), nodeEditor, SLOT(deselect()));
connect(nodeEditor, SIGNAL(nodeSelected(MovableObject*)), mapView, SLOT(select(MovableObject*)));
connect(nodeEditor, SIGNAL(nodeDeselected()), mapView, SLOT(deselect()));
QDockWidget* nodeEditorDock = new QDockWidget("Node Editor", this);
nodeEditorDock->setWidget(nodeEditor);
addDockWidget(Qt::LeftDockWidgetArea, nodeEditorDock);
nodeEditorDock->toggleViewAction()->setShortcut(QKeySequence("Ctrl+1"));
nodeEditorDock->toggleViewAction()->setIcon(QIcon(iconsPath + "node.png"));
ui->menuWindow->addAction(nodeEditorDock->toggleViewAction());
modes[0].addDock(nodeEditorDock);
// Setup Path Behavior Editor
pathBehaviorEditor = new PathBehaviorEditor(map, this);
QDockWidget* pathBehaviorEditorDock = new QDockWidget("Path Setting Editor", this);
pathBehaviorEditorDock->setWidget(pathBehaviorEditor);
addDockWidget(Qt::RightDockWidgetArea, pathBehaviorEditorDock);
pathBehaviorEditorDock->toggleViewAction()->setShortcut(QKeySequence("Ctrl+2"));
pathBehaviorEditorDock->toggleViewAction()->setIcon(QIcon(iconsPath + "path_setting.png"));
ui->menuWindow->addAction(pathBehaviorEditorDock->toggleViewAction());
modes[0].addDock(pathBehaviorEditorDock);
// Setup Map Objects Editor
mapObjectEditor = new MapObjectEditor(map, mapView, this);
connect(mapObjectEditor, SIGNAL(redrawMap()), mapView, SLOT(repaint()));
connect(mapView, SIGNAL(changeSelectedMapObj(MapObject*)), mapObjectEditor, SLOT(select(MapObject*)));
connect(mapView, SIGNAL(changeDeselectedMapObj()), mapObjectEditor, SLOT(deselect()));
connect(mapObjectEditor, SIGNAL(objectSelected(MovableObject*)), mapView, SLOT(select(MovableObject*)));
connect(mapObjectEditor, SIGNAL(objectDeselected()), mapView, SLOT(deselect()));
QDockWidget* mapObjectEditorDock = new QDockWidget("Map Objects Editor", this);
mapObjectEditorDock->setWidget(mapObjectEditor);
addDockWidget(Qt::RightDockWidgetArea, mapObjectEditorDock);
mapObjectEditorDock->toggleViewAction()->setShortcut(QKeySequence("Ctrl+3"));
mapObjectEditorDock->toggleViewAction()->setIcon(QIcon(iconsPath + "object.png"));
ui->menuWindow->addAction(mapObjectEditorDock->toggleViewAction());
modes[0].addDock(mapObjectEditorDock);
// Setup Sprites Editor
spritesEditor = new SpritesEditor(map, this);
QDockWidget* spritesEditorDock = new QDockWidget("Sprites Editor", this);
spritesEditorDock->setWidget(spritesEditor);
addDockWidget(Qt::RightDockWidgetArea, spritesEditorDock);
spritesEditorDock->toggleViewAction()->setShortcut(QKeySequence("Ctrl+4"));
spritesEditorDock->toggleViewAction()->setIcon(QIcon(iconsPath + "sprite.png"));
ui->menuWindow->addAction(spritesEditorDock->toggleViewAction());
modes[0].addDock(spritesEditorDock);
ui->menuWindow->addSeparator();
// Setup Animations Editor
animationsEditor = new AnimationsEditor(map, mapView, this);
connect(animationsEditor, SIGNAL(redrawMap()), mapView, SLOT(repaint()));
connect(mapView, SIGNAL(changeSelectedKeyframe(Keyframe*)), animationsEditor, SLOT(kSelect(Keyframe*)));
connect(mapView, SIGNAL(changeDeselectedKeyframe()), animationsEditor, SLOT(kDeselect()));
connect(animationsEditor, SIGNAL(keyframeSelected(MovableObject*)), mapView, SLOT(select(MovableObject*)));
connect(animationsEditor, SIGNAL(keyframeDeselected()), mapView, SLOT(deselect()));
QDockWidget* animationsEditorDock = new QDockWidget("Path Animations Editor", this);
animationsEditorDock->setWidget(animationsEditor);
addDockWidget(Qt::LeftDockWidgetArea, animationsEditorDock);
animationsEditorDock->toggleViewAction()->setShortcut(QKeySequence("Ctrl+1"));
animationsEditorDock->toggleViewAction()->setIcon(QIcon(iconsPath + "walk.png"));
ui->menuWindow->addAction(animationsEditorDock->toggleViewAction());
modes[1].addDock(animationsEditorDock);
// Update Mode Selector
for (int i = 0; i < modes.length(); i++)
{
modes[i].deactivate();
modeBox->addItem(modes[i].getName());
}
modes[0].activate();
// Load Settings
ui->actionGrid->setChecked(settings->value("GridEnabled", "1").toBool());
mapView->toggleGrid(settings->value("GridEnabled", "1").toBool());
// We cannot scroll before the window is created
QTimer::singleShot(0, this, SLOT(onLoad()));
}
MainWindow::~MainWindow()
{
settings->setValue("GridEnabled", ui->actionGrid->isChecked());
delete settings;
delete ui;
delete mapView;
delete map;
}
void MainWindow::onLoad()
{
mapScrollTo(0, 0);
}
QMenu* MainWindow::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
filteredMenu->removeAction(ui->toolBar->toggleViewAction());
return filteredMenu;
}
void MainWindow::modeChanged(int modeIndex)
{
for (int i = 0; i < modes.length(); i++)
modes[i].deactivate();
modes[modeIndex].activate();
mapView->setMode(modeIndex);
}
void MainWindow::updateMap(QFile* file)
{
delete map;
if (file == NULL)
map = new Map();
else
map = new Map(file);
mapView->setMap(map);
nodeEditor->setMap(map);
pathBehaviorEditor->setMap(map);
mapObjectEditor->setMap(map);
spritesEditor->setMap(map);
animationsEditor->setMap(map);
}
// Slots
void MainWindow::setMapName(QString mapName)
{
setWindowTitle(mapName + " - Goombatlas");
}
void MainWindow::mapScrollTo(QPoint p)
{
mapScrollTo(p.x(), p.y());
}
void MainWindow::mapScrollTo(int x, int y)
{
float zoom = mapView->getZoom();
int wR = mapView->visibleRegion().boundingRect().width();
int hR = mapView->visibleRegion().boundingRect().height();
mapViewArea->horizontalScrollBar()->setValue(qMax((int)((x+mapView->getCenterX())*zoom)-wR/2, 0));
mapViewArea->verticalScrollBar()->setValue(qMax((int)((y+mapView->getCenterY())*zoom)-hR/2, 0));
}
void MainWindow::mapScrollTo_(int x, int y)
{
qMax(0, x);
qMax(0, y);
mapViewArea->horizontalScrollBar()->setValue(x);
mapViewArea->verticalScrollBar()->setValue(y);
}
void MainWindow::on_actionNew_triggered()
{
updateMap();
mapScrollTo(0, 0);
}
void MainWindow::on_actionOpen_triggered()
{
QString openPath = QFileDialog::getOpenFileName(this, "Open Map", "", "Goombatlas Map (*.gbmap)");
if (openPath.isEmpty() || openPath.isNull())
return;
QFileInfo fileInfo(openPath);
if (!fileInfo.exists())
return;
QFile file(openPath);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::information(this, "Goombatlas", "Could not open " + openPath + " for reading!", QMessageBox::Ok);
return;
}
updateMap(&file);
setMapName(fileInfo.fileName());
}
void MainWindow::on_actionSave_triggered()
{
if (!QFileInfo(map->getPath()).exists())
on_actionSaveAs_triggered();
else
map->save(this);
}
void MainWindow::on_actionSaveAs_triggered()
{
QString savePath = QFileDialog::getSaveFileName(this, "Save Map", "", "Goombatlas Map (*.gbmap)");
if (savePath == "")
return;
map->save(this, savePath);
QFileInfo fileInfo(savePath);
setMapName(fileInfo.fileName());
}
void MainWindow::on_actionExportC_triggered()
{
ExportMapDialog emd(this);
emd.exec();
map->exportAsC(emd.getCPath(), emd.getReplacesPath(), emd.getPrefix(), emd.getWorldId());
}
void MainWindow::on_actionGrid_triggered(bool checked)
{
mapView->toggleGrid(checked);
}
void MainWindow::on_actionZoom100_triggered()
{
mapView->setZoom(2);
}
void MainWindow::on_actionZoomIn_triggered()
{
mapView->zoomIn();
}
void MainWindow::on_actionZoomOut_triggered()
{
mapView->zoomOut();
}
void MainWindow::on_actionMaximumZoom_triggered()
{
mapView->zoomMax();
}
void MainWindow::on_actionMinimumZoom_triggered()
{
mapView->zoomMin();
}