81 lines
3.5 KiB
GDScript
81 lines
3.5 KiB
GDScript
extends Control
|
||
# 文件名: ui_top_info.gd
|
||
# 附加到 UI_Top_Info 节点 (Control)
|
||
|
||
# --- UI 节点引用 ---
|
||
@onready var year_label: Label = $Info/Time/Year
|
||
@onready var month_label: Label = $Info/Time/Month
|
||
@onready var week_label: Label = $Info/Time/Week
|
||
@onready var money_button: Button = $Info/Money
|
||
@onready var idea_button: Button = $Info/Idea
|
||
|
||
|
||
# --- 辅助函数:安全地将 Variant 转换为整数并格式化为字符串 ---
|
||
# 这个函数封装了类型检查、整数转换和字符串化的逻辑
|
||
func _format_value_as_int_string(value: Variant, default_string: String = "0") -> String:
|
||
# 检查值的类型是否为整数或浮点数
|
||
if typeof(value) == TYPE_INT or typeof(value) == TYPE_FLOAT:
|
||
# 先强制转换为整数,再转换为字符串
|
||
return str(int(value))
|
||
else:
|
||
# 如果不是数字类型,或者 value 为 null,则返回默认字符串
|
||
printerr("警告:尝试格式化的值不是数字类型,将使用默认值 '%s'。值为: %s" % [default_string, value])
|
||
return default_string
|
||
|
||
# --- 初始化 ---
|
||
func _ready() -> void:
|
||
if not GameState:
|
||
printerr("错误:GameState Autoload 未找到!UI 无法初始化。")
|
||
# 如果 GameState 不存在,设置所有 UI 为错误状态并返回
|
||
year_label.text = "ERR"
|
||
month_label.text = "ERR"
|
||
week_label.text = "ERR"
|
||
money_button.text = "ERR"
|
||
idea_button.text = "ERR"
|
||
return
|
||
|
||
# 1. 初始化时间显示
|
||
var time_data: Dictionary = GameState.get_value("time", {})
|
||
if not time_data.is_empty() and time_data is Dictionary:
|
||
# 使用辅助函数来格式化年、月、周
|
||
# 如果字典中缺少键,.get() 会返回 null,_format_value_as_int_string 会处理这种情况
|
||
year_label.text = _format_value_as_int_string(time_data.get("year"), "ERR")
|
||
month_label.text = _format_value_as_int_string(time_data.get("month"), "ERR")
|
||
week_label.text = _format_value_as_int_string(time_data.get("week"), "ERR")
|
||
else:
|
||
year_label.text = "ERR"
|
||
month_label.text = "ERR"
|
||
week_label.text = "ERR"
|
||
printerr("警告:从 GameState 获取 'time' 数据失败或格式不正确,无法初始化时间显示。")
|
||
|
||
# 2. 初始化金钱显示
|
||
var money_value: Variant = GameState.get_value("money") # 默认值由辅助函数处理
|
||
money_button.text = _format_value_as_int_string(money_value, "0") # 默认显示 "0"
|
||
|
||
# 3. 初始化点子显示
|
||
var idea_value: Variant = GameState.get_value("idea") # 默认值由辅助函数处理
|
||
idea_button.text = _format_value_as_int_string(idea_value, "0") # 默认显示 "0"
|
||
|
||
print("UI_Main 初始化完成:时间、金钱、点子已显示(确保为整数)。")
|
||
|
||
GameState.date_changed.connect(_on_date_changed)
|
||
GameState.state_value_changed.connect(_on_state_value_changed)
|
||
|
||
# --- 信号处理函数 (将在后续添加具体实现) ---
|
||
func _on_date_changed(new_date: Dictionary) -> void:
|
||
#print("接收到 date_changed 信号:", new_date)
|
||
if not new_date.is_empty() and new_date is Dictionary:
|
||
year_label.text = _format_value_as_int_string(new_date.get("year"), "ERR")
|
||
month_label.text = _format_value_as_int_string(new_date.get("month"), "ERR")
|
||
week_label.text = _format_value_as_int_string(new_date.get("week"), "ERR")
|
||
else:
|
||
printerr("警告:接收到的 date_changed 信号数据格式不正确。")
|
||
|
||
func _on_state_value_changed(key: String, new_value: Variant) -> void:
|
||
print("接收到 state_value_changed 信号: key=", key, ", value=", new_value)
|
||
match key:
|
||
"money":
|
||
money_button.text = _format_value_as_int_string(new_value, "0")
|
||
"idea":
|
||
idea_button.text = _format_value_as_int_string(new_value, "0")
|