author | Wuzzy <Wuzzy2@mail.ru> |
Wed, 14 Mar 2018 20:00:09 +0100 | |
changeset 13204 | 9565569e410c |
parent 13203 | eaa494f0b19e |
child 13209 | f5d36be88c61 |
permissions | -rw-r--r-- |
4976 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
10002
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
4976 | 17 |
*/ |
18 |
||
4423 | 19 |
#include <QGraphicsSceneMouseEvent> |
4426 | 20 |
#include <QGraphicsPathItem> |
4427 | 21 |
#include <QtEndian> |
4937 | 22 |
#include <QDebug> |
9555 | 23 |
#include <QTransform> |
9551 | 24 |
#include <math.h> |
4423 | 25 |
|
26 |
#include "drawmapscene.h" |
|
27 |
||
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
28 |
#define DRAWN_MAP_COLOR_LAND (Qt::yellow) |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
29 |
#define DRAWN_MAP_COLOR_CURSOR_PEN (Qt::green) |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
30 |
#define DRAWN_MAP_COLOR_CURSOR_ERASER (Qt::red) |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
31 |
|
4434 | 32 |
template <class T> T sqr(const T & x) |
33 |
{ |
|
34 |
return x*x; |
|
35 |
} |
|
36 |
||
4423 | 37 |
DrawMapScene::DrawMapScene(QObject *parent) : |
4424 | 38 |
QGraphicsScene(parent), |
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
39 |
m_pen(DRAWN_MAP_COLOR_LAND), |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
40 |
m_brush(DRAWN_MAP_COLOR_LAND), |
13203
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
41 |
m_cursor(new QGraphicsEllipseItem(-5, -5, 5, 5)) |
4423 | 42 |
{ |
43 |
setSceneRect(0, 0, 4096, 2048); |
|
4424 | 44 |
|
45 |
QLinearGradient gradient(0, 0, 0, 2048); |
|
4426 | 46 |
gradient.setColorAt(0, QColor(60, 60, 155)); |
47 |
gradient.setColorAt(1, QColor(155, 155, 60)); |
|
6873 | 48 |
|
49 |
m_eraser = QBrush(gradient); |
|
50 |
setBackgroundBrush(m_eraser); |
|
51 |
m_isErasing = false; |
|
4424 | 52 |
|
9551 | 53 |
m_pathType = Polyline; |
54 |
||
6784 | 55 |
m_pen.setWidth(76); |
4426 | 56 |
m_pen.setJoinStyle(Qt::RoundJoin); |
57 |
m_pen.setCapStyle(Qt::RoundCap); |
|
58 |
m_currPath = 0; |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
59 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
60 |
m_isCursorShown = false; |
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
61 |
QPen cursorPen = QPen(DRAWN_MAP_COLOR_CURSOR_PEN); |
13203
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
62 |
cursorPen.setJoinStyle(Qt::RoundJoin); |
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
63 |
cursorPen.setCapStyle(Qt::RoundCap); |
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
64 |
cursorPen.setWidth(m_pen.width()); |
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
65 |
m_cursor->setPen(cursorPen); |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
66 |
m_cursor->setZValue(1); |
4423 | 67 |
} |
68 |
||
69 |
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
70 |
{ |
|
4426 | 71 |
if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton)) |
72 |
{ |
|
73 |
QPainterPath path = m_currPath->path(); |
|
9555 | 74 |
QPointF currentPos = mouseEvent->scenePos(); |
75 |
||
76 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
77 |
currentPos = putSomeConstraints(paths.first().initialPoint, currentPos); |
|
4937 | 78 |
|
9551 | 79 |
switch (m_pathType) |
4937 | 80 |
{ |
9551 | 81 |
case Polyline: |
82 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
83 |
{ |
|
84 |
int c = path.elementCount(); |
|
9555 | 85 |
path.setElementPositionAt(c - 1, currentPos.x(), currentPos.y()); |
4937 | 86 |
|
9551 | 87 |
} |
88 |
else |
|
89 |
{ |
|
9555 | 90 |
path.lineTo(currentPos); |
9551 | 91 |
paths.first().points.append(mouseEvent->scenePos().toPoint()); |
92 |
} |
|
93 |
break; |
|
94 |
case Rectangle: { |
|
95 |
path = QPainterPath(); |
|
96 |
QPointF p1 = paths.first().initialPoint; |
|
9555 | 97 |
QPointF p2 = currentPos; |
9551 | 98 |
path.moveTo(p1); |
99 |
path.lineTo(p1.x(), p2.y()); |
|
100 |
path.lineTo(p2); |
|
101 |
path.lineTo(p2.x(), p1.y()); |
|
102 |
path.lineTo(p1); |
|
103 |
break; |
|
104 |
} |
|
105 |
case Ellipse: { |
|
106 |
path = QPainterPath(); |
|
9555 | 107 |
QList<QPointF> points = makeEllipse(paths.first().initialPoint, currentPos); |
9551 | 108 |
path.addPolygon(QPolygonF(QVector<QPointF>::fromList(points))); |
109 |
break; |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
110 |
} |
4937 | 111 |
} |
9551 | 112 |
|
4426 | 113 |
m_currPath->setPath(path); |
4427 | 114 |
|
115 |
emit pathChanged(); |
|
4426 | 116 |
} |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
117 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
118 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
119 |
showCursor(); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
120 |
m_cursor->setPos(mouseEvent->scenePos()); |
4423 | 121 |
} |
122 |
||
123 |
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
124 |
{ |
|
4426 | 125 |
m_currPath = addPath(QPainterPath(), m_pen); |
126 |
||
127 |
QPainterPath path = m_currPath->path(); |
|
128 |
QPointF p = mouseEvent->scenePos(); |
|
129 |
p += QPointF(0.01, 0.01); |
|
130 |
path.moveTo(p); |
|
131 |
path.lineTo(mouseEvent->scenePos()); |
|
6873 | 132 |
|
133 |
PathParams params; |
|
134 |
params.width = serializePenWidth(m_pen.width()); |
|
135 |
params.erasing = m_isErasing; |
|
9551 | 136 |
params.initialPoint = mouseEvent->scenePos().toPoint(); |
137 |
params.points = QList<QPoint>() << params.initialPoint; |
|
6873 | 138 |
paths.prepend(params); |
4426 | 139 |
m_currPath->setPath(path); |
140 |
||
4427 | 141 |
emit pathChanged(); |
4423 | 142 |
} |
143 |
||
144 |
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent) |
|
145 |
{ |
|
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
|
146 |
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
|
147 |
{ |
9555 | 148 |
QPointF currentPos = mouseEvent->scenePos(); |
149 |
||
150 |
if(mouseEvent->modifiers() & Qt::ControlModifier) |
|
151 |
currentPos = putSomeConstraints(paths.first().initialPoint, currentPos); |
|
152 |
||
9551 | 153 |
switch (m_pathType) |
154 |
{ |
|
155 |
case Polyline: { |
|
156 |
QPainterPath path = m_currPath->path(); |
|
157 |
path.lineTo(mouseEvent->scenePos()); |
|
9555 | 158 |
paths.first().points.append(currentPos.toPoint()); |
9551 | 159 |
m_currPath->setPath(path); |
9553 | 160 |
simplifyLast(); |
9551 | 161 |
break; |
162 |
} |
|
163 |
case Rectangle: { |
|
164 |
QPoint p1 = paths.first().initialPoint; |
|
9555 | 165 |
QPoint p2 = currentPos.toPoint(); |
9551 | 166 |
QList<QPoint> rpoints; |
167 |
rpoints << p1 << QPoint(p1.x(), p2.y()) << p2 << QPoint(p2.x(), p1.y()) << p1; |
|
168 |
paths.first().points = rpoints; |
|
169 |
break; |
|
170 |
} |
|
171 |
case Ellipse: |
|
172 |
QPoint p1 = paths.first().initialPoint; |
|
9555 | 173 |
QPoint p2 = currentPos.toPoint(); |
9551 | 174 |
QList<QPointF> points = makeEllipse(p1, p2); |
175 |
QList<QPoint> epoints; |
|
176 |
foreach(const QPointF & p, points) |
|
177 |
epoints.append(p.toPoint()); |
|
178 |
paths.first().points = epoints; |
|
179 |
break; |
|
180 |
} |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
181 |
|
9553 | 182 |
m_currPath = 0; |
4439 | 183 |
|
9553 | 184 |
emit pathChanged(); |
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
|
185 |
} |
4423 | 186 |
} |
4424 | 187 |
|
6781 | 188 |
void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) |
189 |
{ |
|
6784 | 190 |
if(wheelEvent->delta() > 0 && m_pen.width() < 516) |
6781 | 191 |
m_pen.setWidth(m_pen.width() + 10); |
6784 | 192 |
else if(wheelEvent->delta() < 0 && m_pen.width() >= 16) |
6781 | 193 |
m_pen.setWidth(m_pen.width() - 10); |
194 |
||
13203
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
195 |
QPen cursorPen = m_cursor->pen(); |
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
196 |
cursorPen.setWidth(m_pen.width()); |
eaa494f0b19e
Fix cursor circle being too large in drawn map editor
Wuzzy <Wuzzy2@mail.ru>
parents:
11046
diff
changeset
|
197 |
m_cursor->setPen(cursorPen); |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
198 |
|
6781 | 199 |
if(m_currPath) |
200 |
{ |
|
201 |
m_currPath->setPen(m_pen); |
|
6873 | 202 |
paths.first().width = serializePenWidth(m_pen.width()); |
6781 | 203 |
} |
204 |
} |
|
205 |
||
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
206 |
void DrawMapScene::showCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
207 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
208 |
if(!m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
209 |
addItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
210 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
211 |
m_isCursorShown = true; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
212 |
} |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
213 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
214 |
void DrawMapScene::hideCursor() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
215 |
{ |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
216 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
217 |
removeItem(m_cursor); |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
218 |
|
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
219 |
m_isCursorShown = false; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
220 |
} |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
221 |
|
4426 | 222 |
void DrawMapScene::undo() |
4424 | 223 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
224 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
225 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
226 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
227 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
228 |
if(paths.size()) |
4427 | 229 |
{ |
4426 | 230 |
removeItem(items().first()); |
4439 | 231 |
paths.removeFirst(); |
4427 | 232 |
|
233 |
emit pathChanged(); |
|
234 |
} |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
235 |
else if(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
236 |
{ |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
237 |
while(oldItems.size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
238 |
addItem(oldItems.takeFirst()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
239 |
paths = oldPaths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
240 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
241 |
emit pathChanged(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
242 |
} |
4424 | 243 |
} |
4427 | 244 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
245 |
void DrawMapScene::clearMap() |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
246 |
{ |
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
247 |
// cursor is a part of items() |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
248 |
if(m_isCursorShown) |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
249 |
return; |
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
250 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
251 |
// don't clear if already cleared |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
252 |
if(!items().size()) |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
253 |
return; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
254 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
255 |
m_specialPoints.clear(); |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
256 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
257 |
|
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
258 |
// 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
|
259 |
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
|
260 |
{ |
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
261 |
oldItems.push_front(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
262 |
removeItem(items().first()); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
263 |
} |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
264 |
|
9483
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
265 |
items().clear(); |
14ad1ac00ac9
Modify Racer script so it could work with special points
unc0rr
parents:
9472
diff
changeset
|
266 |
|
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
267 |
oldPaths = paths; |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
268 |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
269 |
paths.clear(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
270 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
271 |
emit pathChanged(); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
272 |
} |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
273 |
|
6873 | 274 |
|
275 |
void DrawMapScene::setErasing(bool erasing) |
|
276 |
{ |
|
277 |
m_isErasing = erasing; |
|
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
278 |
QPen cursorPen = m_cursor->pen(); |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
279 |
if(erasing) { |
6873 | 280 |
m_pen.setBrush(m_eraser); |
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
281 |
cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_ERASER); |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
282 |
} else { |
6873 | 283 |
m_pen.setBrush(m_brush); |
13204
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
284 |
cursorPen.setColor(DRAWN_MAP_COLOR_CURSOR_PEN); |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
285 |
} |
9565569e410c
Drawn map page: Enable antialiasing, turn eraser cursor red
Wuzzy <Wuzzy2@mail.ru>
parents:
13203
diff
changeset
|
286 |
m_cursor->setPen(cursorPen); |
6873 | 287 |
} |
288 |
||
4427 | 289 |
QByteArray DrawMapScene::encode() |
290 |
{ |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
291 |
QByteArray b(m_specialPoints); |
4427 | 292 |
|
4666 | 293 |
for(int i = paths.size() - 1; i >= 0; --i) |
4427 | 294 |
{ |
295 |
int cnt = 0; |
|
6873 | 296 |
PathParams params = paths.at(i); |
297 |
foreach(QPoint point, params.points) |
|
4427 | 298 |
{ |
299 |
qint16 px = qToBigEndian((qint16)point.x()); |
|
300 |
qint16 py = qToBigEndian((qint16)point.y()); |
|
6781 | 301 |
quint8 flags = 0; |
7298
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
302 |
if(!cnt) |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
303 |
{ |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
304 |
flags = 0x80 + params.width; |
a5f2fa95e711
Don't set erasing flag when it is unnecessary so hwmap could compress better
unc0rr
parents:
7146
diff
changeset
|
305 |
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
|
306 |
} |
4427 | 307 |
b.append((const char *)&px, 2); |
308 |
b.append((const char *)&py, 2); |
|
4457 | 309 |
b.append((const char *)&flags, 1); |
4427 | 310 |
|
311 |
++cnt; |
|
312 |
} |
|
313 |
||
314 |
} |
|
315 |
||
316 |
return b; |
|
317 |
} |
|
4434 | 318 |
|
4442 | 319 |
void DrawMapScene::decode(QByteArray data) |
320 |
{ |
|
10235 | 321 |
hideCursor(); |
322 |
||
6873 | 323 |
bool erasing = m_isErasing; |
324 |
||
5858
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
325 |
oldItems.clear(); |
1f4a8cf9efcb
hand drawn map editor: allow "undo" after "clear"
sheepluva
parents:
5108
diff
changeset
|
326 |
oldPaths.clear(); |
4442 | 327 |
clear(); |
328 |
paths.clear(); |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
329 |
m_specialPoints.clear(); |
4442 | 330 |
|
6873 | 331 |
PathParams params; |
4442 | 332 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
333 |
bool isSpecial = true; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
334 |
|
4442 | 335 |
while(data.size() >= 5) |
336 |
{ |
|
337 |
qint16 px = qFromBigEndian(*(qint16 *)data.data()); |
|
338 |
data.remove(0, 2); |
|
339 |
qint16 py = qFromBigEndian(*(qint16 *)data.data()); |
|
340 |
data.remove(0, 2); |
|
4457 | 341 |
quint8 flags = *(quint8 *)data.data(); |
342 |
data.remove(0, 1); |
|
10235 | 343 |
//qDebug() << px << py; |
6781 | 344 |
if(flags & 0x80) |
4442 | 345 |
{ |
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
346 |
isSpecial = false; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
347 |
|
6873 | 348 |
if(params.points.size()) |
6781 | 349 |
{ |
6873 | 350 |
addPath(pointsToPath(params.points), m_pen); |
6781 | 351 |
|
6873 | 352 |
paths.prepend(params); |
4666 | 353 |
|
6873 | 354 |
params.points.clear(); |
6781 | 355 |
} |
356 |
||
6873 | 357 |
quint8 penWidth = flags & 0x3f; |
6781 | 358 |
m_pen.setWidth(deserializePenWidth(penWidth)); |
7146 | 359 |
params.erasing = flags & 0x40; |
360 |
if(params.erasing) |
|
6873 | 361 |
m_pen.setBrush(m_eraser); |
362 |
else |
|
363 |
m_pen.setBrush(m_brush); |
|
364 |
params.width = penWidth; |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
365 |
} else |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
366 |
if(isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
367 |
{ |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
368 |
QPainterPath path; |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
369 |
path.addEllipse(QPointF(px, py), 10, 10); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
370 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
371 |
addPath(path); |
4442 | 372 |
|
9472
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
373 |
qint16 x = qToBigEndian(px); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
374 |
qint16 y = qToBigEndian(py); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
375 |
m_specialPoints.append((const char *)&x, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
376 |
m_specialPoints.append((const char *)&y, 2); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
377 |
m_specialPoints.append((const char *)&flags, 1); |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
378 |
} |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
379 |
|
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
380 |
if(!isSpecial) |
265e5997580e
Incomplete implementation of 'special points' in drawn maps (crashes engine)
unc0rr
parents:
9080
diff
changeset
|
381 |
params.points.append(QPoint(px, py)); |
4666 | 382 |
} |
383 |
||
6873 | 384 |
if(params.points.size()) |
4666 | 385 |
{ |
6873 | 386 |
addPath(pointsToPath(params.points), m_pen); |
387 |
paths.prepend(params); |
|
4442 | 388 |
} |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
389 |
|
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
390 |
emit pathChanged(); |
6873 | 391 |
|
392 |
setErasing(erasing); |
|
4442 | 393 |
} |
394 |
||
4439 | 395 |
void DrawMapScene::simplifyLast() |
4434 | 396 |
{ |
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
4520
diff
changeset
|
397 |
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
|
398 |
|
6873 | 399 |
QList<QPoint> points = paths.at(0).points; |
4439 | 400 |
|
401 |
QPoint prevPoint = points.first(); |
|
402 |
int i = 1; |
|
403 |
while(i < points.size()) |
|
4434 | 404 |
{ |
10223
b6b98dfa3807
Optimize out path closing point for one point paths
unc0rr
parents:
10108
diff
changeset
|
405 |
if( ((i != points.size() - 1) || (prevPoint == points[i])) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
406 |
&& (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000) |
4471 | 407 |
) |
4439 | 408 |
points.removeAt(i); |
409 |
else |
|
410 |
{ |
|
411 |
prevPoint = points[i]; |
|
412 |
++i; |
|
413 |
} |
|
414 |
} |
|
4434 | 415 |
|
6873 | 416 |
paths[0].points = points; |
4439 | 417 |
|
418 |
||
419 |
// redraw path |
|
420 |
{ |
|
6934
14a230552c2e
Cursor for DrawMapScene. Feel free to ajust its look.
unc0rr
parents:
6873
diff
changeset
|
421 |
QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[m_isCursorShown ? 1 : 0]); |
6873 | 422 |
pathItem->setPath(pointsToPath(paths[0].points)); |
4434 | 423 |
} |
424 |
} |
|
4442 | 425 |
|
6935 | 426 |
int DrawMapScene::pointsCount() |
427 |
{ |
|
428 |
int cnt = 0; |
|
429 |
foreach(PathParams p, paths) |
|
430 |
cnt += p.points.size(); |
|
431 |
||
432 |
return cnt; |
|
433 |
} |
|
434 |
||
4442 | 435 |
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points) |
436 |
{ |
|
437 |
QPainterPath path; |
|
438 |
||
439 |
if(points.size()) |
|
440 |
{ |
|
441 |
QPointF p = points[0] + QPointF(0.01, 0.01); |
|
442 |
path.moveTo(p); |
|
443 |
||
444 |
foreach(QPoint p, points) |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
5858
diff
changeset
|
445 |
path.lineTo(p); |
4442 | 446 |
} |
447 |
||
448 |
return path; |
|
449 |
} |
|
6781 | 450 |
|
451 |
quint8 DrawMapScene::serializePenWidth(int width) |
|
452 |
{ |
|
453 |
return (width - 6) / 10; |
|
454 |
} |
|
455 |
||
456 |
int DrawMapScene::deserializePenWidth(quint8 width) |
|
457 |
{ |
|
458 |
return width * 10 + 6; |
|
459 |
} |
|
9551 | 460 |
|
461 |
void DrawMapScene::setPathType(PathType pathType) |
|
462 |
{ |
|
463 |
m_pathType = pathType; |
|
464 |
} |
|
465 |
||
466 |
QList<QPointF> DrawMapScene::makeEllipse(const QPointF ¢er, const QPointF &corner) |
|
467 |
{ |
|
468 |
QList<QPointF> l; |
|
469 |
qreal rx = qAbs(center.x() - corner.x()); |
|
470 |
qreal ry = qAbs(center.y() - corner.y()); |
|
9553 | 471 |
qreal r = qMax(rx, ry); |
9551 | 472 |
|
473 |
if(r < 4) |
|
474 |
{ |
|
475 |
l.append(center); |
|
476 |
} else |
|
477 |
{ |
|
9949
2aa9cf5badfc
Added cast to qreal, avoiding ftbfs due to issue #758
Gianfranco Costamagna <costamagnagianfranco@yahoo.it>
parents:
9555
diff
changeset
|
478 |
qreal angleDelta = qMax(static_cast<qreal> (0.1), qMin(static_cast<qreal> (0.7), 120 / r)); |
9551 | 479 |
for(qreal angle = 0.0; angle < 2*M_PI; angle += angleDelta) |
480 |
l.append(center + QPointF(rx * cos(angle), ry * sin(angle))); |
|
481 |
l.append(l.first()); |
|
482 |
} |
|
483 |
||
484 |
return l; |
|
485 |
} |
|
9555 | 486 |
|
487 |
QPointF DrawMapScene::putSomeConstraints(const QPointF &initialPoint, const QPointF &point) |
|
488 |
{ |
|
489 |
QPointF vector = point - initialPoint; |
|
490 |
||
491 |
for(int angle = 0; angle < 180; angle += 15) |
|
492 |
{ |
|
493 |
QTransform transform; |
|
494 |
transform.rotate(angle); |
|
495 |
||
496 |
QPointF rotated = transform.map(vector); |
|
497 |
||
498 |
if(rotated.x() == 0) return point; |
|
499 |
if(qAbs(rotated.y() / rotated.x()) < 0.05) return initialPoint + transform.inverted().map(QPointF(rotated.x(), 0)); |
|
500 |
} |
|
501 |
||
502 |
return point; |
|
503 |
} |
|
10235 | 504 |
|
505 |
void DrawMapScene::optimize() |
|
506 |
{ |
|
507 |
if(!paths.size()) return; |
|
508 |
||
509 |
// break paths into segments |
|
510 |
Paths pth; |
|
511 |
||
512 |
foreach(const PathParams & pp, paths) |
|
513 |
{ |
|
514 |
int l = pp.points.size(); |
|
515 |
||
516 |
if(l == 1) |
|
517 |
{ |
|
518 |
pth.prepend(pp); |
|
519 |
} else |
|
520 |
{ |
|
521 |
for(int i = l - 2; i >= 0; --i) |
|
522 |
{ |
|
523 |
PathParams p = pp; |
|
524 |
p.points = QList<QPoint>() << p.points[i] << p.points[i + 1]; |
|
525 |
pth.prepend(pp); |
|
526 |
} |
|
527 |
} |
|
528 |
} |
|
529 |
||
530 |
// clear the scene |
|
531 |
oldItems.clear(); |
|
532 |
oldPaths.clear(); |
|
533 |
clear(); |
|
534 |
paths.clear(); |
|
535 |
m_specialPoints.clear(); |
|
536 |
||
537 |
// render the result |
|
538 |
foreach(const PathParams & p, pth) |
|
539 |
{ |
|
540 |
if(p.erasing) |
|
541 |
m_pen.setBrush(m_eraser); |
|
542 |
else |
|
543 |
m_pen.setBrush(m_brush); |
|
544 |
||
545 |
m_pen.setWidth(deserializePenWidth(p.width)); |
|
546 |
||
547 |
addPath(pointsToPath(p.points), m_pen); |
|
548 |
} |
|
549 |
||
550 |
emit pathChanged(); |
|
551 |
} |