revert Merge pull request '聊天系统' (#13) from qbb0530/whale-town-front:main into main

Reviewed-on: #13
This commit is contained in:
2026-01-14 16:44:46 +08:00
parent d671e4d311
commit 229461c83f
291 changed files with 65 additions and 25926 deletions

View File

@@ -328,7 +328,7 @@ func _on_login_enter(_text: String):
# ============ 控制器信号处理 ============
# 登录成功处理
func _on_controller_login_success(username: String) -> void:
func _on_controller_login_success(username: String):
# 清空表单
login_username.text = ""
login_password.text = ""
@@ -336,13 +336,7 @@ func _on_controller_login_success(username: String) -> void:
_hide_field_error(login_username_error)
_hide_field_error(login_password_error)
_hide_field_error(login_verification_error)
# 设置 token 给 ChatManager用于 WebSocket 聊天认证)
var token: String = auth_manager.get_access_token()
if not token.is_empty():
ChatManager.set_game_token(token)
print("✅ 已设置 ChatManager token: ", token.substr(0, 20) + "...")
# 发送登录成功信号给上层
login_success.emit(username)

View File

@@ -1,14 +1,10 @@
[gd_scene load_steps=13 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="Script" uid="uid://b514h2wuido0h" path="res://scenes/ui/AuthScene.gd" id="3_script"]
[ext_resource type="Texture2D" uid="uid://dyma4hpodhdxi" path="res://assets/ui/auth/登录背景.png" id="3_wh4n4"]
[ext_resource type="Texture2D" uid="uid://cnrffaqbtw8f5" path="res://assets/ui/auth/输入框.png" id="4_lnw07"]
[ext_resource type="Texture2D" uid="uid://de4q4s1gxivtf" path="res://assets/ui/auth/login_frame_smart_transparent.png" id="2_frame"]
[ext_resource type="Script" path="res://scenes/ui/AuthScene.gd" id="3_script"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_26vyf"]
[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_cjyup"]
texture = ExtResource("4_lnw07")
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hover"]
bg_color = Color(0.3, 0.6, 0.9, 1)
@@ -63,8 +59,6 @@ Button/styles/hover = SubResource("StyleBoxFlat_hover")
Button/styles/normal = SubResource("StyleBoxFlat_normal")
Button/styles/pressed = SubResource("StyleBoxFlat_pressed")
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1"]
[node name="AuthScene" type="Control"]
layout_mode = 3
anchors_preset = 15
@@ -87,14 +81,22 @@ texture = ExtResource("1_background")
expand_mode = 1
stretch_mode = 6
[node name="ColorRect" type="ColorRect" parent="."]
modulate = Color(0.84313726, 0.92941177, 0.98039216, 0.47058824)
[node name="WhaleFrame" type="TextureRect" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -300.0
offset_top = -300.0
offset_right = 300.0
offset_bottom = 300.0
grow_horizontal = 2
grow_vertical = 2
texture = ExtResource("2_frame")
expand_mode = 1
stretch_mode = 5
[node name="CenterContainer" type="CenterContainer" parent="."]
layout_mode = 1
@@ -110,45 +112,35 @@ offset_bottom = 236.0
grow_horizontal = 2
grow_vertical = 2
[node name="WhaleFrame" type="TextureRect" parent="CenterContainer"]
custom_minimum_size = Vector2(500, 0)
layout_mode = 2
texture = ExtResource("3_wh4n4")
expand_mode = 4
stretch_mode = 5
[node name="LoginPanel" type="Panel" parent="CenterContainer"]
custom_minimum_size = Vector2(300, 0)
custom_minimum_size = Vector2(350, 400)
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxEmpty_26vyf")
theme_override_styles/panel = SubResource("StyleBoxEmpty_1")
[node name="VBoxContainer" type="VBoxContainer" parent="CenterContainer/LoginPanel"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -140.0
offset_top = -185.5
offset_right = 140.0
offset_bottom = 185.5
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 30.0
offset_top = 30.0
offset_right = -30.0
offset_bottom = -30.0
grow_horizontal = 2
grow_vertical = 2
[node name="TitleLabel" type="Label" parent="CenterContainer/LoginPanel/VBoxContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_colors/font_shadow_color = Color(0.011764706, 0.12156863, 0.101960786, 0)
theme_override_font_sizes/font_size = 32
theme_override_font_sizes/font_size = 24
text = "Whaletown"
horizontal_alignment = 1
vertical_alignment = 1
[node name="SubtitleLabel" type="Label" parent="CenterContainer/LoginPanel/VBoxContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(0.53333336, 0.53333336, 0.53333336, 1)
theme_override_font_sizes/font_size = 16
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_font_sizes/font_size = 14
text = "开始你的小镇之旅!"
horizontal_alignment = 1
vertical_alignment = 1
@@ -188,13 +180,10 @@ theme_override_font_sizes/font_size = 12
text = "用户名不能为空"
horizontal_alignment = 2
[node name="UsernameInput" type="LineEdit" parent="CenterContainer/LoginPanel/VBoxContainer/LoginForm"]
custom_minimum_size = Vector2(0, 48)
[node name="UsernameInput" type="LineEdit" parent="CenterContainer/LoginPanel/VBoxContainer/LoginForm/UsernameContainer"]
layout_mode = 2
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_colors/font_placeholder_color = Color(0.5, 0.5, 0.5, 1)
theme_override_colors/selection_color = Color(0.5, 0.5, 0.5, 1)
theme_override_styles/normal = SubResource("StyleBoxTexture_cjyup")
placeholder_text = "用户名/手机/邮箱"
[node name="PasswordContainer" type="VBoxContainer" parent="CenterContainer/LoginPanel/VBoxContainer/LoginForm"]
@@ -563,8 +552,6 @@ theme = SubResource("Theme_button")
text = "返回登录"
[node name="ToastContainer" type="Control" parent="."]
modulate = Color(0.84313726, 0.92941177, 0.98039216, 1)
self_modulate = Color(0.84313726, 0.92941177, 0.9843137, 1)
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0

View File

@@ -1,333 +0,0 @@
extends Control
# ============================================================================
# ChatUI.gd - 聊天界面控制器Enter 显示/隐藏版本)
# ============================================================================
# 聊天系统的用户界面控制器
#
# 核心职责:
# - 显示聊天消息历史
# - 处理用户输入
# - 显示连接状态
# - Enter 显示/隐藏 + 点击外部取消输入状态 + 5秒自动隐藏
# - 只有按 Enter 才会取消倒计时
# - Call Down: 通过 EventSystem 订阅聊天事件
#
# 使用方式:
# var chat_ui := preload("res://scenes/ui/ChatUI.tscn").instantiate()
# add_child(chat_ui)
#
# 注意事项:
# - 遵循 "Signal Up, Call Down" 架构
# - 使用 @onready 缓存节点引用
# - 所有 UI 操作通过 ChatManager
# ============================================================================
# ============================================================================
# 节点引用
# ============================================================================
# 聊天面板
@onready var chat_panel: Control = %ChatPanel
# 聊天历史容器
@onready var chat_history: ScrollContainer = %ChatHistory
# 消息列表
@onready var message_list: VBoxContainer = %MessageList
# 聊天输入框
@onready var chat_input: LineEdit = %ChatInput
# ============================================================================
# 预加载资源
# ============================================================================
# 聊天消息场景
@onready var chat_message_scene: PackedScene = preload("res://scenes/prefabs/ui/ChatMessage.tscn")
# ============================================================================
# 成员变量
# ============================================================================
# 是否显示聊天框
var _is_chat_visible: bool = false
# 隐藏计时器
var _hide_timer: Timer = null
# 是否在输入中(输入时不隐藏)
var _is_typing: bool = false
# 当前用户名
var _current_username: String = ""
# ============================================================================
# 生命周期方法
# ============================================================================
# 准备就绪
func _ready() -> void:
# 初始隐藏聊天框
hide_chat()
# 创建隐藏计时器
_create_hide_timer()
# 订阅事件Call Down via EventSystem
_subscribe_to_events()
# 连接 UI 信号
_connect_ui_signals()
# 清理
func _exit_tree() -> void:
# 取消事件订阅
if EventSystem:
EventSystem.disconnect_event(EventNames.CHAT_MESSAGE_RECEIVED, _on_chat_message_received, self)
EventSystem.disconnect_event(EventNames.CHAT_ERROR_OCCURRED, _on_chat_error, self)
EventSystem.disconnect_event(EventNames.CHAT_CONNECTION_STATE_CHANGED, _on_connection_state_changed, self)
EventSystem.disconnect_event(EventNames.CHAT_LOGIN_SUCCESS, _on_login_success, self)
# 清理计时器
if _hide_timer:
_hide_timer.queue_free()
# ============================================================================
# 输入处理
# ============================================================================
# 处理全局输入
func _input(event: InputEvent) -> void:
# 检查是否按下 Enter 键
if event is InputEventKey and event.keycode == KEY_ENTER:
_handle_enter_pressed()
# 处理 Enter 键按下
func _handle_enter_pressed() -> void:
# 如果聊天框未显示,显示它
if not _is_chat_visible:
show_chat()
# 使用 call_deferred 避免在同一个事件周期内触发 LineEdit 的 text_submitted 信号
call_deferred("_grab_input_focus")
return
# 如果聊天框已显示且输入框有焦点,检查输入框内容
if chat_input.has_focus():
# 如果输入框有内容,发送消息
if not chat_input.text.is_empty():
_on_send_button_pressed()
return
# 如果聊天框已显示但输入框无焦点,重新聚焦(取消倒计时)
chat_input.grab_focus()
# 延迟获取输入框焦点(避免事件冲突)
func _grab_input_focus() -> void:
if chat_input:
chat_input.grab_focus()
# 处理 GUI 输入(鼠标点击)
func _gui_input(event: InputEvent) -> void:
# 检查鼠标点击
if event is InputEventMouseButton and event.pressed:
if event.button_index == MOUSE_BUTTON_LEFT:
_handle_click_outside()
# 处理点击聊天框外部区域
func _handle_click_outside() -> void:
# 检查点击是否在聊天面板外部
if not chat_panel.get_global_rect().has_point(get_global_mouse_position()):
# 延迟释放输入框焦点,避免事件冲突
if chat_input.has_focus():
call_deferred("_release_input_focus")
# 延迟释放输入框焦点(由 call_deferred 调用)
func _release_input_focus() -> void:
if chat_input and chat_input.has_focus():
chat_input.release_focus()
# ============================================================================
# 显示/隐藏逻辑
# ============================================================================
# 显示聊天框
func show_chat() -> void:
if _is_chat_visible:
return
_is_chat_visible = true
chat_panel.show()
# 停止隐藏计时器
_stop_hide_timer()
# 隐藏聊天框
func hide_chat() -> void:
if not _is_chat_visible:
return
_is_chat_visible = false
chat_panel.hide()
# 停止隐藏计时器
_stop_hide_timer()
# 创建隐藏计时器
func _create_hide_timer() -> void:
_hide_timer = Timer.new()
_hide_timer.wait_time = 5.0 # 5 秒
_hide_timer.one_shot = true
_hide_timer.timeout.connect(_on_hide_timeout)
add_child(_hide_timer)
# 开始隐藏倒计时
func _start_hide_timer() -> void:
if _is_typing:
return # 输入时不隐藏
_stop_hide_timer() # 先停止之前的计时器
_hide_timer.start()
# 停止隐藏倒计时
func _stop_hide_timer() -> void:
if _hide_timer:
_hide_timer.stop()
# 隐藏计时器超时
func _on_hide_timeout() -> void:
hide_chat()
# ============================================================================
# UI 事件处理
# ============================================================================
# 连接 UI 信号
func _connect_ui_signals() -> void:
# 输入框回车
chat_input.text_submitted.connect(_on_chat_input_submitted)
# 输入框焦点变化
chat_input.focus_entered.connect(_on_input_focus_entered)
chat_input.focus_exited.connect(_on_input_focus_exited)
# 输入框获得焦点
func _on_input_focus_entered() -> void:
_is_typing = true
_stop_hide_timer() # 停止隐藏计时器
# 输入框失去焦点
func _on_input_focus_exited() -> void:
_is_typing = false
# 开始 5 秒倒计时
if not _is_chat_visible:
return
_start_hide_timer()
# 发送按钮点击处理
func _on_send_button_pressed() -> void:
var content: String = chat_input.text.strip_edges()
if content.is_empty():
return
# 发送消息
ChatManager.send_chat_message(content, "local")
# 清空输入框
chat_input.clear()
# 重新聚焦输入框
chat_input.grab_focus()
# 聊天输入提交(回车键)处理
func _on_chat_input_submitted(text: String) -> void:
_on_send_button_pressed()
# ============================================================================
# 事件订阅Call Down
# ============================================================================
# 订阅事件
func _subscribe_to_events() -> void:
# 订阅聊天消息接收事件
EventSystem.connect_event(EventNames.CHAT_MESSAGE_RECEIVED, _on_chat_message_received, self)
# 订阅聊天错误事件
EventSystem.connect_event(EventNames.CHAT_ERROR_OCCURRED, _on_chat_error, self)
# 订阅连接状态变化事件
EventSystem.connect_event(EventNames.CHAT_CONNECTION_STATE_CHANGED, _on_connection_state_changed, self)
# 订阅登录成功事件
EventSystem.connect_event(EventNames.CHAT_LOGIN_SUCCESS, _on_login_success, self)
# ============================================================================
# 事件处理器
# ============================================================================
# 处理接收到的聊天消息
func _on_chat_message_received(data: Dictionary) -> void:
var from_user: String = data.get("from_user", "")
var content: String = data.get("content", "")
var timestamp: float = data.get("timestamp", 0.0)
# 添加到消息历史
add_message_to_history(from_user, content, timestamp, false)
# 处理聊天错误
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)
# 处理连接状态变化
func _on_connection_state_changed(data: Dictionary) -> void:
# 连接状态变化处理当前不更新UI
pass
# 处理登录成功
func _on_login_success(data: Dictionary) -> void:
_current_username = data.get("username", "")
# ============================================================================
# 公共 API - 消息管理
# ============================================================================
# 添加消息到历史
#
# 参数:
# from_user: String - 发送者用户名
# content: String - 消息内容
# timestamp: float - 时间戳
# is_self: bool - 是否为自己发送的消息(默认 false
func add_message_to_history(from_user: String, content: String, timestamp: float, is_self: bool) -> void:
# 如果聊天框隐藏,自动显示
if not _is_chat_visible:
show_chat()
# 创建消息节点
var message_node: ChatMessage = chat_message_scene.instantiate()
# 设置消息内容
message_node.set_message(from_user, content, timestamp, is_self)
# 添加到列表
message_list.add_child(message_node)
# 自动滚动到底部
call_deferred("_scroll_to_bottom")
# ============================================================================
# 内部方法 - UI 更新
# ============================================================================
# 滚动到底部
func _scroll_to_bottom() -> void:
# 等待一帧,确保 UI 更新完成
await get_tree().process_frame
# 滚动到底部
if is_instance_valid(chat_history):
chat_history.scroll_vertical = chat_history.get_v_scroll_bar().max_value

View File

@@ -1 +0,0 @@
uid://pibdlvhb12q8

View File

@@ -1,119 +0,0 @@
[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="Texture2D" uid="uid://cchjgp6qh7u61" 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"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_xo31h"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_1ahvy"]
[node name="ChatUI" type="Control"]
unique_name_in_owner = true
custom_minimum_size = Vector2(10, 20)
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1")
[node name="ChatPanel" type="Control" parent="."]
unique_name_in_owner = true
layout_mode = 1
anchor_left = 0.007
anchor_top = 0.98700005
anchor_right = 0.007
anchor_bottom = 0.98700005
offset_left = 0.36800003
offset_top = -240.01605
offset_right = 340.368
offset_bottom = -0.016052246
grow_vertical = 0
[node name="TextureRect" type="TextureRect" parent="ChatPanel"]
layout_mode = 1
anchors_preset = -1
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -240.0
offset_right = 340.0
texture = ExtResource("2_7dhmv")
expand_mode = 1
[node name="VBoxContainer" type="VBoxContainer" parent="ChatPanel"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 15.0
offset_top = 15.0
offset_right = -15.0
offset_bottom = -55.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/separation = 8
[node name="ChatHistory" type="ScrollContainer" parent="ChatPanel/VBoxContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3
[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
theme_override_constants/separation = 8
[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
[node name="TextureRect3" type="TextureRect" parent="ChatPanel"]
layout_mode = 0
offset_left = 320.0
offset_top = 180.0
offset_right = 330.0
offset_bottom = 190.0
texture = ExtResource("5_xlxdo")
expand_mode = 1
[node name="TextureRect4" type="TextureRect" parent="ChatPanel"]
layout_mode = 1
anchors_preset = -1
anchor_top = 1.0
anchor_bottom = 1.0
offset_left = 15.0
offset_top = -40.0
offset_right = 325.0
offset_bottom = -10.0
texture = ExtResource("3_fbft8")
expand_mode = 1
[node name="InputContainer" type="HBoxContainer" parent="ChatPanel"]
self_modulate = Color(1, 1, 1, 0.03529412)
layout_mode = 0
offset_left = 25.0
offset_top = 202.5
offset_right = 315.0
offset_bottom = 227.5
theme_override_constants/separation = 4
[node name="ChatInput" type="LineEdit" parent="ChatPanel/InputContainer"]
unique_name_in_owner = true
layout_mode = 2
size_flags_horizontal = 3
localize_numeral_system = false
theme_override_font_sizes/font_size = 12
theme_override_styles/normal = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/read_only = SubResource("StyleBoxEmpty_xo31h")
theme_override_styles/focus = SubResource("StyleBoxEmpty_1ahvy")