-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleveldatamodel.cpp
More file actions
183 lines (146 loc) · 4.07 KB
/
leveldatamodel.cpp
File metadata and controls
183 lines (146 loc) · 4.07 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
#include "leveldatamodel.h"
#include <QPolygonF>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lvldm, "app.models.leveldatamodel")
LevelDataModel::LevelDataModel(QObject *parent)
: QAbstractListModel { parent }
{
}
int LevelDataModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_objects.count();
}
QVariant LevelDataModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_objects.count())
return QVariant();
const ObjectDescription &data = m_objects[index.row()];
switch (role) {
case Type:
return data.type;
case BoundingBox:
return data.boundingBox;
case Static:
return data.isStatic;
case Polygon:
return data.polygon;
case Points:
return QVariant::fromValue(data.points);
case GameItem:
return data.gameItem;
case Color:
return data.color;
}
return QVariant();
}
QHash<int, QByteArray> LevelDataModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Type] = "type";
roles[BoundingBox] = "boundingBox";
roles[Static] = "static";
roles[Polygon] = "polygon";
roles[Points] = "points";
roles[GameItem] = "gameItem";
roles[Color] = "color";
return roles;
}
QString LevelDataModel::name() const
{
return m_name;
}
void LevelDataModel::setLevelData(const LevelData &data)
{
setName(data.name);
setBackgroundImage(data.backgroundImage);
setMusic(data.music);
beginResetModel();
m_objects = data.objects;
endResetModel();
}
void LevelDataModel::addSimpleObject(int type, const QRectF &boundingRect, bool isStatic, bool gameItem, const QColor &color)
{
ObjectDescription objectDescription = {
type, boundingRect, {}, {}, isStatic, gameItem, color,
};
qCDebug(lvldm) << "adding simple object" << objectDescription << m_objects.count();
beginInsertRows(QModelIndex(), m_objects.count(), m_objects.count());
m_objects << objectDescription;
endInsertRows();
}
void LevelDataModel::addPolygonObject(int type, const QPolygonF &originalPoints, const QList<QVariantList> &optimizedPoints,
bool isStatic, bool gameItem, const QColor &color)
{
ObjectDescription objectDescription = {
type, originalPoints.boundingRect(), originalPoints, optimizedPoints, isStatic, gameItem, color,
};
qCDebug(lvldm) << "adding polygon object" << objectDescription << m_objects.count();
beginInsertRows(QModelIndex(), m_objects.count(), m_objects.count());
m_objects << objectDescription;
endInsertRows();
}
void LevelDataModel::addLineObject(int type, const QPolygonF &originalPoints, const QList<QVariantList> &lineSegments,
bool isStatic, bool gameItem, const QColor &color)
{
ObjectDescription objectDescription = {
type, originalPoints.boundingRect(), originalPoints, lineSegments, isStatic, gameItem, color,
};
qCDebug(lvldm) << "adding line object" << objectDescription << m_objects.count();
beginInsertRows(QModelIndex(), m_objects.count(), m_objects.count());
m_objects << objectDescription;
endInsertRows();
}
void LevelDataModel::removeObject(int index)
{
qCDebug(lvldm) << "removing object with index" << index;
beginRemoveRows(QModelIndex(), index, index);
m_objects.remove(index);
endRemoveRows();
}
void LevelDataModel::clear()
{
qCDebug(lvldm) << "clearing";
setName(QString());
setBackgroundImage(QUrl());
setMusic(QUrl());
beginResetModel();
m_objects.clear();
endResetModel();
}
QList<ObjectDescription> LevelDataModel::objects() const
{
return m_objects;
}
void LevelDataModel::setName(const QString &name)
{
if (m_name == name) {
return;
}
m_name = name;
emit nameChanged();
}
QUrl LevelDataModel::backgroundImage() const
{
return m_backgroundImage;
}
void LevelDataModel::setBackgroundImage(const QUrl &url)
{
if (m_backgroundImage == url) {
return;
}
m_backgroundImage = url;
emit backgroundImageChanged();
}
QUrl LevelDataModel::music() const
{
return m_music;
}
void LevelDataModel::setMusic(const QUrl &url)
{
if (m_music == url) {
return;
}
m_music = url;
emit musicChanged();
}