From 5f9344550659e3465fd7a083f1dae06a18e84054 Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Thu, 16 Oct 2025 09:38:58 +0300 Subject: [PATCH] Made names bold in chat --- src/Server/MainServer.cpp | 3 +- src/Toolbar.cpp | 76 +++++++++++++++++---------------------- src/Toolbar.hpp | 14 +++++--- src/World.cpp | 23 ++++++------ src/World.hpp | 2 +- 5 files changed, 56 insertions(+), 62 deletions(-) diff --git a/src/Server/MainServer.cpp b/src/Server/MainServer.cpp index 051141e..37bf269 100644 --- a/src/Server/MainServer.cpp +++ b/src/Server/MainServer.cpp @@ -204,9 +204,8 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID): }); netServer->add_recv_callback(SERVER_CHAT_MESSAGE, [&](std::shared_ptr client, cereal::PortableBinaryInputArchive& message) { std::string chatMessage; - Vector4f c{1.0f, 1.0f, 1.0f, 1.0f}; message(chatMessage); - netServer->send_items_to_all_clients_except(client, RELIABLE_COMMAND_CHANNEL, CLIENT_CHAT_MESSAGE, chatMessage, c); + netServer->send_items_to_all_clients_except(client, RELIABLE_COMMAND_CHANNEL, CLIENT_CHAT_MESSAGE, client->customID, chatMessage); }); netServer->add_recv_callback(SERVER_NEW_BOOKMARK, [&](std::shared_ptr client, cereal::PortableBinaryInputArchive& message) { std::string name; diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index 2c658e6..9358f07 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #define UPDATE_DOWNLOAD_URL "https://infinipaint.com/download.html" @@ -825,6 +826,33 @@ void Toolbar::bookmark_menu(bool justOpened) { gui.pop_id(); } +std::unique_ptr Toolbar::build_paragraph_from_chat_message(const ChatMessage& message, float alpha) { + skia::textlayout::ParagraphStyle pStyle; + pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft); + skia::textlayout::TextStyle tStyle; + tStyle.setFontSize(io->fontSize); + tStyle.setFontFamilies({SkString{"Roboto"}}); + tStyle.setFontStyle(SkFontStyle::Bold()); + tStyle.setForegroundColor(SkPaint{color_mul_alpha(message.type == ChatMessage::JOIN ? io->theme->fillColor4 : io->theme->frontColor1, alpha)}); + pStyle.setTextStyle(tStyle); + + skia::textlayout::ParagraphBuilderImpl a(pStyle, io->fontCollection, SkUnicodes::ICU::Make()); + if(message.type == ChatMessage::JOIN) { + std::string messageName = message.name + " "; + a.addText(messageName.c_str(), messageName.length()); + } + else { + std::string messageName = "[" + message.name + "] "; + a.addText(messageName.c_str(), messageName.length()); + } + + tStyle.setFontStyle(SkFontStyle::Normal()); + a.pushStyle(tStyle); + a.addText(message.message.c_str(), message.message.length()); + + return a.Build(); +} + void Toolbar::chat_box() { constexpr float CHATBOX_WIDTH = 700; if(main.world->network_being_used()) { @@ -870,30 +898,10 @@ void Toolbar::chat_box() { }) { int id = 0; for(auto& chatMessage : main.world->chatMessages | std::views::reverse) { - id++; - SkColor4f c; - switch(chatMessage.color) { - case ChatMessage::COLOR_NORMAL: - c = io->theme->frontColor1; - break; - case ChatMessage::COLOR_JOIN: - c = io->theme->fillColor4; - break; - } gui.push_id(id); - skia::textlayout::ParagraphStyle pStyle; - pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft); - skia::textlayout::TextStyle tStyle; - tStyle.setFontSize(io->fontSize); - tStyle.setFontFamilies({SkString{"Roboto"}}); - tStyle.setForegroundColor(SkPaint{c}); - pStyle.setTextStyle(tStyle); - - skia::textlayout::ParagraphBuilderImpl a(pStyle, io->fontCollection, SkUnicodes::ICU::Make()); - a.addText(chatMessage.text.c_str(), chatMessage.text.length()); - - gui.text_paragraph("text", a.Build(), CHATBOX_WIDTH); + gui.text_paragraph("text", build_paragraph_from_chat_message(chatMessage, 1.0f), CHATBOX_WIDTH); gui.pop_id(); + id++; } } gui.pop_id(); @@ -930,19 +938,9 @@ void Toolbar::chat_box() { gui.push_id("messages popup"); int id = 0; for(auto& chatMessage : main.world->chatMessages | std::views::reverse) { - id++; chatMessage.time.update_time_since(); if(chatMessage.time < DISPLAY_TIME) { float a = 1.0f - lerp_time(chatMessage.time, DISPLAY_TIME, FADE_START_TIME); - SkColor4f c{0, 0, 0, 0}; - switch(chatMessage.color) { - case ChatMessage::COLOR_NORMAL: - c = io->theme->frontColor1; - break; - case ChatMessage::COLOR_JOIN: - c = io->theme->fillColor4; - break; - } CLAY({ .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) }, @@ -955,21 +953,11 @@ void Toolbar::chat_box() { }) { gui.push_id(id); gui.obstructing_window(); - skia::textlayout::ParagraphStyle pStyle; - pStyle.setTextAlign(skia::textlayout::TextAlign::kLeft); - skia::textlayout::TextStyle tStyle; - tStyle.setFontSize(io->fontSize); - tStyle.setFontFamilies({SkString{"Roboto"}}); - tStyle.setForegroundColor(SkPaint{color_mul_alpha(c, a)}); - pStyle.setTextStyle(tStyle); - - skia::textlayout::ParagraphBuilderImpl a(pStyle, io->fontCollection, SkUnicodes::ICU::Make()); - a.addText(chatMessage.text.c_str(), chatMessage.text.length()); - - gui.text_paragraph("text", a.Build(), CHATBOX_WIDTH); + gui.text_paragraph("text", build_paragraph_from_chat_message(chatMessage, a), CHATBOX_WIDTH); gui.pop_id(); } } + id++; } gui.pop_id(); } diff --git a/src/Toolbar.hpp b/src/Toolbar.hpp index f6ebdb8..824a339 100644 --- a/src/Toolbar.hpp +++ b/src/Toolbar.hpp @@ -4,6 +4,7 @@ #include "Helpers/FileDownloader.hpp" #include "Server/CommandList.hpp" #include +#include #include #include #include @@ -22,11 +23,12 @@ class Toolbar { }; struct ChatMessage { - std::string text; - enum Color { - COLOR_NORMAL = 0, - COLOR_JOIN - } color; + std::string name; + std::string message; + enum Type { + NORMAL = 0, + JOIN + } type; TimePoint time; }; @@ -222,6 +224,8 @@ class Toolbar { std::filesystem::path testing; + std::unique_ptr build_paragraph_from_chat_message(const ChatMessage& message, float alpha); + void start_gui(); void end_gui(); diff --git a/src/World.cpp b/src/World.cpp index baa1d70..e67a969 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -6,6 +6,7 @@ #include "Server/CommandList.hpp" #include "MainProgram.hpp" #include "SharedTypes.hpp" +#include "Toolbar.hpp" #include "WorldGrid.hpp" #include "cereal/archives/portable_binary.hpp" #include @@ -96,14 +97,14 @@ void World::init_client_callbacks() { message(id, displayName, cursorColor); clients[id].displayName = displayName; clients[id].cursorColor = cursorColor; - add_chat_message(clients[id].displayName + " joined", Toolbar::ChatMessage::COLOR_JOIN); + add_chat_message(clients[id].displayName, "joined", Toolbar::ChatMessage::Type::JOIN); }); con.client_add_recv_callback(CLIENT_USER_DISCONNECT, [&](cereal::PortableBinaryInputArchive& message) { ServerPortionID id; message(id); auto it = clients.find(id); if(it != clients.end()) { - add_chat_message(clients[id].displayName + " left", Toolbar::ChatMessage::COLOR_JOIN); + add_chat_message(clients[id].displayName, "left", Toolbar::ChatMessage::Type::JOIN); clients.erase(id); } }); @@ -114,9 +115,12 @@ void World::init_client_callbacks() { message(c.camCoords, c.windowSize, c.cursorPos); }); con.client_add_recv_callback(CLIENT_CHAT_MESSAGE, [&](cereal::PortableBinaryInputArchive& message) { + ServerPortionID senderID; std::string chatMessage; - message(chatMessage); - add_chat_message(chatMessage, Toolbar::ChatMessage::COLOR_NORMAL); + message(senderID, chatMessage); + auto it = clients.find(senderID); + if(it != clients.end()) + add_chat_message(clients[senderID].displayName, chatMessage, Toolbar::ChatMessage::Type::NORMAL); }); con.client_add_recv_callback(CLIENT_CANVAS_COLOR, [&](cereal::PortableBinaryInputArchive& message) { Vector3f newBackColor; @@ -207,15 +211,14 @@ void World::set_canvas_background_color(const Vector3f& newBackColor, bool sendC void World::send_chat_message(const std::string& message) { if(!clientStillConnecting) { - std::string newMessage = "[" + displayName + "] " + message; - con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_CHAT_MESSAGE, newMessage); - add_chat_message(newMessage, Toolbar::ChatMessage::COLOR_NORMAL); + con.client_send_items_to_server(RELIABLE_COMMAND_CHANNEL, SERVER_CHAT_MESSAGE, message); + add_chat_message(displayName, message, Toolbar::ChatMessage::Type::NORMAL); } } -void World::add_chat_message(const std::string& message, Toolbar::ChatMessage::Color color) { - Logger::get().log("CHAT", message); - chatMessages.emplace_front(Toolbar::ChatMessage{message, color}); +void World::add_chat_message(const std::string& name, const std::string& message, Toolbar::ChatMessage::Type type) { + Logger::get().log("CHAT", type == Toolbar::ChatMessage::JOIN ? (name + " " + message) : ("[" + name + "] " + message)); + chatMessages.emplace_front(Toolbar::ChatMessage{name, message, type}); if(chatMessages.size() == CHAT_SIZE) chatMessages.pop_back(); } diff --git a/src/World.hpp b/src/World.hpp index 838e8d3..7994ddd 100644 --- a/src/World.hpp +++ b/src/World.hpp @@ -59,7 +59,7 @@ class World { void start_hosting(const std::string& initNetSource, const std::string& serverLocalID); void send_chat_message(const std::string& message); - void add_chat_message(const std::string& message, Toolbar::ChatMessage::Color color); + void add_chat_message(const std::string& name, const std::string& message, Toolbar::ChatMessage::Type type); WorldVec get_mouse_world_pos(); WorldVec get_mouse_world_move();