QTfrontend/drawmapscene.cpp
author unc0rr
Mon, 13 Dec 2010 22:12:30 +0300
changeset 4520 e7882bd1a894
parent 4471 tools/drawMapTest/drawmapscene.cpp@220d44cb8659
child 4560 5d6c7f88db73
permissions -rw-r--r--
Allow to draw maps in frontend and play them (only locally, some bugs to fix)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     1
#include <QGraphicsSceneMouseEvent>
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
     2
#include <QGraphicsPathItem>
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
     3
#include <QtEndian>
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     4
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     5
#include "drawmapscene.h"
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
     6
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
     7
template <class T> T sqr(const T & x)
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
     8
{
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
     9
    return x*x;
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    10
}
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
    11
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    12
DrawMapScene::DrawMapScene(QObject *parent) :
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    13
    QGraphicsScene(parent),
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    14
    m_pen(Qt::yellow),
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    15
    m_brush(Qt::yellow)
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    16
{
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    17
    setSceneRect(0, 0, 4096, 2048);
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    18
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    19
    QLinearGradient gradient(0, 0, 0, 2048);
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    20
    gradient.setColorAt(0, QColor(60, 60, 155));
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    21
    gradient.setColorAt(1, QColor(155, 155, 60));
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    22
    setBackgroundBrush(QBrush(gradient));
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    23
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    24
    m_pen.setWidth(67);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    25
    m_pen.setJoinStyle(Qt::RoundJoin);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    26
    m_pen.setCapStyle(Qt::RoundCap);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    27
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    28
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    29
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    30
void DrawMapScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    31
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    32
    if(m_currPath && (mouseEvent->buttons() & Qt::LeftButton))
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    33
    {
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    34
        QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    35
        path.lineTo(mouseEvent->scenePos());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    36
        paths.first().append(mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    37
        m_currPath->setPath(path);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    38
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    39
        emit pathChanged();
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    40
    }
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    41
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    42
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    43
void DrawMapScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    44
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    45
    m_currPath = addPath(QPainterPath(), m_pen);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    46
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    47
    QPainterPath path = m_currPath->path();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    48
    QPointF p = mouseEvent->scenePos();
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    49
    p += QPointF(0.01, 0.01);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    50
    path.moveTo(p);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    51
    path.lineTo(mouseEvent->scenePos());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    52
    paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    53
    m_currPath->setPath(path);
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    54
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    55
    emit pathChanged();
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    56
}
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    57
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    58
void DrawMapScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    59
{
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    60
    simplifyLast();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    61
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    62
    m_currPath = 0;
4423
4391526e436e Initial commit of the Draw Map Scene
unc0rr
parents:
diff changeset
    63
}
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    64
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    65
void DrawMapScene::undo()
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    66
{
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    67
    if(items().size())
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    68
    {
4426
969e411c72aa Improve map editor a bit
unc0rr
parents: 4424
diff changeset
    69
        removeItem(items().first());
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
    70
        paths.removeFirst();
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    71
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    72
        emit pathChanged();
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    73
    }
4424
3225ea34e415 Some basic functionality
unc0rr
parents: 4423
diff changeset
    74
}
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    75
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    76
QByteArray DrawMapScene::encode()
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    77
{
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    78
    QByteArray b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    79
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    80
    foreach(QList<QPoint> points, paths)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    81
    {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    82
        int cnt = 0;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    83
        foreach(QPoint point, points)
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    84
        {
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    85
            qint16 px = qToBigEndian((qint16)point.x());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    86
            qint16 py = qToBigEndian((qint16)point.y());
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    87
            quint8 flags = 2;
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
    88
            if(!cnt) flags |= 0x80;
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    89
            b.append((const char *)&px, 2);
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    90
            b.append((const char *)&py, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
    91
            b.append((const char *)&flags, 1);
4427
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    92
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    93
            ++cnt;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    94
        }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    95
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    96
    }
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    97
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    98
    return b;
c5193713055f Basic encoding of drawn map
unc0rr
parents: 4426
diff changeset
    99
}
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   100
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   101
void DrawMapScene::decode(QByteArray data)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   102
{
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   103
    clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   104
    paths.clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   105
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   106
    QList<QPoint> points;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   107
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   108
    while(data.size() >= 5)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   109
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   110
        qint16 px = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   111
        data.remove(0, 2);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   112
        qint16 py = qFromBigEndian(*(qint16 *)data.data());
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   113
        data.remove(0, 2);
4457
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   114
        quint8 flags = *(quint8 *)data.data();
ffb766e85150 - Change painted map file format
unc0rr
parents: 4442
diff changeset
   115
        data.remove(0, 1);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   116
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   117
        //last chunk or first point
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   118
        if((data.size() < 5) || (flags & 0x80))
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   119
        {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   120
            if(points.size())
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   121
            {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   122
                addPath(pointsToPath(points), m_pen);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   123
                paths.prepend(points);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   124
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   125
                points.clear();
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   126
            }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   127
        }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   128
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   129
        points.append(QPoint(px, py));
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   130
    }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   131
}
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   132
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   133
void DrawMapScene::simplifyLast()
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   134
{
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   135
    QList<QPoint> points = paths[0];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   136
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   137
    QPoint prevPoint = points.first();
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   138
    int i = 1;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   139
    while(i < points.size())
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   140
    {
4471
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   141
        if( (i != points.size() - 1)
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   142
            && (sqr(prevPoint.x() - points[i].x()) + sqr(prevPoint.y() - points[i].y()) < 1000)
220d44cb8659 Always keep last point of drawn polyline
unc0rr
parents: 4457
diff changeset
   143
          )
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   144
            points.removeAt(i);
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   145
        else
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   146
        {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   147
            prevPoint = points[i];
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   148
            ++i;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   149
        }
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   150
    }
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   151
4439
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   152
    paths[0] = points;
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   153
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   154
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   155
    // redraw path
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   156
    {
27a896207aae Some more improvements
unC0Rr
parents: 4434
diff changeset
   157
        QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   158
        pathItem->setPath(pointsToPath(paths[0]));
4434
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   159
    }
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   160
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   161
    emit pathChanged();
34c305fbc63c Simple simplify() function
unc0rr
parents: 4427
diff changeset
   162
}
4442
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   163
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   164
QPainterPath DrawMapScene::pointsToPath(const QList<QPoint> points)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   165
{
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   166
    QPainterPath path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   167
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   168
    if(points.size())
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   169
    {
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   170
        QPointF p = points[0] + QPointF(0.01, 0.01);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   171
        path.moveTo(p);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   172
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   173
        foreach(QPoint p, points)
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   174
            path.lineTo(p);
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   175
    }
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   176
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   177
    return path;
f8424e1bc936 - Implement decode() function.
unc0rr
parents: 4439
diff changeset
   178
}