author | unc0rr |
Fri, 13 Apr 2012 00:08:46 +0400 | |
changeset 6883 | 70aec33185e2 |
parent 6873 | 30840365af0a |
child 6934 | 14a230552c2e |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2012 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), |
34 |
m_brush(Qt::yellow) |
|
4423 | 35 |
{ |
36 |
setSceneRect(0, 0, 4096, 2048); |
|
4424 | 37 |
|
38 |
QLinearGradient gradient(0, 0, 0, 2048); |
|
4426 | 39 |
gradient.setColorAt(0, QColor(60, 60, 155)); |
40 |
gradient.setColorAt(1, QColor(155, 155, 60)); |
|
6873 | 41 |
|
42 |
m_eraser = QBrush(gradient); |
|
43 |
setBackgroundBrush(m_eraser); |
|
44 |
m_isErasing = false; |
|
4424 | 45 |
|
6784 | 46 |
m_pen.setWidth(76); |
4426 | 47 |
m_pen.setJoinStyle(Qt::RoundJoin); |
48 |
m_pen.setCapStyle(Qt::RoundCap); |
|
49 |
m_currPath = 0; |
|
4423 | 50 |
} |
51 |
||
52 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
53 |
{ |
|
4426 | 54 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
55 |
{ |
|
56 |
QPainterPath path = m_currPath->path(); |
|
4937 | 57 |
|
4938
0985edac2ad7
use ctrl instead of alt to avoid colliding with linux window positioning
nemo
parents:
4937
diff
changeset
|
58 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
4937 | 59 |
{ |
60 |
int c = path.elementCount(); |
|
61 |
QPointF pos = mouseEvent->scenePos(); |
|
62 |
path.setElementPositionAt(c - 1, pos.x(), pos.y()); |
|
63 |
||
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
64 |
} |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
65 |
else |
4937 | 66 |
{ |
67 |
path.lineTo(mouseEvent->scenePos()); |
|
6873 | 68 |
paths.first().points.append(mouseEvent->scenePos().toPoint()); |
4937 | 69 |
} |
4426 | 70 |
m_currPath->setPath(path); |
4427 | 71 |
|
72 |
emit pathChanged(); |
|
4426 | 73 |
} |
4423 | 74 |
} |
75 |
||
76 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
77 |
{ |
|
4426 | 78 |
m_currPath = addPath(QPainterPath(), m_pen); |
79 |
||
80 |
QPainterPath path = m_currPath->path(); |
|
81 |
QPointF p = mouseEvent->scenePos(); |
|
82 |
p += QPointF(0.01, 0.01); |
|
83 |
path.moveTo(p); |
|
84 |
path.lineTo(mouseEvent->scenePos()); |
|
6873 | 85 |
|
86 |
PathParams params; |
|
87 |
params.width = serializePenWidth(m_pen.width()); |
|
88 |
params.erasing = m_isErasing; |
|
89 |
params.points = QList<QPoint>() << mouseEvent->scenePos().toPoint(); |
|
90 |
paths.prepend(params); |
|
4426 | 91 |
m_currPath->setPath(path); |
92 |
||
4427 | 93 |
emit pathChanged(); |
4423 | 94 |
} |
95 |
||
96 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
97 |
{ |
|
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
|
98 |
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
|
99 |
{ |
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
|
100 |
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
|
101 |
path.lineTo(mouseEvent->scenePos()); |
6873 | 102 |
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
|
103 |
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
|
104 |
|
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
|
105 |
simplifyLast(); |
4439 | 106 |
|
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
|
107 |
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
|
108 |
} |
4423 | 109 |
} |
4424 | 110 |
|
6781 | 111 |
void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) |
112 |
{ |
|
6784 | 113 |
if(wheelEvent->delta() > 0 && m_pen.width() < 516) |
6781 | 114 |
m_pen.setWidth(m_pen.width() + 10); |
6784 | 115 |
else if(wheelEvent->delta() < 0 && m_pen.width() >= 16) |
6781 | 116 |
m_pen.setWidth(m_pen.width() - 10); |
117 |
||
118 |
if(m_currPath) |
|
119 |
{ |
|
120 |
m_currPath->setPen(m_pen); |
|
6873 | 121 |
paths.first().width = serializePenWidth(m_pen.width()); |
6781 | 122 |
} |
123 |
} |
|
124 |
||
4426 | 125 |
void DrawMapScene::undo() |
4424 | 126 |
{ |
4426 | 127 |
if(items().size()) |
4427 | 128 |
{ |
4426 | 129 |
removeItem(items().first()); |
4439 | 130 |
paths.removeFirst(); |
4427 | 131 |
|
132 |
emit pathChanged(); |
|
133 |
} |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
134 |
else if(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
135 |
{ |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
136 |
while(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
137 |
addItem(oldItems.takeFirst()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
138 |
paths = oldPaths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
139 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
140 |
emit pathChanged(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
141 |
} |
4424 | 142 |
} |
4427 | 143 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
144 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
145 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
146 |
// don't clear if already cleared |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
147 |
if(!items().size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
148 |
return; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
149 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
150 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
151 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
152 |
// do this since clear() would _destroy_ all items |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
153 |
while(items().size()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
154 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
155 |
oldItems.push_front(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
156 |
removeItem(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
157 |
} |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
158 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
159 |
oldPaths = paths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
160 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
161 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
162 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
163 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
164 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
165 |
|
6873 | 166 |
|
167 |
void DrawMapScene::setErasing(bool erasing) |
|
168 |
{ |
|
169 |
m_isErasing = erasing; |
|
170 |
if(erasing) |
|
171 |
m_pen.setBrush(m_eraser); |
|
172 |
else |
|
173 |
m_pen.setBrush(m_brush); |
|
174 |
} |
|
175 |
||
4427 | 176 |
QByteArray DrawMapScene::encode() |
177 |
{ |
|
178 |
QByteArray b; |
|
179 |
||
4666 | 180 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 181 |
{ |
182 |
int cnt = 0; |
|
6873 | 183 |
PathParams params = paths.at(i); |
184 |
foreach(QPoint point, params.points) |
|
4427 | 185 |
{ |
186 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
187 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
6781 | 188 |
quint8 flags = 0; |
6873 | 189 |
if(!cnt) flags = 0x80 + params.width; |
190 |
if(params.erasing) flags |= 0x40; |
|
4427 | 191 |
b.append((const char *)&px, 2); |
192 |
b.append((const char *)&py, 2); |
|
4457 | 193 |
b.append((const char *)&flags, 1); |
4427 | 194 |
|
195 |
++cnt; |
|
196 |
} |
|
197 |
||
198 |
} |
|
199 |
||
200 |
return b; |
|
201 |
} |
|
4434 | 202 |
|
4442 | 203 |
void DrawMapScene::decode(QByteArray data) |
204 |
{ |
|
6873 | 205 |
bool erasing = m_isErasing; |
206 |
||
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
207 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
208 |
oldPaths.clear(); |
4442 | 209 |
clear(); |
210 |
paths.clear(); |
|
211 |
||
6873 | 212 |
PathParams params; |
4442 | 213 |
|
214 |
while(data.size() >= 5) |
|
215 |
{ |
|
216 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
217 |
data.remove(0, 2); |
|
218 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
219 |
data.remove(0, 2); |
|
4457 | 220 |
quint8 flags = *(quint8 *)data.data(); |
221 |
data.remove(0, 1); |
|
4442 | 222 |
|
6781 | 223 |
if(flags & 0x80) |
4442 | 224 |
{ |
6873 | 225 |
if(params.points.size()) |
6781 | 226 |
{ |
6873 | 227 |
addPath(pointsToPath(params.points), m_pen); |
6781 | 228 |
|
6873 | 229 |
paths.prepend(params); |
4666 | 230 |
|
6873 | 231 |
params.points.clear(); |
6781 | 232 |
} |
233 |
||
6873 | 234 |
quint8 penWidth = flags & 0x3f; |
6781 | 235 |
m_pen.setWidth(deserializePenWidth(penWidth)); |
6873 | 236 |
if(flags & 0x40) |
237 |
m_pen.setBrush(m_eraser); |
|
238 |
else |
|
239 |
m_pen.setBrush(m_brush); |
|
240 |
params.width = penWidth; |
|
4666 | 241 |
} |
4442 | 242 |
|
6873 | 243 |
params.points.append(QPoint(px, py)); |
4666 | 244 |
} |
245 |
||
6873 | 246 |
if(params.points.size()) |
4666 | 247 |
{ |
6873 | 248 |
addPath(pointsToPath(params.points), m_pen); |
249 |
paths.prepend(params); |
|
4442 | 250 |
} |
4560
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(); |
6873 | 253 |
|
254 |
setErasing(erasing); |
|
4442 | 255 |
} |
256 |
||
4439 | 257 |
void DrawMapScene::simplifyLast() |
4434 | 258 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
259 |
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
|
260 |
|
6873 | 261 |
QList<QPoint> points = paths.at(0).points; |
4439 | 262 |
|
263 |
QPoint prevPoint = points.first(); |
|
264 |
int i = 1; |
|
265 |
while(i < points.size()) |
|
4434 | 266 |
{ |
4471 | 267 |
if( (i != points.size() - 1) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
268 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 269 |
) |
4439 | 270 |
points.removeAt(i); |
271 |
else |
|
272 |
{ |
|
273 |
prevPoint = points[i]; |
|
274 |
++i; |
|
275 |
} |
|
276 |
} |
|
4434 | 277 |
|
6873 | 278 |
paths[0].points = points; |
4439 | 279 |
|
280 |
||
281 |
// redraw path |
|
282 |
{ |
|
283 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
|
6873 | 284 |
pathItem->setPath(pointsToPath(paths[0].points)); |
4434 | 285 |
} |
286 |
||
287 |
emit pathChanged(); |
|
288 |
} |
|
4442 | 289 |
|
290 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
|
291 |
{ |
|
292 |
QPainterPath path; |
|
293 |
||
294 |
if(points.size()) |
|
295 |
{ |
|
296 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
297 |
path.moveTo(p); |
|
298 |
||
299 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
300 |
path.lineTo(p); |
4442 | 301 |
} |
302 |
||
303 |
return path; |
|
304 |
} |
|
6781 | 305 |
|
306 |
quint8 DrawMapScene::serializePenWidth(int width) |
|
307 |
{ |
|
308 |
return (width - 6) / 10; |
|
309 |
} |
|
310 |
||
311 |
int DrawMapScene::deserializePenWidth(quint8 width) |
|
312 |
{ |
|
313 |
return width * 10 + 6; |
|
314 |
} |