Made names bold in chat

This commit is contained in:
Yousef Khadadeh 2025-10-16 09:38:58 +03:00
parent 921e111255
commit 5f93445506
5 changed files with 56 additions and 62 deletions

View file

@ -204,9 +204,8 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
});
netServer->add_recv_callback(SERVER_CHAT_MESSAGE, [&](std::shared_ptr<NetServer::ClientData> 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<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
std::string name;

View file

@ -22,6 +22,7 @@
#include <modules/skparagraph/include/ParagraphStyle.h>
#include <modules/skparagraph/include/FontCollection.h>
#include <modules/skparagraph/include/TextStyle.h>
#include <include/core/SkFontStyle.h>
#include <modules/skunicode/include/SkUnicode_icu.h>
#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<skia::textlayout::Paragraph> 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<float>(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();
}

View file

@ -4,6 +4,7 @@
#include "Helpers/FileDownloader.hpp"
#include "Server/CommandList.hpp"
#include <filesystem>
#include <modules/skparagraph/include/Paragraph.h>
#include <nlohmann/json.hpp>
#include <Helpers/Serializers.hpp>
#include <SDL3/SDL_dialog.h>
@ -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<skia::textlayout::Paragraph> build_paragraph_from_chat_message(const ChatMessage& message, float alpha);
void start_gui();
void end_gui();

View file

@ -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 <fstream>
@ -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();
}

View file

@ -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();