123 lines
4.3 KiB
GDScript
123 lines
4.3 KiB
GDScript
class_name TextureProvider
|
||
extends RefCounted
|
||
|
||
# 资源访问门面。
|
||
#
|
||
# Egret 项目同时存在:
|
||
# - png + json 图集帧名;
|
||
# - 运行时区域裁剪名;
|
||
# - 零散 png;
|
||
# - Godot 已转换的 tres。
|
||
#
|
||
# 面板层不要直接关心这些差异,统一从 TextureProvider 取 Texture2D。
|
||
|
||
const TextureRegionCatalogScript := preload("res://scripts/resources/texture_region_catalog.gd")
|
||
const GOLD_BEAN_ICON_PATH := "res://assets/replacement/ui/gold_bean.png"
|
||
|
||
# AtlasTextureCache 负责真正的 load/cache;本类只负责给业务模块提供稳定命名入口。
|
||
var _atlas_cache
|
||
var _fallback_texture: Texture2D
|
||
var _icon_texture_provider
|
||
var _standalone_cache: Dictionary = {}
|
||
|
||
# 逻辑名 -> 图集路径。逻辑名由 GameScene 装配,例如 common、sign、lottery。
|
||
var _atlas_paths: Dictionary = {}
|
||
|
||
|
||
func setup(atlas_cache, atlas_paths: Dictionary, fallback_texture: Texture2D = null, icon_texture_provider = null) -> void:
|
||
# fallback_texture 允许工具脚本或单元检查环境没有资源时仍能安全返回。
|
||
_atlas_cache = atlas_cache
|
||
_atlas_paths = atlas_paths
|
||
_fallback_texture = fallback_texture
|
||
_icon_texture_provider = icon_texture_provider
|
||
|
||
|
||
func common(name: String) -> Texture2D:
|
||
# common 图集使用频率最高,保留短方法减少面板代码噪音。
|
||
if name == "sicon_gold":
|
||
return standalone(GOLD_BEAN_ICON_PATH)
|
||
return region(_atlas_paths.get("common", ""), name)
|
||
|
||
|
||
func friend(name: String) -> Texture2D:
|
||
return region(_atlas_paths.get("friend", ""), name)
|
||
|
||
|
||
func rank(name: String) -> Texture2D:
|
||
return region(_atlas_paths.get("rank", ""), name)
|
||
|
||
|
||
func building(name: String) -> Texture2D:
|
||
return region(_atlas_paths.get("building", ""), name)
|
||
|
||
|
||
func landup(name: String) -> Texture2D:
|
||
return region(_atlas_paths.get("landup", ""), name)
|
||
|
||
|
||
func sign(name: String) -> Texture2D:
|
||
return region(_atlas_paths.get("sign", ""), name)
|
||
|
||
|
||
func region(atlas_path: String, region_name: String) -> Texture2D:
|
||
# region 使用 TextureRegionCatalog 的固定区域表,适合早期手工录入的 common 资源。
|
||
if _atlas_cache == null or atlas_path.is_empty():
|
||
return _fallback_texture
|
||
return _atlas_cache.get_region(atlas_path, region_name)
|
||
|
||
|
||
func frame(atlas_path: String, frame_name: String) -> Texture2D:
|
||
# frame 使用 Egret json 内的帧名,适合完整图集迁移后的业务面板。
|
||
if _atlas_cache == null or atlas_path.is_empty():
|
||
return _fallback_texture
|
||
return _atlas_cache.get_frame(atlas_path, frame_name)
|
||
|
||
|
||
func standalone(path: String, fallback: Texture2D = null) -> Texture2D:
|
||
# 零散图片优先直接 load;不存在时返回调用方 fallback,再退到 common item_bg。
|
||
# 这样资源缺失不会让整个弹窗崩溃,视觉问题可以通过截图再修。
|
||
if _standalone_cache.has(path):
|
||
return _standalone_cache[path]
|
||
if ResourceLoader.exists(path):
|
||
var texture := load(path) as Texture2D
|
||
_standalone_cache[path] = texture
|
||
return texture
|
||
if fallback != null:
|
||
return fallback
|
||
return _fallback_texture if _fallback_texture != null else common("item_bg")
|
||
|
||
|
||
func item_icon(icon_id: int) -> Texture2D:
|
||
if _icon_texture_provider != null and _icon_texture_provider.has_method("item_icon"):
|
||
return _icon_texture_provider.item_icon(icon_id)
|
||
return common("item_bg")
|
||
|
||
|
||
func house(level: int, max_level := 12) -> Texture2D:
|
||
if _icon_texture_provider != null and _icon_texture_provider.has_method("house"):
|
||
return _icon_texture_provider.house(level, max_level)
|
||
return _fallback_texture if _fallback_texture != null else common("item_bg")
|
||
|
||
|
||
func region_for(atlas_path: String, name: String) -> Rect2:
|
||
# 保留给 UiBuilder.create_common_panel_shell:旧 common 图集标题/icon 需要先拿 Rect2。
|
||
return TextureRegionCatalogScript.region_for(atlas_path, name)
|
||
|
||
|
||
func atlas_texture(atlas_path: String, texture_region: Rect2) -> Texture2D:
|
||
# 直接按区域生成 AtlasTexture,用于没有 Egret frame json 的老素材。
|
||
if _atlas_cache == null or atlas_path.is_empty():
|
||
return _fallback_texture
|
||
return _atlas_cache.get_atlas_texture(atlas_path, texture_region)
|
||
|
||
|
||
func atlas_path(name: String) -> String:
|
||
# 面板只知道逻辑名,不硬编码真实资源路径。
|
||
return str(_atlas_paths.get(name, ""))
|
||
|
||
|
||
func clear_icon_cache() -> void:
|
||
_standalone_cache.clear()
|
||
if _icon_texture_provider != null and _icon_texture_provider.has_method("clear"):
|
||
_icon_texture_provider.clear()
|