]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/PlaygroundScene.cpp
Changed the image for Eurobot 2012 playground (now we have playground_eurobot2012...
[eurobot/public.git] / src / robomon / PlaygroundScene.cpp
1 /*
2  * PlaygroundScene.cpp                  07/10/31
3  *
4  * Draw playground and likewise stuffs.
5  *
6  * Copyright: (c) 2007 CTU Dragons
7  *            CTU FEE - Department of Control Engineering
8  * Authors: Martin Zidek, Michal Sojka, Tran Duy Khanh
9  *          Michal Vokac, Petr Kubiznak
10  * License: GNU GPL v.2
11  */
12
13 #include <QPointF>
14 #include <QGraphicsSceneMouseEvent>
15 #include <QGraphicsScene>
16 #include <QGraphicsRectItem>
17 #include <QImage>
18 #include <QColor>
19 #include <QBrush>
20
21 using namespace Qt;
22
23 #include "PlaygroundScene.h"
24 #include <robodim.h>
25 #include <math.h>
26 #include "../robofsm/corns_configs.h"
27
28 //margin around borders (just for better look)
29 #define MARGIN  40
30
31 //playground borders
32 #define BORDER_WIDTH    10
33 #define BORDER_COLOR    black
34
35 //playground
36 #define PLAYGROUND_WIDTH        PLAYGROUND_WIDTH_MM
37 #define PLAYGROUND_HEIGHT       PLAYGROUND_HEIGHT_MM
38
39 PlaygroundScene::PlaygroundScene(QObject *parent)
40         : QGraphicsScene(parent)
41 {
42         using namespace Qt;
43         QGraphicsRectItem *playgroundRect;
44         
45         /* All scene units are milimeters */
46         setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
47
48         /* playground border */
49         addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));           //left
50         addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));                //top
51         addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));             //right
52         addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));            //bottom
53         
54         /* playground */
55         playgroundRect = addRect(
56                                 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
57                                 QPen(),
58                                 QPixmap(":/images/playground_eurobot2012_1024.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
59                                 );
60         playgroundRect->setZValue(0);
61                 
62         /* obstacles */
63         obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
64         obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
65         obstacle->setZValue(5);
66         obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
67         obstacle->setVisible(false);
68         obstacle->setPos(QPointF(2000, 1000));
69         this->addItem(obstacle);
70 }
71
72 PlaygroundScene::~PlaygroundScene()
73 {
74 }
75
76 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
77 {
78         QPointF pos = scene2world(mouseEvent->scenePos());
79         emit mouseMoved(pos);
80         QGraphicsScene::mouseMoveEvent(mouseEvent);
81 }
82
83 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
84 {
85         QGraphicsScene::mousePressEvent(mouseEvent);
86         QPointF pos = scene2world(mouseEvent->scenePos());
87         obstacle->setPos(mouseEvent->scenePos().x(),
88                          mouseEvent->scenePos().y());
89         emit obstacleChanged(pos);
90 }
91
92 /*!
93     \fn PlaygroundScene::scene2world(QPointF scenePos)
94  */
95 QPointF PlaygroundScene::scene2world(QPointF scenePos)
96 {
97         return QPointF((scenePos.x()) / 1000.0,
98                        (scenePos.y()) / 1000.0);
99 }
100
101
102 /*!
103     \fn PlaygroundScene::world2scene(QPointF worldPos)
104  */
105 QPointF PlaygroundScene::world2scene(QPointF worldPos)
106 {
107         return QPointF(worldPos.x() * 1000,
108                        worldPos.y() * 1000);
109 }
110
111
112 /*!
113     \fn PlaygroundScene::showObstacle(int val)
114  */
115 void PlaygroundScene::showObstacle(int val)
116 {
117         if (val)
118                 obstacle->setVisible(true);
119         else
120                 obstacle->setVisible(false);
121 }