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

59 lines
1.7 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 IconTextureProvider
extends RefCounted
# 物品/房屋图标资源缓存。
#
# 原 Egret 图标分散在 item_icon、icon、petIcon、plant、house 多个目录。
# 这里统一按优先级查找并缓存 Texture2D避免 UI 面板和 GameScene 重复 load。
const ITEM_ICON_PATHS := [
"res://assets/egret/item_icon/%s.png",
"res://assets/egret/assets/icon/icon/%s.png",
"res://assets/egret/assets/icon/petIcon/%s.png",
"res://assets/egret/assets/icon/plant/%s.png",
]
const ITEM_FALLBACK_PATH := "res://assets/egret/item_icon/1010.png"
const HOUSE_PATH := "res://assets/egret/assets/icon/house/%s.png"
const HOUSE_FALLBACK_PATH := "res://assets/egret/assets/icon/house/1.png"
var _cache: Dictionary = {}
func item_icon(icon_id: int) -> Texture2D:
var key := "item:%s" % icon_id
if _cache.has(key):
return _cache[key]
var texture := _load_first_existing(_item_icon_candidates(icon_id), ITEM_FALLBACK_PATH)
_cache[key] = texture
return texture
func house(level: int, max_level := 12) -> Texture2D:
var farm_level := clampi(level, 1, max_level)
var key := "house:%s" % farm_level
if _cache.has(key):
return _cache[key]
var texture := _load_first_existing([HOUSE_PATH % farm_level], HOUSE_FALLBACK_PATH)
_cache[key] = texture
return texture
func clear() -> void:
_cache.clear()
func _item_icon_candidates(icon_id: int) -> Array[String]:
var paths: Array[String] = []
for template in ITEM_ICON_PATHS:
paths.append(template % icon_id)
return paths
func _load_first_existing(paths: Array[String], fallback_path: String) -> Texture2D:
for path in paths:
if ResourceLoader.exists(path):
return load(path) as Texture2D
if ResourceLoader.exists(fallback_path):
return load(fallback_path) as Texture2D
return null