forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogviewer.cpp
More file actions
126 lines (104 loc) · 3.8 KB
/
Copy pathlogviewer.cpp
File metadata and controls
126 lines (104 loc) · 3.8 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
#include "logviewer.hpp"
#include "imgui.hpp"
#include "logbuffer.hpp"
extern "C"
{
#include "style.h"
}
namespace
{
constexpr float ViewerW = VITA_WIDTH * 0.9f;
constexpr float ViewerH = VITA_HEIGHT * 0.85f;
}
void LogViewer::render(const pkgi_input& input)
{
const auto lines = pkgi_log_buffer_snapshot();
const int n = static_cast<int>(lines.size());
// ── Navigation ────────────────────────────────────────────────────────────
// Use input.active — same field as pkgi_do_main — so repeat rate and
// initial-press delay are identical to the main game list.
bool nav_step = false;
if ((input.active & PKGI_BUTTON_UP) && n > 0)
{
_selected = std::max(0, _selected - 1);
nav_step = true;
}
else if ((input.active & PKGI_BUTTON_DOWN) && n > 0)
{
_selected = std::min(n - 1, _selected + 1);
nav_step = true;
}
// Clamp in case log buffer shrank
if (n == 0)
_selected = 0;
else
_selected = std::max(0, std::min(_selected, n - 1));
// Inverted order: index 0 is newest entry (top of viewport).
// _selected is still in [0, n-1], where 0 means newest.
// ── Window ────────────────────────────────────────────────────────────────
ImGui::SetNextWindowPos(
ImVec2((VITA_WIDTH - ViewerW) / 2.f,
(VITA_HEIGHT - ViewerH) / 2.f));
ImGui::SetNextWindowSize(ImVec2(ViewerW, ViewerH), 0);
ImGui::Begin(
"Log Viewer###logviewer",
nullptr,
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoSavedSettings |
ImGuiWindowFlags_NoScrollbar |
ImGuiWindowFlags_NoScrollWithMouse);
ImGui::Text("Lines: %d", n);
ImGui::Separator();
const float footer_h =
ImGui::GetFrameHeightWithSpacing() + ImGui::GetStyle().ItemSpacing.y;
// NoNav: we handle navigation ourselves
ImGui::BeginChild(
"##logrows",
ImVec2(0.f, -footer_h),
false,
ImGuiWindowFlags_NoNav);
if (n == 0)
{
ImGui::TextDisabled("(no log messages yet)");
}
else
{
for (int i = 0; i < n; ++i)
{
ImGui::PushID(i);
const int idx = n - 1 - i; // newest first
const bool selected = (i == _selected);
const LogEntry& entry = lines[idx];
const char* text = entry.text.empty() ? " " : entry.text.c_str();
// Color by level
ImVec4 col;
switch (entry.level)
{
case LogLevel::Error:
col = ImVec4(1.00f, 0.35f, 0.35f, 1.f);
break;
case LogLevel::Warn:
col = ImVec4(1.00f, 0.85f, 0.10f, 1.f);
break;
default:
col = ImVec4(1.00f, 1.00f, 1.00f, 1.f);
break;
}
ImGui::PushStyleColor(ImGuiCol_Text, col);
ImGui::Selectable(text, selected, ImGuiSelectableFlags_Disabled);
ImGui::PopStyleColor();
// Scroll the selected row into view whenever navigation occurred
if (selected && nav_step)
ImGui::SetScrollHereY(0.5f);
ImGui::PopID();
}
}
ImGui::EndChild();
ImGui::Separator();
ImGui::TextDisabled(
"[Up/Down] navigate "
"(hold 1s = fast scroll) "
"[Circle] close");
ImGui::End();
}