diff --git a/gui/src/GUI/Data/Map.cpp b/gui/src/GUI/Data/Map.cpp index f10a0f77..57e90526 100644 --- a/gui/src/GUI/Data/Map.cpp +++ b/gui/src/GUI/Data/Map.cpp @@ -159,6 +159,7 @@ void Map::displayTacticalView(int start_x, int start_y, int end_x, int end_y, co float ressourceX = tileX + (i % 3) * tileSize / 3; float ressourceY = tileY + (i / 3) * tileSize / 3; + if (ressources[i] > 0 && ressources[i] <= 1) { if (ressources[i] > 0 && ressources[i] < 2) { DrawRectangle(ressourceX, ressourceY, tileSize / 3, tileSize / 3, ORANGE); } else if (ressources[i] >= 2) { diff --git a/gui/src/GUI/Display.cpp b/gui/src/GUI/Display.cpp index f9c7c90b..595c8e0b 100644 --- a/gui/src/GUI/Display.cpp +++ b/gui/src/GUI/Display.cpp @@ -44,7 +44,7 @@ void Display::handleEvent() if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) { map.checkCollision(offsetX + 400, offsetY, newWidth + offsetX, newHeight + offsetY, infoBox); } - messageBox.handleInput(offsetX, offsetY + newHeight - 300, 400, 300); + messageBox.handleInput(offsetX, offsetY + newHeight - 200, 400, 200); } void Display::run() @@ -60,7 +60,7 @@ void Display::run() DrawRectangle(offsetX, offsetY, newWidth, newHeight, RAYWHITE); map.displayTacticalView(offsetX + 400, offsetY, newWidth + offsetX, newHeight + offsetY, infoBox); infoBox.display(offsetX, offsetY, 400, 300); - messageBox.display(offsetX, offsetY + newHeight - 300, 400, 300); + messageBox.display(offsetX, offsetY + newHeight - 200, 400, 200); EndDrawing(); } } diff --git a/gui/src/GUI/MessageBox.cpp b/gui/src/GUI/MessageBox.cpp index 246070f3..d4444b7e 100644 --- a/gui/src/GUI/MessageBox.cpp +++ b/gui/src/GUI/MessageBox.cpp @@ -5,14 +5,15 @@ ** MessageBox */ + + +#include "MessageBox.hpp" #include #include #include - -#include "MessageBox.hpp" #include "define.hpp" -MessageBox::MessageBox() : m_scrollOffset(0), m_lineHeight(20) {} +MessageBox::MessageBox() : m_scrollOffset(0), m_lineHeight(20), m_totalLines(0), m_maxLines(0) {} void MessageBox::addMessage(const std::string &message, int user) { @@ -30,6 +31,11 @@ void MessageBox::addMessage(const std::string &message, int user) formattedMessage << "[" << timeStr << "] " << userStr << ": " << message; m_formattedMessages.push_back({now, userStr, {formattedMessage.str()}}); + + m_totalLines = 0; + for (const auto &msg : m_formattedMessages) { + m_totalLines += wrapText(msg.lines[0], m_lineHeight).size(); + } } std::string MessageBox::formatTime(const std::chrono::steady_clock::time_point &tp) const @@ -81,7 +87,8 @@ bool MessageBox::isMouseOver(int x, int y, int width, int height) const void MessageBox::scroll(int amount) { - m_scrollOffset = std::max(0, m_scrollOffset + amount); + int maxOffset = std::max(0, m_totalLines - m_maxLines); + m_scrollOffset = std::clamp(m_scrollOffset + amount, 0, maxOffset); } void MessageBox::handleInput(int x, int y, int width, int height) @@ -95,28 +102,35 @@ void MessageBox::handleInput(int x, int y, int width, int height) } } -void MessageBox::display(int x, int y, int width, int height) const +void MessageBox::display(int x, int y, int width, int height) { DrawRectangle(x, y, width, height, (Color){0, 0, 0, 200}); - int maxLines = height / m_lineHeight; - int totalLines = 0; + m_maxLines = height / m_lineHeight; + m_totalLines = 0; std::vector> wrappedMessages; for (const auto &msg : m_formattedMessages) { - wrappedMessages.push_back(wrapText(msg.lines[0], width, m_lineHeight)); - totalLines += wrappedMessages.back().size(); + wrappedMessages.push_back(wrapText(msg.lines[0], width - 20, m_lineHeight)); // Adjust width for scrollbar + m_totalLines += wrappedMessages.back().size(); } - int startLine = std::max(0, totalLines - maxLines - m_scrollOffset); + // Draw scrollbar + float scrollbarHeight = + static_cast(height) * (static_cast(m_maxLines) / static_cast(m_totalLines)); + float scrollbarY = y + (static_cast(m_scrollOffset) / static_cast(m_totalLines)) * height; + DrawRectangle(x + width - 20, y, 20, height, (Color){255, 255, 255, 50}); + DrawRectangle(x + width - 20, scrollbarY, 20, scrollbarHeight, (Color){255, 255, 255, 100}); + + int startLine = std::max(0, m_totalLines - m_maxLines - m_scrollOffset); int currentLine = 0; int lineCount = 0; - for (auto it = wrappedMessages.rbegin(); it != wrappedMessages.rend() && lineCount < maxLines; ++it) { + for (auto it = wrappedMessages.rbegin(); it != wrappedMessages.rend() && lineCount < m_maxLines; ++it) { const auto &msgLines = *it; for (const auto &line : msgLines) { - if (currentLine >= startLine && lineCount < maxLines) { + if (currentLine >= startLine && lineCount < m_maxLines) { DrawText(line.c_str(), x, y + (lineCount * m_lineHeight), m_lineHeight, WHITE); lineCount++; } diff --git a/gui/src/GUI/MessageBox.hpp b/gui/src/GUI/MessageBox.hpp index d61341eb..610de9d6 100644 --- a/gui/src/GUI/MessageBox.hpp +++ b/gui/src/GUI/MessageBox.hpp @@ -7,21 +7,21 @@ #pragma once -#include #include #include #include +#include class MessageBox { - public: +public: MessageBox(); void addMessage(const std::string &message, int user); - void display(int x, int y, int width, int height) const; + void display(int x, int y, int width, int height); void scroll(int amount); bool isMouseOver(int x, int y, int width, int height) const; void handleInput(int x, int y, int width, int height); - private: +private: struct FormattedMessage { std::chrono::steady_clock::time_point time; std::string user; @@ -29,10 +29,12 @@ class MessageBox { }; std::string formatTime(const std::chrono::steady_clock::time_point &tp) const; - std::vector wrapText(const std::string &text, int width, int fontSize) const; + std::vector wrapText(const std::string &text, int width, int fontSize = 20) const; int m_scrollOffset; int m_lineHeight; + int m_totalLines; + int m_maxLines; std::vector m_formattedMessages; std::vector m_team; };