]> rtime.felk.cvut.cz Git - eurobot/public.git/blobdiff - src/robomon/PlaygroundScene.cpp
robomon: Set simulated obstacle visible on a click in playground scene
[eurobot/public.git] / src / robomon / PlaygroundScene.cpp
index ef2ab7f235cf73eaa3d9e9a38627570c2fc6f561..99bec3de0d1cd38d786d56b30cc3b57097bcd320 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright: (c) 2007 CTU Dragons
  *            CTU FEE - Department of Control Engineering
  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
- * Edit:               Petr Kubiznak
+ *          Michal Vokac, Petr Kubiznak
  * License: GNU GPL v.2
  */
 
 #include <QGraphicsSceneMouseEvent>
 #include <QGraphicsScene>
 #include <QGraphicsRectItem>
+#include <QImage>
+#include <QColor>
+#include <QBrush>
+
 using namespace Qt;
 
 #include "PlaygroundScene.h"
 #include <robodim.h>
+#include <math.h>
 
 //margin around borders (just for better look)
-#define MARGIN                                                 40
+#define MARGIN 40
 
 //playground borders
-#define BORDER_WIDTH                           22
-#define BORDER_COLOR                           black
+#define BORDER_WIDTH   10
+#define BORDER_COLOR   black
 
 //playground
-#define PLAYGROUND_WIDTH               3000
-#define PLAYGROUND_HEIGHT              2100
-#define PLAYGROUND_COLOR               green
-
-//starting areas
-#define STARTAREA_WIDTH                        500
-#define STARTAREA_HEIGHT               500
-#define STARTAREA_L_COLOR              blue
-#define STARTAREA_R_COLOR              yellow
-
-//baskets
-#define BASK_WIDTH                                     500
-#define BASK_HEIGHT                                    300
-#define BASK_L_COLOR                           (STARTAREA_R_COLOR)
-#define BASK_R_COLOR                           (STARTAREA_L_COLOR)
-
-//raised area
-#define RAISED_AREA_HEIGHT     500
-#define RAISED_AREA_WIDTH              1519
-#define RAISED_AREA_TOP_WIDTH          519
-#define RAISED_AREA_STRIP_WIDTH        100
-#define RAISED_AREA_COLOR              white
-
-//corns
-#define CORN_WIDTH                                     50
-#define CORN_ROWS_NUM                          3
-#define CORN_COLS_NUM                          7
-#define CORN_ROWS_SPACE                        500
-#define CORN_COLS_SPACE                        450
-#define CORN_COLS_DRIFT                        250
-#define CORN_INIT_X                                    (PLAYGROUND_WIDTH / 2)
-#define CORN_INIT_Y                                    (128 - CORN_ROWS_SPACE)
-#define CORN_COLOR                                     gray
-
-//tomatoes
-#define TOMATO_WIDTH                           100
-#define TOMATO_ROWS_NUM                        2
-#define TOMATO_COLS_NUM                        7
-#define TOMATO_ROWS_SPACE              500
-#define TOMATO_COLS_SPACE              450
-#define TOMATO_COLS_DRIFT              250
-#define TOMATO_INIT_X                          (PLAYGROUND_WIDTH / 2)
-#define TOMATO_INIT_Y                          (CORN_INIT_Y + 250)
-#define TOMATO_COLOR                           red
-
-//orange trees
-#define ORANGE_WIDTH                           100
-#define ORANGE_LEFT_X                          ( (PLAYGROUND_WIDTH - RAISED_AREA_TOP_WIDTH)/2 + 135 )
-#define ORANGE_RIGHT_X                 ( (PLAYGROUND_WIDTH + RAISED_AREA_TOP_WIDTH)/2 - 135 )
-#define ORANGE_TOP_Y                           (PLAYGROUND_HEIGHT - 145)
-#define ORANGE_BOTTOM_Y                        (ORANGE_TOP_Y - 225)
-#define ORANGE_COLOR                           QBrush(QColor(255, 165, 0))
-
-/* draws a corn */
-void PlaygroundScene::putCorn(QGraphicsEllipseItem *g, int centerX, int centerY) {
-       using namespace Qt;
-       g = addEllipse(QRect(centerX - CORN_WIDTH/2, centerY - CORN_WIDTH/2, CORN_WIDTH, CORN_WIDTH), QPen(), QBrush(CORN_COLOR));
-       g->setZValue(3);
-}
+#define PLAYGROUND_WIDTH       PLAYGROUND_WIDTH_MM
+#define PLAYGROUND_HEIGHT      PLAYGROUND_HEIGHT_MM
 
