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

@@ -29,6 +29,8 @@ void SettingsScene::on_enter() {
m_slider_variable.sfx = config.get("volume.SFX", m_slider_variable.sfx);
m_slider_variable.rendering_distance = config.get(
"world.rendering_distance", m_slider_variable.rendering_distance);
m_slider_variable.player_voice =
config.get("volume.player_voice", m_slider_variable.player_voice);
}
void SettingsScene::on_leave() {
save_and_apply();
@@ -54,6 +56,7 @@ void SettingsScene::save_and_apply() {
m_slider_variable.rendering_distance);
config.set("volume.SFX", m_slider_variable.sfx);
config.set("volume.music", m_slider_variable.music);
config.set("volume.player_voice", m_slider_variable.player_voice);
config.save_to_file();
auto& app = m_scene_manager.app();
app.audio().reload_config();

View File

@@ -70,9 +70,7 @@ void WorldScene::render(Renderer& renderer) {
}
}
bool WorldScene::handle_event(const Event& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
return std::visit(
Overloaded{[this](const MouseMoveEvent& e) {
if (handle_mouse_move_event(e)) {
@@ -98,7 +96,12 @@ bool WorldScene::handle_event(const Event& e) {
}
return false;
},
[](const TextInputEvent&) { return false; },
[this](const TextInputEvent& e) {
if (handle_text_input_event(e)) {
return true;
}
return false;
},
[this](const WindowResizeEvent& e) {
handle_window_resize_event(e);
return false;
@@ -110,7 +113,7 @@ bool WorldScene::handle_event(const Event& e) {
}
void WorldScene::on_enter() {
auto& param = m_scene_manager.world_scene_param();
load_config();
m_error_ui.init();
if (param.host_game) {
@@ -136,7 +139,7 @@ void WorldScene::on_enter() {
m_dev_panel.init();
m_pasue_menu.init();
m_hud_ui.init();
m_scene_manager.app().audio().set_client(m_client);
m_scene_manager.app().window().set_game_running(true);
} catch (const std::exception& e) {
m_error_ui.set_error(e.what());
@@ -158,6 +161,7 @@ void WorldScene::on_re_enter() {
m_error_ui.on_re_enter();
m_pasue_menu.on_re_enter();
m_client_world.reload_config();
load_config();
auto width = m_scene_manager.app().renderer().window_width();
auto height = m_scene_manager.app().renderer().window_height();
@@ -165,7 +169,27 @@ void WorldScene::on_re_enter() {
WindowResizeEvent{static_cast<int>(width), static_cast<int>(height)});
}
void WorldScene::load_config() {
auto type = m_scene_manager.app().config().get("voice_input", "PTT");
if (type == "off") {
m_input_type = VoiceInputType::OFF;
m_client_world.get_audio().audio_recording().stop();
} else if (type == "PTT") {
m_input_type = VoiceInputType::PTT;
m_client_world.get_audio().audio_recording().stop();
} else if (type == "always") {
m_input_type = VoiceInputType::ALWAYS;
m_client_world.get_audio().audio_recording().start();
}
}
bool WorldScene::handle_mouse_move_event(const MouseMoveEvent& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -186,6 +210,12 @@ bool WorldScene::handle_mouse_move_event(const MouseMoveEvent& e) {
return false;
}
bool WorldScene::handle_mouse_button_event(const MouseButtonEvent& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -206,23 +236,26 @@ bool WorldScene::handle_mouse_button_event(const MouseButtonEvent& e) {
}
bool WorldScene::handle_window_resize_event(const WindowResizeEvent& e) {
if (m_pasue_menu.handle_event(e)) {
return true;
}
if (m_hud_ui.handle_event(e)) {
return true;
}
if (m_camera.handle_event(e)) {
return true;
}
m_error_ui.handle_event(e);
m_pasue_menu.handle_event(e);
m_hud_ui.handle_event(e);
m_camera.handle_event(e);
// world event needs to be processed last
if (m_client_world.handle_event(e)) {
return true;
}
m_client_world.handle_event(e);
return false;
}
bool WorldScene::handle_mouse_wheel_event(const MouseWheelEvent& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
if (m_chatting) {
return true;
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -242,8 +275,15 @@ bool WorldScene::handle_mouse_wheel_event(const MouseWheelEvent& e) {
return false;
}
bool WorldScene::handle_key_event(const KeyEvent& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
if (e.key == Key::ESCAPE && e.action == KeyAction::PRESS) {
if (m_chatting && !m_paused) {
set_chatting(false, false);
return true;
}
bool pasued = pause();
pasued = !pasued;
set_pause(pasued);
@@ -257,6 +297,35 @@ bool WorldScene::handle_key_event(const KeyEvent& e) {
m_show_dev_pannel = !m_show_dev_pannel;
return true;
}
if (e.key == Key::T && e.action == KeyAction::PRESS) {
if (!m_chatting) {
set_chatting(true, true);
return true;
}
}
if (e.key == Key::ENTER && e.action == KeyAction::PRESS) {
if (m_chatting) {
set_chatting(false, true);
return true;
}
}
if (e.key == Key::V) {
if (m_input_type == VoiceInputType::PTT) {
auto& recording = m_client_world.get_audio().audio_recording();
if (e.action == KeyAction::PRESS) {
recording.start();
return true;
}
if (e.action == KeyAction::RELEASE) {
recording.stop();
return true;
}
}
}
if (m_paused) {
if (m_pasue_menu.handle_event(e)) {
return true;
@@ -265,6 +334,9 @@ bool WorldScene::handle_key_event(const KeyEvent& e) {
if (m_hud_ui.handle_event(e)) {
return true;
}
if (m_chatting) {
return true;
}
if (m_camera.handle_event(e)) {
return true;
}
@@ -276,6 +348,17 @@ bool WorldScene::handle_key_event(const KeyEvent& e) {
return false;
}
bool WorldScene::handle_text_input_event(const TextInputEvent& e) {
if (m_error_ui.has_error()) {
return m_error_ui.handle_event(e);
}
if (m_paused) {
return m_pasue_menu.handle_text_input_event(e);
}
return m_hud_ui.handle_text_input_event(e);
}
Camera& WorldScene::camera() { return m_camera; }
SceneManager& WorldScene::scene_manager() { return m_scene_manager; }
ClientWorld& WorldScene::client_world() { return m_client_world; }
@@ -286,13 +369,34 @@ void WorldScene::set_pause(bool pause) {
return;
}
m_paused = pause;
set_mouse(pause);
}
void WorldScene::set_mouse(bool enable) {
auto& window = m_scene_manager.app().window();
window.set_game_running(!m_paused);
if (m_paused) {
window.set_game_running(!enable);
if (enable) {
m_client_world.reset_key_status();
}
}
void WorldScene::set_chatting(bool chatting, bool send) {
if (m_paused) {
m_hud_ui.set_chatting(false, false);
return;
}
m_chatting = chatting;
m_hud_ui.set_chatting(chatting, send);
set_mouse(chatting);
}
// Not thread safe
void WorldScene::handle_chat_message(ChatMessage& message) {
m_hud_ui.add_chat_message(message);
}
bool WorldScene::is_recording() const {
return m_client_world.get_audio().audio_recording().is_recording();
}
void WorldScene::set_error(std::string_view error) {
Logger::error("WorldScene Error Set {}", error);
m_error_ui.set_error(error);