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