Variable pen width
authorunc0rr
Tue, 13 Mar 2012 22:46:55 +0400
changeset 6781 23f627ba8ee9
parent 6780 166ee5275915
child 6782 33009ac4de80
Variable pen width
QTfrontend/drawmapscene.cpp
QTfrontend/drawmapscene.h
QTfrontend/ui/page/pagedrawmap.cpp
hedgewars/uLandPainted.pas
--- a/QTfrontend/drawmapscene.cpp	Tue Mar 13 12:22:56 2012 -0400
+++ b/QTfrontend/drawmapscene.cpp	Tue Mar 13 22:46:55 2012 +0400
@@ -40,7 +40,7 @@
     gradient.setColorAt(1, QColor(155, 155, 60));
     setBackgroundBrush(QBrush(gradient));
 
-    m_pen.setWidth(67);
+    m_pen.setWidth(72);
     m_pen.setJoinStyle(Qt::RoundJoin);
     m_pen.setCapStyle(Qt::RoundCap);
     m_currPath = 0;
@@ -62,7 +62,7 @@
         else
         {
             path.lineTo(mouseEvent->scenePos());
-            paths.first().append(mouseEvent->scenePos().toPoint());
+            paths.first().second.append(mouseEvent->scenePos().toPoint());
         }
         m_currPath->setPath(path);
 
@@ -79,7 +79,7 @@
     p += QPointF(0.01, 0.01);
     path.moveTo(p);
     path.lineTo(mouseEvent->scenePos());
-    paths.prepend(QList<QPoint>() << mouseEvent->scenePos().toPoint());
+    paths.prepend(qMakePair(serializePenWidth(m_pen.width()), QList<QPoint>() << mouseEvent->scenePos().toPoint()));
     m_currPath->setPath(path);
 
     emit pathChanged();
@@ -91,7 +91,7 @@
     {
         QPainterPath path = m_currPath->path();
         path.lineTo(mouseEvent->scenePos());
-        paths.first().append(mouseEvent->scenePos().toPoint());
+        paths.first().second.append(mouseEvent->scenePos().toPoint());
         m_currPath->setPath(path);
 
         simplifyLast();
@@ -100,6 +100,20 @@
     }
 }
 
