-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_window.hpp
More file actions
200 lines (171 loc) · 6.22 KB
/
main_window.hpp
File metadata and controls
200 lines (171 loc) · 6.22 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
#ifndef MAIN_WINDOW_HPP_
#define MAIN_WINDOW_HPP_
#include "navmesh_display_base.hpp"
#include "navmesh_display.hpp"
#include "zoomable_scroll_area.hpp"
#include <Pathfinder/behaviorBuilder.h>
#include <Pathfinder/pathfinder.h>
#include <Pathfinder/triangle_lib_navmesh.h>
#include <QAction>
#include <QActionGroup>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QMainWindow>
#include <QProgressBar>
#include <QPushButton>
#include <QSlider>
#include <QString>
#include <atomic>
#include <memory>
#include <optional>
#include <thread>
#include <variant>
#include <vector>
class PointGenerator {
public:
PointGenerator(std::optional<pathfinder::Vector> start, std::optional<pathfinder::Vector> goal) : start_(start), goal_(goal) {
if (start_.has_value() == goal_.has_value()) {
// Only supports receiving a start or a goal, not both nor neither.
throw std::runtime_error("Given invalid start/goal pair");
}
}
virtual std::pair<pathfinder::Vector, pathfinder::Vector> getNext() = 0;
virtual int getCurrent() const = 0;
virtual int getTotal() const = 0;
pathfinder::Vector getLast() const {
return last_;
}
protected:
const std::optional<pathfinder::Vector> start_;
const std::optional<pathfinder::Vector> goal_;
pathfinder::Vector last_;
};
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
using TriangleLibNavmeshTriangulationType = pathfinder::navmesh::TriangleLibNavmesh;
using TriangleLibPathfinderType = pathfinder::Pathfinder<TriangleLibNavmeshTriangulationType>;
using TriangleLibPathfindingResult = TriangleLibPathfinderType::PathfindingResult;
// Toolbar UI elements
QAction *dragAction_;
QAction *movePathStartAction_;
QAction *movePathGoalAction_;
QAction *switchStartAndGoalAction_;
QActionGroup *toolbarActionGroup_;
// Config UI elements
QTabWidget *configTabWidget_;
std::optional<int> navmeshRegionTabIndex_;
QLineEdit *agentRadiusLineEdit_;
QSlider *agentRadiusSlider_;
QProgressBar *generationProgressBar_;
QCheckBox *showNoPathToGoalCheckBox_;
QCheckBox *showExceptionCheckBox_;
// Navmesh Triangulation config UI elements
QCheckBox *verticesCheckBox_;
QCheckBox *nonConstraintEdgesCheckBox_;
QCheckBox *triangleLabelsCheckBox_;
QCheckBox *edgeLabelsCheckBox_;
QCheckBox *vertexLabelsCheckBox_;
// Polyanya animation UI elements
QGroupBox *polyanyaAnimationGroupbox_;
QPushButton *stepBackPlaybackButton_;
QPushButton *startPlaybackButton_;
QPushButton *pausePlaybackButton_;
QPushButton *stopPlaybackButton_;
QPushButton *stepForwardPlaybackButton_;
QLineEdit *animationCurrentFrameLineEdit_;
QLabel *animationFrameCountLabel_;
NavmeshDisplayBase *navmeshDisplay_{nullptr};
// UI creation functions
void createMenubar();
void createToolbar();
void createLocalConnections();
template<typename NavmeshTriangulationType>
void createNavmeshDisplay();
void generateSingleStartRegular();
void generateSingleStartRandom();
void generateSingleGoalRegular();
void generateSingleGoalRandom();
template<typename TriangulationType>
void generate(const TriangulationType &triangulation, std::unique_ptr<PointGenerator> generator);
void stopGenerating();
void createConfigDock();
void populateConfigDock();
void createConnectionsToNavmeshDisplay();
// UI maninpulation functions
bool matchingAgentRadiusLineEditAndSlider_{false};
bool matchingTriangulationMinimumAngleLineEditAndSlider_{false};
void setAgentRadiusLineEdit();
void setAgentRadiusSlider();
// Navmesh data
std::optional<std::variant<TriangleLibNavmeshTriangulationType>> navmeshTriangulation_;
// TriangleLib-specific navmesh data
pathfinder::BehaviorBuilder behaviorBuilder_;
// Navmesh functions
void updateAgentRadius(double newRadius);
void resetPathData();
void resetAllPairsDistanceMap();
// TriangleLib-specific navmesh functions
void openPolyFile(const QString &filename);
void buildTriangleLibNavmeshTriangulationFromFile(QString fileName);
// Path data
double agentRadius_{7.5};
bool movePathStartEnabled_{false};
bool movePathGoalEnabled_{false};
std::optional<pathfinder::Vector> startPoint_, goalPoint_;
std::optional<std::variant<TriangleLibPathfindingResult>> pathfindingResult_;
// All pairs generation
public:
enum class AllPairsIsFor {
kNone,
kStart,
kGoal
};
private:
AllPairsIsFor allPairsIsFor_{AllPairsIsFor::kNone};
std::atomic_bool shouldStopGeneration_;
std::thread generationThread_;
// Path functions
void rebuildPath();
void movePathStart(const pathfinder::Vector &pos);
void movePathGoal(const pathfinder::Vector &pos);
signals:
void newPairsDistanceFound(double x, double y, double distance);
void generationProgressUpdate(int countCompleted, int countTotal);
private slots:
void addPairsDistance(double x, double y, double distance);
void updateGenerationProgress(int countCompleted, int countTotal);
void openNavmeshFilePrompt();
void animationDataUpdated(int currentIndex, int frameCount);
void draggingMouseOnNavmesh(const pathfinder::Vector &navmeshPoint);
void movingMouseOnNavmesh(const pathfinder::Vector &navmeshPoint);
void setMovePathStartEnabled(bool enabled);
void setMovePathGoalEnabled(bool enabled);
void switchStartAndGoal();
void agentRadiusTextChanged(const QString &text);
void agentRadiusSliderChanged(int value);
};
template<typename NavmeshDisplayType>
void MainWindow::createNavmeshDisplay() {
// Check if the navmesh display is already this type
if (navmeshDisplay_ != nullptr) {
if (dynamic_cast<NavmeshDisplayType*>(navmeshDisplay_) != nullptr) {
// Already the desired type.
return;
}
}
navmeshDisplay_ = new NavmeshDisplayType;
setCentralWidget(navmeshDisplay_);
// When we call setCentralWidget, we pass ownership. No need to worry about deleting the previous one, if there is one.
navmeshDisplay_->getNavmeshRenderArea()->setAgentRadius(agentRadius_);
navmeshDisplay_->setDragModeEnabled(dragAction_->isChecked());
setMovePathStartEnabled(movePathStartAction_->isChecked());
setMovePathGoalEnabled(movePathGoalAction_->isChecked());
createConnectionsToNavmeshDisplay();
populateConfigDock();
}
#endif // MAIN_WINDOW_HPP_