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
0 commit comments