feat(chat-ui): 更新聊天UI和场景配置

- 优化聊天消息显示
- 调整UI布局
This commit is contained in:
王浩
2026-01-09 23:23:41 +08:00
parent 136e1344a0
commit e335a35f6c
6 changed files with 31 additions and 35 deletions

View File

@@ -30,10 +30,10 @@ gdscript/warnings/treat_warnings_as_errors=false
[display] [display]
window/size/viewport_width=1376 window/size/viewport_width=1920
window/size/viewport_height=768 window/size/viewport_height=1080
window/size/mode=2 window/size/mode=2
window/stretch/mode="canvas_items" window/stretch/mode="viewport"
window/stretch/aspect="expand" window/stretch/aspect="expand"
[gui] [gui]

View File

@@ -88,13 +88,16 @@ func _on_login_success(username: String):
# 登录成功后的处理 # 登录成功后的处理
current_user = username current_user = username
print("用户 ", username, " 登录成功!") print("用户 ", username, " 登录成功!")
# 连接到聊天服务器(在进入游戏界面之前) # 连接到聊天服务器(在进入游戏界面之前)
print("🔌 开始连接聊天服务器...") print("🔌 开始连接聊天服务器...")
ChatManager.connect_to_chat_server() ChatManager.connect_to_chat_server()
show_main_game() show_main_game()
# 登录成功后隐藏聊天框需要按Enter才显示
chat_ui.hide_chat()
func _on_logout_pressed(): func _on_logout_pressed():
# 登出处理 # 登出处理
current_user = "" current_user = ""

View File

@@ -103,8 +103,8 @@ func _apply_style() -> void:
# 设置面板样式 # 设置面板样式
add_theme_stylebox_override("panel", _get_self_style()) add_theme_stylebox_override("panel", _get_self_style())
# 设置文字颜色 # 设置文字颜色 - ID使用金色 #FFD700
username_label.add_theme_color_override("font_color", Color.WHITE) username_label.add_theme_color_override("font_color", Color(1.0, 0.8431373, 0.0))
timestamp_label.add_theme_color_override("font_color", Color(0.7, 0.7, 0.7)) timestamp_label.add_theme_color_override("font_color", Color(0.7, 0.7, 0.7))
else: else:
# 他人的消息:左侧对齐,灰色背景 # 他人的消息:左侧对齐,灰色背景
@@ -114,8 +114,8 @@ func _apply_style() -> void:
# 设置面板样式 # 设置面板样式
add_theme_stylebox_override("panel", _get_other_style()) add_theme_stylebox_override("panel", _get_other_style())
# 设置文字颜色 # 设置文字颜色 - ID使用蓝色 #69c0ff
username_label.add_theme_color_override("font_color", Color(0.2, 0.4, 0.8)) username_label.add_theme_color_override("font_color", Color(0.4117647, 0.7529412, 1.0))
timestamp_label.add_theme_color_override("font_color", Color(0.5, 0.5, 0.5)) timestamp_label.add_theme_color_override("font_color", Color(0.5, 0.5, 0.5))
# 获取自己消息的样式 # 获取自己消息的样式

View File

