forked from datawhale/whale-town-front
refactor(chat-ui): 移除HeaderContainer和SendButton,添加装饰图片,修复Enter键监听
- 删除 HeaderContainer 和 StatusLabel(状态显示) - 删除 SendButton(发送按钮) - 添加聊天框背景图和装饰图片 - 设置 TextureRect modulate 为白色,修复黑框问题 - 移除 ChatPanel 背景色,使背景透明 - 修复 is_key_pressed 错误,改用 InputEventKey 类型检查 - 移除 _update_connection_status 函数及相关调用
This commit is contained in:
@@ -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 更新完成
|
||||
|
||||
Reference in New Issue
Block a user