mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
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:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "Cubed/tools/system_info.hpp"
|
||||
#include "Cubed/tools/system_window_manager.hpp"
|
||||
#include "Cubed/ui/rect.hpp"
|
||||
#include "version.hpp"
|
||||
|
||||
#include <SDL3/SDL_video.h>
|
||||
@@ -19,15 +20,33 @@ std::unique_ptr<DebugCollector>& DebugCollector::get_ptr() {
|
||||
}
|
||||
void DebugCollector::destory() { get_ptr().reset(); }
|
||||
void DebugCollector::init(int width, int height) {
|
||||
constexpr float SCALE = 0.6f;
|
||||
constexpr float SCALE = 0.7f;
|
||||
constexpr Color COLOR = Color::GRAY;
|
||||
constexpr float ALPHA = 0.6f;
|
||||
|
||||
m_widget.set_window_size(width, height);
|
||||
m_widget.set_spacing(15);
|
||||
m_widget.set_anchor(Anchor::TOP_LEFT);
|
||||
m_widget.set_offset({0, 5});
|
||||
m_widget.set_child_anchor(ColumnLayoutAnchor::LEFT);
|
||||
|
||||
auto add_label = [&](std::string_view text, std::string_view key = "") {
|
||||
auto& label = m_widget.add_child<Label>();
|
||||
auto rect = std::make_unique<Rect>(&label);
|
||||
rect->set_color(COLOR).set_alpha(ALPHA).set_fill_parent(true);
|
||||
label.set_background(std::move(rect));
|
||||
label.set_color(Color::WHITE).set_text(text).set_scale(SCALE);
|
||||
if (!key.empty()) {
|
||||
auto [_, insert] =
|
||||
m_component.try_emplace(std::string(key), &label);
|
||||
if (!insert) {
|
||||
Logger::error("DebugCollector Key {} already esist", key);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// version_text
|
||||
auto& version_text = m_widget.add_child<Label>();
|
||||
|
||||
std::string version{"Version: " CUBED_VERSION};
|
||||
|
||||
#ifdef DEBUG_MODE
|
||||
@@ -36,87 +55,56 @@ void DebugCollector::init(int width, int height) {
|
||||
version.append("-release");
|
||||
#endif
|
||||
|
||||
version_text.set_color(Color::WHITE).set_text(version).set_scale(SCALE);
|
||||
add_label(version);
|
||||
|
||||
{
|
||||
auto& compiler = m_widget.add_child<Label>();
|
||||
compiler.set_text(Tools::get_compiler_info()).set_scale(SCALE);
|
||||
}
|
||||
add_label(Tools::get_compiler_info());
|
||||
|
||||
// fps
|
||||
auto& fps_text = m_widget.add_child<Label>();
|
||||
fps_text.set_text("FPS: 0").set_scale(SCALE);
|
||||
m_component.try_emplace("fps", &fps_text);
|
||||
|
||||
add_label("FPS: 0", "fps");
|
||||
|
||||
// player_pos
|
||||
auto& player_pos_text = m_widget.add_child<Label>();
|
||||
player_pos_text.set_text("x: 0.00 y: 0.00 z: 0.00").set_scale(SCALE);
|
||||
m_component.try_emplace("player_pos", &player_pos_text);
|
||||
add_label("x: 0.00 y: 0.00 z: 0.00", "player_pos");
|
||||
|
||||
// rendered_chunk
|
||||
auto& rendered_chunk_text = m_widget.add_child<Label>();
|
||||
rendered_chunk_text.set_text("Rendered Chunk: 0").set_scale(SCALE);
|
||||
m_component.try_emplace("rendered_chunk", &rendered_chunk_text);
|
||||
add_label("Rendered Chunk: 0", "rendered_chunk");
|
||||
|
||||
// rss
|
||||
auto& rss_text = m_widget.add_child<Label>();
|
||||
rss_text.set_text("RSS: 0mb").set_scale(SCALE);
|
||||
m_component.try_emplace("rss", &rss_text);
|
||||
add_label("RSS: 0mb", "rss");
|
||||
|
||||
// os
|
||||
std::string os;
|
||||
auto& os_text = m_widget.add_child<Label>();
|
||||
os_text.set_scale(SCALE);
|
||||
std::string os_text;
|
||||
if (Tools::get_os_version(os)) {
|
||||
os_text.set_text("OS: " + os);
|
||||
os_text = "OS: " + os;
|
||||
Logger::info("OS System: {}", os);
|
||||
} else {
|
||||
os_text.set_text("OS: Unknown");
|
||||
os_text = "OS: Unknown";
|
||||
}
|
||||
add_label(os_text);
|
||||
{
|
||||
std::string wm{"WM: "};
|
||||
wm.append(Tools::detect_wm());
|
||||
auto& wm_label = m_widget.add_child<Label>();
|
||||
wm_label.set_text(wm).set_scale(SCALE);
|
||||
add_label(wm);
|
||||
}
|
||||
// cpu
|
||||
auto& cpu_text = m_widget.add_child<Label>();
|
||||
cpu_text.set_text("CPU: " + Tools::get_cpu_info()).set_scale(SCALE);
|
||||
add_label("CPU: " + Tools::get_cpu_info());
|
||||
|
||||
// gpu
|
||||
auto& gpu_text = m_widget.add_child<Label>();
|
||||
gpu_text
|
||||
.set_text(std::string{"GPU: "} +
|
||||
reinterpret_cast<const char*>(glGetString(GL_RENDERER)))
|
||||
.set_scale(SCALE);
|
||||
|
||||
add_label(std::string{"GPU: "} +
|
||||
reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
|
||||
// opengl_version
|
||||
auto& opengl_version_text = m_widget.add_child<Label>();
|
||||
opengl_version_text
|
||||
.set_text("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
||||
std::to_string(GLVersion.minor))
|
||||
.set_scale(SCALE);
|
||||
{
|
||||
auto& video_driver = m_widget.add_child<Label>();
|
||||
video_driver
|
||||
.set_text(
|
||||
std::format("VideoDiver: {}", SDL_GetCurrentVideoDriver()))
|
||||
.set_scale(SCALE);
|
||||
}
|
||||
add_label("OpenGL: " + std::to_string(GLVersion.major) + "." +
|
||||
std::to_string(GLVersion.minor));
|
||||
|
||||
add_label(std::format("VideoDiver: {}", SDL_GetCurrentVideoDriver()));
|
||||
|
||||
// speed
|
||||
auto& speed_text = m_widget.add_child<Label>();
|
||||
speed_text.set_text("Speed: 0 m/s").set_scale(SCALE);
|
||||
m_component.try_emplace("speed", &speed_text);
|
||||
{
|
||||
auto& window_size = m_widget.add_child<Label>();
|
||||
window_size.set_text("Window W: {} H: {}").set_scale(SCALE);
|
||||
m_component.try_emplace("window_size", &window_size);
|
||||
}
|
||||
{
|
||||
auto& frame_size = m_widget.add_child<Label>();
|
||||
frame_size.set_text("FrameBuffer W: {} H: {}").set_scale(SCALE);
|
||||
m_component.try_emplace("frame_buffer", &frame_size);
|
||||
}
|
||||
add_label("Speed: 0 m/s", "speed");
|
||||
|
||||
add_label("Window W: {} H: {}", "window_size");
|
||||
|
||||
add_label("FrameBuffer W: {} H: {}", "frame_buffer");
|
||||
}
|
||||
|
||||
Widget& DebugCollector::get_widget() { return m_widget; }
|
||||
|
||||
Reference in New Issue
Block a user