@@ -1,8 +1,8 @@
[gd_scene load_steps=10 format=3 uid="uid://by7m8snb4xllf"] [gd_scene load_steps=10 format=3 uid="uid://by7m8snb4xllf"]
[ext_resource type="Texture2D" uid="uid://bx17oy8lvaca4" path="res://assets/ui/auth/bg_auth_scene.png" id="1_background"] [ext_resource type="Texture2D" uid="uid://bx17oy8lvaca4" path="res://assets/ui/auth/bg_auth_scene.png" id="1_background"]
[ext_resource type="Texture2D" uid="uid://de4q4s1gxivtf" path="res://assets/ui/auth/login_frame_smart_transparent.png" id="2_frame"] [ext_resource type="Texture2D" uid="uid://b6wlro1vixo8a" path="res://assets/ui/auth/清空内部组件的登录框架 (1).png" id="3_26vyf"]
[ext_resource type="Script" path="res://scenes/ui/AuthScene.gd" id="3_script"] [ext_resource type="Script" uid="uid://b514h2wuido0h" path="res://scenes/ui/AuthScene.gd" id="3_script"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1"]
@@ -94,7 +94,7 @@ offset_right = 300.0
offset_bottom = 300.0 offset_bottom = 300.0
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 2 grow_vertical = 2
texture = ExtResource("2_frame") texture = ExtResource("3_26vyf")
expand_mode = 1 expand_mode = 1
stretch_mode = 5 stretch_mode = 5

View File

@@ -68,14 +68,12 @@ var _current_username: String = ""
# 准备就绪 # 准备就绪
func _ready() -> void: func _ready() -> void:
print("ChatUI 初始化完成")
# 初始隐藏聊天框 # 初始隐藏聊天框
hide_chat() hide_chat()
# 创建隐藏计时器 # 创建隐藏计时器
_create_hide_timer() _create_hide_timer()
# 订阅事件Call Down via EventSystem # 订阅事件Call Down via EventSystem
_subscribe_to_events() _subscribe_to_events()
@@ -110,18 +108,24 @@ func _handle_enter_pressed() -> void:
# 如果聊天框未显示,显示它 # 如果聊天框未显示,显示它
if not _is_chat_visible: if not _is_chat_visible:
show_chat() show_chat()
chat_input.grab_focus() # 使用 call_deferred 避免在同一个事件周期内触发 LineEdit 的 text_submitted 信号
call_deferred("_grab_input_focus")
return return
# 如果聊天框已显示且输入框有焦点,发送消息 # 如果聊天框已显示且输入框有焦点,检查输入框内容
if chat_input.has_focus(): if chat_input.has_focus():
# 发送消息 # 如果输入框有内容,发送消息
_on_send_button_pressed() if not chat_input.text.is_empty():
_on_send_button_pressed()
return return
# 如果聊天框已显示但输入框无焦点,重新聚焦(取消倒计时) # 如果聊天框已显示但输入框无焦点,重新聚焦(取消倒计时)
chat_input.grab_focus() chat_input.grab_focus()
print("🔄 重新聚焦输入框,取消隐藏倒计时")
# 延迟获取输入框焦点(避免事件冲突)
func _grab_input_focus() -> void:
if chat_input:
chat_input.grab_focus()
# 处理 GUI 输入(鼠标点击) # 处理 GUI 输入(鼠标点击)
func _gui_input(event: InputEvent) -> void: func _gui_input(event: InputEvent) -> void:
@@ -137,7 +141,6 @@ func _handle_click_outside() -> void:
# 延迟释放输入框焦点,避免事件冲突 # 延迟释放输入框焦点,避免事件冲突
if chat_input.has_focus(): if chat_input.has_focus():
call_deferred("_release_input_focus") call_deferred("_release_input_focus")
print("🖱️ 点击外部,取消输入状态")
# 延迟释放输入框焦点(由 call_deferred 调用) # 延迟释放输入框焦点(由 call_deferred 调用)
func _release_input_focus() -> void: func _release_input_focus() -> void:
@@ -155,7 +158,6 @@ func show_chat() -> void:
_is_chat_visible = true _is_chat_visible = true
chat_panel.show() chat_panel.show()
print("👁️ 聊天框已显示")
# 停止隐藏计时器 # 停止隐藏计时器
_stop_hide_timer() _stop_hide_timer()
@@ -167,7 +169,6 @@ func hide_chat() -> void:
_is_chat_visible = false _is_chat_visible = false
chat_panel.hide() chat_panel.hide()
print("🚫 聊天框已隐藏")
# 停止隐藏计时器 # 停止隐藏计时器
_stop_hide_timer() _stop_hide_timer()
@@ -187,7 +188,6 @@ func _start_hide_timer() -> void:
_stop_hide_timer() # 先停止之前的计时器 _stop_hide_timer() # 先停止之前的计时器
_hide_timer.start() _hide_timer.start()
print("⏱️ 开始 5 秒倒计时...")
# 停止隐藏倒计时 # 停止隐藏倒计时
func _stop_hide_timer() -> void: func _stop_hide_timer() -> void:
@@ -215,7 +215,6 @@ func _connect_ui_signals() -> void:
func _on_input_focus_entered() -> void: func _on_input_focus_entered() -> void:
_is_typing = true _is_typing = true
_stop_hide_timer() # 停止隐藏计时器 _stop_hide_timer() # 停止隐藏计时器
print("✍️ 开始输入")
# 输入框失去焦点 # 输入框失去焦点
func _on_input_focus_exited() -> void: func _on_input_focus_exited() -> void:
@@ -224,7 +223,6 @@ func _on_input_focus_exited() -> void:
if not _is_chat_visible: if not _is_chat_visible:
return return
_start_hide_timer() _start_hide_timer()
print("🚫 停止输入,开始 5 秒隐藏倒计时")
# 发送按钮点击处理 # 发送按钮点击处理
func _on_send_button_pressed() -> void: func _on_send_button_pressed() -> void:
@@ -281,7 +279,7 @@ func _on_chat_message_received(data: Dictionary) -> void:
func _on_chat_error(data: Dictionary) -> void: func _on_chat_error(data: Dictionary) -> void:
var error_code: String = data.get("error_code", "") var error_code: String = data.get("error_code", "")
var message: String = data.get("message", "") var message: String = data.get("message", "")
print("❌ ChatUI 错误: [", error_code, "] ", message) print("❌ ChatUI 错误: [", error_code, "] ", message)
# 处理连接状态变化 # 处理连接状态变化
@@ -292,7 +290,6 @@ func _on_connection_state_changed(data: Dictionary) -> void:
# 处理登录成功 # 处理登录成功
func _on_login_success(data: Dictionary) -> void: func _on_login_success(data: Dictionary) -> void:
_current_username = data.get("username", "") _current_username = data.get("username", "")
print("✅ ChatUI: 登录成功,用户名: ", _current_username)
# ============================================================================ # ============================================================================
# 公共 API - 消息管理 # 公共 API - 消息管理

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=3 uid="uid://bv7k2nan4xj8q"] [gd_scene load_steps=8 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://cchjgp6qh7u61" path="res://assets/ui/chat/缩略框背景.png" id="2_7dhmv"] [ext_resource type="Texture2D" uid="uid://cchjgp6qh7u61" path="res://assets/ui/chat/缩略框背景.png" id="2_7dhmv"]
@@ -8,8 +8,6 @@
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_xo31h"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_xo31h"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_xlxdo"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1ahvy"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1ahvy"]
[node name="ChatUI" type="Control"] [node name="ChatUI" type="Control"]
@@ -104,9 +102,9 @@ expand_mode = 1
[node name="InputContainer" type="HBoxContainer" parent="ChatPanel"] [node name="InputContainer" type="HBoxContainer" parent="ChatPanel"]
self_modulate = Color(1, 1, 1, 0.03529412) self_modulate = Color(1, 1, 1, 0.03529412)
layout_mode = 0 layout_mode = 0
offset_left = 20.0 offset_left = 25.0
offset_top = 202.5 offset_top = 202.5
offset_right = 320.0 offset_right = 315.0
offset_bottom = 227.5 offset_bottom = 227.5
theme_override_constants/separation = 4 theme_override_constants/separation = 4
@@ -117,7 +115,5 @@ size_flags_horizontal = 3
localize_numeral_system = false localize_numeral_system = false
theme_override_font_sizes/font_size = 12 theme_override_font_sizes/font_size = 12
theme_override_styles/normal = SubResource("StyleBoxEmpty_xo31h") theme_override_styles/normal = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/focus = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/hover = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/read_only = SubResource("StyleBoxEmpty_xo31h") theme_override_styles/read_only = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/focus = SubResource("StyleBoxEmpty_1ahvy") theme_override_styles/focus = SubResource("StyleBoxEmpty_1ahvy")