Skip to content

Commit ed5bab5

Browse files
committed
Create menubar and more improvements
1 parent 5dc48d9 commit ed5bab5

File tree

21 files changed

+317
-197
lines changed

21 files changed

+317
-197
lines changed

include/Game.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class Game {
3636
void update();
3737
void render();
3838
void initGuiTheme();
39-
void renderGui();
39+
void renderMenuBar(ImGuiIO& io);
4040
void setGraphBasedPlanner(const int id);
4141
void setSamplingBasedPlanner(const int id);
42+
void showHowToUseWindow();
43+
void showAboutWindow();
4244

4345
private:
4446
sf::RenderWindow* window_;
@@ -47,6 +49,10 @@ class Game {
4749
float dt_;
4850
std::stack<std::unique_ptr<State>> states_;
4951
std::string curr_planner_;
52+
std::shared_ptr<LoggerPanel> logger_panel_;
53+
bool disable_run_;
54+
bool show_how_to_use_window_{false};
55+
bool show_about_window_{false};
5056
};
5157

5258
} // namespace path_finding_visualizer

include/LoggerPanel.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#pragma once
2+
3+
#include <imgui-SFML.h>
4+
#include <imgui.h>
5+
6+
namespace path_finding_visualizer {
7+
8+
class LoggerPanel {
9+
public:
10+
LoggerPanel() {
11+
AutoScroll = true;
12+
clear();
13+
}
14+
15+
void clear() {
16+
Buf.clear();
17+
LineOffsets.clear();
18+
LineOffsets.push_back(0);
19+
}
20+
21+
void render(const char* title) {
22+
if (!ImGui::Begin(title)) {
23+
ImGui::End();
24+
return;
25+
}
26+
27+
ImGui::BeginChild("scrolling", ImVec2(0, 0), false,
28+
ImGuiWindowFlags_HorizontalScrollbar);
29+
30+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));
31+
const char* buf = Buf.begin();
32+
const char* buf_end = Buf.end();
33+
{
34+
ImGuiListClipper clipper;
35+
clipper.Begin(LineOffsets.Size);
36+
while (clipper.Step()) {
37+
for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd;
38+
line_no++) {
39+
const char* line_start = buf + LineOffsets[line_no];
40+
const char* line_end = (line_no + 1 < LineOffsets.Size)
41+
? (buf + LineOffsets[line_no + 1] - 1)
42+
: buf_end;
43+
ImGui::TextUnformatted(line_start, line_end);
44+
}
45+
}
46+
clipper.End();
47+
}
48+
ImGui::PopStyleVar();
49+
50+
if (AutoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
51+
ImGui::SetScrollHereY(1.0f);
52+
53+
ImGui::EndChild();
54+
ImGui::End();
55+
}
56+
57+
void info(const std::string& msg) { AddLog("[INFO] %s\n", msg.c_str()); }
58+
59+
private:
60+
ImGuiTextBuffer Buf;
61+
ImVector<int> LineOffsets; // Index to lines offset. We maintain this with
62+
// AddLog() calls.
63+
bool AutoScroll; // Keep scrolling if already at the bottom.
64+
65+
void AddLog(const char* fmt, ...) IM_FMTARGS(2) {
66+
int old_size = Buf.size();
67+
va_list args;
68+
va_start(args, fmt);
69+
Buf.appendfv(fmt, args);
70+
va_end(args);
71+
for (int new_size = Buf.size(); old_size < new_size; old_size++)
72+
if (Buf[old_size] == '\n') LineOffsets.push_back(old_size + 1);
73+
}
74+
};
75+
76+
} // namespace path_finding_visualizer

include/State.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#include <fstream>
77
#include <iostream>
88
#include <map>
9+
#include <memory>
910
#include <stack>
1011
#include <string>
1112
#include <vector>
1213

