using System.Collections.Generic;
using Godot;
namespace Views;
///
/// 套装主题管理类
///
public class SuitTheme {
private readonly Dictionary _sprites = new();
private readonly Dictionary _theme = new();
///
/// 是否使用16x16精灵
///
public bool Use16 { get; set; }
///
/// 是否为头像
///
public bool IsPortrait { get; set; }
///
/// 缓存组件精灵
///
/// 资源类型
/// 精灵对象
public void CacheComponent(Res.Type type, Sprite2D sprite) {
_sprites[type] = sprite;
if (_theme.TryGetValue(type, out var value)) {
sprite.Texture = ResourceLoader.Load(Res.GetResourcePathWithId(value, type).Path);
}
}
///
/// 解析主题ID
///
/// 主题ID
private void _extractThemeId(ulong themeId) {
for (Res.Type typeId = 0; typeId < Res.Type.ResTypeMax; typeId++)
_theme[typeId] = (ushort)((themeId >> (8 * (int)typeId)) & 0xfful);
}
///
/// 获取主题ID
///
/// 主题ID
public ulong GetThemeId() {
ulong result = 0;
for (Res.Type typeId = 0; typeId < Res.Type.ResTypeMax; typeId++) {
if (!_theme.TryGetValue(typeId, out var value)) continue;
result |= (ulong)(value & 0xff) << (8 * (int)typeId);
}
return result;
}
///
/// 应用主题
///
/// 主题ID
public void ApplyTheme(ulong themeId) {
_extractThemeId(themeId);
foreach (var kvp in _sprites)
if (_theme.TryGetValue(kvp.Key, out var value))
kvp.Value.Texture = ResourceLoader.Load(
Res.GetResourcePathWithId(value, kvp.Key, Use16, IsPortrait).Path
);
}
///
/// 应用组件资源
///
/// 资源类型
/// 资源路径对象
public void ApplyComponent(Res.Type type, Res.ResPathWithId resPath) {
if (!_sprites.TryGetValue(type, out var value)) return;
value.Texture = ResourceLoader.Load(resPath.Path);
_theme[type] = (ushort)resPath.Id;
}
///
/// 是否为空
///
/// 如果是空则返回true
public bool IsEmpty() {
return _sprites.Count == 0;
}
}