-
Notifications
You must be signed in to change notification settings - Fork 0
/
streetwidget.cpp
128 lines (111 loc) · 3.29 KB
/
streetwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "streetwidget.h"
#include "graphiccore.h"
#include "core.h"
#include <QPaintEvent>
#include <QBitmap>
#include <cmath> // std::ceil
#ifdef ENABLE_DRAW_TIME_MEASUREMENT
#include <QDebug>
#include <chrono>
#endif
StreetWidget::StreetWidget(QWidget* parent) : QGraphicsView(parent)
{
QObject::connect(this, &StreetWidget::start_update, this, &StreetWidget::update);
this->setAutoFillBackground(true);
this->setPalette(QPalette(Qt::black));
this->setScene(&scene);
//setDragMode(QGraphicsView::ScrollHandDrag);
//setDragMode(QGraphicsView::RubberBandDrag);
this->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
this->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
update();
}
StreetWidget::~StreetWidget()
{
GraphicCore::stop();
}
void StreetWidget::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_Space)
GraphicCore::step();
else if(event->key() == Qt::Key_N)
GraphicCore::new_game();
else if(event->key() == Qt::Key_R)
{
if(GraphicCore::is_running())
GraphicCore::stop();
else
GraphicCore::start();
}
else if(event->key() == Qt::Key_Up)
{
if(event->modifiers() & Qt::ControlModifier)
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - 100);
else
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() - 10);
}
else if(event->key() == Qt::Key_Down)
{
if(event->modifiers() & Qt::ControlModifier)
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() + 100);
else
this->verticalScrollBar()->setValue(this->verticalScrollBar()->value() + 10);
}
else if(event->key() == Qt::Key_Left)
{
if(event->modifiers() & Qt::ControlModifier)
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - 100);
else
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() - 10);
}
else if(event->key() == Qt::Key_Right)
{
if(event->modifiers() & Qt::ControlModifier)
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() + 100);
else
this->horizontalScrollBar()->setValue(this->horizontalScrollBar()->value() + 10);
}
else if(event->key() == Qt::Key_Plus)
this->scale(1.1, 1.1);
else if(event->key() == Qt::Key_Minus)
this->scale(0.9, 0.9);
update();
event->ignore();
}
void StreetWidget::wheelEvent(QWheelEvent* event)
{
double scale_factor = 1 + (event->angleDelta().y() / 360. * 0.3);
this->scale(scale_factor, scale_factor);
update();
}
void StreetWidget::reset_position()
{
this->resetTransform();
this->resetMatrix();
this->horizontalScrollBar()->setValue(0);
this->verticalScrollBar()->setValue(0);
}
void StreetWidget::enable_focus()
{
// enable own focus
this->setFocusPolicy(Qt::StrongFocus);
}
void StreetWidget::disable_focus()
{
// disable own focus
this->setFocusPolicy(Qt::NoFocus);
}
void StreetWidget::update()
{
std::lock_guard<typename std::remove_reference<decltype(GraphicCore::get_mutex())>::type> lock(GraphicCore::get_mutex());
if(current_lanes != Core::get_lanes() || current_length != Core::get_length())
{
scene.clear();
for(std::size_t lane = 0; lane < Core::get_lanes(); ++lane)
for(std::size_t pos = 0; pos < Core::get_length(); ++pos)
scene.addItem(new CarItem(pos, lane));
current_lanes = Core::get_lanes();
current_length = Core::get_length();
}
scene.update(sceneRect());
}