Debian Codegraph @

src/editor/map.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 "map.hpp"
17 #include "tracktile.hpp"
18
19 #include <QPoint>
20 #include <QPointF>
21
22 #include <memory>
23
24 Map::Map(size_t cols, size_t rows)
25 : MapBase(cols, rows)
26 {
27 createEmptyTiles();
28 }
29
30 Map::Map(const Map & other)
31 : MapBase(other.cols(), other.rows())
32 {
33 copyTiles(other);
34 }
35
36 void Map::copyTiles(const Map & other)
37 {
38 // Create tiles and set coordinates.
39 for (size_t j = 0; j < rows(); j++)
40 {
41 for (size_t i = 0; i < cols(); i++)
42 {
43 setTile(i, j, std::make_shared<TrackTile>(*std::dynamic_pointer_cast<TrackTile>(other.getTile(i, j))));
44 }
45 }
46 }
47
48 void Map::createEmptyTiles()
49 {
50 // Create tiles and set coordinates.
51 for (size_t j = 0; j < rows(); j++)
52 {
53 for (size_t i = 0; i < cols(); i++)
54 {
55 if (!getTile(i, j))
56 {
57 const auto newTile = std::make_shared<TrackTile>(
58 QPointF(TrackTile::width() / 2 + i * TrackTile::width(),
59 TrackTile::height() / 2 + j * TrackTile::height()),
60 QPoint(static_cast<int>(i), static_cast<int>(j)));
61 setTile(i, j, newTile);
62 }
63 else
64 {
65 getTile(i, j)->setLocation(
66 QPointF(
67 TrackTile::width() / 2 + i * TrackTile::width(),
68 TrackTile::height() / 2 + j * TrackTile::height()));
69 getTile(i, j)->setMatrixLocation(QPoint(static_cast<int>(i), static_cast<int>(j)));
70 }
71 }
72 }
73 }
74
75 void Map::moveTilesAfterColumnDeletion(size_t at)
76 {
77 for (size_t j = 0; j < rows(); j++)
78 {
79 for (size_t i = at + 1; i < cols(); i++)
80 {
81 const QPointF location(getTile(i, j)->location());
82 getTile(i, j)->setLocation(
83 QPointF(
84 location.x() - TrackTile::width(),
85 location.y()));
86 getTile(i, j)->setMatrixLocation(QPoint(static_cast<int>(i) - 1, static_cast<int>(j)));
87 }
88 }
89 }
90
91 void Map::moveTilesAfterRowDeletion(size_t at)
92 {
93 for (size_t j = at + 1; j < rows(); j++)
94 {
95 for (size_t i = 0; i < cols(); i++)
96 {
97 const QPointF location(getTile(i, j)->location());
98 getTile(i, j)->setLocation(
99 QPointF(
100 location.x(),
101 location.y() - TrackTile::height()));
102 getTile(i, j)->setMatrixLocation(QPoint(static_cast<int>(i), static_cast<int>(j) - 1));
103 }
104 }
105 }
106
107 void Map::resize(size_t newCols, size_t newRows)
108 {
109 MapBase::resize(newCols, newRows);
110
111 createEmptyTiles();
112 }
113
114 size_t Map::insertColumn(size_t at, InsertDirection insertDirection)
115 {
116 at = MapBase::insertColumn(at, insertDirection);
117
118 createEmptyTiles();
119
120 return at;
121 }
122
123 std::vector<TrackTileBasePtr> Map::deleteColumn(size_t at)
124 {
125 moveTilesAfterColumnDeletion(at);
126 return MapBase::deleteColumn(at);
127 }
128
129 size_t Map::insertRow(size_t at, InsertDirection insertDirection)
130 {
131 at = MapBase::insertRow(at, insertDirection);
132
133 createEmptyTiles();
134
135 return at;
136 }
137
138 std::vector<TrackTileBasePtr> Map::deleteRow(size_t at)
139 {
140 moveTilesAfterRowDeletion(at);
141 return MapBase::deleteRow(at);
142 }
143
144 Map::~Map()
145 {
146 }