-/* draws a tomato */
-void PlaygroundScene::putTomato(QGraphicsEllipseItem *g, int centerX, int centerY) {
-       using namespace Qt;
-       g = addEllipse(QRect(centerX - TOMATO_WIDTH/2, centerY - TOMATO_WIDTH/2, TOMATO_WIDTH, TOMATO_WIDTH), QPen(), QBrush(TOMATO_COLOR));
-       g->setZValue(3);
-}
-
-/* draws an orange tree (coordinates concern the middle branch) */
-void PlaygroundScene::putTree(QGraphicsEllipseItem *g, int centerX, int centerY, bool isLeft) {
-       using namespace Qt;
-       int leftMove1 = (isLeft ? -55 : 55);                            //x-pos of the top branch according to the middle
-       int leftMove2 = (isLeft ? -80 : 80);                            //x-pos of the bottom branch according to the middle
-
-       g = addEllipse(QRect(centerX - ORANGE_WIDTH/2, centerY - ORANGE_WIDTH/2, ORANGE_WIDTH, ORANGE_WIDTH), QPen(), QBrush(ORANGE_COLOR));            //middle branch
-       g->setZValue(5);
-       g = addEllipse(QRect(centerX + leftMove1 - ORANGE_WIDTH/2, centerY + 75 - ORANGE_WIDTH/2, ORANGE_WIDTH, ORANGE_WIDTH), QPen(), QBrush(ORANGE_COLOR));           //top branch
-       g->setZValue(5);
-       g = addEllipse(QRect(centerX + leftMove2 - ORANGE_WIDTH/2, centerY - 50 - ORANGE_WIDTH/2, ORANGE_WIDTH, ORANGE_WIDTH), QPen(), QBrush(ORANGE_COLOR));           //bottom branch
-       g->setZValue(5);
-}
+// grid
+#define GRID_HEIGHT       5000
+#define GRID_WIDTH        5000
 
 PlaygroundScene::PlaygroundScene(QObject *parent)
        : QGraphicsScene(parent)
 {
        using namespace Qt;
-       QGraphicsRectItem *tempRect;
+       QGraphicsRectItem *playgroundRect;
+        QGraphicsLineItem *tempLine;
        
        /* All scene units are milimeters */
-       setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN - BASK_HEIGHT), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
-       
+       setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
+
        /* playground border */
        addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));           //left
        addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));                //top
        addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));             //right
-       addRect(QRect(BASK_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH - 2*BASK_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));         //bottom
+       addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));            //bottom
        
        /* playground */