14+
#include "LoggerPanel.h"
15+
1316
/*
1417
State Base Class
1518
*/
@@ -20,21 +23,27 @@ class State {
2023
private:
2124
protected:
2225
std::stack<std::unique_ptr<State>> &states_;
23-
2426
sf::RenderWindow *window_;
27+
std::shared_ptr<LoggerPanel> logger_panel_;
2528
sf::Vector2i mousePositionWindow_;
2629
bool quit_;
30+
bool is_reset_;
31+
bool is_running_;
2732

2833
public:
2934
// Constructor
30-
State(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states);
35+
State(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
36+
std::shared_ptr<LoggerPanel> logger_panel);
3137

3238
// Destructor
3339
virtual ~State();
3440

3541
// Accessors
3642
const bool getQuit() const;
3743

44+
void setReset(bool is_reset) { is_reset_ = is_reset; }
45+
void setRunning(bool is_running) { is_running_ = is_running; }
46+
3847
// Functions
3948
virtual void checkForQuit();
4049
virtual void updateMousePosition();

include/States/Algorithms/GraphBased/ASTAR/ASTAR.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ struct MinimumDistanceASTAR {
2121
class ASTAR : public BFS {
2222
public:
2323
// Constructor
24-
ASTAR(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states);
24+
ASTAR(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
25+
std::shared_ptr<LoggerPanel> logger_panel);
2526

2627
// Destructor
2728
virtual ~ASTAR();

include/States/Algorithms/GraphBased/BFS/BFS.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace graph_based {
1111
class BFS : public GraphBased {
1212
public:
1313
// Constructor
14-
BFS(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states);
14+
BFS(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
15+
std::shared_ptr<LoggerPanel> logger_panel);
1516

1617
// Destructor
1718
virtual ~BFS();

include/States/Algorithms/GraphBased/DFS/DFS.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace graph_based {
1010
class DFS : public BFS {
1111
public:
1212
// Constructor
13-
DFS(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states);
13+
DFS(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
14+
std::shared_ptr<LoggerPanel> logger_panel);
1415

1516
// Destructor
1617
virtual ~DFS();

include/States/Algorithms/GraphBased/DIJKSTRA/DIJKSTRA.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct MinimumDistanceDIJKSTRA {
2121
class DIJKSTRA : public BFS {
2222
public:
2323
// Constructor
24-
DIJKSTRA(sf::RenderWindow *window,
25-
std::stack<std::unique_ptr<State>> &states);
24+
DIJKSTRA(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
25+
std::shared_ptr<LoggerPanel> logger_panel);
2626

2727
// Destructor
2828
virtual ~DIJKSTRA();

include/States/Algorithms/GraphBased/GraphBased.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <queue>
1111
#include <vector>
1212

13+
#include "LoggerPanel.h"
1314
#include "MessageQueue.h"
1415
#include "State.h"
1516
#include "States/Algorithms/GraphBased/Node.h"
@@ -22,7 +23,8 @@ class GraphBased : public State {
2223
public:
2324
// Constructor
2425
GraphBased(sf::RenderWindow* window,
25-
std::stack<std::unique_ptr<State>>& states);
26+
std::stack<std::unique_ptr<State>>& states,
27+
std::shared_ptr<LoggerPanel> logger_panel);
2628

2729
// Destructor
2830
virtual ~GraphBased();
@@ -72,9 +74,7 @@ class GraphBased : public State {
7274
std::shared_ptr<MessageQueue<bool>> message_queue_;
7375

7476
// logic flags
75-
bool is_running_;
7677
bool is_initialized_;
77-
bool is_reset_;
7878
bool is_solved_;
7979
bool disable_run_;
8080
bool disable_gui_parameters_;

include/States/Algorithms/SamplingBased/RRT/RRT.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace sampling_based {
1111
class RRT : public SamplingBased {
1212
public:
1313
// Constructor
14-
RRT(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states);
14+
RRT(sf::RenderWindow *window, std::stack<std::unique_ptr<State>> &states,
15+
std::shared_ptr<LoggerPanel> logger_panel, const std::string &name);
1516

1617
// Destructor
1718
virtual ~RRT();

include/States/Algorithms/SamplingBased/RRT_STAR/RRT_STAR.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace sampling_based {
1010
class RRT_STAR : public RRT {
1111
public:
1212
// Constructor
13-
RRT_STAR(sf::RenderWindow* window,
14-
std::stack<std::unique_ptr<State>>& states);
13+
RRT_STAR(sf::RenderWindow* window, std::stack<std::unique_ptr<State>>& states,
14+
std::shared_ptr<LoggerPanel> logger_panel, const std::string& name);
1515

1616
// Destructor
1717
virtual ~RRT_STAR();

0 commit comments

Comments
 (0)