|
1 | 1 | #include <imgui.h> |
| 2 | +#include <imgui_internal.h> |
2 | 3 | #include <facade/editor/common.hpp> |
3 | 4 | #include <facade/editor/log.hpp> |
4 | 5 | #include <facade/util/fixed_string.hpp> |
5 | 6 |
|
6 | 7 | namespace facade::editor { |
7 | | -void Log::render() { |
8 | | - ImGui::SetNextWindowSize({500.0f, 200.0f}, ImGuiCond_Once); |
9 | | - if (auto window = editor::Window{"Log", &show}) { |
10 | | - ImGui::Text("%s", FixedString{"Count: {}", m_display_count}.c_str()); |
11 | | - ImGui::SameLine(); |
12 | | - float spacing = ImGui::GetStyle().ItemInnerSpacing.x; |
13 | | - ImGui::PushButtonRepeat(true); |
14 | | - if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { m_display_count = std::clamp(m_display_count - 10, 0, 1000); } |
15 | | - ImGui::SameLine(0.0f, spacing); |
16 | | - if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { m_display_count = std::clamp(m_display_count + 10, 0, 1000); } |
17 | | - ImGui::PopButtonRepeat(); |
| 8 | +void Log::render(NotClosed<Window> window) { |
| 9 | + ImGui::Text("%s", FixedString{"Count: {}", m_display_count}.c_str()); |
| 10 | + ImGui::SameLine(); |
| 11 | + float spacing = ImGui::GetStyle().ItemInnerSpacing.x; |
| 12 | + ImGui::PushButtonRepeat(true); |
| 13 | + auto const max_logs = static_cast<int>(logger::buffer_size().total()); |
| 14 | + if (ImGui::ArrowButton("##left", ImGuiDir_Left)) { m_display_count = std::clamp(m_display_count - 10, 0, max_logs); } |
| 15 | + ImGui::SameLine(0.0f, spacing); |
| 16 | + if (ImGui::ArrowButton("##right", ImGuiDir_Right)) { m_display_count = std::clamp(m_display_count + 10, 0, max_logs); } |
| 17 | + ImGui::PopButtonRepeat(); |
18 | 18 |
|
19 | | - static constexpr std::string_view levels_v[] = {"Error", "Warn", "Info", "Debug"}; |
20 | | - static constexpr auto max_log_level_v = debug_v ? logger::Level::eDebug : logger::Level::eInfo; |
21 | | - for (logger::Level l = logger::Level::eError; l <= max_log_level_v; l = static_cast<logger::Level>(static_cast<int>(l) + 1)) { |
22 | | - ImGui::SameLine(); |
23 | | - ImGui::Checkbox(levels_v[static_cast<std::size_t>(l)].data(), &m_level_filter[l]); |
24 | | - } |
| 19 | + static constexpr std::string_view levels_v[] = {"Error", "Warn", "Info", "Debug"}; |
| 20 | + static constexpr auto max_log_level_v = debug_v ? logger::Level::eDebug : logger::Level::eInfo; |
| 21 | + for (logger::Level l = logger::Level::eError; l <= max_log_level_v; l = static_cast<logger::Level>(static_cast<int>(l) + 1)) { |
| 22 | + ImGui::SameLine(); |
| 23 | + ImGui::Checkbox(levels_v[static_cast<std::size_t>(l)].data(), &m_level_filter[l]); |
| 24 | + } |
| 25 | + ImGui::SameLine(); |
| 26 | + ImGui::SeparatorEx(ImGuiSeparatorFlags_Vertical); |
| 27 | + ImGui::SameLine(); |
| 28 | + ImGui::Checkbox("Auto-scroll", &m_auto_scroll); |
25 | 29 |
|
26 | | - logger::access_buffer(*this); |
27 | | - auto child = editor::Window{window, "scroll", {}, {}, ImGuiWindowFlags_HorizontalScrollbar}; |
28 | | - static constexpr auto im_colour = [](logger::Level const l) { |
29 | | - switch (l) { |
30 | | - case logger::Level::eError: return ImVec4{1.0f, 0.0f, 0.0f, 1.0f}; |
31 | | - case logger::Level::eWarn: return ImVec4{1.0f, 1.0f, 0.0f, 1.0f}; |
32 | | - default: |
33 | | - case logger::Level::eInfo: return ImVec4{1.0f, 1.0f, 1.0f, 1.0f}; |
34 | | - case logger::Level::eDebug: return ImVec4{0.5f, 0.5f, 0.5f, 1.0f}; |
35 | | - } |
36 | | - }; |
37 | | - if (auto style = editor::StyleVar{ImGuiStyleVar_ItemSpacing, glm::vec2{}}) { |
38 | | - for (auto const* entry : m_list) ImGui::TextColored(im_colour(entry->level), "%s", entry->message.c_str()); |
| 30 | + ImGui::Separator(); |
| 31 | + logger::access_buffer(*this); |
| 32 | + auto child = editor::Window{window, "scroll", {}, {}, ImGuiWindowFlags_HorizontalScrollbar}; |
| 33 | + static constexpr auto im_colour = [](logger::Level const l) { |
| 34 | + switch (l) { |
| 35 | + case logger::Level::eError: return ImVec4{1.0f, 0.0f, 0.0f, 1.0f}; |
| 36 | + case logger::Level::eWarn: return ImVec4{1.0f, 1.0f, 0.0f, 1.0f}; |
| 37 | + default: |
| 38 | + case logger::Level::eInfo: return ImVec4{1.0f, 1.0f, 1.0f, 1.0f}; |
| 39 | + case logger::Level::eDebug: return ImVec4{0.5f, 0.5f, 0.5f, 1.0f}; |
39 | 40 | } |
| 41 | + }; |
| 42 | + auto span = std::span{m_list}; |
| 43 | + auto const max_size = static_cast<std::size_t>(m_display_count); |
| 44 | + if (span.size() > max_size) { span = span.subspan(span.size() - max_size); } |
| 45 | + if (auto style = editor::StyleVar{ImGuiStyleVar_ItemSpacing, glm::vec2{}}) { |
| 46 | + for (auto const* entry : span) ImGui::TextColored(im_colour(entry->level), "%s", entry->message.c_str()); |
40 | 47 | } |
| 48 | + |
| 49 | + if (m_auto_scroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) { ImGui::SetScrollHereY(1.0f); } |
41 | 50 | } |
42 | 51 |
|
43 | 52 | void Log::operator()(std::span<logger::Entry const> entries) { |
44 | 53 | m_list.clear(); |
45 | 54 | for (auto const& entry : entries) { |
46 | | - if (m_list.size() >= static_cast<std::size_t>(m_display_count)) { break; } |
47 | 55 | if (!m_level_filter[entry.level]) { continue; } |
48 | 56 | m_list.push_back(&entry); |
49 | 57 | } |
|
0 commit comments