author | unc0rr |
Fri, 18 Oct 2013 15:26:43 +0400 | |
changeset 9551 | 61f160dfd0f1 |
parent 9483 | 14ad1ac00ac9 |
child 9553 | f92d43816186 |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
9080 | 3 |
* Copyright (c) 2004-2013 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> |
9551 | 23 |
#include <math.h> |
4423 | 24 |
|
25 |
#include "drawmapscene.h" |
|
26 |
||
4434 | 27 |
template <class T> T sqr(const T & x) |
28 |
{ |
|
29 |
return x*x; |
|
30 |
} |
|
31 |
||
4423 | 32 |
DrawMapScene::DrawMapScene(QObject *parent) : |
4424 | 33 |
QGraphicsScene(parent), |
4426 | 34 |
m_pen(Qt::yellow), |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
35 |
m_brush(Qt::yellow), |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
36 |
m_cursor(new QGraphicsEllipseItem(-0.5, -0.5, 1, 1)) |
4423 | 37 |
{ |
38 |
setSceneRect(0, 0, 4096, 2048); |
|
4424 | 39 |
|
40 |
QLinearGradient gradient(0, 0, 0, 2048); |
|
4426 | 41 |
gradient.setColorAt(0, QColor(60, 60, 155)); |
42 |
gradient.setColorAt(1, QColor(155, 155, 60)); |
|
6873 | 43 |
|
44 |
m_eraser = QBrush(gradient); |
|
45 |
setBackgroundBrush(m_eraser); |
|
46 |
m_isErasing = false; |
|
4424 | 47 |
|
9551 | 48 |
m_pathType = Polyline; |
49 |
||
6784 | 50 |
m_pen.setWidth(76); |
4426 | 51 |
m_pen.setJoinStyle(Qt::RoundJoin); |
52 |
m_pen.setCapStyle(Qt::RoundCap); |
|
53 |
m_currPath = 0; |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
54 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
55 |
m_isCursorShown = false; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
56 |
m_cursor->setPen(QPen(Qt::green)); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
57 |
m_cursor->setZValue(1); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
58 |
m_cursor->setScale(m_pen.width()); |
4423 | 59 |
} |
60 |
||
61 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
62 |
{ |
|
4426 | 63 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
64 |
{ |
|
65 |
QPainterPath path = m_currPath->path(); |
|
4937 | 66 |
|
9551 | 67 |
switch (m_pathType) |
4937 | 68 |
{ |
9551 | 69 |
case Polyline: |
70 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
71 |
{ |
|
72 |
int c = path.elementCount(); |
|
73 |
QPointF pos = mouseEvent->scenePos(); |
|
74 |
path.setElementPositionAt(c - 1, pos.x(), pos.y()); |
|
4937 | 75 |
|
9551 | 76 |
} |
77 |
else |
|
78 |
{ |
|
79 |
path.lineTo(mouseEvent->scenePos()); |
|
80 |
paths.first().points.append(mouseEvent->scenePos().toPoint()); |
|
81 |
} |
|
82 |
break; |
|
83 |
case Rectangle: { |
|
84 |
path = QPainterPath(); |
|
85 |
QPointF p1 = paths.first().initialPoint; |
|
86 |
QPointF p2 = mouseEvent->scenePos(); |
|
87 |
path.moveTo(p1); |
|
88 |
path.lineTo(p1.x(), p2.y()); |
|
89 |
path.lineTo(p2); |
|
90 |
path.lineTo(p2.x(), p1.y()); |
|
91 |
path.lineTo(p1); |
|
92 |
break; |
|
93 |
} |
|
94 |
case Ellipse: { |
|
95 |
path = QPainterPath(); |
|
96 |
QList<QPointF> points = makeEllipse(paths.first().initialPoint, mouseEvent->scenePos()); |
|
97 |
path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points))); |
|
98 |
break; |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
99 |
} |
4937 | 100 |
} |
9551 | 101 |
|
4426 | 102 |
m_currPath->setPath(path); |
4427 | 103 |
|
104 |
emit pathChanged(); |
|
4426 | 105 |
} |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
106 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
107 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
108 |
showCursor(); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
109 |
m_cursor->setPos(mouseEvent->scenePos()); |
4423 | 110 |
} |
111 |
||
112 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
113 |
{ |
|
4426 | 114 |
m_currPath = addPath(QPainterPath(), m_pen); |
115 |
||
116 |
QPainterPath path = m_currPath->path(); |
|
117 |
QPointF p = mouseEvent->scenePos(); |
|
118 |
p += QPointF(0.01, 0.01); |
|
119 |
path.moveTo(p); |
|
120 |
path.lineTo(mouseEvent->scenePos()); |
|
6873 | 121 |
|
122 |
PathParams params; |
|
123 |
params.width = serializePenWidth(m_pen.width()); |
|
124 |
params.erasing = m_isErasing; |
|
9551 | 125 |
params.initialPoint = mouseEvent->scenePos().toPoint(); |
126 |
params.points = QList<QPoint>() << params.initialPoint; |
|
6873 | 127 |
paths.prepend(params); |
4426 | 128 |
m_currPath->setPath(path); |
129 |
||
4427 | 130 |
emit pathChanged(); |
4423 | 131 |
} |
132 |
||
133 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
134 |
{ |
|
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
|
135 |
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
|
136 |
{ |
9551 | 137 |
switch (m_pathType) |
138 |
{ |
|
139 |
case Polyline: { |
|
140 |
QPainterPath path = m_currPath->path(); |
|
141 |
path.lineTo(mouseEvent->scenePos()); |
|
142 |
paths.first().points.append(mouseEvent->scenePos().toPoint()); |
|
143 |
m_currPath->setPath(path); |
|
144 |
break; |
|
145 |
} |
|
146 |
case Rectangle: { |
|
147 |
QPoint p1 = paths.first().initialPoint; |
|
148 |
QPoint p2 = mouseEvent->scenePos().toPoint(); |
|
149 |
QList<QPoint> rpoints; |
|
150 |
rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1; |
|
151 |
paths.first().points = rpoints; |
|
152 |
break; |
|
153 |
} |
|
154 |
case Ellipse: |
|
155 |
QPoint p1 = paths.first().initialPoint; |
|
156 |
QPoint p2 = mouseEvent->scenePos().toPoint(); |
|
157 |
QList<QPointF> points = makeEllipse(p1, p2); |
|
158 |
QList<QPoint> epoints; |
|
159 |
foreach(const QPointF & p, points) |
|
160 |
epoints.append(p.toPoint()); |
|
161 |
paths.first().points = epoints; |
|
162 |
break; |
|
163 |
} |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
164 |
|
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
|
165 |
simplifyLast(); |
4439 | 166 |
|
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
|
167 |
m_currPath = 0; |
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
|
168 |
} |
4423 | 169 |
} |
4424 | 170 |
|
6781 | 171 |
void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) |
172 |
{ |
|
6784 | 173 |
if(wheelEvent->delta() > 0 && m_pen.width() < 516) |
6781 | 174 |
m_pen.setWidth(m_pen.width() + 10); |
6784 | 175 |
else if(wheelEvent->delta() < 0 && m_pen.width() >= 16) |
6781 | 176 |
m_pen.setWidth(m_pen.width() - 10); |
177 |
||
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
178 |
m_cursor->setScale(m_pen.width()); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
179 |
|
6781 | 180 |
if(m_currPath) |
181 |
{ |
|
182 |
m_currPath->setPen(m_pen); |
|
6873 | 183 |
paths.first().width = serializePenWidth(m_pen.width()); |
6781 | 184 |
} |
185 |
} |
|
186 |
||
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
187 |
void DrawMapScene::showCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
188 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
189 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
190 |
addItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
191 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
192 |
m_isCursorShown = true; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
193 |
} |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
194 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
195 |
void DrawMapScene::hideCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
196 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
197 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
198 |
removeItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
199 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
200 |
m_isCursorShown = false; |
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 |
|
4426 | 203 |
void DrawMapScene::undo() |
4424 | 204 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
205 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
206 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
207 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
208 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
209 |
if(paths.size()) |
4427 | 210 |
{ |
4426 | 211 |
removeItem(items().first()); |
4439 | 212 |
paths.removeFirst(); |
4427 | 213 |
|
214 |
emit pathChanged(); |
|
215 |
} |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
216 |
else if(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
217 |
{ |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
218 |
while(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
219 |
addItem(oldItems.takeFirst()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
220 |
paths = oldPaths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
221 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
222 |
emit pathChanged(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
223 |
} |
4424 | 224 |
} |
4427 | 225 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
226 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
227 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
228 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
229 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
230 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
231 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
232 |
// don't clear if already cleared |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
233 |
if(!items().size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
234 |
return; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
235 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
236 |
m_specialPoints.clear(); |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
237 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
238 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
239 |
// 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
|
240 |
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
|
241 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
242 |
oldItems.push_front(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
243 |
removeItem(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
244 |
} |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
245 |
|
9483
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
246 |
items().clear(); |
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
247 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
248 |
oldPaths = paths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
249 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
250 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
251 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
252 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
253 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
254 |
|
6873 | 255 |
|
256 |
void DrawMapScene::setErasing(bool erasing) |
|
257 |
{ |
|
258 |
m_isErasing = erasing; |
|
259 |
if(erasing) |
|
260 |
m_pen.setBrush(m_eraser); |
|
261 |
else |
|
262 |
m_pen.setBrush(m_brush); |
|
263 |
} |
|
264 |
||
4427 | 265 |
QByteArray DrawMapScene::encode() |
266 |
{ |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
267 |
QByteArray b(m_specialPoints); |
4427 | 268 |
|
4666 | 269 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 270 |
{ |
271 |
int cnt = 0; |
|
6873 | 272 |
PathParams params = paths.at(i); |
273 |
foreach(QPoint point, params.points) |
|
4427 | 274 |
{ |
275 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
276 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
6781 | 277 |
quint8 flags = 0; |
7298
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
278 |
if(!cnt) |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
279 |
{ |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
280 |
flags = 0x80 + params.width; |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
281 |
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
|
282 |
} |
4427 | 283 |
b.append((const char *)&px, 2); |
284 |
b.append((const char *)&py, 2); |
|
4457 | 285 |
b.append((const char *)&flags, 1); |
4427 | 286 |
|
287 |
++cnt; |
|
288 |
} |
|
289 |
||
290 |
} |
|
291 |
||
292 |
return b; |
|
293 |
} |
|
4434 | 294 |
|
4442 | 295 |
void DrawMapScene::decode(QByteArray data) |
296 |
{ |
|
6873 | 297 |
bool erasing = m_isErasing; |
298 |
||
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
299 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
300 |
oldPaths.clear(); |
4442 | 301 |
clear(); |
302 |
paths.clear(); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
303 |
m_specialPoints.clear(); |
4442 | 304 |
|
6873 | 305 |
PathParams params; |
4442 | 306 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
307 |
bool isSpecial = true; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
308 |
|
4442 | 309 |
while(data.size() >= 5) |
310 |
{ |
|
311 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
312 |
data.remove(0, 2); |
|
313 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
314 |
data.remove(0, 2); |
|
4457 | 315 |
quint8 flags = *(quint8 *)data.data(); |
316 |
data.remove(0, 1); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
317 |
qDebug() << px << py; |
6781 | 318 |
if(flags & 0x80) |
4442 | 319 |
{ |
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
320 |
isSpecial = false; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
321 |
|
6873 | 322 |
if(params.points.size()) |
6781 | 323 |
{ |
6873 | 324 |
addPath(pointsToPath(params.points), m_pen); |
6781 | 325 |
|
6873 | 326 |
paths.prepend(params); |
4666 | 327 |
|
6873 | 328 |
params.points.clear(); |
6781 | 329 |
} |
330 |
||
6873 | 331 |
quint8 penWidth = flags & 0x3f; |
6781 | 332 |
m_pen.setWidth(deserializePenWidth(penWidth)); |
7146 | 333 |
params.erasing = flags & 0x40; |
334 |
if(params.erasing) |
|
6873 | 335 |
m_pen.setBrush(m_eraser); |
336 |
else |
|
337 |
m_pen.setBrush(m_brush); |
|
338 |
params.width = penWidth; |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
339 |
} else |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
340 |
if(isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
341 |
{ |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
342 |
QPainterPath path; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
343 |
path.addEllipse(QPointF(px, py), 10, 10); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
344 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
345 |
addPath(path); |
4442 | 346 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
347 |
qint16 x = qToBigEndian(px); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
348 |
qint16 y = qToBigEndian(py); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
349 |
m_specialPoints.append((const char *)&x, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
350 |
m_specialPoints.append((const char *)&y, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
351 |
m_specialPoints.append((const char *)&flags, 1); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
352 |
} |
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 |
if(!isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
355 |
params.points.append(QPoint(px, py)); |
4666 | 356 |
} |
357 |
||
6873 | 358 |
if(params.points.size()) |
4666 | 359 |
{ |
6873 | 360 |
addPath(pointsToPath(params.points), m_pen); |
361 |
paths.prepend(params); |
|
4442 | 362 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
363 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
364 |
emit pathChanged(); |
6873 | 365 |
|
366 |
setErasing(erasing); |
|
4442 | 367 |
} |
368 |
||
4439 | 369 |
void DrawMapScene::simplifyLast() |
4434 | 370 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
371 |
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
|
372 |
|
6873 | 373 |
QList<QPoint> points = paths.at(0).points; |
4439 | 374 |
|
375 |
QPoint prevPoint = points.first(); |
|
376 |
int i = 1; |
|
377 |
while(i < points.size()) |
|
4434 | 378 |
{ |
4471 | 379 |
if( (i != points.size() - 1) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
380 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 381 |
) |
4439 | 382 |
points.removeAt(i); |
383 |
else |
|
384 |
{ |
|
385 |
prevPoint = points[i]; |
|
386 |
++i; |
|
387 |
} |
|
388 |
} |
|
4434 | 389 |
|
6873 | 390 |
paths[0].points = points; |
4439 | 391 |
|
392 |
||
393 |
// redraw path |
|
394 |
{ |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
395 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]); |
6873 | 396 |
pathItem->setPath(pointsToPath(paths[0].points)); |
4434 | 397 |
} |
398 |
||
399 |
emit pathChanged(); |
|
400 |
} |
|
4442 | 401 |
|
6935 | 402 |
int DrawMapScene::pointsCount() |
403 |
{ |
|
404 |
int cnt = 0; |
|
405 |
foreach(PathParams p, paths) |
|
406 |
cnt += p.points.size(); |
|
407 |
||
408 |
return cnt; |
|
409 |
} |
|
410 |
||
4442 | 411 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
412 |
{ |
|
413 |
QPainterPath path; |
|
414 |
||
415 |
if(points.size()) |
|
416 |
{ |
|
417 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
418 |
path.moveTo(p); |
|
419 |
||
420 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
421 |
path.lineTo(p); |
4442 | 422 |
} |
423 |
||
424 |
return path; |
|
425 |
} |
|
6781 | 426 |
|
427 |
quint8 DrawMapScene::serializePenWidth(int width) |
|
428 |
{ |
|
429 |
return (width - 6) / 10; |
|
430 |
} |
|
431 |
||
432 |
int DrawMapScene::deserializePenWidth(quint8 width) |
|
433 |
{ |
|
434 |
return width * 10 + 6; |
|
435 |
} |
|
9551 | 436 |
|
437 |
void DrawMapScene::setPathType(PathType pathType) |
|
438 |
{ |
|
439 |
m_pathType = pathType; |
|
440 |
} |
|
441 |
||
442 |
QList<QPointF> DrawMapScene::makeEllipse(const QPointF ¢er, const QPointF &corner) |
|
443 |
{ |
|
444 |
QList<QPointF> l; |
|
445 |
qreal r = (center - corner).manhattanLength(); |
|
446 |
qreal rx = qAbs(center.x() - corner.x()); |
|
447 |
qreal ry = qAbs(center.y() - corner.y()); |
|
448 |
||
449 |
if(r < 4) |
|
450 |
{ |
|
451 |
l.append(center); |
|
452 |
} else |
|
453 |
{ |
|
454 |
qreal angleDelta = 12 / r; |
|
455 |
for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta) |
|
456 |
l.append(center + QPointF(rx * cos(angle), ry * sin(angle))); |
|
457 |
l.append(l.first()); |
|
458 |
} |
|
459 |
||
460 |
return l; |
|
461 |
} |