hy-farm/godot/FarmGodot/scripts/ui/game_scene_context.gd
2026-05-29 19:54:56 +08:00

168 lines
6.4 KiB
GDScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class_name GameSceneContext
extends RefCounted
# 主场景上下文。
#
# 这个对象收敛跨 UI 模块共享的状态、资源和宿主操作入口:
# - 新模块优先注入 context按显式方法读取玩家、库存、纹理和路由能力。
# - 旧面板仍可通过 legacy_callbacks() 取兼容表,避免一次性重写所有面板。
# - Bootstrap 不再维护一大段散落 callback 字典,后续迁移只改这里。
const UiMotionScript := preload("res://scripts/ui/common/ui_motion.gd")
var owner: Node
var ui_builder
var texture_provider
var game_config
var player_state
var inventory_facade
var panel_router
var constants: Dictionary = {}
var _callbacks: Dictionary = {}
func setup(options: Dictionary) -> void:
owner = options.get("owner") as Node
ui_builder = options.get("ui_builder")
texture_provider = options.get("texture_provider")
game_config = options.get("game_config")
player_state = options.get("player_state")
inventory_facade = options.get("inventory_facade")
panel_router = options.get("panel_router")
constants = options.get("constants", {}).duplicate(true)
_rebuild_callbacks()
func legacy_callbacks() -> Dictionary:
return _callbacks
func callback(name: String) -> Callable:
return _callbacks.get(name, Callable()) as Callable
func call_action(name: String, args: Array = [], fallback: Variant = null) -> Variant:
var callable := callback(name)
if not callable.is_valid():
return fallback
return callable.callv(args)
func call_action_async(name: String, args: Array = []) -> Variant:
var callable := callback(name)
if not callable.is_valid():
return null
return await callable.callv(args)
func player_data() -> Dictionary:
if owner != null and owner.has_method("_player_data_ref"):
var result = owner.call("_player_data_ref")
if result is Dictionary:
return result
if player_state != null and player_state.has_method("player_data"):
return player_state.player_data()
if inventory_facade != null and inventory_facade.has_method("player_data"):
return inventory_facade.player_data()
return {}
func token() -> String:
if player_state != null and player_state.has_method("token"):
return player_state.token()
return ""
func player_id() -> int:
if player_state != null and player_state.has_method("int_value"):
var id := int(player_state.int_value("id", 0))
if id <= 0:
id = int(player_state.int_value("user_id", 0))
return id
return 0
func item_icon(icon_id: int) -> Texture2D:
if texture_provider != null and texture_provider.has_method("item_icon"):
return texture_provider.item_icon(icon_id)
return null
func house_texture(level: int) -> Texture2D:
if texture_provider != null and texture_provider.has_method("house"):
return texture_provider.house(level, int(constants.get("max_farm_level", 12)))
return null
func common_atlas_path() -> String:
return str(constants.get("common_atlas_path", ""))
func friend_atlas_path() -> String:
return str(constants.get("friend_atlas_path", ""))
func landup_atlas_path() -> String:
return str(constants.get("landup_atlas_path", ""))
func _rebuild_callbacks() -> void:
_callbacks = {
"common_atlas_path": common_atlas_path(),
"friend_atlas_path": friend_atlas_path(),
"landup_atlas_path": landup_atlas_path(),
"create_common_panel_shell": Callable(ui_builder, "create_common_panel_shell"),
"add_nine_patch": Callable(ui_builder, "add_nine_patch"),
"add_texture": Callable(ui_builder, "add_texture"),
"add_texture_button": Callable(ui_builder, "add_texture_button"),
"add_panel_label": Callable(ui_builder, "add_panel_label"),
"create_tab_bar": Callable(ui_builder, "create_tab_bar"),
"configure_vertical_scroll": Callable(ui_builder, "configure_vertical_scroll"),
"apply_empty_button_style": Callable(UiMotionScript, "apply_empty_button_style"),
"style_label": Callable(ui_builder, "style_label"),
"common_texture": Callable(texture_provider, "common"),
"friend_texture": Callable(texture_provider, "friend"),
"sign_texture": Callable(texture_provider, "sign"),
"get_item_icon_texture": Callable(self, "item_icon"),
"get_house_texture": Callable(self, "house_texture"),
"shop_tab_pressed": Callable(panel_router, "on_shop_tab_pressed"),
"warehouse_tab_pressed": Callable(panel_router, "on_warehouse_tab_pressed"),
"open_modal_panel": Callable(panel_router, "open_modal_panel"),
"close_modal_panel": Callable(panel_router, "close_modal_panel"),
"player_data": Callable(self, "player_data"),
"player_id": Callable(self, "player_id"),
"lands_by_position": Callable(owner, "_lands_by_position_ref"),
"token": Callable(self, "token"),
"is_operation_in_progress": Callable(owner, "_is_operation_in_progress"),
"set_operation_in_progress": Callable(owner, "_set_operation_in_progress"),
"show_toast": Callable(owner, "_show_toast"),
"response_data": Callable(owner, "_response_data"),
"render_player": Callable(owner, "_render_player"),
"render_house_art": Callable(owner, "_render_house_art"),
"render_scene_skin": Callable(owner, "_render_scene_skin"),
"render_lands": Callable(owner, "_render_lands"),
"add_player_exp": Callable(owner, "_add_player_exp"),
"apply_land_data": Callable(owner, "_apply_land_data"),
"refresh_store_house": Callable(owner, "_refresh_store_house"),
"decrease_player_currency": Callable(owner, "_decrease_player_currency"),
"warehouse_items": Callable(owner, "_warehouse_items_ref"),
"decrease_warehouse_item": Callable(owner, "_decrease_warehouse_item"),
"add_player_gold": Callable(owner, "_add_player_gold"),
"owned_amount": Callable(owner, "_get_owned_amount"),
"parse_cost_string": Callable(owner, "_parse_cost_string"),
"has_costs": Callable(owner, "_has_costs"),
"apply_cost_deduction": Callable(owner, "_apply_cost_deduction"),
"reward_fields_to_text": Callable(owner, "_reward_fields_to_text"),
"format_reward_text": Callable(owner, "_format_reward_text"),
"add_reward_items": Callable(owner, "_add_reward_items"),
"response_status_text": Callable(owner, "_response_status_text"),
"format_duration": Callable(owner, "_format_duration"),
"set_dog_hunger_end_time": Callable(owner, "_set_dog_hunger_end_time"),
"set_use_dog_item_id": Callable(owner, "_set_use_dog_item_id"),
"set_texture_button_enabled": Callable(owner, "_set_texture_button_enabled"),
"open_pay_panel": Callable(panel_router, "open_pay_panel"),
"open_pet_panel": Callable(panel_router, "open_pet_panel"),
"visit_friend_home": Callable(owner, "_visit_friend_home"),
}