-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
executable file
·211 lines (172 loc) · 4.18 KB
/
mainwindow.cpp
File metadata and controls
executable file
·211 lines (172 loc) · 4.18 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtConcurrent>
#include <QLayout>
#include <QTimer>
#include "graphicswidget.h"
#include "hexgrid/gridpainter.h"
#include "hexgrid/gridsearcher.h"
#include "hexgrid/hexgrid.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow()),
graphicsWidget(new GraphicsWidget(this)),
grid(new HexGrid(this, 50, 40)),
searchTimer(new QTimer(this)),
painter(new GridPainter(*grid)),
searcher(nullptr),
searchChannel(nullptr),
leftMouseButton(false)
{
ui->setupUi(this);
layout()->addWidget(graphicsWidget);
graphicsWidget->draw(grid);
connect(searchTimer, &QTimer::timeout,
this, &MainWindow::on_searchTimer_timeout);
}
MainWindow::~MainWindow()
{
}
void MainWindow::on_searchTimer_timeout(void)
{
if (searchChannel != nullptr)
{
//try to get a thing from the search channel
GridSearchEvent searchEvent;
if (searchChannel->pop(searchEvent))
{
//take the info from the search event and use it to modify the grid
if (searchEvent.eventType == GridSearchEvent::BACKTRACE)
{
grid->getEntry(searchEvent.point).path = true;
}
else if (searchEvent.eventType == GridSearchEvent::EXPAND)
{
grid->getEntry(searchEvent.point).searched = true;
}
grid->getEntry(searchEvent.point).modified = true;
}
else
{
//we didn't successfully get an item from the channel. the search must be over.
searchTimer->stop();
searchChannel = nullptr;
}
graphicsWidget->draw(grid);
}
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
leftMouseButton = true;
QPoint pickedCell = graphicsWidget->pickCell(graphicsWidget->mapFromGlobal(QCursor::pos()));
painter->start(pickedCell);
mouseMoveEvent(event);
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
leftMouseButton = false;
}
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event)
if (leftMouseButton && !searchTimer->isActive())
{
QPoint pickedCell = graphicsWidget->pickCell(graphicsWidget->mapFromGlobal(QCursor::pos()));
painter->paint(pickedCell);
}
graphicsWidget->draw(grid);
}
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
Q_UNUSED(event)
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_W:
case Qt::Key_S:
case Qt::Key_G:
case Qt::Key_O:
painter->keyPressed((Qt::Key)event->key());
break;
case Qt::Key_Return:
case Qt::Key_Enter:
startSearch();
break;
case Qt::Key_Space:
togglePauseSearch();
break;
case Qt::Key_Delete:
case Qt::Key_Backspace:
cancelSearch();
break;
case Qt::Key_Escape:
cancelSearch();
grid->resetAll();
break;
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_W:
case Qt::Key_S:
case Qt::Key_G:
case Qt::Key_O:
painter->keyReleased((Qt::Key)event->key());
break;
}
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
Q_UNUSED(event)
}
void MainWindow::startSearch(void)
{
grid->resetSearched();
//create a new grid searcher
searcher = std::unique_ptr<GridSearcher>(new GridSearcher(*grid));
//create a new channel to put results into
searchChannel = std::make_shared<Channel<GridSearchEvent>>(
Channel<GridSearchEvent>::BLOCK, 20);
//start the search process in a new thread
//don't store the future object because we 100% don't care what happens to the thread
QtConcurrent::run(
searcher.get(),
&GridSearcher::search,
searchChannel
);
//start the timer that will pull results out every 1ms
searchTimer->start(0);
}
void MainWindow::cancelSearch(void)
{
if (searchTimer->isActive())
{
//we won't be using any more of the results of the search, so just close the channel
searchChannel->closeFront();
searchChannel = nullptr;
//stop the search timer
searchTimer->stop();
}
//wipe all the search results from the grid
grid->resetSearched();
//redraw the grid
graphicsWidget->draw(grid);
}
void MainWindow::togglePauseSearch(void)
{
if (searchTimer->isActive())
searchTimer->stop();
else
if (searchChannel != nullptr)
searchTimer->start();
}