]> rtime.felk.cvut.cz Git - eurobot/public.git/commitdiff
robomon: Fixed and enhanced map display
authorMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 13 Apr 2009 22:40:10 +0000 (00:40 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Mon, 13 Apr 2009 22:44:47 +0000 (00:44 +0200)
Enhancements:
- map is transparent
- map can be shown and than hidden again
- map is not declared as static variable in a method

src/robomon/PlaygroundScene.cpp
src/robomon/PlaygroundScene.h
src/robomon/RobomonAtlantis.cpp
src/robomon/RobomonAtlantis.h

index b9c92bfdbfb3ff43326087e7272cdcf4284d84cc..1e781e033a12631e540b5f9431804d264cb6d1a9 100644 (file)
@@ -54,9 +54,11 @@ PlaygroundScene::PlaygroundScene(QObject *parent)
        obstacle = new QGraphicsEllipseItem(0, 0, 300, 300);
        obstacle->translate(-150,-150);
        obstacle->setZValue(5);
-       obstacle->setBrush(QBrush(QColor(blue)));
+       obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
        obstacle->setVisible(false);
        this->addItem(obstacle);
+       
+       initMap();
 }
 
 PlaygroundScene::~PlaygroundScene()
@@ -102,3 +104,32 @@ void PlaygroundScene::showObstacle(int val)
        else
                obstacle->setVisible(false);
 }
+
+void PlaygroundScene::initMap()
+{
+       this->map = new QGraphicsItemGroup();
+       for(int i=0; i < MAP_WIDTH; i++) {
+               for(int j=0; j<MAP_HEIGHT; j++) {
+                       rects[i][j] = new QGraphicsRectItem(
+                                       QRectF(i*MAP_CELL_SIZE_MM, (MAP_HEIGHT-1-j)*MAP_CELL_SIZE_MM,
+                                              MAP_CELL_SIZE_MM,MAP_CELL_SIZE_MM));
+                       rects[i][j]->setPen(QPen());
+                       rects[i][j]->setBrush(QBrush(Qt::lightGray));
+                       map->addToGroup(rects[i][j]);
+               }
+       }
+       map->setVisible(false);
+       map->setZValue(4);
+       addItem(map);
+}
+
+void PlaygroundScene::showMap(int show)
+{
+       map->setVisible(show);
+}
+
+void PlaygroundScene::setMapColor(int x, int y, QColor color)
+{
+       color.setAlpha(200);
+       rects[x][y]->setBrush(QBrush(color));
+}
index ff40cdc21735282d763eadaaafa1586904e3bfa5..c21245ddf939eaf45e726b7adb329100dbc674d2 100644 (file)
@@ -13,6 +13,7 @@
 #define PLAYGROUND_SCENE_H
 
 #include <QGraphicsScene>
+#include <map.h>
 
 class PlaygroundScene : public QGraphicsScene
 {
@@ -24,17 +25,20 @@ public:
        virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
        static QPointF scene2world(QPointF scenePos);
        static QPointF world2scene(QPointF worldPos);
+       void setMapColor(int x, int y, QColor color);
 
 signals:
        void obstacleChanged(QPointF pos);
 
 private:
-       void createPlaygroundEurobot2007();
-       void createPlaygroundEurobot2008();
+       void initMap();
+       QGraphicsItemGroup *map;
+       QGraphicsRectItem *rects[MAP_WIDTH][MAP_HEIGHT];  // This must be slow - convert to pixmap. -- M.S.
        QGraphicsEllipseItem *obstacle;
 
 public slots:
        void showObstacle(int val);
+       void showMap(int show);
 };
 
 #endif
index bfed9667f45c84c6154ba7096887d9c0c99232e9..482faef0d95a5b640247d40e5bc01062fad6d6d7 100644 (file)
@@ -546,6 +546,7 @@ void RobomonAtlantis::showMap()
        disconnect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
        connect(showMapPushButton, SIGNAL(clicked()),
                                this, SLOT(showPlayground()));
+       playgroundScene->showMap(true);
 }
 
 void RobomonAtlantis::showPlayground()
@@ -560,13 +561,12 @@ void RobomonAtlantis::showPlayground()
        disconnect(showMapPushButton, SIGNAL(clicked()),
                                this, SLOT(showPlayground()));
        connect(showMapPushButton, SIGNAL(clicked()), this, SLOT(showMap()));
+       playgroundScene->showMap(false);
 }
 
 void RobomonAtlantis::paintMap()
 {
        using namespace Qt;
-       static QGraphicsRectItem *rects[MAP_WIDTH][MAP_HEIGHT];
-       static bool firstMap = true;
        int x, y;
         struct map *map = ShmapIsMapInit();
 
@@ -574,21 +574,6 @@ void RobomonAtlantis::paintMap()
        
        for(int i=0; i < MAP_WIDTH; i++) {
                for(int j=0; j<MAP_HEIGHT; j++) {
-                       x = TOP_LEFT_X+i*cellSize;
-                       y = TOP_LEFT_Y+j*cellSize;
-                               
-                       if(firstMap) {                  
-                               rects[i][j] = playgroundScene->addRect(
-                                               QRectF(x,y,cellSize,cellSize), 
-                                               QPen(QBrush(Qt::black), 
-                                               1,
-                                               Qt::SolidLine,
-                                               Qt::FlatCap,
-                                               Qt::BevelJoin),
-                                               QBrush(Qt::lightGray));
-                               rects[i][j]->setZValue(4);
-                       }
-                       
                         QColor color;
                                
                         struct map_cell *cell = &map->cells[j][i];
@@ -616,10 +601,9 @@ void RobomonAtlantis::paintMap()
                         if (cell->flags & MAP_FLAG_DET_OBST)
                                 color = cyan;
                         
-                        rects[i][j]->setBrush(QBrush(color));
+                        playgroundScene->setMapColor(i, j, color);
                 }
        }
-       firstMap = false;
 }
 
 void RobomonAtlantis::setSimulation(int state)
@@ -1014,8 +998,6 @@ void RobomonAtlantis::openSharedMemory()
        if (sharedMemoryOpened)
                return;
 
-       cellSize = PG_X / MAP_WIDTH;
-       
        sharedSegmentSize = sizeof(char) * MAP_WIDTH * MAP_HEIGHT;
        
        /* Get segment identificator in a read only mode  */
index 61d09366c19e32f08d5a4686aa552d873e77b5dc..b607356c5d32033bcd6520cedd68b632543ea8f8 100644 (file)
@@ -38,13 +38,6 @@ class QSlider;
 class QProgressBar;
 class QFont;
 
-#define PG_X 600
-#define PG_Y 420
-
-#define TOP_LEFT QPointF(10,0)
-#define TOP_LEFT_X 10
-#define TOP_LEFT_Y 0
-
 class RobomonAtlantis : public QWidget
 {
        Q_OBJECT
@@ -198,7 +191,6 @@ private:
        /* map */
        void openSharedMemory();
        bool sharedMemoryOpened;
-       int cellSize;
        QTimer *mapTimer;
 
        /* obstacle simulation */