author | Wuzzy <Wuzzy2@mail.ru> |
Sun, 11 Mar 2018 21:38:08 +0100 | |
changeset 13165 | 89a6b862e692 |
parent 11046 | 47a8c19ecb60 |
child 13203 | eaa494f0b19e |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> |
4976 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
10002
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
4976 | 17 |
*/ |
18 |
||
4423 | 19 |
#include <QGraphicsSceneMouseEvent> |
4426 | 20 |
#include <QGraphicsPathItem> |
4427 | 21 |
#include <QtEndian> |
4937 | 22 |
#include <QDebug> |
9555 | 23 |
#include <QTransform> |
9551 | 24 |
#include <math.h> |
4423 | 25 |
|
26 |
#include "drawmapscene.h" |
|
27 |
||
4434 | 28 |
template <class T> T sqr(const T & x) |
29 |
{ |
|
30 |
return x*x; |
|
31 |
} |
|
32 |
||
4423 | 33 |
DrawMapScene::DrawMapScene(QObject *parent) : |
4424 | 34 |
QGraphicsScene(parent), |
4426 | 35 |
m_pen(Qt::yellow), |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
36 |
m_brush(Qt::yellow), |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
37 |
m_cursor(new QGraphicsEllipseItem(-0.5, -0.5, 1, 1)) |
4423 | 38 |
{ |
39 |
setSceneRect(0, 0, 4096, 2048); |
|
4424 | 40 |
|
41 |
QLinearGradient gradient(0, 0, 0, 2048); |
|
4426 | 42 |
gradient.setColorAt(0, QColor(60, 60, 155)); |
43 |
gradient.setColorAt(1, QColor(155, 155, 60)); |
|
6873 | 44 |
|
45 |
m_eraser = QBrush(gradient); |
|
46 |
setBackgroundBrush(m_eraser); |
|
47 |
m_isErasing = false; |
|
4424 | 48 |
|
9551 | 49 |
m_pathType = Polyline; |
50 |
||
6784 | 51 |
m_pen.setWidth(76); |
4426 | 52 |
m_pen.setJoinStyle(Qt::RoundJoin); |
53 |
m_pen.setCapStyle(Qt::RoundCap); |
|
54 |
m_currPath = 0; |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
55 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
56 |
m_isCursorShown = false; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
57 |
m_cursor->setPen(QPen(Qt::green)); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
58 |
m_cursor->setZValue(1); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
59 |
m_cursor->setScale(m_pen.width()); |
4423 | 60 |
} |
61 |
||
62 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
63 |
{ |
|
4426 | 64 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
65 |
{ |
|
66 |
QPainterPath path = m_currPath->path(); |
|
9555 | 67 |
QPointF currentPos = mouseEvent->scenePos(); |
68 |
||
69 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
70 |
currentPos = putSomeConstraints(paths.first().initialPoint, currentPos); |
|
4937 | 71 |
|
9551 | 72 |
switch (m_pathType) |
4937 | 73 |
{ |
9551 | 74 |
case Polyline: |
75 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
76 |
{ |
|
77 |
int c = path.elementCount(); |
|
9555 | 78 |
path.setElementPositionAt(c - 1, currentPos.x(), currentPos.y()); |
4937 | 79 |
|
9551 | 80 |
} |
81 |
else |
|
82 |
{ |
|
9555 | 83 |
path.lineTo(currentPos); |
9551 | 84 |
paths.first().points.append(mouseEvent->scenePos().toPoint()); |
85 |
} |
|
86 |
break; |
|
87 |
case Rectangle: { |
|
88 |
path = QPainterPath(); |
|
89 |
QPointF p1 = paths.first().initialPoint; |
|
9555 | 90 |
QPointF p2 = currentPos; |
9551 | 91 |
path.moveTo(p1); |
92 |
path.lineTo(p1.x(), p2.y()); |
|
93 |
path.lineTo(p2); |
|
94 |
path.lineTo(p2.x(), p1.y()); |
|
95 |
path.lineTo(p1); |
|
96 |
break; |
|
97 |
} |
|
98 |
case Ellipse: { |
|
99 |
path = QPainterPath(); |
|
9555 | 100 |
QList<QPointF> points = makeEllipse(paths.first().initialPoint, currentPos); |
9551 | 101 |
path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points))); |
102 |
break; |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
103 |
} |
4937 | 104 |
} |
9551 | 105 |
|
4426 | 106 |
m_currPath->setPath(path); |
4427 | 107 |
|
108 |
emit pathChanged(); |
|
4426 | 109 |
} |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
110 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
111 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
112 |
showCursor(); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
113 |
m_cursor->setPos(mouseEvent->scenePos()); |
4423 | 114 |
} |
115 |
||
116 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
117 |
{ |
|
4426 | 118 |
m_currPath = addPath(QPainterPath(), m_pen); |
119 |
||
120 |
QPainterPath path = m_currPath->path(); |
|
121 |
QPointF p = mouseEvent->scenePos(); |
|
122 |
p += QPointF(0.01, 0.01); |
|
123 |
path.moveTo(p); |
|
124 |
path.lineTo(mouseEvent->scenePos()); |
|
6873 | 125 |
|
126 |
PathParams params; |
|
127 |
params.width = serializePenWidth(m_pen.width()); |
|
128 |
params.erasing = m_isErasing; |
|
9551 | 129 |
params.initialPoint = mouseEvent->scenePos().toPoint(); |
130 |
params.points = QList<QPoint>() << params.initialPoint; |
|
6873 | 131 |
paths.prepend(params); |
4426 | 132 |
m_currPath->setPath(path); |
133 |
||
4427 | 134 |
emit pathChanged(); |
4423 | 135 |
} |
136 |
||
137 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
138 |
{ |
|
5108
b7483e29ea8c
Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents:
5040
diff
changeset
|
139 |
if (m_currPath) |
b7483e29ea8c
Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents:
5040
diff
changeset
|
140 |
{ |
9555 | 141 |
QPointF currentPos = mouseEvent->scenePos(); |
142 |
||
143 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
144 |
currentPos = putSomeConstraints(paths.first().initialPoint, currentPos); |
|
145 |
||
9551 | 146 |
switch (m_pathType) |
147 |
{ |
|
148 |
case Polyline: { |
|
149 |
QPainterPath path = m_currPath->path(); |
|
150 |
path.lineTo(mouseEvent->scenePos()); |
|
9555 | 151 |
paths.first().points.append(currentPos.toPoint()); |
9551 | 152 |
m_currPath->setPath(path); |
9553 | 153 |
simplifyLast(); |
9551 | 154 |
break; |
155 |
} |
|
156 |
case Rectangle: { |
|
157 |
QPoint p1 = paths.first().initialPoint; |
|
9555 | 158 |
QPoint p2 = currentPos.toPoint(); |
9551 | 159 |
QList<QPoint> rpoints; |
160 |
rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1; |
|
161 |
paths.first().points = rpoints; |
|
162 |
break; |
|
163 |
} |
|
164 |
case Ellipse: |
|
165 |
QPoint p1 = paths.first().initialPoint; |
|
9555 | 166 |
QPoint p2 = currentPos.toPoint(); |
9551 | 167 |
QList<QPointF> points = makeEllipse(p1, p2); |
168 |
QList<QPoint> epoints; |
|
169 |
foreach(const QPointF & p, points) |
|
170 |
epoints.append(p.toPoint()); |
|
171 |
paths.first().points = epoints; |
|
172 |
break; |
|
173 |
} |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
174 |
|
9553 | 175 |
m_currPath = 0; |
4439 | 176 |
|
9553 | 177 |
emit pathChanged(); |
5108
b7483e29ea8c
Fixing issue 211. Check to see if m_currPath is NULL before doing anything in mouseReleaseEvent. Multiple mouse release events can occur after a mouse press and before the next mouse press if you mouse-click really fast.
Zorg <zorgiepoo@gmail.com>
parents:
5040
diff
changeset
|
178 |
} |
4423 | 179 |
} |
4424 | 180 |
|
6781 | 181 |
void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) |
182 |
{ |
|
6784 | 183 |
if(wheelEvent->delta() > 0 && m_pen.width() < 516) |
6781 | 184 |
m_pen.setWidth(m_pen.width() + 10); |
6784 | 185 |
else if(wheelEvent->delta() < 0 && m_pen.width() >= 16) |
6781 | 186 |
m_pen.setWidth(m_pen.width() - 10); |
187 |
||
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
188 |
m_cursor->setScale(m_pen.width()); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
189 |
|
6781 | 190 |
if(m_currPath) |
191 |
{ |
|
192 |
m_currPath->setPen(m_pen); |
|
6873 | 193 |
paths.first().width = serializePenWidth(m_pen.width()); |
6781 | 194 |
} |
195 |
} |
|
196 |
||
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
197 |
void DrawMapScene::showCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
198 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
199 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
200 |
addItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
201 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
202 |
m_isCursorShown = true; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
203 |
} |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
204 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
205 |
void DrawMapScene::hideCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
206 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
207 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
208 |
removeItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
209 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
210 |
m_isCursorShown = false; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
211 |
} |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
212 |
|
4426 | 213 |
void DrawMapScene::undo() |
4424 | 214 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
215 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
216 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
217 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
218 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
219 |
if(paths.size()) |
4427 | 220 |
{ |
4426 | 221 |
removeItem(items().first()); |
4439 | 222 |
paths.removeFirst(); |
4427 | 223 |
|
224 |
emit pathChanged(); |
|
225 |
} |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
226 |
else if(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
227 |
{ |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
228 |
while(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
229 |
addItem(oldItems.takeFirst()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
230 |
paths = oldPaths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
231 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
232 |
emit pathChanged(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
233 |
} |
4424 | 234 |
} |
4427 | 235 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
236 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
237 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
238 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
239 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
240 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
241 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
242 |
// don't clear if already cleared |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
243 |
if(!items().size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
244 |
return; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
245 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
246 |
m_specialPoints.clear(); |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
247 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
248 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
249 |
// do this since clear() would _destroy_ all items |
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
250 |
for(int i = paths.size() - 1; i >= 0; --i) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
251 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
252 |
oldItems.push_front(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
253 |
removeItem(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
254 |
} |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
255 |
|
9483
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
256 |
items().clear(); |
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
257 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
258 |
oldPaths = paths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
259 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
260 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
261 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
262 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
263 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
264 |
|
6873 | 265 |
|
266 |
void DrawMapScene::setErasing(bool erasing) |
|
267 |
{ |
|
268 |
m_isErasing = erasing; |
|
269 |
if(erasing) |
|
270 |
m_pen.setBrush(m_eraser); |
|
271 |
else |
|
272 |
m_pen.setBrush(m_brush); |
|
273 |
} |
|
274 |
||
4427 | 275 |
QByteArray DrawMapScene::encode() |
276 |
{ |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
277 |
QByteArray b(m_specialPoints); |
4427 | 278 |
|
4666 | 279 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 280 |
{ |
281 |
int cnt = 0; |
|
6873 | 282 |
PathParams params = paths.at(i); |
283 |
foreach(QPoint point, params.points) |
|
4427 | 284 |
{ |
285 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
286 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
6781 | 287 |
quint8 flags = 0; |
7298
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
288 |
if(!cnt) |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
289 |
{ |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
290 |
flags = 0x80 + params.width; |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
291 |
if(params.erasing) flags |= 0x40; |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
292 |
} |
4427 | 293 |
b.append((const char *)&px, 2); |
294 |
b.append((const char *)&py, 2); |
|
4457 | 295 |
b.append((const char *)&flags, 1); |
4427 | 296 |
|
297 |
++cnt; |
|
298 |
} |
|
299 |
||
300 |
} |
|
301 |
||
302 |
return b; |
|
303 |
} |
|
4434 | 304 |
|
4442 | 305 |
void DrawMapScene::decode(QByteArray data) |
306 |
{ |
|
10235 | 307 |
hideCursor(); |
308 |
||
6873 | 309 |
bool erasing = m_isErasing; |
310 |
||
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
311 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
312 |
oldPaths.clear(); |
4442 | 313 |
clear(); |
314 |
paths.clear(); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
315 |
m_specialPoints.clear(); |
4442 | 316 |
|
6873 | 317 |
PathParams params; |
4442 | 318 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
319 |
bool isSpecial = true; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
320 |
|
4442 | 321 |
while(data.size() >= 5) |
322 |
{ |
|
323 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
324 |
data.remove(0, 2); |
|
325 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
326 |
data.remove(0, 2); |
|
4457 | 327 |
quint8 flags = *(quint8 *)data.data(); |
328 |
data.remove(0, 1); |
|
10235 | 329 |
//qDebug() << px << py; |
6781 | 330 |
if(flags & 0x80) |
4442 | 331 |
{ |
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
332 |
isSpecial = false; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
333 |
|
6873 | 334 |
if(params.points.size()) |
6781 | 335 |
{ |
6873 | 336 |
addPath(pointsToPath(params.points), m_pen); |
6781 | 337 |
|
6873 | 338 |
paths.prepend(params); |
4666 | 339 |
|
6873 | 340 |
params.points.clear(); |
6781 | 341 |
} |
342 |
||
6873 | 343 |
quint8 penWidth = flags & 0x3f; |
6781 | 344 |
m_pen.setWidth(deserializePenWidth(penWidth)); |
7146 | 345 |
params.erasing = flags & 0x40; |
346 |
if(params.erasing) |
|
6873 | 347 |
m_pen.setBrush(m_eraser); |
348 |
else |
|
349 |
m_pen.setBrush(m_brush); |
|
350 |
params.width = penWidth; |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
351 |
} else |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
352 |
if(isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
353 |
{ |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
354 |
QPainterPath path; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
355 |
path.addEllipse(QPointF(px, py), 10, 10); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
356 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
357 |
addPath(path); |
4442 | 358 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
359 |
qint16 x = qToBigEndian(px); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
360 |
qint16 y = qToBigEndian(py); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
361 |
m_specialPoints.append((const char *)&x, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
362 |
m_specialPoints.append((const char *)&y, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
363 |
m_specialPoints.append((const char *)&flags, 1); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
364 |
} |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
365 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
366 |
if(!isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
367 |
params.points.append(QPoint(px, py)); |
4666 | 368 |
} |
369 |
||
6873 | 370 |
if(params.points.size()) |
4666 | 371 |
{ |
6873 | 372 |
addPath(pointsToPath(params.points), m_pen); |
373 |
paths.prepend(params); |
|
4442 | 374 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
375 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
376 |
emit pathChanged(); |
6873 | 377 |
|
378 |
setErasing(erasing); |
|
4442 | 379 |
} |
380 |
||
4439 | 381 |
void DrawMapScene::simplifyLast() |
4434 | 382 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
383 |
if(!paths.size()) return; |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
384 |
|
6873 | 385 |
QList<QPoint> points = paths.at(0).points; |
4439 | 386 |
|
387 |
QPoint prevPoint = points.first(); |
|
388 |
int i = 1; |
|
389 |
while(i < points.size()) |
|
4434 | 390 |
{ |
10223
b6b98dfa3807
Optimize out path closing point for one point paths
unc0rr
parents:
10108
diff
changeset
|
391 |
if( ((i != points.size() - 1) || (prevPoint == points[i])) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
392 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 393 |
) |
4439 | 394 |
points.removeAt(i); |
395 |
else |
|
396 |
{ |
|
397 |
prevPoint = points[i]; |
|
398 |
++i; |
|
399 |
} |
|
400 |
} |
|
4434 | 401 |
|
6873 | 402 |
paths[0].points = points; |
4439 | 403 |
|
404 |
||
405 |
// redraw path |
|
406 |
{ |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
407 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]); |
6873 | 408 |
pathItem->setPath(pointsToPath(paths[0].points)); |
4434 | 409 |
} |
410 |
} |
|
4442 | 411 |
|
6935 | 412 |
int DrawMapScene::pointsCount() |
413 |
{ |
|
414 |
int cnt = 0; |
|
415 |
foreach(PathParams p, paths) |
|
416 |
cnt += p.points.size(); |
|
417 |
||
418 |
return cnt; |
|
419 |
} |
|
420 |
||
4442 | 421 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
422 |
{ |
|
423 |
QPainterPath path; |
|
424 |
||
425 |
if(points.size()) |
|
426 |
{ |
|
427 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
428 |
path.moveTo(p); |
|
429 |
||
430 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
431 |
path.lineTo(p); |
4442 | 432 |
} |
433 |
||
434 |
return path; |
|
435 |
} |
|
6781 | 436 |
|
437 |
quint8 DrawMapScene::serializePenWidth(int width) |
|
438 |
{ |
|
439 |
return (width - 6) / 10; |
|
440 |
} |
|
441 |
||
442 |
int DrawMapScene::deserializePenWidth(quint8 width) |
|
443 |
{ |
|
444 |
return width * 10 + 6; |
|
445 |
} |
|
9551 | 446 |
|
447 |
void DrawMapScene::setPathType(PathType pathType) |
|
448 |
{ |
|
449 |
m_pathType = pathType; |
|
450 |
} |
|
451 |
||
452 |
QList<QPointF> DrawMapScene::makeEllipse(const QPointF ¢er, const QPointF &corner) |
|
453 |
{ |
|
454 |
QList<QPointF> l; |
|
455 |
qreal rx = qAbs(center.x() - corner.x()); |
|
456 |
qreal ry = qAbs(center.y() - corner.y()); |
|
9553 | 457 |
qreal r = qMax(rx, ry); |
9551 | 458 |
|
459 |
if(r < 4) |
|
460 |
{ |
|
461 |
l.append(center); |
|
462 |
} else |
|
463 |
{ |
|
9949
2aa9cf5badfc
Added cast to qreal, avoiding ftbfs due to issue #758
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9555
diff
changeset
|
464 |
qreal angleDelta = qMax(static_cast<qreal> (0.1), qMin(static_cast<qreal> (0.7), 120 / r)); |
9551 | 465 |
for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta) |
466 |
l.append(center + QPointF(rx * cos(angle), ry * sin(angle))); |
|
467 |
l.append(l.first()); |
|
468 |
} |
|
469 |
||
470 |
return l; |
|
471 |
} |
|
9555 | 472 |
|
473 |
QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point) |
|
474 |
{ |
|
475 |
QPointF vector = point - initialPoint; |
|
476 |
||
477 |
for(int angle = 0; angle < 180; angle += 15) |
|
478 |
{ |
|
479 |
QTransform transform; |
|
480 |
transform.rotate(angle); |
|
481 |
||
482 |
QPointF rotated = transform.map(vector); |
|
483 |
||
484 |
if(rotated.x() == 0) return point; |
|
485 |
if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0)); |
|
486 |
} |
|
487 |
||
488 |
return point; |
|
489 |
} |
|
10235 | 490 |
|
491 |
void DrawMapScene::optimize() |
|
492 |
{ |
|
493 |
if(!paths.size()) return; |
|
494 |
||
495 |
// break paths into segments |
|
496 |
Paths pth; |
|
497 |
||
498 |
foreach(const PathParams & pp, paths) |
|
499 |
{ |
|
500 |
int l = pp.points.size(); |
|
501 |
||
502 |
if(l == 1) |
|
503 |
{ |
|
504 |
pth.prepend(pp); |
|
505 |
} else |
|
506 |
{ |
|
507 |
for(int i = l - 2; i >= 0; --i) |
|
508 |
{ |
|
509 |
PathParams p = pp; |
|
510 |
p.points = QList<QPoint>() << p.points[i] << p.points[i + 1]; |
|
511 |
pth.prepend(pp); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
// clear the scene |
|
517 |
oldItems.clear(); |
|
518 |
oldPaths.clear(); |
|
519 |
clear(); |
|
520 |
paths.clear(); |
|
521 |
m_specialPoints.clear(); |
|
522 |
||
523 |
// render the result |
|
524 |
foreach(const PathParams & p, pth) |
|
525 |
{ |
|
526 |
if(p.erasing) |
|
527 |
m_pen.setBrush(m_eraser); |
|
528 |
else |
|
529 |
m_pen.setBrush(m_brush); |
|
530 |
||
531 |
m_pen.setWidth(deserializePenWidth(p.width)); |
|
532 |
||
533 |
addPath(pointsToPath(p.points), m_pen); |
|
534 |
} |
|
535 |
||
536 |
emit pathChanged(); |
|
537 |
} |