shoko/Media/ImageMediaHandler.cs

68 lines
1.7 KiB
C#

using ImGuiNET;
using Raylib_cs;
using rlImGui_cs;
namespace Shoko;
[MediaType("image/png")]
[MediaType("image/gif")]
[MediaType("image/qoi")]
[MediaType("image/x-qoi")]
class ImageMediaHandler : MediaHandler
{
Dictionary<string, string> MediaTypes = new(){
["image/png"] = ".png",
["image/gif"] = ".gif",
["image/qoi"] = ".qoi",
["image/x-qoi"] = ".qoi",
};
protected Texture2D Texture;
protected float Zoom = 1;
public ImageMediaHandler()
{
}
public ImageMediaHandler(ProtoHandler content)
{
Content = content;
}
public override async Task Load()
{
Title = Content.URL.AbsolutePath;
using(var memory = new MemoryStream())
{
await Content.Content.CopyToAsync(memory);
var image = Raylib.LoadImageFromMemory(MediaTypes[Content.MediaType], memory.ToArray());
Texture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
}
OnLoaded();
}
public override void Render()
{
if(Zoom == 1)
rlImGui.Image(Texture);
else
rlImGui.ImageSize(Texture, (int)(Texture.width*Zoom), (int)(Texture.height*Zoom));
}
public override void MenuBar()
{
Gui.Menu("Image", ()=>{
ImGui.Text(string.Format("{0}, {1}x{2}", Content.MediaType, Texture.width, Texture.height));
ImGui.SliderFloat("Zoom", ref Zoom, 0.0625f, 16f, (Zoom*100f).ToString("0.0"), ImGuiSliderFlags.Logarithmic);
});
}
public virtual void Render(int width, int height)
{
rlImGui.ImageSize(Texture, width, height);
}
~ImageMediaHandler()
{
Raylib.UnloadTexture(Texture);
}
}