refactor(chat-ui): 移除HeaderContainer和SendButton,添加装饰图片,修复Enter键监听

- 删除 HeaderContainer 和 StatusLabel(状态显示)
- 删除 SendButton(发送按钮)
- 添加聊天框背景图和装饰图片
- 设置 TextureRect modulate 为白色,修复黑框问题
- 移除 ChatPanel 背景色,使背景透明
- 修复 is_key_pressed 错误,改用 InputEventKey 类型检查
- 移除 _update_connection_status 函数及相关调用
This commit is contained in:
王浩
2026-01-08 18:14:48 +08:00
parent 9e288dbb62
commit 9c2e3bf15a
2 changed files with 46 additions and 72 deletions

View File

@@ -39,12 +39,6 @@ extends Control
# 聊天输入框 # 聊天输入框
@onready var chat_input: LineEdit = %ChatInput @onready var chat_input: LineEdit = %ChatInput
# 发送按钮
@onready var send_button: Button = %SendButton
# 状态标签
@onready var status_label: Label = %StatusLabel
# ============================================================================ # ============================================================================
# 预加载资源 # 预加载资源
# ============================================================================ # ============================================================================
@@ -82,9 +76,6 @@ func _ready() -> void:
# 创建隐藏计时器 # 创建隐藏计时器
_create_hide_timer() _create_hide_timer()
# 设置初始状态
_update_connection_status(false)
# 订阅事件Call Down via EventSystem # 订阅事件Call Down via EventSystem
_subscribe_to_events() _subscribe_to_events()
@@ -111,7 +102,7 @@ func _exit_tree() -> void:
# 处理全局输入 # 处理全局输入
func _input(event: InputEvent) -> void: func _input(event: InputEvent) -> void:
# 检查是否按下 Enter 键 # 检查是否按下 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() _handle_enter_pressed()
# 处理 Enter 键按下 # 处理 Enter 键按下
@@ -208,9 +199,6 @@ func _on_hide_timeout() -> void:
# 连接 UI 信号 # 连接 UI 信号
func _connect_ui_signals() -> void: func _connect_ui_signals() -> void:
# 发送按钮点击
send_button.pressed.connect(_on_send_button_pressed)
# 输入框回车 # 输入框回车
chat_input.text_submitted.connect(_on_chat_input_submitted) chat_input.text_submitted.connect(_on_chat_input_submitted)
@@ -291,26 +279,10 @@ func _on_chat_error(data: Dictionary) -> void:
print("❌ ChatUI 错误: [", error_code, "] ", 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: func _on_connection_state_changed(data: Dictionary) -> void:
var state_names := ["DISCONNECTED", "CONNECTING", "CONNECTED", "RECONNECTING", "ERROR"] # 连接状态变化处理当前不更新UI
var state: int = data.get("state", 0) pass
match state:
2: # CONNECTED
_update_connection_status(true)
_:
_update_connection_status(false)
# 处理登录成功 # 处理登录成功
func _on_login_success(data: Dictionary) -> void: func _on_login_success(data: Dictionary) -> void:
@@ -349,19 +321,6 @@ func add_message_to_history(from_user: String, content: String, timestamp: float
# 内部方法 - UI 更新 # 内部方法 - 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: func _scroll_to_bottom() -> void:
# 等待一帧,确保 UI 更新完成 # 等待一帧,确保 UI 更新完成

View File

@@ -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="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://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"] [node name="ChatUI" type="Control"]
unique_name_in_owner = true
custom_minimum_size = Vector2(10, 20) custom_minimum_size = Vector2(10, 20)
layout_mode = 3 layout_mode = 3
anchors_preset = 15 anchors_preset = 15
@@ -12,18 +16,8 @@ anchor_bottom = 1.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
mouse_filter = 2 mouse_filter = 2
unique_name_in_owner = true
script = ExtResource("1") 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="."] [node name="ChatPanel" type="Panel" parent="."]
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 1 layout_mode = 1
@@ -35,6 +29,15 @@ offset_top = -410.0
offset_right = 460.0 offset_right = 460.0
offset_bottom = -10.0 offset_bottom = -10.0
grow_vertical = 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"] [node name="VBoxContainer" type="VBoxContainer" parent="ChatPanel"]
layout_mode = 1 layout_mode = 1
@@ -49,17 +52,6 @@ grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
theme_override_constants/separation = 8 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"] [node name="ChatHistory" type="ScrollContainer" parent="ChatPanel/VBoxContainer"]
unique_name_in_owner = true unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
@@ -82,7 +74,30 @@ layout_mode = 2
size_flags_horizontal = 3 size_flags_horizontal = 3
placeholder_text = "输入消息..." placeholder_text = "输入消息..."
[node name="SendButton" type="Button" parent="ChatPanel/VBoxContainer/InputContainer"] [node name="TextureRect" type="TextureRect" parent="ChatPanel/VBoxContainer/InputContainer/ChatInput"]
unique_name_in_owner = true layout_mode = 0
layout_mode = 2 offset_right = 435.0
text = "发送" 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)