This commit is contained in:
WhaleTown Developer
2026-01-20 20:11:07 +08:00
parent e989b4adf1
commit 603e7d9fc6
4 changed files with 154 additions and 43 deletions

View File

@@ -272,8 +272,12 @@ func _on_chat_message_received(data: Dictionary) -> void:
var content: String = data.get("content", "")
var timestamp: float = data.get("timestamp", 0.0)
var is_self: bool = bool(data.get("is_self", false))
if not data.has("is_self") and not _current_username.is_empty() and from_user == _current_username:
is_self = true
# 添加到消息历史
add_message_to_history(from_user, content, timestamp, false)
add_message_to_history(from_user, content, timestamp, is_self)
# 处理聊天错误
func _on_chat_error(data: Dictionary) -> void:
@@ -307,15 +311,23 @@ func add_message_to_history(from_user: String, content: String, timestamp: float
if not _is_chat_visible:
show_chat()
# 每条消息用一行容器包起来,方便左右对齐且不挤在一起
var row := HBoxContainer.new()
row.layout_mode = 2 # 让 VBoxContainer 接管布局,否则会重叠在同一位置
row.size_flags_horizontal = Control.SIZE_EXPAND_FILL
row.size_flags_vertical = Control.SIZE_SHRINK_BEGIN
row.alignment = BoxContainer.ALIGNMENT_END if is_self else BoxContainer.ALIGNMENT_BEGIN
# 创建消息节点
var message_node: ChatMessage = chat_message_scene.instantiate()
# 先加入场景树,再设置内容(避免 ChatMessage._ready 尚未执行导致节点引用为空)
message_list.add_child(row)
row.add_child(message_node)
# 设置消息内容
message_node.set_message(from_user, content, timestamp, is_self)
# 添加到列表
message_list.add_child(message_node)
# 自动滚动到底部
call_deferred("_scroll_to_bottom")