author | nemo |
Wed, 31 Aug 2011 11:08:03 -0400 | |
changeset 5716 | 9505a6f3fe05 |
parent 5108 | b7483e29ea8c |
child 5858 | 1f4a8cf9efcb |
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 |
||
61 |
} else |
|
62 |
{ |
|
63 |
path.lineTo(mouseEvent->scenePos()); |
|
64 |
paths.first().append(mouseEvent->scenePos().toPoint()); |
|
65 |
} |
|
4426 | 66 |
m_currPath->setPath(path); |
4427 | 67 |
|
68 |
emit pathChanged(); |
|
4426 | 69 |
} |
4423 | 70 |
} |
71 |
||
72 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
73 |
{ |
|
4426 | 74 |
m_currPath = addPath(QPainterPath(), m_pen); |
75 |
||
76 |
QPainterPath path = m_currPath->path(); |
|
77 |
QPointF p = mouseEvent->scenePos(); |
|
78 |
p += QPointF(0.01, 0.01); |
|
79 |
path.moveTo(p); |
|
80 |
path.lineTo(mouseEvent->scenePos()); |
|
4439 | 81 |
paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint()); |
4426 | 82 |
m_currPath->setPath(path); |
83 |
||
4427 | 84 |
emit pathChanged(); |
4423 | 85 |
} |
86 |
||
87 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
88 |
{ |
|
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
|
89 |
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
|
90 |
{ |
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 |
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
|
92 |
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
|
93 |
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
|
94 |
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
|
95 |
|
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
|
96 |
simplifyLast(); |
4439 | 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 |
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
|
99 |
} |
4423 | 100 |
} |
4424 | 101 |
|
4426 | 102 |
void DrawMapScene::undo() |
4424 | 103 |
{ |
4426 | 104 |
if(items().size()) |
4427 | 105 |
{ |
4426 | 106 |
removeItem(items().first()); |
4439 | 107 |
paths.removeFirst(); |
4427 | 108 |
|
109 |
emit pathChanged(); |
|
110 |
} |
|
4424 | 111 |
} |
4427 | 112 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
113 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
114 |
{ |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
115 |
clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
116 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
117 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
118 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
119 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
120 |
|
4427 | 121 |
QByteArray DrawMapScene::encode() |
122 |
{ |
|
123 |
QByteArray b; |
|
124 |
||
4666 | 125 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 126 |
{ |
127 |
int cnt = 0; |
|
4666 | 128 |
QList<QPoint> points = paths.at(i); |
4427 | 129 |
foreach(QPoint point, points) |
130 |
{ |
|
131 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
132 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
133 |
quint8 flags = 2; |
|
4442 | 134 |
if(!cnt) flags |= 0x80; |
4427 | 135 |
b.append((const char *)&px, 2); |
136 |
b.append((const char *)&py, 2); |
|
4457 | 137 |
b.append((const char *)&flags, 1); |
4427 | 138 |
|
139 |
++cnt; |
|
140 |
} |
|
141 |
||
142 |
} |
|
143 |
||
144 |
return b; |
|
145 |
} |
|
4434 | 146 |
|
4442 | 147 |
void DrawMapScene::decode(QByteArray data) |
148 |
{ |
|
149 |
clear(); |
|
150 |
paths.clear(); |
|
151 |
||
152 |
QList<QPoint> points; |
|
153 |
||
154 |
while(data.size() >= 5) |
|
155 |
{ |
|
156 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
157 |
data.remove(0, 2); |
|
158 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
159 |
data.remove(0, 2); |
|
4457 | 160 |
quint8 flags = *(quint8 *)data.data(); |
161 |
data.remove(0, 1); |
|
4442 | 162 |
|
4666 | 163 |
if((flags & 0x80) && points.size()) |
4442 | 164 |
{ |
4666 | 165 |
addPath(pointsToPath(points), m_pen); |
166 |
paths.prepend(points); |
|
167 |
||
168 |
points.clear(); |
|
169 |
} |
|
4442 | 170 |
|
4666 | 171 |
points.append(QPoint(px, py)); |
172 |
} |
|
173 |
||
174 |
if(points.size()) |
|
175 |
{ |
|
176 |
addPath(pointsToPath(points), m_pen); |
|
177 |
paths.prepend(points); |
|
4442 | 178 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
179 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
180 |
emit pathChanged(); |
4442 | 181 |
} |
182 |
||
4439 | 183 |
void DrawMapScene::simplifyLast() |
4434 | 184 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
185 |
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
|
186 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
187 |
QList<QPoint> points = paths.at(0); |
4439 | 188 |
|
189 |
QPoint prevPoint = points.first(); |
|
190 |
int i = 1; |
|
191 |
while(i < points.size()) |
|
4434 | 192 |
{ |
4471 | 193 |
if( (i != points.size() - 1) |
194 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
|
195 |
) |
|
4439 | 196 |
points.removeAt(i); |
197 |
else |
|
198 |
{ |
|
199 |
prevPoint = points[i]; |
|
200 |
++i; |
|
201 |
} |
|
202 |
} |
|
4434 | 203 |
|
4439 | 204 |
paths[0] = points; |
205 |
||
206 |
||
207 |
// redraw path |
|
208 |
{ |
|
209 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]); |
|
4442 | 210 |
pathItem->setPath(pointsToPath(paths[0])); |
4434 | 211 |
} |
212 |
||
213 |
emit pathChanged(); |
|
214 |
} |
|
4442 | 215 |
|
216 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
|
217 |
{ |
|
218 |
QPainterPath path; |
|
219 |
||
220 |
if(points.size()) |
|
221 |
{ |
|
222 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
223 |
path.moveTo(p); |
|
224 |
||
225 |
foreach(QPoint p, points) |
|
226 |
path.lineTo(p); |
|
227 |
} |
|
228 |
||
229 |
return path; |
|
230 |
} |