]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/PlaygroundScene.cpp
robofsm: Strategy
[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
27 //margin around borders (just for better look)
28 #define MARGIN  40
29
30 //playground borders
31 #define BORDER_WIDTH    10
32 #define BORDER_COLOR    black
33
34 //playground
35 #define PLAYGROUND_WIDTH        PLAYGROUND_WIDTH_MM
36 #define PLAYGROUND_HEIGHT       PLAYGROUND_HEIGHT_MM
37
38 PlaygroundScene::PlaygroundScene(QObject *parent)
39         : QGraphicsScene(parent)
40 {
41         using namespace Qt;
42         QGraphicsRectItem *playgroundRect;
43         
44         /* All scene units are milimeters */
45         setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
46
47         /* playground border */
48         addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));           //left
49         addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));                //top
50         addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));             //right
51         addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));            //bottom
52         
53         /* playground */
54         playgroundRect = addRect(
55                                 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
56                                 QPen(),
57                                 QPixmap(":/images/playground_eurobot2012_1024.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
58                                 );
59         playgroundRect->setZValue(0);
60                 
61         /* obstacles */
62         obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
63         obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
64         obstacle->setZValue(5);
65         obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
66         obstacle->setVisible(false);
67         obstacle->setPos(QPointF(2000, 1000));
68         this->addItem(obstacle);
69 }
70
71 PlaygroundScene::~PlaygroundScene()
72 {
73 }
74
75 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
76 {
77         QPointF pos = scene2world(mouseEvent->scenePos());
78         emit mouseMoved(pos);
79         QGraphicsScene::mouseMoveEvent(mouseEvent);
80 }
81
82 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
83 {
84         QGraphicsScene::mousePressEvent(mouseEvent);
85         QPointF pos = scene2world(mouseEvent->scenePos());
86         obstacle->setPos(mouseEvent->scenePos().x(),
87                          mouseEvent->scenePos().y());
88         emit obstacleChanged(pos);
89 }
90
91 /*!
92     \fn PlaygroundScene::scene2world(QPointF scenePos)
93  */
94 QPointF PlaygroundScene::scene2world(QPointF scenePos)
95 {
96         return QPointF((scenePos.x()) / 1000.0,
97                        (scenePos.y()) / 1000.0);
98 }
99
100
101 /*!
102     \fn PlaygroundScene::world2scene(QPointF worldPos)
103  */
104 QPointF PlaygroundScene::world2scene(QPointF worldPos)
105 {
106         return QPointF(worldPos.x() * 1000,
107                        worldPos.y() * 1000);
108 }
109
110
111 /*!
112     \fn PlaygroundScene::showObstacle(int val)
113  */
114 void PlaygroundScene::showObstacle(int val)
115 {
116         if (val)
117                 obstacle->setVisible(true);
118         else
119                 obstacle->setVisible(false);
120 }