diff --git a/imgui.ini b/imgui.ini index fecbc7a..d4c8a11 100644 --- a/imgui.ini +++ b/imgui.ini @@ -21,18 +21,18 @@ Collapsed=0 DockId=0x00000002,0 [Window][##TOAST0] -Pos=1010,635 -Size=250,65 +Pos=982,635 +Size=278,65 Collapsed=0 [Window][##TOAST1] -Pos=1010,560 +Pos=1010,543 Size=250,65 Collapsed=0 [Window][##TOAST2] -Pos=1010,485 -Size=250,65 +Pos=982,485 +Size=278,65 Collapsed=0 [Docking][Data] diff --git a/include/GuiManager.h b/include/GuiManager.h index c4477df..8185a25 100644 --- a/include/GuiManager.h +++ b/include/GuiManager.h @@ -6,6 +6,11 @@ #include "gui/ImGuiNotify.hpp" class Application; +struct LogEntry { + std::string message; + ImVec4 color; +}; + class GuiManager { public: @@ -15,13 +20,14 @@ public: void Initialize(Application* application); void Render(RenderTexture2D& renderTexture); void Shutdown(); - + std::vector logEntries; private: void SetupDockspace(RenderTexture2D& renderTexture); void RenderNotifications(); void RenderErrorBanner(); void SetTheme(); + bool showLogWindow = true; Application *app; }; \ No newline at end of file diff --git a/scripts/update.as b/scripts/update.as index 9ab5d7d..237741a 100644 --- a/scripts/update.as +++ b/scripts/update.as @@ -2,7 +2,7 @@ float x = 50; float y = 100; void Update(float dt) { - x += 640 * dt; + x += 64 * dt; if (x > 800) { x = 0; Print("X position reset!"); diff --git a/src/GuiManager.cpp b/src/GuiManager.cpp index 92018d8..9971a8d 100644 --- a/src/GuiManager.cpp +++ b/src/GuiManager.cpp @@ -7,17 +7,50 @@ #include "Application.h" #include // Add this for cross-platform file handling -#ifdef _WIN32 -#include -#include -#include -#undef ShowCursor // Prevent conflict with Windows API -#endif - GuiManager::GuiManager() {} GuiManager::~GuiManager() {} +void LogCallback(log_Event *ev) +{ + // Retrieve the GuiManager instance from udata + GuiManager *guiManager = static_cast(ev->udata); + + // Format the log message + char buffer[1024]; + vsnprintf(buffer, sizeof(buffer), ev->fmt, ev->ap); + + // Determine the color based on the log level + ImVec4 color; + switch (ev->level) + { + case LOG_TRACE: + color = ImVec4(0.5f, 0.5f, 0.5f, 1.0f); + break; // Gray + case LOG_DEBUG: + color = ImVec4(0.0f, 0.5f, 1.0f, 1.0f); + break; // Blue + case LOG_INFO: + color = ImVec4(0.0f, 1.0f, 0.0f, 1.0f); + break; // Green + case LOG_WARNING: + color = ImVec4(1.0f, 1.0f, 0.0f, 1.0f); + break; // Yellow + case LOG_ERROR: + color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f); + break; // Red + case LOG_FATAL: + color = ImVec4(1.0f, 0.0f, 1.0f, 1.0f); + break; // Magenta + default: + color = ImVec4(1.0f, 1.0f, 1.0f, 1.0f); + break; // White + } + + // Add the log entry to the vector + guiManager->logEntries.push_back({buffer, color}); +} + void GuiManager::Initialize(Application *application) { app = application; @@ -37,6 +70,9 @@ void GuiManager::Initialize(Application *application) iconsConfig.GlyphMinAdvanceX = iconFontSize; io.Fonts->AddFontFromMemoryCompressedTTF(fa_solid_900_compressed_data, fa_solid_900_compressed_size, iconFontSize, &iconsConfig, iconsRanges); SetTheme(); + logEntries.clear(); + log_add_callback(LogCallback, this, LOG_ALL); + log_trace("GuiManager::Initialize - Started"); } @@ -108,73 +144,17 @@ void GuiManager::SetupDockspace(RenderTexture2D &renderTexture) { // Log Viewer Window ImGui::Begin("Log Viewer"); - static std::string logContent; - static size_t lastFileSize = 0; - - // Use std::filesystem to construct the log file path - std::filesystem::path logFilePath = std::filesystem::current_path() / "log.txt"; - - // Debugging: Print the log file path to the console - std::cout << "Log file path: " << logFilePath << std::endl; - - // Check if the log file exists - if (std::filesystem::exists(logFilePath)) - { -#ifdef _WIN32 - // Convert the path to a narrow-character string - std::string logFilePathStr = logFilePath.string(); - - // Open the file with shared access on Windows - HANDLE hFile = CreateFileA(logFilePathStr.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (hFile != INVALID_HANDLE_VALUE) - { - LARGE_INTEGER fileSize; - if (GetFileSizeEx(hFile, &fileSize) && fileSize.QuadPart != lastFileSize) - { - lastFileSize = static_cast(fileSize.QuadPart); - logContent.resize(lastFileSize); - - DWORD bytesRead; - SetFilePointer(hFile, 0, NULL, FILE_BEGIN); - ReadFile(hFile, logContent.data(), static_cast(lastFileSize), &bytesRead, NULL); - } - CloseHandle(hFile); - } - else - { - std::cerr << "Failed to open log file with shared access: " << logFilePath << std::endl; - } -#else - // Standard file reading for non-Windows platforms - std::ifstream logFile(logFilePath, std::ios::ate); - if (logFile.is_open()) - { - size_t fileSize = logFile.tellg(); - if (fileSize != lastFileSize) - { - lastFileSize = fileSize; - logFile.seekg(0, std::ios::beg); - logContent.assign((std::istreambuf_iterator(logFile)), - std::istreambuf_iterator()); - } - logFile.close(); - } - else - { - std::cerr << "Failed to open log file: " << logFilePath << std::endl; - } -#endif - } - else - { - std::cerr << "Log file does not exist: " << logFilePath << std::endl; - } - - // Display the log content in a scrollable text area ImGui::BeginChild("LogText", ImVec2(0, 0), true, ImGuiWindowFlags_HorizontalScrollbar); - ImGui::TextUnformatted(logContent.c_str()); + + // Display each log entry with its color + for (const auto &entry : logEntries) + { + ImGui::TextColored(entry.color, "%s", entry.message.c_str()); + } + if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) ImGui::SetScrollHereY(1.0f); // Auto-scroll to the bottom + ImGui::EndChild(); ImGui::End(); }