-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCScene.cpp
More file actions
228 lines (194 loc) · 6.91 KB
/
CScene.cpp
File metadata and controls
228 lines (194 loc) · 6.91 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
#include "CScene.h"
#include "CEntity.h"
#include "CStaticGeometryEntity.h"
#include "CAnimatedGeometryEntity.h"
#include "CResourceManager.h"
#include "Globals.h"
#include <QMessageBox>
#include <QFile>
#include <QDebug>
#include "QJson/serializer.h"
#include "QJson/parser.h"
//-----------------------------------------------------
CScene::CScene() : mFreeEntityId(0)
{
}
//-----------------------------------------------------
CScene::~CScene()
{
}
//-----------------------------------------------------
CStaticGeometryEntity* CScene::createStaticGeometry(CResource* resource, int entityId)
{
int finalId = (entityId == -1 ? ++mFreeEntityId : entityId);
if (entityId != -1 && entityId > mFreeEntityId)
mFreeEntityId = entityId+1;
CStaticGeometryEntity* entity = new CStaticGeometryEntity(finalId, resource);
entity->addToScene(Globals::getCurrentGraphicsScene());
mEntities.push_back(entity);
return entity;
}
//-----------------------------------------------------
CAnimatedGeometryEntity* CScene::createAnimatedGeometry(CResource* resource, int entityId)
{
int finalId = (entityId == -1 ? ++mFreeEntityId : entityId);
if (entityId != -1 && entityId > mFreeEntityId)
mFreeEntityId = entityId+1;
CAnimatedGeometryEntity* entity = new CAnimatedGeometryEntity(finalId, resource);
entity->addToScene(Globals::getCurrentGraphicsScene());
mEntities.push_back(entity);
return entity;
}
//-----------------------------------------------------
void CScene::addAllElementsToScene(QGraphicsScene *scene)
{
for (QList<CEntity*>::iterator it = mEntities.begin(); it != mEntities.end(); ++it)
{
(*it)->addToScene(scene);
}
}
//-----------------------------------------------------
CEntity* CScene::getEntityFromGraphicsView(QGraphicsItem *element)
{
for (QList<CEntity*>::iterator it = mEntities.begin(); it != mEntities.end(); ++it)
{
if ((*it)->getSceneItem() == element)
{
return (*it);
}
}
return 0;
}
//-----------------------------------------------------
CEntity* CScene::cloneEntity(CEntity *src)
{
switch (src->getType())
{
case ENTITY_TYPE_STATIC_GEOMETRY:
{
CStaticGeometryEntity* ent = static_cast<CStaticGeometryEntity*>(src);
CStaticGeometryEntity* clone = createStaticGeometry(ent->getResource());
ent->copyPropertiesTo(clone);
return clone;
}
case ENTITY_TYPE_ANIMATED_GEOMETRY:
{
CAnimatedGeometryEntity* ent = static_cast<CAnimatedGeometryEntity*>(src);
CAnimatedGeometryEntity* clone = createAnimatedGeometry(ent->getResource());
ent->copyPropertiesTo(clone);
return clone;
}
default:
QMessageBox::critical(0, "Erreur", "Clonage de ce type d'entité non supporté!!! CScene::cloneEntity");
break;
}
return 0;
}
//-----------------------------------------------------
void CScene::save(const QString &filename)
{
// Serialize the scene
QVariantMap output;
// Serialize scene settings
// TODO: Background, name, etc
output.insert("settings", QVariantMap());
// Serialize scene elements
QVariantMap entities;
for (QList<CEntity*>::iterator it = mEntities.begin(); it != mEntities.end(); ++it)
{
CEntity* ent = (*it);
QVariantMap props;
props.insert("properties", ent->getProperties());
props.insert("type", ent->getType());
entities.insert(QString::number(ent->getEntityId()), props);
}
output.insert("entities", entities);
QJson::Serializer serializer;
QByteArray json = serializer.serialize(output);
// Write data
QFile file(filename);
file.open(QIODevice::WriteOnly | QIODevice::Text);
QDataStream out(&file);
out << json; // serialize as string
file.flush();
file.close();
}
//-----------------------------------------------------
void CScene::load(const QString &filename)
{
// Read the scene status
// Read raw scene file
qDebug() << "Starting loading scene " << filename;
QFile file(filename);
QString jsonData;
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
while (!in.atEnd())
{
//!!!! WARNING HAAAAAX
// Somehow, I get \0 as the very first character of the saved scene file,
// meaning that the whole JSON data is cut at the first character (which is pretty
// embarrassing!) - so I'm manually overriding this here. I'll take a closer look
// at this another time.
QString line = in.readLine().trimmed(); //.replace('\0', ' ');
jsonData.append(line);
}
}
else
{
QMessageBox::critical(0, "Erreur", "Erreur lors de l'ouverture du fichier!");
return;
}
// Parse all teh JSONz!
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (jsonData.toAscii(), &ok).toMap();
if (!ok)
{
QMessageBox::critical(0, "Erreur" ,"Erreur lors du décodage: " + parser.errorString() + " at line " + QString::number(parser.errorLine()));
return;
}
// Read JSON data
// TODO: Background, name, etc... (see save())
// De-serialize scene elements
QVariantMap entities = result["entities"].toMap();
for (QVariantMap::iterator it = entities.begin(); it != entities.end(); ++it)
{
int entityId = it.key().toInt();
QVariantMap entityValue = it.value().toMap();
// Read the types and the properties
EntityType entityType = (EntityType)entityValue["type"].toInt();
QVariantMap entityProps = entityValue["properties"].toMap();
CEntity* entity = 0;
// Create the right entity based on the type stored
switch (entityType)
{
case ENTITY_TYPE_STATIC_GEOMETRY:
entity = createStaticGeometry(CResourceManager::getInstance()->get(entityProps[CStaticGeometryEntity::PROP_KEY_RESOURCE].toString()), entityId);
break;
case ENTITY_TYPE_ANIMATED_GEOMETRY:
entity = createAnimatedGeometry(CResourceManager::getInstance()->get(entityProps[CAnimatedGeometryEntity::PROP_KEY_RESOURCE].toString()), entityId);
break;
default:
QMessageBox::critical(0, "Erreur!", "Type d'entité non supportée: " + QString::number(entityType));
continue;
break; // weirdness ahead!
}
// Re-apply all properties
for (QVariantMap::iterator propsIt = entityProps.begin(); propsIt != entityProps.end(); ++propsIt)
{
QString propKey = propsIt.key();
QVariant propValue = propsIt.value();
entity->setProperty(propKey, propValue);
}
// Cave Johnson, we're done here.
}
}
//-----------------------------------------------------
void CScene::remove(CEntity *ent)
{
delete ent;
mEntities.removeAll(ent);
}
//-----------------------------------------------------