2024-08-11 14:23:56 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Godot;
|
2024-08-24 16:10:08 +00:00
|
|
|
using KeepersCompound.LGS;
|
2024-08-11 14:23:56 +00:00
|
|
|
|
|
|
|
namespace KeepersCompound.TMV;
|
|
|
|
|
2024-08-12 17:46:30 +00:00
|
|
|
public partial class TextureLoader
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
private readonly string _fmName;
|
2024-08-11 14:23:56 +00:00
|
|
|
private readonly List<ImageTexture> _textureCache = new();
|
|
|
|
private readonly Dictionary<int, int> _idMap = new();
|
|
|
|
private readonly Dictionary<string, int> _pathMap = new();
|
|
|
|
|
2024-08-24 16:10:08 +00:00
|
|
|
public TextureLoader(string fmName)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
_fmName = fmName;
|
2024-08-11 14:23:56 +00:00
|
|
|
LoadDefaultTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadDefaultTexture()
|
|
|
|
{
|
|
|
|
// TODO: This should be a resource loaded from RES
|
|
|
|
const string path = "user://textures/jorge.png";
|
|
|
|
var texture = ImageTexture.CreateFromImage(Image.LoadFromFile(path));
|
|
|
|
_textureCache.Add(texture);
|
|
|
|
}
|
|
|
|
|
2024-08-24 16:10:08 +00:00
|
|
|
public bool Load(ResourcePathManager installManager, int id, string path)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var loaded = false;
|
|
|
|
string texPath;
|
|
|
|
if (_fmName != null)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
texPath = installManager.GetTexturePath(_fmName, path);
|
|
|
|
texPath ??= installManager.GetTexturePath(path);
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
2024-08-24 16:10:08 +00:00
|
|
|
else
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
texPath = installManager.GetTexturePath(path);
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
2024-08-24 16:10:08 +00:00
|
|
|
|
|
|
|
if (texPath != null)
|
2024-08-11 14:23:56 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var texture = LoadTexture(texPath);
|
|
|
|
if (texture != null)
|
|
|
|
{
|
|
|
|
_textureCache.Add(texture);
|
|
|
|
loaded = true;
|
|
|
|
}
|
2024-08-11 14:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var index = loaded ? _textureCache.Count - 1 : 0;
|
|
|
|
_idMap.TryAdd(id, index);
|
|
|
|
_pathMap.TryAdd(path, index);
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2024-08-18 09:27:44 +00:00
|
|
|
public static ImageTexture LoadTexture(string path)
|
2024-08-12 17:46:30 +00:00
|
|
|
{
|
|
|
|
var ext = path.GetExtension().ToLower();
|
2024-08-24 16:10:08 +00:00
|
|
|
string[] validExtensions = { "png", "tga", "pcx", "gif" };
|
|
|
|
if (validExtensions.Contains(ext))
|
2024-08-12 17:46:30 +00:00
|
|
|
{
|
2024-08-24 16:10:08 +00:00
|
|
|
var texture = ext switch
|
|
|
|
{
|
|
|
|
"pcx" => LoadPcx(path),
|
|
|
|
"gif" => LoadGif(path),
|
|
|
|
_ => ImageTexture.CreateFromImage(Image.LoadFromFile(path)),
|
|
|
|
};
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
return null;
|
2024-08-12 17:46:30 +00:00
|
|
|
}
|
|
|
|
|
2024-08-11 14:23:56 +00:00
|
|
|
public ImageTexture Get(int id)
|
|
|
|
{
|
|
|
|
if (!_idMap.ContainsKey(id))
|
|
|
|
{
|
|
|
|
return _textureCache[0];
|
|
|
|
}
|
|
|
|
return _textureCache[_idMap[id]];
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageTexture Get(string path)
|
|
|
|
{
|
|
|
|
if (!_pathMap.ContainsKey(path))
|
|
|
|
{
|
|
|
|
return _textureCache[0];
|
|
|
|
}
|
|
|
|
return _textureCache[_pathMap[path]];
|
|
|
|
}
|
|
|
|
}
|