shoko/Media/ImageMediaHandler.cs

66 lines
1.6 KiB
C#
Raw Normal View History

2023-10-02 18:49:24 +00:00
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()
{
}
2023-10-02 18:49:24 +00:00
public ImageMediaHandler(ProtoHandler content)
{
Content = content;
}
public override void Load()
{
using(var memory = new MemoryStream())
{
Content.Content.CopyTo(memory);
var image = Raylib.LoadImageFromMemory(MediaTypes[Content.MediaType], memory.ToArray());
Texture = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
}
}
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);
}
}