2 * PlaygroundScene.cpp 07/10/31
4 * Draw playground and likewise stuffs.
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
14 #include <QGraphicsSceneMouseEvent>
15 #include <QGraphicsScene>
16 #include <QGraphicsRectItem>
23 #include "PlaygroundScene.h"
27 //margin around borders (just for better look)
31 #define BORDER_WIDTH 10
32 #define BORDER_COLOR black
35 #define PLAYGROUND_WIDTH PLAYGROUND_WIDTH_MM
36 #define PLAYGROUND_HEIGHT PLAYGROUND_HEIGHT_MM
39 #define GRID_HEIGHT 5000
40 #define GRID_WIDTH 5000
42 PlaygroundScene::PlaygroundScene(QObject *parent)
43 : QGraphicsScene(parent)
46 QGraphicsRectItem *playgroundRect;
47 QGraphicsLineItem *tempLine;
49 /* All scene units are milimeters */
50 setSceneRect(QRectF(QPointF(-MARGIN, -MARGIN), QPointF(PLAYGROUND_WIDTH+MARGIN, PLAYGROUND_HEIGHT+MARGIN)));
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
59 playgroundRect = addRect(
60 QRect(0, 0, PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT),
62 // QPixmap(":/images/playground_sick-day-2012.png").scaled(PLAYGROUND_WIDTH, PLAYGROUND_HEIGHT)
64 playgroundRect->setZValue(0);
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);
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);
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);
88 PlaygroundScene::~PlaygroundScene()
92 void PlaygroundScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
94 QPointF pos = scene2world(mouseEvent->scenePos());
96 QGraphicsScene::mouseMoveEvent(mouseEvent);
99 void PlaygroundScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
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);
110 \fn PlaygroundScene::scene2world(QPointF scenePos)
112 QPointF PlaygroundScene::scene2world(QPointF scenePos)
114 return QPointF((scenePos.x()) / 1000.0,
115 (scenePos.y()) / 1000.0);
120 \fn PlaygroundScene::world2scene(QPointF worldPos)
122 QPointF PlaygroundScene::world2scene(QPointF worldPos)
124 return QPointF(worldPos.x() * 1000,
125 worldPos.y() * 1000);
130 \fn PlaygroundScene::showObstacle(int val)
132 void PlaygroundScene::showObstacle(int val)
135 obstacle->setVisible(true);
137 obstacle->setVisible(false);