Skip to content

Commit

Permalink
feat(gui/msgbox): add visual scrollbar
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelR-T authored Jun 3, 2024
1 parent 07b936d commit 6c555f0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions gui/src/GUI/Data/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions gui/src/GUI/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
}
}
Expand Down
38 changes: 26 additions & 12 deletions gui/src/GUI/MessageBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
** MessageBox
*/



#include "MessageBox.hpp"
#include <algorithm>
#include <iomanip>
#include <sstream>

#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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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<std::vector<std::string>> 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<float>(height) * (static_cast<float>(m_maxLines) / static_cast<float>(m_totalLines));
float scrollbarY = y + (static_cast<float>(m_scrollOffset) / static_cast<float>(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++;
}
Expand Down
12 changes: 7 additions & 5 deletions gui/src/GUI/MessageBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,34 @@

#pragma once

#include <chrono>
#include <raylib.h>
#include <string>
#include <vector>
#include <chrono>

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;
std::vector<std::string> lines;
};

std::string formatTime(const std::chrono::steady_clock::time_point &tp) const;
std::vector<std::string> wrapText(const std::string &text, int width, int fontSize) const;
std::vector<std::string> 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<FormattedMessage> m_formattedMessages;
std::vector<std::string> m_team;
};

0 comments on commit 6c555f0

Please sign in to comment.