RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
DebugActions.h
Go to the documentation of this file.
1#pragma once
2#include <chrono>
3#include <cstdint>
4#include <string>
5#include <vector>
6#include <functional>
7#include <algorithm>
8
9#include "imgui.h"
10#include "DebugConsole.h"
11#include "Logger.h"
12#include "ImGuiToaster.h"
13
14namespace DebugActions
15{
16
17 // ---------- Small helpers ----------------------------------------------------
18 using Clock = std::chrono::steady_clock;
19
20 inline bool timeElapsedMs(Clock::time_point& last, int ms)
21 {
22 auto now = Clock::now();
23 if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last).count() >= ms)
24 {
25 last = now;
26 return true;
27 }
28 return false;
29 }
30
31 inline bool keyDownImGui(ImGuiKey imguiKey)
32 {
33 // If you use ImGui backend keys:
34 return ImGui::IsKeyDown(imguiKey);
35 }
36
37 // ---------- Global master switch (optional mirror of DebugConsole master) ----
38 inline bool gMasterEnabled = false;
39
40 // ---------- Debug: Toaster Notification Demo ---------------------------------
41 inline bool gEnableToasterTest = false;
42 inline Clock::time_point gToasterLast = Clock::now();
43
44 inline void ToasterChanged(std::weak_ptr<ImGuiToaster> toaster_, bool on)
45 {
46 Logger::instance().log("main", std::string("Toaster Notification Test ") + (on ? "ENABLED" : "DISABLED"));
47 if (auto t = toaster_.lock())
48 {
49 if (on)
50 {
51 t->Push(ImGuiToaster::Level::Info, "Toaster Debug ON!!", 1.0f);
52 }
53 else
54 {
55 t->Push(ImGuiToaster::Level::Info, "Toaster Debug OFF!!", 1.0f);
56 }
57 }
58 }
59
60 inline void ToasterTick(std::weak_ptr<ImGuiToaster> toaster_)
61 {
62 if (auto t = toaster_.lock())
63 {
64 if (ImGui::IsKeyPressed(ImGuiKey_1) && timeElapsedMs(gToasterLast, 200))
65 {
66 t->Push(ImGuiToaster::Level::Info, "Info: Hello!", 5.0f);
67 Logger::instance().log("main", Logger::Level::Info, "[toast] Info Demo");
68 }
69 if (ImGui::IsKeyPressed(ImGuiKey_2) && timeElapsedMs(gToasterLast, 200))
70 {
71 t->Push(ImGuiToaster::Level::Good, "Good: Saved", 4.0f);
72 Logger::instance().log("main", Logger::Level::Success, "[toast] Good Demo");
73 }
74 if (ImGui::IsKeyPressed(ImGuiKey_3) && timeElapsedMs(gToasterLast, 200))
75 {
76 t->Push(ImGuiToaster::Level::Warning, "Warning: Ping high", 6.0f);
77 Logger::instance().log("main", Logger::Level::Warn, "[toast] Warning Demo");
78 }
79 if (ImGui::IsKeyPressed(ImGuiKey_4) && timeElapsedMs(gToasterLast, 200))
80 {
81 t->Push(ImGuiToaster::Level::Error, "Error: Failed op");
82 Logger::instance().log("main", Logger::Level::Error, "[toast] Error Demo");
83 }
84 }
85 }
86
87 // ---------- Debug: Mouse Circle Overlay --------------------------------------
88 inline bool gEnableMouseCircle = false;
89 inline void MouseCircleChanged(bool on)
90 {
91 Logger::instance().log("main", std::string("Mouse Circle ") + (on ? "ENABLED" : "DISABLED"));
92 }
93 inline void MouseCircleTick()
94 {
95 // Draw a simple circle at mouse (using ImGui draw list for example)
96 ImDrawList* dl = ImGui::GetForegroundDrawList();
97 ImVec2 p = ImGui::GetMousePos();
98 float radius = 14.f;
99 ImU32 colFill = IM_COL32(255, 255, 0, 48);
100 ImU32 colLine = IM_COL32(255, 255, 0, 200);
101 dl->AddCircleFilled(p, radius, colFill, 24);
102 dl->AddCircle(p, radius, colLine, 24, 2.0f);
103 }
104
105 // ---------- Debug: FPS Overlay (example) -------------------------------------
106 inline bool gEnableFpsOverlay = false;
107 inline void FpsOverlayChanged(bool on)
108 {
109 Logger::instance().log("main", std::string("FPS Overlay ") + (on ? "ENABLED" : "DISABLED"));
110 }
111 inline void FpsOverlayTick()
112 {
113 // Small FPS text in top-left corner
114 ImDrawList* dl = ImGui::GetForegroundDrawList();
115 ImVec2 pos = ImVec2(12, 12);
116 char buf[64];
117 // Use ImGui::GetIO().Framerate or your timing system
118 snprintf(buf, sizeof(buf), "FPS: %.1f", ImGui::GetIO().Framerate);
119 dl->AddText(pos, IM_COL32(255, 255, 255, 220), buf);
120 }
121
122 // ---------- Debug: Network Ping Log (example) --------------------------------
123 inline bool gEnablePingLog = false;
124 inline Clock::time_point gPingLast = Clock::now();
125 inline void PingLogChanged(bool on)
126 {
127 Logger::instance().log("main", std::string("Ping Log ") + (on ? "ENABLED" : "DISABLED"));
128 }
129 inline void PingLogTick()
130 {
131 // Throttled periodic log
132 if (timeElapsedMs(gPingLast, 1000))
133 {
134 // Replace with a real metric if you have one
135 Logger::instance().log("main", "[net] heartbeat");
136 }
137 }
138
139 // ---------- Registration helpers ----------------------------------------------
140 inline void RegisterToasterToggles(std::weak_ptr<ImGuiToaster> toaster_)
141 {
143 "Toaster Notifications (1–4)",
145 /* onChanged */ [t = toaster_](bool on) { // capture the shared_ptr by value
146 ToasterChanged(t, on); // call your function WITH the arg
147 },
148 /* onTick */ [t = toaster_]() { // capture the shared_ptr by value
149 ToasterTick(t); // call your function WITH the arg
150 },
151 });
152 }
153
155 {
156 // Helper shim to avoid direct include dependency in this header:
157 auto add = [](const char* label, bool* f, std::function<void(bool)> oc, std::function<void()> ot)
158 {
159 auto toggle = DebugToggle{
160 label,
161 f,
162 oc,
163 ot};
164
166 };
167
168 add("Debug Mouse Circle", &gEnableMouseCircle, MouseCircleChanged, MouseCircleTick);
169
171
172 add("Network Ping Log", &gEnablePingLog, PingLogChanged, PingLogTick);
173 }
174
175} // namespace DebugActions
static void addToggle(const DebugToggle &t)
void log(const std::string &channel, const std::string &line)
Definition Logger.h:48
static Logger & instance()
Definition Logger.h:39
void FpsOverlayChanged(bool on)
std::chrono::steady_clock Clock
void MouseCircleChanged(bool on)
void ToasterChanged(std::weak_ptr< ImGuiToaster > toaster_, bool on)
bool gMasterEnabled
void PingLogTick()
bool gEnableToasterTest
Clock::time_point gPingLast
bool timeElapsedMs(Clock::time_point &last, int ms)
bool keyDownImGui(ImGuiKey imguiKey)
bool gEnableMouseCircle
void ToasterTick(std::weak_ptr< ImGuiToaster > toaster_)
void RegisterAllDefaultToggles()
void FpsOverlayTick()
void RegisterToasterToggles(std::weak_ptr< ImGuiToaster > toaster_)
Clock::time_point gToasterLast
void PingLogChanged(bool on)
void MouseCircleTick()
bool gEnableFpsOverlay