fix: 修复聊天消息显示问题
- AuthScene: 修复节点路径错误 (WhaleFrame, UsernameInput) - ChatManager: 修复 timestamp 类型转换 (String -> float) - ChatMessage: 修复节点引用获取方式和 UI 显示 - ChatUI: 优化消息列表布局对齐
This commit is contained in:
@@ -578,12 +578,12 @@ func _handle_chat_error(data: Dictionary) -> void:
|
||||
"message": error_message
|
||||
})
|
||||
|
||||
# 处理接收到的聊天消息
|
||||
# 处理接收到的聊天消息
|
||||
func _handle_chat_render(data: Dictionary) -> void:
|
||||
var from_user: String = data.get("from", "")
|
||||
var content: String = data.get("txt", "")
|
||||
var show_bubble: bool = data.get("bubble", false)
|
||||
var timestamp: float = data.get("timestamp", 0.0)
|
||||
var timestamp: float = float(data.get("timestamp", "0.0"))
|
||||
|
||||
print("📨 收到聊天消息: ", from_user, " -> ", content)
|
||||
|
||||
|
||||
@@ -33,16 +33,38 @@ class_name ChatMessage
|
||||
# ============================================================================
|
||||
|
||||
# 用户名标签
|
||||
@onready var username_label: Label = %UsernameLabel
|
||||
var username_label: Label
|
||||
|
||||
# 时间戳标签
|
||||
@onready var timestamp_label: Label = %TimestampLabel
|
||||
var timestamp_label: Label
|
||||
|
||||
# 内容标签
|
||||
@onready var content_label: RichTextLabel = %ContentLabel
|
||||
var content_label: RichTextLabel
|
||||
|
||||
# 用户信息容器
|
||||
@onready var user_info_container: HBoxContainer = %UserInfoContainer
|
||||
var user_info_container: HBoxContainer
|
||||
|
||||
# ============================================================================
|
||||
# 生命周期方法
|
||||
# ============================================================================
|
||||
|
||||
func _ready() -> void:
|
||||
# 使用 get_node 获取节点引用
|
||||
username_label = get_node_or_null("VBoxContainer/UserInfoContainer/UsernameLabel")
|
||||
timestamp_label = get_node_or_null("VBoxContainer/UserInfoContainer/TimestampLabel")
|
||||
content_label = get_node_or_null("VBoxContainer/ContentLabel")
|
||||
user_info_container = get_node_or_null("VBoxContainer/UserInfoContainer")
|
||||
|
||||
# 调试输出
|
||||
if username_label:
|
||||
print("✅ UsernameLabel found")
|
||||
else:
|
||||
print("❌ UsernameLabel NOT found!")
|
||||
# 打印所有子节点帮助调试
|
||||
print("Available children: ", _get_all_children_names(self))
|
||||
|
||||
# 应用最大宽度限制
|
||||
custom_minimum_size.x = min(max_width, get_parent().size.x)
|
||||
|
||||
# ============================================================================
|
||||
# 成员变量
|
||||
@@ -51,15 +73,6 @@ class_name ChatMessage
|
||||
# 是否为自己发送的消息
|
||||
var _is_self: bool = false
|
||||
|
||||
# ============================================================================
|
||||
# 生命周期方法
|
||||
# ============================================================================
|
||||
|
||||
# 准备就绪
|
||||
func _ready() -> void:
|
||||
# 应用最大宽度限制
|
||||
custom_minimum_size.x = min(max_width, get_parent().size.x)
|
||||
|
||||
# ============================================================================
|
||||
# 公共 API
|
||||
# ============================================================================
|
||||
@@ -77,14 +90,27 @@ func _ready() -> void:
|
||||
func set_message(from_user: String, content: String, timestamp: float, is_self: bool = false) -> void:
|
||||
_is_self = is_self
|
||||
|
||||
# 设置用户名
|
||||
username_label.text = from_user
|
||||
# 设置用户名(带空值检查)
|
||||
if username_label:
|
||||
username_label.text = from_user
|
||||
else:
|
||||
push_error("ChatMessage: username_label is null!")
|
||||
return
|
||||
|
||||
# 设置内容
|
||||
content_label.text = content
|
||||
if content_label:
|
||||
content_label.clear() # 清除默认文本和所有内容
|
||||
content_label.bbcode_text = content # 使用 bbcode_text 因为 bbcode_enabled = true
|
||||
else:
|
||||
push_error("ChatMessage: content_label is null!")
|
||||
return
|
||||
|
||||
# 设置时间戳
|
||||
timestamp_label.text = _format_timestamp(timestamp)
|
||||
if timestamp_label:
|
||||
timestamp_label.text = _format_timestamp(timestamp)
|
||||
else:
|
||||
push_error("ChatMessage: timestamp_label is null!")
|
||||
return
|
||||
|
||||
# 应用样式
|
||||
_apply_style()
|
||||
@@ -93,8 +119,18 @@ func set_message(from_user: String, content: String, timestamp: float, is_self:
|
||||
# 内部方法 - 样式处理
|
||||
# ============================================================================
|
||||
|
||||
# 应用样式(自己和他人的消息不同)
|
||||
# 应用样式(自己和他人的消息不同)
|
||||
func _apply_style() -> void:
|
||||
if not username_label or not timestamp_label or not user_info_container:
|
||||
return
|
||||
|
||||
# 设置大小约束 - 宽度固定,高度适应内容
|
||||
if get_parent():
|
||||
custom_minimum_size.x = min(max_width, get_parent().size.x)
|
||||
|
||||
# 重要:设置垂直 size flags 让 Panel 适应内容高度
|
||||
size_flags_vertical = Control.SIZE_SHRINK_BEGIN
|
||||
|
||||
if _is_self:
|
||||
# 自己的消息:右侧对齐,蓝色背景
|
||||
size_flags_horizontal = Control.SIZE_SHRINK_END
|
||||
@@ -160,8 +196,16 @@ func _get_other_style() -> StyleBoxFlat:
|
||||
func _format_timestamp(timestamp: float) -> String:
|
||||
if timestamp == 0:
|
||||
return ""
|
||||
|
||||
|
||||
var datetime := Time.get_datetime_dict_from_unix_time(timestamp)
|
||||
|
||||
|
||||
# 格式化为 HH:MM
|
||||
return "%02d:%02d" % [datetime.hour, datetime.minute]
|
||||
|
||||
# 获取所有子节点名称(调试用)
|
||||
func _get_all_children_names(node: Node, _indent: int = 0) -> String:
|
||||
var result := ""
|
||||
for child in node.get_children():
|
||||
result += " ".repeat(_indent) + child.name + "\n"
|
||||
result += _get_all_children_names(child, _indent + 1)
|
||||
return result
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
|
||||
[node name="ChatMessage" type="Panel"]
|
||||
offset_right = 400.0
|
||||
offset_bottom = 100.0
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 6
|
||||
script = ExtResource("1")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
@@ -18,28 +18,23 @@ grow_vertical = 2
|
||||
theme_override_constants/separation = 4
|
||||
|
||||
[node name="UserInfoContainer" type="HBoxContainer" parent="VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
theme_override_constants/separation = 8
|
||||
|
||||
[node name="UsernameLabel" type="Label" parent="VBoxContainer/UserInfoContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
theme_override_colors/font_color = Color(0.2, 0.4, 0.8, 1)
|
||||
theme_override_font_sizes/font_size = 14
|
||||
text = "Username"
|
||||
|
||||
[node name="TimestampLabel" type="Label" parent="VBoxContainer/UserInfoContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
theme_override_colors/font_color = Color(0.5, 0.5, 0.5, 1)
|
||||
theme_override_font_sizes/font_size = 12
|
||||
text = "12:34"
|
||||
|
||||
[node name="ContentLabel" type="RichTextLabel" parent="VBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
bbcode_enabled = true
|
||||
text = "Message content here"
|
||||
fit_content = true
|
||||
|
||||
@@ -36,10 +36,10 @@ signal login_success(username: String)
|
||||
@onready var register_panel: Panel = $CenterContainer/RegisterPanel # 注册面板
|
||||
@onready var title_label: Label = $CenterContainer/LoginPanel/VBoxContainer/TitleLabel # 标题标签
|
||||
@onready var subtitle_label: Label = $CenterContainer/LoginPanel/VBoxContainer/SubtitleLabel # 副标题标签
|
||||
@onready var whale_frame: TextureRect = $WhaleFrame # 鲸鱼装饰框
|
||||
@onready var whale_frame: TextureRect = $CenterContainer/WhaleFrame # 鲸鱼装饰框
|
||||
|
||||
# 登录表单输入控件
|
||||
@onready var login_username: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/UsernameContainer/UsernameInput
|
||||
@onready var login_username: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/UsernameInput
|
||||
@onready var login_password: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/PasswordContainer/PasswordInput
|
||||
@onready var login_verification: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/VerificationContainer/VerificationInputContainer/VerificationInput
|
||||
|
||||
|
||||
@@ -61,12 +61,13 @@ theme_override_constants/separation = 8
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_vertical = 3
|
||||
horizontal_scroll_mode = 0
|
||||
|
||||
[node name="MessageList" type="VBoxContainer" parent="ChatPanel/VBoxContainer/ChatHistory"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
size_flags_vertical = 3
|
||||
size_flags_vertical = 1
|
||||
theme_override_constants/separation = 8
|
||||
|
||||
[node name="TextureRect2" type="TextureRect" parent="ChatPanel"]
|
||||
|
||||
Reference in New Issue
Block a user