fix letterboxing calculation & use new UiGeometry

This commit is contained in:
Colin McMillen 2023-07-07 16:00:43 -04:00
parent b41321b0c7
commit 72e65b912c

View File

@ -6,6 +6,7 @@ using OpenTK.Windowing.GraphicsLibraryFramework;
// https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html // https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Exif; using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Image = SixLabors.ImageSharp.Image; using Image = SixLabors.ImageSharp.Image;
@ -133,6 +134,7 @@ void main() {
public class Texture : IDisposable { public class Texture : IDisposable {
public int Handle; public int Handle;
// FIXME: use a Vector2i for dimensions.
public int Width; public int Width;
public int Height; public int Height;
@ -188,6 +190,7 @@ public class UiGeometry {
public readonly Vector2i WindowSize; public readonly Vector2i WindowSize;
public readonly List<Box2i> ThumbnailBoxes = new(); public readonly List<Box2i> ThumbnailBoxes = new();
public readonly Box2i PhotoBox;
public UiGeometry() : this(MIN_WINDOW_SIZE) {} public UiGeometry() : this(MIN_WINDOW_SIZE) {}
@ -201,6 +204,8 @@ public class UiGeometry {
Box2i box = Util.makeBox(WindowSize.X - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight); Box2i box = Util.makeBox(WindowSize.X - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight);
ThumbnailBoxes.Add(box); ThumbnailBoxes.Add(box);
} }
PhotoBox = new(0, 0, WindowSize.X - thumbnailWidth, WindowSize.Y);
} }
} }
@ -312,14 +317,16 @@ public class Game : GameWindow {
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
int maxPhotoWidth = geometry.WindowSize.X - geometry.ThumbnailBoxes[0].Size.X; // FIXME
Texture active = textures[textureIndex]; Texture active = textures[textureIndex];
// TODO: handle the case where we need to letterbox vertically instead.
int photoWidth = (int) (1.0 * geometry.WindowSize.Y / active.Height * active.Width);
int letterboxWidth = (maxPhotoWidth - photoWidth) / 2;
DrawTexture(active, Util.makeBox(letterboxWidth, 0, photoWidth, geometry.WindowSize.Y)); // FIXME: make a function for scaling & centering one box on another.
double scaleX = 1.0 * geometry.PhotoBox.Size.X / active.Width;
double scaleY = 1.0 * geometry.PhotoBox.Size.Y / active.Height;
double scale = Math.Min(scaleX, scaleY);
int renderWidth = (int) (active.Width * scale);
int renderHeight = (int) (active.Height * scale);
Box2i photoBox = Util.makeBox((int) geometry.PhotoBox.Center.X - renderWidth / 2, (int) geometry.PhotoBox.Center.Y - renderHeight / 2, renderWidth, renderHeight);
DrawTexture(active, photoBox);
for (int i = 0; i < textures.Count; i++) { for (int i = 0; i < textures.Count; i++) {
Box2i box = geometry.ThumbnailBoxes[i]; Box2i box = geometry.ThumbnailBoxes[i];
DrawTexture(textures[i], box); DrawTexture(textures[i], box);