supervisor-simulator/scripts/Views/SuitTheme.cs

89 lines
2.7 KiB
C#

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