mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +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:
@@ -1,7 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/audio/audio_fade.hpp"
|
||||
#include "Cubed/audio/audio_recording.hpp"
|
||||
#include "Cubed/audio/audio_source.hpp"
|
||||
#include "Cubed/audio/audio_stream_source.hpp"
|
||||
#include "Cubed/audio/sound_manager.hpp"
|
||||
#include "Cubed/audio/source_pool.hpp"
|
||||
#include "Cubed/config.hpp"
|
||||
@@ -10,13 +12,15 @@
|
||||
#include <AL/alc.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <opus/opus.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
|
||||
class ClientWorld;
|
||||
class NetworkClient;
|
||||
|
||||
class AudioEngine {
|
||||
|
||||
public:
|
||||
AudioEngine(Config& config);
|
||||
AudioEngine(const AudioEngine&) = delete;
|
||||
@@ -41,11 +45,23 @@ public:
|
||||
|
||||
float& bgm_target_volume();
|
||||
|
||||
void set_client(std::weak_ptr<NetworkClient> client);
|
||||
|
||||
void send_voice(const std::array<int16_t, AudioRecording::FRAME_SAMPLES>&);
|
||||
void receive_voice(std::span<const char> opus, const glm::vec3& pos);
|
||||
|
||||
AudioRecording& audio_recording();
|
||||
const AudioRecording& audio_recording() const;
|
||||
|
||||
private:
|
||||
using FadeMap = std::unordered_map<std::string, AudioFade>;
|
||||
bool m_init{false};
|
||||
ALCdevice* device{nullptr};
|
||||
ALCcontext* context{nullptr};
|
||||
OpusEncoder* m_encoder{nullptr};
|
||||
OpusDecoder* m_decoder{nullptr};
|
||||
AudioRecording m_recording;
|
||||
std::weak_ptr<NetworkClient> m_client;
|
||||
glm::vec3 listener_pos;
|
||||
std::unique_ptr<AudioSource> m_bgm;
|
||||
FadeMap m_fade_map;
|
||||
@@ -56,6 +72,9 @@ private:
|
||||
bool m_underwater = false;
|
||||
float m_music_volume = 1.0f;
|
||||
float m_sfx_volume = 1.0f;
|
||||
float m_player_voice_volume = 1.0f;
|
||||
|
||||
std::unique_ptr<AudioStreamSource> m_voice_source;
|
||||
std::unique_ptr<AudioFilter> m_low_pass_filter;
|
||||
std::unique_ptr<AudioEffect> m_underwater_effect;
|
||||
std::unique_ptr<AudioEffectSlot> m_underwater_slot;
|
||||
|
||||
33
include/Cubed/audio/audio_recording.hpp
Normal file
33
include/Cubed/audio/audio_recording.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <alc.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
namespace Cubed {
|
||||
class AudioEngine;
|
||||
class AudioRecording {
|
||||
public:
|
||||
static constexpr int SAMPLE_RATE = 48000;
|
||||
static constexpr int FRAME_MS = 20;
|
||||
static constexpr int FRAME_SAMPLES = SAMPLE_RATE * FRAME_MS / 1000;
|
||||
AudioRecording(AudioEngine& engine);
|
||||
AudioRecording(const AudioRecording&) = delete;
|
||||
AudioRecording(AudioRecording&&) = delete;
|
||||
AudioRecording& operator=(const AudioRecording&) = delete;
|
||||
AudioRecording& operator=(AudioRecording&&) = delete;
|
||||
~AudioRecording();
|
||||
|
||||
void update();
|
||||
|
||||
void init();
|
||||
void start();
|
||||
void stop();
|
||||
bool is_recording() const;
|
||||
|
||||
private:
|
||||
AudioEngine& m_engine;
|
||||
ALCdevice* m_capture = nullptr;
|
||||
bool m_recording = false;
|
||||
void send_voice(const std::array<int16_t, FRAME_SAMPLES>& pcm);
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
namespace Cubed {
|
||||
|
||||
enum class AudioState { INITIAL, PLAYING, PAUSED, STOPPED };
|
||||
@@ -21,8 +22,9 @@ public:
|
||||
void set_pitch(float pitch);
|
||||
void play();
|
||||
void play_2d(const AudioBuffer& buffer);
|
||||
void play_2d(std::unique_ptr<AudioBuffer> buffer);
|
||||
void play_3d(const AudioBuffer& buffer, const glm::vec3& pos);
|
||||
|
||||
void play_3d(std::unique_ptr<AudioBuffer> buffer, const glm::vec3& pos);
|
||||
void stop();
|
||||
void pause();
|
||||
float duration() const;
|
||||
@@ -42,6 +44,8 @@ public:
|
||||
void clear_effect_slot();
|
||||
|
||||
private:
|
||||
std::unique_ptr<AudioBuffer> m_buffer;
|
||||
|
||||
ALuint m_source = 0;
|
||||
float m_target_volume = 1.0f;
|
||||
float m_duration = 0.0f;
|
||||
|
||||
46
include/Cubed/audio/audio_stream_source.hpp
Normal file
46
include/Cubed/audio/audio_stream_source.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cubed/audio/audio_effect_slot.hpp"
|
||||
#include "Cubed/audio/audio_filter.hpp"
|
||||
#include "glm/ext/vector_float3.hpp"
|
||||
|
||||
#include <al.h>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
namespace Cubed {
|
||||
class AudioStreamSource {
|
||||
public:
|
||||
static constexpr int NUM_BUFFERS = 4;
|
||||
AudioStreamSource();
|
||||
AudioStreamSource(const AudioStreamSource&) = delete;
|
||||
AudioStreamSource(AudioStreamSource&&) = delete;
|
||||
AudioStreamSource& operator=(const AudioStreamSource&) = delete;
|
||||
AudioStreamSource& operator=(AudioStreamSource&&) = delete;
|
||||
~AudioStreamSource();
|
||||
|
||||
void push_pcm(std::span<const int16_t> pcm, ALsizei sample_rate);
|
||||
|
||||
void stop();
|
||||
bool is_playing() const;
|
||||
void reset();
|
||||
|
||||
void set_volume(float volume);
|
||||
void set_pitch(float pitch);
|
||||
void set_pos(glm::vec3 pos);
|
||||
void set_filter(const AudioFilter& filter);
|
||||
void clear_filter();
|
||||
void set_effect_slot(const AudioEffectSlot& slot);
|
||||
void clear_effect_slot();
|
||||
|
||||
private:
|
||||
ALuint m_source = 0;
|
||||
std::array<ALuint, NUM_BUFFERS> m_buffers{};
|
||||
std::vector<ALuint> m_free_buffers;
|
||||
bool m_playing = false;
|
||||
|
||||
void unqueue_processed();
|
||||
};
|
||||
} // namespace Cubed
|
||||
@@ -4,7 +4,7 @@
|
||||
namespace Cubed {
|
||||
class SourcePool {
|
||||
private:
|
||||
std::vector<AudioSource> m_sources;
|
||||
std::vector<std::unique_ptr<AudioSource>> m_sources;
|
||||
|
||||
public:
|
||||
explicit SourcePool(size_t size);
|
||||
@@ -15,9 +15,10 @@ public:
|
||||
SourcePool& operator=(SourcePool&&) = delete;
|
||||
|
||||
void update();
|
||||
[[nodiscard]]
|
||||
AudioSource* acquire();
|
||||
|
||||
std::vector<AudioSource>& sources();
|
||||
std::vector<std::unique_ptr<AudioSource>>& sources();
|
||||
};
|
||||
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user