-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.cpp
More file actions
115 lines (93 loc) · 2.71 KB
/
editor.cpp
File metadata and controls
115 lines (93 loc) · 2.71 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
#include "editor.h"
#include "levelhandler.h"
#include "levelfileio.h"
#include <QDir>
#include <QStandardPaths>
#include <QQuickItemGrabResult>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(editor, "app.editor")
Editor::Editor(QObject *parent)
: ILevelManager { parent }
, m_levelData { new LevelDataModel(this) }
{
connect(this, &Editor::currentShapeChanged, this, [this]() {
setCurrentEditOperation(EditOperationType_Draw);
});
}
Constants::ShapeType Editor::currentShape() const
{
return m_currentShape;
}
void Editor::setCurrentShape(Constants::ShapeType shape)
{
if (m_currentShape == shape) {
return;
}
m_currentShape = shape;
emit currentShapeChanged();
}
QList<Constants::ShapeType> Editor::availableShapes() const
{
return {
Constants::ShapeType_Circle,
Constants::ShapeType_Rectangle,
Constants::ShapeType_Polygon,
Constants::ShapeType_SpecialStar,
};
}
QList<Editor::EditOperationType> Editor::availableEditOperations() const
{
return {
EditOperationType_Draw,
EditOperationType_Move,
EditOperationType_Delete,
};
}
Editor::EditOperationType Editor::currentEditOperation() const
{
return m_currentEditOperation;
}
void Editor::setCurrentEditOperation(EditOperationType operation)
{
if (m_currentEditOperation == operation) {
return;
}
m_currentEditOperation = operation;
emit currentEditOperationChanged();
}
void Editor::saveLevel(const QString &name, QQuickItemGrabResult *screenshot)
{
qCDebug(editor) << "saving level with name:" << name;
LevelHandler::userLevelsDirectory().mkdir(name);
QDir levelDirectory(LevelHandler::userLevelsDirectory().filePath(name));
LevelData levelData;
levelData.name = name;
levelData.backgroundImage = m_levelData->backgroundImage();
levelData.music = m_levelData->music();
levelData.objects = m_levelData->objects();
screenshot->saveToFile(levelDirectory.filePath(LevelHandler::levelPreviewFileName()));
LevelFileIO::storeLevel(levelDirectory.filePath(LevelHandler::levelDataFileName()), levelData);
emit levelSavedSuccessfully();
}
void Editor::resetLevel()
{
m_levelData->clear();
setCurrentShape(Constants::ShapeType_Circle);
setCurrentEditOperation(EditOperationType_Draw);
}
LevelDataModel *Editor::levelData() const
{
return m_levelData;
}
void Editor::loadLevel(const QString &path)
{
qCDebug(editor) << "loading level at path" << path;
QDir levelPath(path);
if (levelPath.exists(LevelHandler::levelDataFileName())) {
m_levelData->setLevelData(LevelFileIO::loadLevel(levelPath.filePath(LevelHandler::levelDataFileName())));
emit levelLoadedSuccessfully();
} else {
qCCritical(editor) << "unable to find level data";
// TODO: emit signal to display UI message
}
}