author | unc0rr |
Fri, 24 Jan 2014 10:39:41 +0400 | |
changeset 10067 | 8df84ec62600 |
parent 10002 | 29acde6f200c |
child 10108 | c68cf030eded |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
9998 | 3 |
* Copyright (c) 2004-2014 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 |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
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 |
{ |
|
6873 | 307 |
bool erasing = m_isErasing; |
308 |
||
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
309 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
310 |
oldPaths.clear(); |
4442 | 311 |
clear(); |
312 |
paths.clear(); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
313 |
m_specialPoints.clear(); |
4442 | 314 |
|
6873 | 315 |
PathParams params; |
4442 | 316 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
317 |
bool isSpecial = true; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
318 |
|
4442 | 319 |
while(data.size() >= 5) |
320 |
{ |
|
321 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
322 |
data.remove(0, 2); |
|
323 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
324 |
data.remove(0, 2); |
|
4457 | 325 |
quint8 flags = *(quint8 *)data.data(); |
326 |
data.remove(0, 1); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
327 |
qDebug() << px << py; |
6781 | 328 |
if(flags & 0x80) |
4442 | 329 |
{ |
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
330 |
isSpecial = false; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
331 |
|
6873 | 332 |
if(params.points.size()) |
6781 | 333 |
{ |
6873 | 334 |
addPath(pointsToPath(params.points), m_pen); |
6781 | 335 |
|
6873 | 336 |
paths.prepend(params); |
4666 | 337 |
|
6873 | 338 |
params.points.clear(); |
6781 | 339 |
} |
340 |
||
6873 | 341 |
quint8 penWidth = flags & 0x3f; |
6781 | 342 |
m_pen.setWidth(deserializePenWidth(penWidth)); |
7146 | 343 |
params.erasing = flags & 0x40; |
344 |
if(params.erasing) |
|
6873 | 345 |
m_pen.setBrush(m_eraser); |
346 |
else |
|
347 |
m_pen.setBrush(m_brush); |
|
348 |
params.width = penWidth; |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
349 |
} else |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
350 |
if(isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
351 |
{ |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
352 |
QPainterPath path; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
353 |
path.addEllipse(QPointF(px, py), 10, 10); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
354 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
355 |
addPath(path); |
4442 | 356 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
357 |
qint16 x = qToBigEndian(px); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
358 |
qint16 y = qToBigEndian(py); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
359 |
m_specialPoints.append((const char *)&x, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
360 |
m_specialPoints.append((const char *)&y, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
361 |
m_specialPoints.append((const char *)&flags, 1); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
362 |
} |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
363 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
364 |
if(!isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
365 |
params.points.append(QPoint(px, py)); |
4666 | 366 |
} |
367 |
||
6873 | 368 |
if(params.points.size()) |
4666 | 369 |
{ |
6873 | 370 |
addPath(pointsToPath(params.points), m_pen); |
371 |
paths.prepend(params); |
|
4442 | 372 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
373 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
374 |
emit pathChanged(); |
6873 | 375 |
|
376 |
setErasing(erasing); |
|
4442 | 377 |
} |
378 |
||
4439 | 379 |
void DrawMapScene::simplifyLast() |
4434 | 380 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
381 |
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
|
382 |
|
6873 | 383 |
QList<QPoint> points = paths.at(0).points; |
4439 | 384 |
|
385 |
QPoint prevPoint = points.first(); |
|
386 |
int i = 1; |
|
387 |
while(i < points.size()) |
|
4434 | 388 |
{ |
4471 | 389 |
if( (i != points.size() - 1) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
390 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 391 |
) |
4439 | 392 |
points.removeAt(i); |
393 |
else |
|
394 |
{ |
|
395 |
prevPoint = points[i]; |
|
396 |
++i; |
|
397 |
} |
|
398 |
} |
|
4434 | 399 |
|
6873 | 400 |
paths[0].points = points; |
4439 | 401 |
|
402 |
||
403 |
// redraw path |
|
404 |
{ |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
405 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]); |
6873 | 406 |
pathItem->setPath(pointsToPath(paths[0].points)); |
4434 | 407 |
} |
408 |
} |
|
4442 | 409 |
|
6935 | 410 |
int DrawMapScene::pointsCount() |
411 |
{ |
|
412 |
int cnt = 0; |
|
413 |
foreach(PathParams p, paths) |
|
414 |
cnt += p.points.size(); |
|
415 |
||
416 |
return cnt; |
|
417 |
} |
|
418 |
||
4442 | 419 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
420 |
{ |
|
421 |
QPainterPath path; |
|
422 |
||
423 |
if(points.size()) |
|
424 |
{ |
|
425 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
426 |
path.moveTo(p); |
|
427 |
||
428 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
429 |
path.lineTo(p); |
4442 | 430 |
} |
431 |
||
432 |
return path; |
|
433 |
} |
|
6781 | 434 |
|
435 |
quint8 DrawMapScene::serializePenWidth(int width) |
|
436 |
{ |
|
437 |
return (width - 6) / 10; |
|
438 |
} |
|
439 |
||
440 |
int DrawMapScene::deserializePenWidth(quint8 width) |
|
441 |
{ |
|
442 |
return width * 10 + 6; |
|
443 |
} |
|
9551 | 444 |
|
445 |
void DrawMapScene::setPathType(PathType pathType) |
|
446 |
{ |
|
447 |
m_pathType = pathType; |
|
448 |
} |
|
449 |
||
450 |
QList<QPointF> DrawMapScene::makeEllipse(const QPointF ¢er, const QPointF &corner) |
|
451 |
{ |
|
452 |
QList<QPointF> l; |
|
453 |
qreal rx = qAbs(center.x() - corner.x()); |
|
454 |
qreal ry = qAbs(center.y() - corner.y()); |
|
9553 | 455 |
qreal r = qMax(rx, ry); |
9551 | 456 |
|
457 |
if(r < 4) |
|
458 |
{ |
|
459 |
l.append(center); |
|
460 |
} else |
|
461 |
{ |
|
9949
2aa9cf5badfc
Added cast to qreal, avoiding ftbfs due to issue #758
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9555
diff
changeset
|
462 |
qreal angleDelta = qMax(static_cast<qreal> (0.1), qMin(static_cast<qreal> (0.7), 120 / r)); |
9551 | 463 |
for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta) |
464 |
l.append(center + QPointF(rx * cos(angle), ry * sin(angle))); |
|
465 |
l.append(l.first()); |
|
466 |
} |
|
467 |
||
468 |
return l; |
|
469 |
} |
|
9555 | 470 |
|
471 |
QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point) |
|
472 |
{ |
|
473 |
QPointF vector = point - initialPoint; |
|
474 |
||
475 |
for(int angle = 0; angle < 180; angle += 15) |
|
476 |
{ |
|
477 |
QTransform transform; |
|
478 |
transform.rotate(angle); |
|
479 |
||
480 |
QPointF rotated = transform.map(vector); |
|
481 |
||
482 |
if(rotated.x() == 0) return point; |
|
483 |
if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0)); |
|
484 |
} |
|
485 |
||
486 |
return point; |
|
487 |
} |