From 9c2e3bf15ab78edd4a44604af78fbadf7dc0eb34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=B5=A9?= Date: Thu, 8 Jan 2026 18:14:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(chat-ui):=20=E7=A7=BB=E9=99=A4HeaderCo?= =?UTF-8?q?ntainer=E5=92=8CSendButton=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=A3=85?= =?UTF-8?q?=E9=A5=B0=E5=9B=BE=E7=89=87=EF=BC=8C=E4=BF=AE=E5=A4=8DEnter?= =?UTF-8?q?=E9=94=AE=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除 HeaderContainer 和 StatusLabel(状态显示) - 删除 SendButton(发送按钮) - 添加聊天框背景图和装饰图片 - 设置 TextureRect modulate 为白色,修复黑框问题 - 移除 ChatPanel 背景色,使背景透明 - 修复 is_key_pressed 错误,改用 InputEventKey 类型检查 - 移除 _update_connection_status 函数及相关调用 --- scenes/ui/ChatUI.gd | 51 ++++---------------------------- scenes/ui/ChatUI.tscn | 67 ++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 72 deletions(-) diff --git a/scenes/ui/ChatUI.gd b/scenes/ui/ChatUI.gd index 1a86109..a8872be 100644 --- a/scenes/ui/ChatUI.gd +++ b/scenes/ui/ChatUI.gd @@ -39,12 +39,6 @@ extends Control # 聊天输入框 @onready var chat_input: LineEdit = %ChatInput -# 发送按钮 -@onready var send_button: Button = %SendButton - -# 状态标签 -@onready var status_label: Label = %StatusLabel - # ============================================================================ # 预加载资源 # ============================================================================ @@ -81,10 +75,7 @@ func _ready() -> void: # 创建隐藏计时器 _create_hide_timer() - - # 设置初始状态 - _update_connection_status(false) - + # 订阅事件(Call Down via EventSystem) _subscribe_to_events() @@ -111,7 +102,7 @@ func _exit_tree() -> void: # 处理全局输入 func _input(event: InputEvent) -> void: # 检查是否按下 Enter 键 - if event.is_action_pressed("ui_text_submit") or event.is_key_pressed(KEY_ENTER): + if event is InputEventKey and event.keycode == KEY_ENTER: _handle_enter_pressed() # 处理 Enter 键按下 @@ -208,9 +199,6 @@ func _on_hide_timeout() -> void: # 连接 UI 信号 func _connect_ui_signals() -> void: - # 发送按钮点击 - send_button.pressed.connect(_on_send_button_pressed) - # 输入框回车 chat_input.text_submitted.connect(_on_chat_input_submitted) @@ -288,29 +276,13 @@ func _on_chat_message_received(data: Dictionary) -> void: func _on_chat_error(data: Dictionary) -> void: var error_code: String = data.get("error_code", "") var message: String = data.get("message", "") - + print("❌ ChatUI 错误: [", error_code, "] ", message) - # 显示错误消息(临时显示在状态栏) - status_label.text = "❌ " + message - status_label.modulate = Color.RED - - # 3秒后恢复状态 - var timer := get_tree().create_timer(3.0) - var timeout_callback := func(): - _update_connection_status(ChatManager.is_chat_connected()) - timer.timeout.connect(timeout_callback) - # 处理连接状态变化 func _on_connection_state_changed(data: Dictionary) -> void: - var state_names := ["DISCONNECTED", "CONNECTING", "CONNECTED", "RECONNECTING", "ERROR"] - var state: int = data.get("state", 0) - - match state: - 2: # CONNECTED - _update_connection_status(true) - _: - _update_connection_status(false) + # 连接状态变化处理(当前不更新UI) + pass # 处理登录成功 func _on_login_success(data: Dictionary) -> void: @@ -349,19 +321,6 @@ func add_message_to_history(from_user: String, content: String, timestamp: float # 内部方法 - UI 更新 # ============================================================================ -# 更新连接状态显示 -func _update_connection_status(connected: bool) -> void: - if connected: - status_label.text = "● 已连接" - status_label.modulate = Color.GREEN - chat_input.editable = true - send_button.disabled = false - else: - status_label.text = "○ 未连接" - status_label.modulate = Color.GRAY - chat_input.editable = false - send_button.disabled = true - # 滚动到底部 func _scroll_to_bottom() -> void: # 等待一帧,确保 UI 更新完成 diff --git a/scenes/ui/ChatUI.tscn b/scenes/ui/ChatUI.tscn index d312dc4..de1a101 100644 --- a/scenes/ui/ChatUI.tscn +++ b/scenes/ui/ChatUI.tscn @@ -1,9 +1,13 @@ -[gd_scene load_steps=3 format=3 uid="uid://bv7k2nan4xj8q"] +[gd_scene load_steps=6 format=3 uid="uid://bv7k2nan4xj8q"] [ext_resource type="Script" uid="uid://pibdlvhb12q8" path="res://scenes/ui/ChatUI.gd" id="1"] [ext_resource type="Texture2D" uid="uid://flepo0gpb55h" path="res://assets/ui/chat/缩略框背景.png" id="2_7dhmv"] +[ext_resource type="Texture2D" uid="uid://clmgyxpeh5742" path="res://assets/ui/chat/输入框背景.png" id="3_fbft8"] +[ext_resource type="Texture2D" uid="uid://q0ijn5y0tbw3" path="res://assets/ui/chat/装饰2.png" id="4_xo31h"] +[ext_resource type="Texture2D" uid="uid://ct0cl4h2i6ydn" path="res://assets/ui/chat/装饰.png" id="5_xlxdo"] [node name="ChatUI" type="Control"] +unique_name_in_owner = true custom_minimum_size = Vector2(10, 20) layout_mode = 3 anchors_preset = 15 @@ -12,18 +16,8 @@ anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 mouse_filter = 2 -unique_name_in_owner = true script = ExtResource("1") -[node name="TextureRect" type="TextureRect" parent="."] -layout_mode = 0 -offset_left = 10.0 -offset_top = 358.0 -offset_right = 460.0 -offset_bottom = 758.0 -texture = ExtResource("2_7dhmv") -expand_mode = 1 - [node name="ChatPanel" type="Panel" parent="."] unique_name_in_owner = true layout_mode = 1 @@ -35,6 +29,15 @@ offset_top = -410.0 offset_right = 460.0 offset_bottom = -10.0 grow_vertical = 0 +theme_override_styles/panel = null + +[node name="TextureRect" type="TextureRect" parent="ChatPanel"] +layout_mode = 0 +offset_right = 450.0 +offset_bottom = 400.0 +texture = ExtResource("2_7dhmv") +expand_mode = 1 +modulate = Color(1, 1, 1, 1) [node name="VBoxContainer" type="VBoxContainer" parent="ChatPanel"] layout_mode = 1 @@ -49,17 +52,6 @@ grow_horizontal = 2 grow_vertical = 2 theme_override_constants/separation = 8 -[node name="HeaderContainer" type="HBoxContainer" parent="ChatPanel/VBoxContainer"] -layout_mode = 2 - -[node name="StatusLabel" type="Label" parent="ChatPanel/VBoxContainer/HeaderContainer"] -unique_name_in_owner = true -layout_mode = 2 -size_flags_horizontal = 3 -theme_override_colors/font_color = Color(0.5, 0.5, 0.5, 1) -theme_override_font_sizes/font_size = 12 -text = "○ 未连接" - [node name="ChatHistory" type="ScrollContainer" parent="ChatPanel/VBoxContainer"] unique_name_in_owner = true layout_mode = 2 @@ -82,7 +74,30 @@ layout_mode = 2 size_flags_horizontal = 3 placeholder_text = "输入消息..." -[node name="SendButton" type="Button" parent="ChatPanel/VBoxContainer/InputContainer"] -unique_name_in_owner = true -layout_mode = 2 -text = "发送" +[node name="TextureRect" type="TextureRect" parent="ChatPanel/VBoxContainer/InputContainer/ChatInput"] +layout_mode = 0 +offset_right = 435.0 +offset_bottom = 30.0 +texture = ExtResource("3_fbft8") +expand_mode = 1 +modulate = Color(1, 1, 1, 1) + +[node name="TextureRect2" type="TextureRect" parent="ChatPanel"] +layout_mode = 0 +offset_left = 10.0 +offset_top = 10.0 +offset_right = 20.0 +offset_bottom = 20.0 +texture = ExtResource("4_xo31h") +expand_mode = 1 +modulate = Color(1, 1, 1, 1) + +[node name="TextureRect3" type="TextureRect" parent="ChatPanel"] +layout_mode = 0 +offset_left = 430.0 +offset_top = 340.0 +offset_right = 440.0 +offset_bottom = 350.0 +texture = ExtResource("5_xlxdo") +expand_mode = 1 +modulate = Color(1, 1, 1, 1)