-       tempRect = addRect(QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT), QPen(), PLAYGROUND_COLOR);
-       tempRect->setZValue(1);
-       
-       /* baskets */
-       addRect(QRect(0, -BASK_HEIGHT, BASK_WIDTH, BASK_HEIGHT), QPen(), BASK_L_COLOR);
-       addRect(QRect(PLAYGROUND_WIDTH-BASK_WIDTH, -BASK_HEIGHT, BASK_WIDTH, BASK_HEIGHT), QPen(), BASK_R_COLOR);
-       
-       /* left starting area */
-       tempRect = addRect(QRect(0, PLAYGROUND_HEIGHT-STARTAREA_HEIGHT, STARTAREA_WIDTH, STARTAREA_HEIGHT), QPen(NoPen), QBrush(STARTAREA_L_COLOR));
-       tempRect->setZValue(3);
-       
-       /* right starting area */
-       tempRect = addRect(QRect(PLAYGROUND_WIDTH-STARTAREA_WIDTH, PLAYGROUND_HEIGHT-STARTAREA_HEIGHT, STARTAREA_WIDTH, STARTAREA_HEIGHT), QPen(NoPen), QBrush(STARTAREA_R_COLOR));
-       tempRect->setZValue(3);
-       
-       /* raised zone */
-       tempRect = addRect(QRect((PLAYGROUND_WIDTH-RAISED_AREA_WIDTH)/2, PLAYGROUND_HEIGHT-RAISED_AREA_HEIGHT, RAISED_AREA_WIDTH, RAISED_AREA_HEIGHT), QPen(), QBrush(RAISED_AREA_COLOR));
-       tempRect->setZValue(3);
-       tempRect = addRect(QRect((PLAYGROUND_WIDTH-RAISED_AREA_TOP_WIDTH)/2, PLAYGROUND_HEIGHT-RAISED_AREA_HEIGHT, RAISED_AREA_TOP_WIDTH, RAISED_AREA_HEIGHT), QPen(), QBrush(RAISED_AREA_COLOR));              //top
-       tempRect->setZValue(4);
-       tempRect = addRect(QRect((PLAYGROUND_WIDTH-RAISED_AREA_WIDTH)/2 - RAISED_AREA_STRIP_WIDTH, PLAYGROUND_HEIGHT-RAISED_AREA_HEIGHT, RAISED_AREA_STRIP_WIDTH, RAISED_AREA_HEIGHT), QPen(), QBrush(RAISED_AREA_COLOR));              //left stripe
-       tempRect->setZValue(3);
-       tempRect = addRect(QRect(PLAYGROUND_WIDTH - (PLAYGROUND_WIDTH-RAISED_AREA_WIDTH)/2, PLAYGROUND_HEIGHT-RAISED_AREA_HEIGHT, RAISED_AREA_STRIP_WIDTH, RAISED_AREA_HEIGHT), QPen(), QBrush(RAISED_AREA_COLOR));             //right stripe
-       tempRect->setZValue(3);
-       tempRect = addRect(QRect((PLAYGROUND_WIDTH-RAISED_AREA_WIDTH)/2, PLAYGROUND_HEIGHT-RAISED_AREA_HEIGHT-BORDER_WIDTH, RAISED_AREA_WIDTH, BORDER_WIDTH), QPen(), QBrush(RAISED_AREA_COLOR));               //border
-       tempRect->setZValue(3);
-       
-       /* corn */
-       for(int x=0; x<=CORN_COLS_NUM/2; x++) {
-               int left_move = x*CORN_COLS_SPACE;              
-               for(int y=0; y<CORN_ROWS_NUM; y++) {
-                       QGraphicsEllipseItem *corn = NULL;
-                       int top = CORN_INIT_Y + x*CORN_COLS_DRIFT + y*CORN_ROWS_SPACE;
-                       if(top > 0) {
-                               putCorn(corn, left_move + CORN_INIT_X, top);
-                               putCorn(corn, -left_move + CORN_INIT_X, top);
-                       }
-               }
-       }
-       
-       /* tomatoes */
-       for(int x=0; x<=TOMATO_COLS_NUM/2; x++) {
-               int left_move = x*TOMATO_COLS_SPACE;
-               for(int y=0; y<TOMATO_ROWS_NUM; y++) {
-                       QGraphicsEllipseItem *tomato = NULL;
-                       int top = TOMATO_INIT_Y + x*TOMATO_COLS_DRIFT + y*TOMATO_ROWS_SPACE;
-                       if(top < 0) top += 2*TOMATO_ROWS_SPACE;                         //moves a tomato "in front of the playground" "over the triangle"
-                       putTomato(tomato, left_move + TOMATO_INIT_X, top);
-                       putTomato(tomato, -left_move + TOMATO_INIT_X, top);
-               }
-       }
-       
-       /* oranges */
-       QGraphicsEllipseItem *orange = NULL;
-       putTree(orange, ORANGE_LEFT_X, ORANGE_TOP_Y, true);
-       putTree(orange, ORANGE_LEFT_X, ORANGE_BOTTOM_Y, true);
-       putTree(orange, ORANGE_RIGHT_X, ORANGE_TOP_Y, false);
-       putTree(orange, ORANGE_RIGHT_X, ORANGE_BOTTOM_Y, false);
-       
-       /* obstacles */
+       playgroundRect = addRect(
+                               QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
+                               QPen() //,
+                               // QPixmap(":/images/playground_sick-day-2012.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
+                               );
+       playgroundRect->setZValue(0);
+
+        /* horizontal grid */
+        for (int i = 0; i < (PLAYGROUND_HEIGHT / GRID_HEIGHT); i++) {
+                tempLine = addLine(QLine( 0, PLAYGROUND_HEIGHT - i*GRID_HEIGHT, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT - i*GRID_HEIGHT), QPen());
+                tempLine->setZValue(1);
+        }
+
+        /* vertical grid */
+        for (int i = 0; i < (PLAYGROUND_WIDTH / GRID_WIDTH); i++) {
+                tempLine = addLine(QLine(PLAYGROUND_WIDTH - i*GRID_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH - i*GRID_WIDTH, 0), QPen());
+                tempLine->setZValue(1);
+        }
+
+       /* opponent obstacle - placeable by mouse clic */
        obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
        obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
        obstacle->setZValue(5);
@@ -191,21 +83,27 @@ PlaygroundScene::PlaygroundScene(QObject *parent)
        obstacle->setVisible(false);
        obstacle->setPos(QPointF(2000, 1000));
        this->addItem(obstacle);
-       
-       initMap();
 }
 
 PlaygroundScene::~PlaygroundScene()
 {
 }
 
+void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
+{
+       QPointF pos = scene2world(mouseEvent->scenePos());
+       emit mouseMoved(pos);
+       QGraphicsScene::mouseMoveEvent(mouseEvent);
+}
+
 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
 {
        QGraphicsScene::mousePressEvent(mouseEvent);
        QPointF pos = scene2world(mouseEvent->scenePos());
+       obstacle->setVisible(true);
        obstacle->setPos(mouseEvent->scenePos().x(),
                         mouseEvent->scenePos().y());
-       obstacleChanged(pos);
+       emit obstacleChanged(pos);
 }
 
 /*!
@@ -214,7 +112,7 @@ void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
 QPointF PlaygroundScene::scene2world(QPointF scenePos)
 {
        return QPointF((scenePos.x()) / 1000.0,
-                       (scenePos.y()) / 1000.0);
+                      (scenePos.y()) / 1000.0);
 }
 
 
@@ -238,32 +136,3 @@ 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(bool show)
-{
-       map->setVisible(show);
-}
-
-void PlaygroundScene::setMapColor(int x, int y, QColor color)
-{
-       color.setAlpha(200);
-       rects[x][y]->setBrush(QBrush(color));
-}