Debian Codegraph @

src/editor/trackio.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2015 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include <QDir>
17 #include <QDomDocument>
18 #include <QDomElement>
19 #include <QFile>
20 #include <QTextStream>
21
22 #include "mainwindow.hpp"
23 #include "object.hpp"
24 #include "objectfactory.hpp"
25 #include "objectmodel.hpp"
26 #include "objectmodelloader.hpp"
27 #include "targetnode.hpp"
28 #include "trackdata.hpp"
29 #include "trackio.hpp"
30 #include "tracktile.hpp"
31
32 #include "../common/config.hpp"
33 #include "../common/datakeywords.hpp"
34 #include "../common/objectbase.hpp"
35 #include "../common/targetnodebase.hpp"
36
37 #include <cassert>
38 #include <memory>
39
40 namespace {
41
42 void readTile(TrackData & newData, const QDomElement & element)
43 {
44 // Init a new tile. QGraphicsScene will take
45 // the ownership eventually.
46 const auto tile = std::dynamic_pointer_cast<TrackTile>(newData.map().getTile(
47 element.attribute(DataKeywords::Tile::i, "0").toUInt(),
48 element.attribute(DataKeywords::Tile::j, "0").toUInt()));
49 assert(tile);
50
51 tile->setRotation(element.attribute(DataKeywords::Tile::orientation, "0").toInt());
52
53 tile->setTileType(element.attribute(DataKeywords::Tile::type, "clear"));
54
55 tile->setComputerHint(
56 static_cast<TrackTileBase::ComputerHint>(element.attribute(DataKeywords::Tile::computerHint, "0").toInt()));
57
58 tile->setExcludeFromMinimap(element.attribute(DataKeywords::Tile::excludeFromMinimap, "0").toUInt());
59 }
60
61 void readObject(TrackData & newData, const QDomElement & element)
62 {
63 // Create a new object. QGraphicsScene will take
64 // the ownership eventually.
65 auto object = std::make_shared<Object>(ObjectFactory::createObject(element.attribute(DataKeywords::Object::role, "clear")));
66
67 object->setLocation(QPointF(
68 element.attribute(DataKeywords::Object::x, "0").toInt(),
69 element.attribute(DataKeywords::Object::y, "0").toInt()));
70
71 object->setRotation(element.attribute(DataKeywords::Object::orientation, "0").toInt());
72
73 object->setForceStationary(element.attribute(DataKeywords::Object::forceStationary, "0").toUInt());
74
75 newData.objects().add(object);
76 }
77
78 void readTargetNode(std::vector<TargetNodeBasePtr> & route, const QDomElement & element)
79 {
80 // Create a new object. QGraphicsScene will take
81 // the ownership eventually.
82 const auto targetNode = std::make_shared<TargetNode>();
83 targetNode->setIndex(element.attribute(DataKeywords::Node::index, "0").toInt());
84
85 const int x = element.attribute(DataKeywords::Node::x, "0").toInt();
86 const int y = element.attribute(DataKeywords::Node::y, "0").toInt();
87
88 targetNode->setLocation(QPointF(x, y));
89
90 const int w = element.attribute(DataKeywords::Node::width, "0").toInt();
91 const int h = element.attribute(DataKeywords::Node::height, "0").toInt();
92
93 if (w > 0 && h > 0)
94 {
95 targetNode->setSize(QSizeF(w, h));
96 }
97
98 route.push_back(targetNode);
99 }
100
101 void writeTiles(const TrackDataPtr trackData, QDomElement & root, QDomDocument & doc)
102 {
103 for (size_t i = 0; i < trackData->map().cols(); i++)
104 {
105 for (size_t j = 0; j < trackData->map().rows(); j++)
106 {
107 auto && tileElement = doc.createElement(DataKeywords::Track::tile);
108 const auto tile = std::dynamic_pointer_cast<TrackTile>(trackData->map().getTile(i, j));
109 tileElement.setAttribute(DataKeywords::Tile::type, tile->tileType());
110 tileElement.setAttribute(DataKeywords::Tile::i, static_cast<int>(i));
111 tileElement.setAttribute(DataKeywords::Tile::j, static_cast<int>(j));
112 tileElement.setAttribute(DataKeywords::Tile::orientation, tile->rotation());
113
114 if (tile->excludeFromMinimap())
115 {
116 tileElement.setAttribute(DataKeywords::Tile::excludeFromMinimap, true);
117 }
118
119 if (tile->computerHint() != TrackTile::ComputerHint::None)
120 {
121 tileElement.setAttribute(DataKeywords::Tile::computerHint, static_cast<int>(tile->computerHint()));
122 }
123
124 root.appendChild(tileElement);
125 }
126 }
127 }
128
129 void writeObjects(TrackDataPtr trackData, QDomElement & root, QDomDocument & doc)
130 {
131 for (auto objectPtr : trackData->objects())
132 {
133 auto object = std::dynamic_pointer_cast<Object>(objectPtr);
134 assert(object);
135
136 auto && objectElement = doc.createElement(DataKeywords::Track::object);
137 objectElement.setAttribute(DataKeywords::Object::category, object->category());
138 objectElement.setAttribute(DataKeywords::Object::role, object->role());
139 objectElement.setAttribute(DataKeywords::Object::x, static_cast<int>(object->location().x()));
140 objectElement.setAttribute(DataKeywords::Object::y, static_cast<int>(object->location().y()));
141 objectElement.setAttribute(DataKeywords::Object::orientation, static_cast<int>(object->rotation()));
142
143 if (object->forceStationary())
144 {
145 objectElement.setAttribute(DataKeywords::Object::forceStationary, static_cast<int>(object->forceStationary()));
146 }
147
148 root.appendChild(objectElement);
149 }
150 }
151
152 void writeTargetNodes(TrackDataPtr trackData, QDomElement & root, QDomDocument & doc)
153 {
154 for (auto && targetNode : trackData->route())
155 {
156 auto && targetNodeElement = doc.createElement(DataKeywords::Track::node);
157 targetNodeElement.setAttribute(DataKeywords::Node::index, targetNode->index());
158 targetNodeElement.setAttribute(DataKeywords::Node::x, static_cast<int>(targetNode->location().x()));
159 targetNodeElement.setAttribute(DataKeywords::Node::y, static_cast<int>(targetNode->location().y()));
160 targetNodeElement.setAttribute(DataKeywords::Node::width, static_cast<int>(targetNode->size().width()));
161 targetNodeElement.setAttribute(DataKeywords::Node::height, static_cast<int>(targetNode->size().height()));
162
163 root.appendChild(targetNodeElement);
164 }
165 }
166
167 } // namespace
168
169 bool TrackIO::save(TrackDataPtr trackData, QString path)
170 {
171 // Create content
172 QDomDocument doc;
173 doc.appendChild(doc.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'"));
174
175 auto && root = doc.createElement(DataKeywords::Header::track);
176 root.setAttribute(DataKeywords::Header::version, Config::Editor::EDITOR_VERSION);
177 root.setAttribute(DataKeywords::Header::name, trackData->name());
178 root.setAttribute(DataKeywords::Header::cols, static_cast<int>(trackData->map().cols()));
179 root.setAttribute(DataKeywords::Header::rows, static_cast<int>(trackData->map().rows()));
180 root.setAttribute(DataKeywords::Header::index, trackData->index());
181
182 if (trackData->isUserTrack()) // Don't add the attribute at all, if not set
183 {
184 root.setAttribute(DataKeywords::Header::user, true);
185 }
186
187 doc.appendChild(root);
188
189 writeTiles(trackData, root, doc);
190 writeObjects(trackData, root, doc);
191 writeTargetNodes(trackData, root, doc);
192
193 // Save to file
194 QFile file(path);
195 if (file.open(QIODevice::WriteOnly | QIODevice::Text))
196 {
197 QTextStream out(&file);
198 out << doc.toString();
199 file.close();
200 return true;
201 }
202
203 return false;
204 }
205
206 TrackDataPtr TrackIO::open(QString path)
207 {
208 QDomDocument doc;
209
210 QFile file(path);
211 if (!file.open(QIODevice::ReadOnly))
212 {
213 return nullptr;
214 }
215
216 if (!doc.setContent(&file))
217 {
218 file.close();
219 return nullptr;
220 }
221
222 file.close();
223
224 auto && root = doc.documentElement();
225 const size_t cols = root.attribute(DataKeywords::Header::cols, "0").toUInt();
226 const size_t rows = root.attribute(DataKeywords::Header::rows, "0").toUInt();
227
228 TrackDataPtr newData;
229 if (cols && rows)
230 {
231 const auto name = root.attribute(DataKeywords::Header::name, "undefined");
232 const bool isUserTrack = root.attribute(DataKeywords::Header::user, "0").toInt();
233 newData = std::make_shared<TrackData>(name, isUserTrack, cols, rows);
234 newData->setFileName(path);
235 newData->setIndex(root.attribute(DataKeywords::Header::index, "0").toUInt());
236
237 // Temporary route vector.
238 std::vector<TargetNodeBasePtr> route;
239
240 auto && node = root.firstChild();
241 while (!node.isNull())
242 {
243 auto element = node.toElement();
244 if (!element.isNull())
245 {
246 // Read a tile element
247 if (element.nodeName() == DataKeywords::Track::tile)
248 {
249 readTile(*newData, element);
250 }
251 // Read an object element
252 else if (element.nodeName() == DataKeywords::Track::object)
253 {
254 readObject(*newData, element);
255 }
256 // Read a target node element
257 else if (element.nodeName() == DataKeywords::Track::node)
258 {
259 readTargetNode(route, element);
260 }
261 }
262
263 node = node.nextSibling();
264 }
265
266 // Sort and build route from the temporary vector.
267 newData->route().buildFromVector(route);
268 }
269
270 return newData;
271 }