+void DrawMapScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
+{
+    if(wheelEvent->delta() > 0 && m_pen.width() < 512)
+        m_pen.setWidth(m_pen.width() + 10);
+    else if(wheelEvent->delta() < 0 && m_pen.width() >= 12)
+        m_pen.setWidth(m_pen.width() - 10);
+
+    if(m_currPath)
+    {
+        m_currPath->setPen(m_pen);
+        paths.first().first = serializePenWidth(m_pen.width());
+    }
+}
+
 void DrawMapScene::undo()
 {
     if(items().size())
@@ -148,13 +162,13 @@
     for(int i = paths.size() - 1; i >= 0; --i)
     {
         int cnt = 0;
-        QList<QPoint> points = paths.at(i);
-        foreach(QPoint point, points)
+        QPair<quint8, QList<QPoint> > points = paths.at(i);
+        foreach(QPoint point, points.second)
         {
             qint16 px = qToBigEndian((qint16)point.x());
             qint16 py = qToBigEndian((qint16)point.y());
-            quint8 flags = 2;
-            if(!cnt) flags |= 0x80;
+            quint8 flags = 0;
+            if(!cnt) flags = 0x80 + points.first;
             b.append((const char *)&px, 2);
             b.append((const char *)&py, 2);
             b.append((const char *)&flags, 1);
@@ -174,7 +188,7 @@
     clear();
     paths.clear();
 
-    QList<QPoint> points;
+    QPair<quint8, QList<QPoint> > points;
 
     while(data.size() >= 5)
     {
@@ -185,20 +199,28 @@
         quint8 flags = *(quint8 *)data.data();
         data.remove(0, 1);
 
-        if((flags & 0x80) && points.size())
+        if(flags & 0x80)
         {
-            addPath(pointsToPath(points), m_pen);
-            paths.prepend(points);
+            if(points.second.size())
+            {
+                addPath(pointsToPath(points.second), m_pen);
+
+                paths.prepend(points);
 
-            points.clear();
+                points.second.clear();
+            }
+
+            quint8 penWidth = flags & 0x7f;
+            m_pen.setWidth(deserializePenWidth(penWidth));
+            points.first = penWidth;
         }
 
-        points.append(QPoint(px, py));
+        points.second.append(QPoint(px, py));
     }
 
-    if(points.size())
+    if(points.second.size())
     {
-        addPath(pointsToPath(points), m_pen);
+        addPath(pointsToPath(points.second), m_pen);
         paths.prepend(points);
     }
 
@@ -209,7 +231,7 @@
 {
     if(!paths.size()) return;
 
-    QList<QPoint> points = paths.at(0);
+    QList<QPoint> points = paths.at(0).second;
 
     QPoint prevPoint = points.first();
     int i = 1;
@@ -226,13 +248,13 @@
         }
     }
 
-    paths[0] = points;
+    paths[0].second = points;
 
 
     // redraw path
     {
         QGraphicsPathItem * pathItem = static_cast<QGraphicsPathItem *>(items()[0]);
-        pathItem->setPath(pointsToPath(paths[0]));
+        pathItem->setPath(pointsToPath(paths[0].second));
     }
 
     emit pathChanged();
@@ -253,3 +275,13 @@
 
     return path;
 }
+
+quint8 DrawMapScene::serializePenWidth(int width)
+{
+    return (width - 6) / 10;
+}
+
+int DrawMapScene::deserializePenWidth(quint8 width)
+{
+    return width * 10 + 6;
+}
--- a/QTfrontend/drawmapscene.h	Tue Mar 13 12:22:56 2012 -0400
+++ b/QTfrontend/drawmapscene.h	Tue Mar 13 22:46:55 2012 +0400
@@ -24,7 +24,7 @@
 
 class QGraphicsPathItem;
 
-typedef QList<QList<QPoint> > Paths;
+typedef QList<QPair<quint8, QList<QPoint> > > Paths;
 
 class DrawMapScene : public QGraphicsScene
 {
@@ -54,8 +54,12 @@
         virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
         virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
         virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
+        virtual void wheelEvent(QGraphicsSceneWheelEvent *);
 
         QPainterPath pointsToPath(const QList<QPoint> points);
+
+        quint8 serializePenWidth(int width);
+        int deserializePenWidth(quint8 width);
 };
 
 #endif // DRAWMAPSCENE_H
--- a/QTfrontend/ui/page/pagedrawmap.cpp	Tue Mar 13 12:22:56 2012 -0400
+++ b/QTfrontend/ui/page/pagedrawmap.cpp	Tue Mar 13 22:46:55 2012 +0400
@@ -62,7 +62,7 @@
 
 void PageDrawMap::save()
 {
-    QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save drawn map"), ".", tr("Drawn Maps") + " (*.hwmap);;" + tr("All files") + " (*)");
+    QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save drawn map"), "./map.hwmap", tr("Drawn Maps") + " (*.hwmap);;" + tr("All files") + " (*)");
 
     if(!fileName.isEmpty())
         drawMapWidget->save(fileName);
--- a/hedgewars/uLandPainted.pas	Tue Mar 13 12:22:56 2012 -0400
+++ b/hedgewars/uLandPainted.pas	Tue Mar 13 22:46:55 2012 +0400
@@ -43,7 +43,7 @@
 
 var pointsListHead, pointsListLast: PPointEntry;
 
-procedure DrawLineOnLand(X1, Y1, X2, Y2: LongInt);
+procedure DrawLineOnLand(X1, Y1, X2, Y2, radius: LongInt);
 var  eX, eY, dX, dY: LongInt;
     i, sX, sY, x, y, d: LongInt;
     b: boolean;
@@ -110,7 +110,7 @@
                 begin
                 inc(len);
                 if (len mod 4) = 0 then
-                    FillRoundInLand(X, Y, 34, lfBasic)
+                    FillRoundInLand(X, Y, radius, lfBasic)
                 end
         end
 end;
@@ -147,10 +147,12 @@
 procedure Draw;
 var pe: PPointEntry;
     prevPoint: PointRec;
+    radius: LongInt;
 begin
     // shutup compiler
     prevPoint.X:= 0;
     prevPoint.Y:= 0;
+    radius:= 0;
 
     pe:= pointsListHead;
     TryDo((pe = nil) or (pe^.point.flags and $80 <> 0), 'Corrupted draw data', true);
@@ -159,13 +161,14 @@
         begin
         if (pe^.point.flags and $80 <> 0) then
             begin
+            radius:= (pe^.point.flags and $7F) * 5 + 3;
             AddFileLog('[DRAW] Move to: ('+inttostr(pe^.point.X)+','+inttostr(pe^.point.Y)+')');
-            FillRoundInLand(pe^.point.X, pe^.point.Y, 34, lfBasic)
+            FillRoundInLand(pe^.point.X, pe^.point.Y, radius, lfBasic)
             end
             else
             begin
             AddFileLog('[DRAW] Line to: ('+inttostr(pe^.point.X)+','+inttostr(pe^.point.Y)+')');
-            DrawLineOnLand(prevPoint.X, prevPoint.Y, pe^.point.X, pe^.point.Y);
+            DrawLineOnLand(prevPoint.X, prevPoint.Y, pe^.point.X, pe^.point.Y, radius);
             end;
 
         prevPoint:= pe^.point;