]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/robomon/PlaygroundScene.cpp
robomon: Set simulated obstacle visible on a click in playground scene
[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 // grid
39 #define GRID_HEIGHT       5000
40 #define GRID_WIDTH        5000
41
42 PlaygroundScene::PlaygroundScene(QObject *parent)
43         : QGraphicsScene(parent)
44 {
45         using namespace Qt;
46         QGraphicsRectItem *playgroundRect;
47         QGraphicsLineItem *tempLine;
48         
49         /* All scene units are milimeters */
50         setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
51
52         /* playground border */
53         addRect(QRect(0, 0, -BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));           //left
54         addRect(QRect(-BORDER_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));                //top
55         addRect(QRect(PLAYGROUND_WIDTH, 0, BORDER_WIDTH, PLAYGROUND_HEIGHT), QPen(), QBrush(BORDER_COLOR));             //right
56         addRect(QRect(-BORDER_WIDTH, -BORDER_WIDTH, PLAYGROUND_WIDTH + 2*BORDER_WIDTH, BORDER_WIDTH), QPen(), QBrush(BORDER_COLOR));            //bottom
57         
58         /* playground */
59         playgroundRect = addRect(
60                                 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
61                                 QPen() //,
62                                 // QPixmap(":/images/playground_sick-day-2012.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
63                                 );
64         playgroundRect->setZValue(0);
65
66         /* horizontal grid */
67         for (int i = 0; i < (PLAYGROUND_HEIGHT / GRID_HEIGHT); i++) {
68                 tempLine = addLine(QLine( 0, PLAYGROUND_HEIGHT - i*GRID_HEIGHT, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT - i*GRID_HEIGHT), QPen());
69                 tempLine->setZValue(1);
70         }
71
72         /* vertical grid */
73         for (int i = 0; i < (PLAYGROUND_WIDTH / GRID_WIDTH); i++) {
74                 tempLine = addLine(QLine(PLAYGROUND_WIDTH - i*GRID_WIDTH, PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH - i*GRID_WIDTH, 0), QPen());
75                 tempLine->setZValue(1);
76         }
77
78         /* opponent obstacle - placeable by mouse clic */
79         obstacle = new QGraphicsEllipseItem(0, 0, SIM_OBST_SIZE_M*1000, SIM_OBST_SIZE_M*1000);
80         obstacle->translate(-SIM_OBST_SIZE_M*1000/2,-SIM_OBST_SIZE_M*1000/2);
81         obstacle->setZValue(5);
82         obstacle->setBrush(QBrush(QColor(0, 0, 255, 200)));
83         obstacle->setVisible(false);
84         obstacle->setPos(QPointF(2000, 1000));
85         this->addItem(obstacle);
86 }
87
88 PlaygroundScene::~PlaygroundScene()
89 {
90 }
91
92 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
93 {
94         QPointF pos = scene2world(mouseEvent->scenePos());
95         emit mouseMoved(pos);
96         QGraphicsScene::mouseMoveEvent(mouseEvent);
97 }
98
99 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
100 {
101         QGraphicsScene::mousePressEvent(mouseEvent);
102         QPointF pos = scene2world(mouseEvent->scenePos());
103         obstacle->setVisible(true);
104         obstacle->setPos(mouseEvent->scenePos().x(),
105                          mouseEvent->scenePos().y());
106         emit obstacleChanged(pos);
107 }
108
109 /*!
110     \fn PlaygroundScene::scene2world(QPointF scenePos)
111  */
112 QPointF PlaygroundScene::scene2world(QPointF scenePos)
113 {
114         return QPointF((scenePos.x()) / 1000.0,
115                        (scenePos.y()) / 1000.0);
116 }
117
118
119 /*!
120     \fn PlaygroundScene::world2scene(QPointF worldPos)
121  */
122 QPointF PlaygroundScene::world2scene(QPointF worldPos)
123 {
124         return QPointF(worldPos.x() * 1000,
125                        worldPos.y() * 1000);
126 }
127
128
129 /*!
130     \fn PlaygroundScene::showObstacle(int val)
131  */
132 void PlaygroundScene::showObstacle(int val)
133 {
134         if (val)
135                 obstacle->setVisible(true);
136         else
137                 obstacle->setVisible(false);
138 }