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