Browse Source

fix letterboxing calculation & use new UiGeometry

main
Colin McMillen 1 year ago
parent
commit
72e65b912c
  1. 19
      Program.cs

19
Program.cs

@ -6,6 +6,7 @@ using OpenTK.Windowing.GraphicsLibraryFramework;
// https://docs.sixlabors.com/api/ImageSharp/SixLabors.ImageSharp.Image.html
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
using System;
using System.Runtime.CompilerServices;
using Image = SixLabors.ImageSharp.Image;
@ -133,6 +134,7 @@ void main() {
public class Texture : IDisposable {
public int Handle;
// FIXME: use a Vector2i for dimensions.
public int Width;
public int Height;
@ -188,6 +190,7 @@ public class UiGeometry {
public readonly Vector2i WindowSize;
public readonly List<Box2i> ThumbnailBoxes = new();
public readonly Box2i PhotoBox;
public UiGeometry() : this(MIN_WINDOW_SIZE) {}
@ -201,6 +204,8 @@ public class UiGeometry {
Box2i box = Util.makeBox(WindowSize.X - thumbnailWidth, i * thumbnailHeight, thumbnailWidth, thumbnailHeight);
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.ActiveTexture(TextureUnit.Texture0);
int maxPhotoWidth = geometry.WindowSize.X - geometry.ThumbnailBoxes[0].Size.X; // FIXME
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++) {
Box2i box = geometry.ThumbnailBoxes[i];
DrawTexture(textures[i], box);

Loading…
Cancel
Save