feature: chat (#36)

* fix(config): load config on demand in get/set instead of asserting

* feat(chat): implement in-game chat box with UTF-8 support

- Add ChatMessage struct and ChatBox widget for displaying and sending messages
- Introduce split_utf8() in text_tools for splitting UTF-8 strings by character count
- Add TimeTools utility (get_time_ticks) for message timestamps
- Update TextField with start_typing/stop_typing methods and set_typing()
- Integrate text input handling in WorldScene and WorldUIManager for chat
- Add utf8cpp library for robust UTF-8 validation and processing
- Add ChatBox rendering and input logic (src/ui/chat_box.cpp)
- Register ChatBox source in CMakeLists.txt
- Fix Renderer::render_lable to skip rendering if VAO is null
- Update credits to include utf8cpp library

* fix(ui): correct chat box layout with dynamic height and bottom anchor

* feat(chat): implement client-server chat messaging

Add protobuf definition for ChatMsg, new packet ID CHAT_MSG (4001), and
handle sending/receiving chat messages on both client and server side.
Wire up UI components (ChatBox, TextField) to allow message input and
display, including a message queue in ClientWorld for thread-safe
processing. Also add default initialization for ChatMessage::time.

* feat(chat): distinguish between send and cancel in chat input

* feat(ui): add background to Label widget and refactor debug overlay

* refactor(ui): decouple ChatBox from image dependencies, allow custom TextField backgrounds

TextField's background is now a generic `Widget` pointer, enabling use of any widget (e.g., `Rect`, `Image`) as background. `ChatBox` no longer creates its own `TextField`; it accepts one via `set_text_field`. Removed `set_d_image` and `set_default_image` in favor of `set_background`. Added a shared default image constant in `default_image.hpp`. Updated `host_game_ui.cpp`, `join_game_ui.cpp`, and `world_ui_manager.cpp` to construct backgrounds accordingly.

* refactor(ui): move width, height, and fill properties to base Widget

Consolidate width, height, and fill (fill_parent, fill_width, fill_height) into the base Widget class. Replace per-widget set_fill(bool) with set_fill_parent(bool), set_fill_width(bool), and set_fill_height(bool).

* refactor(ui): generalize Label background, add ChatBox width method

* feat(chat): implement message expiration after 5 seconds

* feat(ui): add word wrap for chat messages with font width measurement

* feat(audio): implement voice chat using Opus codec

Add voice chat support with Opus encoding/decoding, push-to-talk (V key), and new audio recording infrastructure.

* feat(audio): implement voice chat with Opus codec and streaming audio source

Add AudioStreamSource for continuous PCM playback. Refactor voice send/receive to encode with Opus and stream to a dedicated source instead of per-frame sources. Enable voice transmission over network.

* feat(audio): add spatial voice source with underwater effects

Update voice source to support spatial positioning, low-pass filter, and reverb effect for underwater immersion. Adjust source pool size and low-pass filter parameter. Correct receive_voice parameter type.

* feat(settings): add player voice volume and voice input type options

- Add player voice volume slider and voice input type combo in settings UI.
- Add localization strings for player voice and voice input types.
- Add config overloads for const char* default values.
- Implement voice input types (PTT, always, off) in world scene.
- Refactor WorldScene member order and add load_config method.
- Update audio engine to use player voice volume.
- Downgrade some logging to debug.

* fix(event): delegate error UI events to each handler

Move error UI event check from top of handle_event into each individual
event handler (mouse_move, mouse_button, mouse_wheel, key, text_input).
Also flatten window resize dispatch: all sub-handlers are called regardless
of previous handler return values, and the method always returns false.

* feat(chat): add colored system messages for player join/exit

* chore(logging): reduce verbosity for audio loading and input events

* fix(settings): fix typo in config key "vloume"

* feat(ui): add microphone icon when recording voice

* feat(voice): limit voice message range to 48 units

* chore(logging): downgrade text input log level to debug

* feat(credits): add license identifiers and reorganize license files

* feat: add sensitive word filter for chat messages

Implement AC automaton-based filtering using a sensitive lexicon loaded from a JSON asset file. The filter is applied to both player names and chat messages before broadcasting, and can be toggled via the `sensitive_filter` config option.

* feat(server): add config option to disable voice chat

* fix: correct utf8 validation, add missing include, remove debug log
This commit is contained in:
zhenyan121
2026-07-21 10:21:05 +08:00
committed by GitHub
parent 0f731530da
commit 45d2c8234a
90 changed files with 3931 additions and 357 deletions

View File

@@ -154,6 +154,28 @@ TextMesh Font::vertices(const std::string& text) {
return {std::move(vertices), max_x - min_x, max_y - min_y, 0, 0};
}
float Font::text_width(const std::string& text) {
auto& f = Font::get();
hb_buffer_t* buffer = hb_buffer_create();
hb_buffer_add_utf8(buffer, text.c_str(), text.size(), 0, text.size());
hb_buffer_guess_segment_properties(buffer);
hb_shape(f.m_hb_font, buffer, nullptr, 0);
unsigned int count = 0;
auto* positions = hb_buffer_get_glyph_positions(buffer, &count);
hb_position_t advance = 0;
for (unsigned int i = 0; i < count; ++i) {
advance += positions[i].x_advance;
}
hb_buffer_destroy(buffer);
return static_cast<float>(advance) / 64.0f;
}
const Texture* Font::text_texture() { return m_text_texture.get(); }
const std::string& Font::font_path() { return m_font_path; }

View File

@@ -0,0 +1,118 @@
#include "Cubed/tools/sensitive_filter.hpp"
#include <queue>
#include <utf8cpp/utf8.h>
using nlohmann::json;
namespace Cubed {
SensitiveFilter::SensitiveFilter() {
};
void SensitiveFilter::load(const nlohmann::json& j) {
trie.clear();
trie.emplace_back();
for (const auto& item : j["words"]) {
auto str = item.get<std::string>();
std::u32string word;
utf8::utf8to32(str.begin(), str.end(), std::back_inserter(word));
insert(word);
}
build();
}
std::string SensitiveFilter::filter(std::string_view str) {
if (!utf8::is_valid(str.begin(), str.end())) {
return {};
}
int state = 0;
std::vector<char32_t> text;
auto it = str.begin();
while (it != str.end()) {
text.push_back(utf8::next(it, str.end()));
}
std::vector<uint8_t> mask(text.size(), 0);
for (size_t i = 0; i < text.size(); i++) {
char32_t ch = text[i];
auto it = trie[state].next.find(ch);
while (state && it == trie[state].next.end()) {
state = trie[state].fail;
it = trie[state].next.find(ch);
}
if (it != trie[state].next.end()) {
state = it->second;
}
int t = state;
while (t) {
if (trie[t].end) {
size_t begin = i + 1 - trie[t].length;
for (size_t j = begin; j < begin + trie[t].length; j++)
mask[j] = true;
}
t = trie[t].fail;
}
}
std::string out;
for (size_t i = 0; i < text.size();) {
if (mask[i]) {
out += "***";
while (i < text.size() && mask[i])
++i;
} else {
// char32_t -> UTF8
utf8::append(text[i], std::back_inserter(out));
++i;
}
}
return out;
}
void SensitiveFilter::insert(const std::u32string& word) {
int now = 0;
for (char32_t ch : word) {
auto it = trie[now].next.find(ch);
if (it == trie[now].next.end()) {
trie[now].next[ch] = trie.size();
trie.emplace_back();
}
now = trie[now].next[ch];
}
trie[now].end = true;
trie[now].length = word.size();
}
void SensitiveFilter::build() {
std::queue<int> q;
for (auto [ch, to] : trie[0].next) {
trie[to].fail = 0;
q.push(to);
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto [ch, v] : trie[u].next) {
int f = trie[u].fail;
while (f && !trie[f].next.count(ch))
f = trie[f].fail;
if (trie[f].next.count(ch))
trie[v].fail = trie[f].next[ch];
q.push(v);
}
}
}
} // namespace Cubed