author | koda |
Tue, 07 Feb 2012 00:34:25 +0100 | |
changeset 6634 | e00762923086 |
parent 6616 | f77bb02b669f |
child 6700 | e04da46ee43c |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
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 |
||
4426 | 43 |
m_pen.setWidth(67); |
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()); |
|
65 |
paths.first().append(mouseEvent->scenePos().toPoint()); |
|
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()); |
|
4439 | 82 |
paths.prepend(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()); |
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
|
94 |
paths.first().append(mouseEvent->scenePos().toPoint()); |
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 |
|
4426 | 103 |
void DrawMapScene::undo() |
4424 | 104 |
{ |
4426 | 105 |
if(items().size()) |
4427 | 106 |
{ |
4426 | 107 |
removeItem(items().first()); |
4439 | 108 |
paths.removeFirst(); |
4427 | 109 |
|
110 |
emit pathChanged(); |
|
111 |
} |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
112 |
else if(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
113 |
{ |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
114 |
while(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
115 |
addItem(oldItems.takeFirst()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
116 |
paths = oldPaths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
117 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
118 |
emit pathChanged(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
119 |
} |
4424 | 120 |
} |
4427 | 121 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
122 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
123 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
124 |
// don't clear if already cleared |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
125 |
if(!items().size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
126 |
return; |
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 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
129 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
130 |
// 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
|
131 |
while(items().size()) |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
132 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
133 |
oldItems.push_front(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
134 |
removeItem(items().first()); |
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 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
137 |
oldPaths = paths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
138 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
139 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
140 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
141 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
142 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
143 |
|
4427 | 144 |
QByteArray DrawMapScene::encode() |
145 |
{ |
|
146 |
QByteArray b; |
|
147 |
||
4666 | 148 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 149 |
{ |
150 |
int cnt = 0; |
|
4666 | 151 |
QList<QPoint> points = paths.at(i); |
4427 | 152 |
foreach(QPoint point, points) |
153 |
{ |
|
154 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
155 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
156 |
quint8 flags = 2; |
|
4442 | 157 |
if(!cnt) flags |= 0x80; |
4427 | 158 |
b.append((const char *)&px, 2); |
159 |
b.append((const char *)&py, 2); |
|
4457 | 160 |
b.append((const char *)&flags, 1); |
4427 | 161 |
|
162 |
++cnt; |
|
163 |
} |
|
164 |
||
165 |
} |
|
166 |
||
167 |
return b; |
|
168 |
} |
|
4434 | 169 |
|
4442 | 170 |
void DrawMapScene::decode(QByteArray data) |
171 |
{ |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
172 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
173 |
oldPaths.clear(); |
4442 | 174 |
clear(); |
175 |
paths.clear(); |
|
176 |
||
177 |
QList<QPoint> points; |
|
178 |
||
179 |
while(data.size() >= 5) |
|
180 |
{ |
|
181 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
182 |
data.remove(0, 2); |
|
183 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
184 |
data.remove(0, 2); |
|
4457 | 185 |
quint8 flags = *(quint8 *)data.data(); |
186 |
data.remove(0, 1); |
|
4442 | 187 |
|
4666 | 188 |
if((flags & 0x80) && points.size()) |
4442 | 189 |
{ |
4666 | 190 |
addPath(pointsToPath(points), m_pen); |
191 |
paths.prepend(points); |
|
192 |
||
193 |
points.clear(); |
|
194 |
} |
|
4442 | 195 |
|
4666 | 196 |
points.append(QPoint(px, py)); |
197 |
} |
|
198 |
||
199 |
if(points.size()) |
|
200 |
{ |
|
201 |
addPath(pointsToPath(points), m_pen); |
|
202 |
paths.prepend(points); |
|
4442 | 203 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
204 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
205 |
emit pathChanged(); |
4442 | 206 |
} |
207 |
||
4439 | 208 |
void DrawMapScene::simplifyLast() |
4434 | 209 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
210 |
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
|
211 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
212 |
QList<QPoint> points = paths.at(0); |
4439 | 213 |
|
214 |
QPoint prevPoint = points.first(); |
|
215 |
int i = 1; |
|
216 |
while(i < points.size()) |
|
4434 | 217 |
{ |
4471 | 218 |
if( (i != points.size() - 1) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
219 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 220 |
) |
4439 | 221 |
points.removeAt(i); |
222 |
else |
|
223 |
{ |
|
224 |
prevPoint = points[i]; |
|
225 |
++i; |
|
226 |
} |
|
227 |
} |
|
4434 | 228 |
|
4439 | 229 |
paths[0] = points; |
230 |
||
231 |
||
232 |
// redraw path |
|
233 |
{ |
|
234 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
|
4442 | 235 |
pathItem->setPath(pointsToPath(paths[0])); |
4434 | 236 |
} |
237 |
||
238 |
emit pathChanged(); |
|
239 |
} |
|
4442 | 240 |
|
241 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
|
242 |
{ |
|
243 |
QPainterPath path; |
|
244 |
||
245 |
if(points.size()) |
|
246 |
{ |
|
247 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
248 |
path.moveTo(p); |
|
249 |
||
250 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
251 |
path.lineTo(p); |
4442 | 252 |
} |
253 |
||
254 |
return path; |
|
255 |
} |