forked from datawhale/whale-town
创建新工程
This commit is contained in:
177
scripts/LoginScreen.gd
Normal file
177
scripts/LoginScreen.gd
Normal file
@@ -0,0 +1,177 @@
|
||||
extends Control
|
||||
class_name LoginScreen
|
||||
## 登录界面
|
||||
## 处理用户登录和角色创建入口
|
||||
|
||||
# UI 元素
|
||||
var username_input: LineEdit
|
||||
var login_button: Button
|
||||
var create_character_button: Button
|
||||
var status_label: Label
|
||||
var container: VBoxContainer
|
||||
|
||||
# 信号
|
||||
signal login_requested(username: String)
|
||||
signal create_character_requested()
|
||||
|
||||
func _ready():
|
||||
"""初始化登录界面"""
|
||||
_create_ui()
|
||||
_setup_animations()
|
||||
update_layout()
|
||||
|
||||
## 设置动画效果
|
||||
func _setup_animations():
|
||||
"""设置界面动画效果"""
|
||||
# 为移动设备优化触摸体验
|
||||
TouchFeedbackManager.optimize_ui_for_touch(self)
|
||||
|
||||
# 界面入场动画
|
||||
UIAnimationManager.fade_slide_in(container, "bottom", 0.5)
|
||||
|
||||
## 创建 UI 元素
|
||||
func _create_ui():
|
||||
"""创建登录界面的所有 UI 元素"""
|
||||
# 设置为全屏
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
|
||||
# 创建中心容器
|
||||
container = VBoxContainer.new()
|
||||
container.name = "Container"
|
||||
container.custom_minimum_size = Vector2(300, 0)
|
||||
# 设置容器锚点为居中
|
||||
container.anchor_left = 0.5
|
||||
container.anchor_right = 0.5
|
||||
container.anchor_top = 0.5
|
||||
container.anchor_bottom = 0.5
|
||||
container.offset_left = -150 # 容器宽度的一半
|
||||
container.offset_right = 150
|
||||
container.offset_top = -200 # 估算容器高度的一半
|
||||
container.offset_bottom = 200
|
||||
add_child(container)
|
||||
|
||||
# 标题
|
||||
var title = Label.new()
|
||||
title.text = "AI Town Game"
|
||||
title.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
title.add_theme_font_size_override("font_size", 32)
|
||||
container.add_child(title)
|
||||
|
||||
# 间距
|
||||
container.add_child(_create_spacer(20))
|
||||
|
||||
# 用户名标签
|
||||
var username_label = Label.new()
|
||||
username_label.text = "用户名:"
|
||||
container.add_child(username_label)
|
||||
|
||||
# 用户名输入框
|
||||
username_input = LineEdit.new()
|
||||
username_input.placeholder_text = "输入你的用户名"
|
||||
username_input.custom_minimum_size = Vector2(0, 40)
|
||||
username_input.text_submitted.connect(_on_username_submitted)
|
||||
container.add_child(username_input)
|
||||
|
||||
# 间距
|
||||
container.add_child(_create_spacer(10))
|
||||
|
||||
# 登录按钮
|
||||
login_button = Button.new()
|
||||
login_button.text = "登录"
|
||||
login_button.custom_minimum_size = Vector2(0, 50)
|
||||
login_button.pressed.connect(_on_login_pressed)
|
||||
container.add_child(login_button)
|
||||
|
||||
# 间距
|
||||
container.add_child(_create_spacer(10))
|
||||
|
||||
# 创建角色按钮
|
||||
create_character_button = Button.new()
|
||||
create_character_button.text = "创建新角色"
|
||||
create_character_button.custom_minimum_size = Vector2(0, 50)
|
||||
create_character_button.pressed.connect(_on_create_character_pressed)
|
||||
container.add_child(create_character_button)
|
||||
|
||||
# 间距
|
||||
container.add_child(_create_spacer(20))
|
||||
|
||||
# 状态标签
|
||||
status_label = Label.new()
|
||||
status_label.text = ""
|
||||
status_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||||
status_label.add_theme_color_override("font_color", Color(1, 0.5, 0.5))
|
||||
container.add_child(status_label)
|
||||
|
||||
## 创建间距
|
||||
func _create_spacer(height: float) -> Control:
|
||||
"""创建垂直间距"""
|
||||
var spacer = Control.new()
|
||||
spacer.custom_minimum_size = Vector2(0, height)
|
||||
return spacer
|
||||
|
||||
## 更新布局
|
||||
func update_layout():
|
||||
"""更新布局以适应窗口大小"""
|
||||
if not container:
|
||||
return
|
||||
|
||||
# 容器已经通过锚点设置为居中,无需手动计算位置
|
||||
# 锚点会自动适应窗口大小变化
|
||||
|
||||
## 显示状态消息
|
||||
func show_status(message: String, is_error: bool = false):
|
||||
"""
|
||||
显示状态消息
|
||||
@param message: 消息文本
|
||||
@param is_error: 是否为错误消息
|
||||
"""
|
||||
if status_label:
|
||||
status_label.text = message
|
||||
if is_error:
|
||||
status_label.add_theme_color_override("font_color", Color(1, 0.3, 0.3))
|
||||
else:
|
||||
status_label.add_theme_color_override("font_color", Color(0.3, 1, 0.3))
|
||||
|
||||
## 清除状态消息
|
||||
func clear_status():
|
||||
"""清除状态消息"""
|
||||
if status_label:
|
||||
status_label.text = ""
|
||||
|
||||
## 设置按钮启用状态
|
||||
func set_buttons_enabled(enabled: bool):
|
||||
"""
|
||||
设置所有按钮的启用状态
|
||||
@param enabled: 是否启用
|
||||
"""
|
||||
if login_button:
|
||||
login_button.disabled = not enabled
|
||||
if create_character_button:
|
||||
create_character_button.disabled = not enabled
|
||||
|
||||
## 登录按钮点击
|
||||
func _on_login_pressed():
|
||||
"""登录按钮被点击"""
|
||||
var username = username_input.text.strip_edges()
|
||||
|
||||
if username.is_empty():
|
||||
show_status("请输入用户名", true)
|
||||
# 添加错误动画反馈
|
||||
UIAnimationManager.shake_error(username_input, 8.0, 0.4)
|
||||
return
|
||||
|
||||
# 添加成功反馈动画
|
||||
UIAnimationManager.button_press_feedback(login_button)
|
||||
clear_status()
|
||||
login_requested.emit(username)
|
||||
|
||||
## 创建角色按钮点击
|
||||
func _on_create_character_pressed():
|
||||
"""创建角色按钮被点击"""
|
||||
create_character_requested.emit()
|
||||
|
||||
## 用户名输入框回车
|
||||
func _on_username_submitted(_text: String):
|
||||
"""用户名输入框按下回车"""
|
||||
_on_login_pressed()
|
||||
Reference in New Issue
Block a user