From 75b80507bd0bf7d7eca495f9ba561729dec2e67e Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 28 Jul 2023 11:52:07 -0400 Subject: [PATCH] handle "filter result size = 0" without crashing --- Program.cs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Program.cs b/Program.cs index 6084e7f..9efd8ab 100644 --- a/Program.cs +++ b/Program.cs @@ -647,7 +647,11 @@ public class Game : GameWindow { } // Make sure the photoIndex is actually valid. - photoIndex = Math.Clamp(photoIndex, 0, photos.Count - 1); + if (photos.Count == 0) { + photoIndex = 0; + } else { + photoIndex = Math.Clamp(photoIndex, 0, photos.Count - 1); + } // Handle presses of the "rating" keys -- 0-5 and `. // A normal press just sets the rating of the current photo. @@ -683,7 +687,9 @@ public class Game : GameWindow { if (shifted) { FilterByRating(rating); } else { - photos[photoIndex].Rating = rating; + if (photos.Count > 0) { + photos[photoIndex].Rating = rating; + } } } @@ -714,7 +720,7 @@ public class Game : GameWindow { void FilterByRating(int rating) { Console.WriteLine("filter to " + rating); - Photo previouslyActive = photos[photoIndex]; + Photo previouslyActive = photos.Count > 0 ? photos[photoIndex] : allPhotos[0]; photos = allPhotos.Where(p => p.Rating >= rating).ToList(); // Move photoIndex to wherever the previously active photo was, or if it // was filtered out, to whichever unfiltered photo comes before it. This @@ -773,8 +779,8 @@ public class Game : GameWindow { // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); - // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); - string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); + string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); + // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); for (int i = 0; i < files.Count(); i++) { string file = files[i]; @@ -843,6 +849,16 @@ public class Game : GameWindow { GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.ActiveTexture(TextureUnit.Texture0); + if (photos.Count > 0) { + DrawPhotos(); + } else { + DrawText("No photos found.", 10, 10); + } + + SwapBuffers(); + } + + void DrawPhotos() { Photo activePhoto = photos[photoIndex]; Texture active = activePhoto.Texture(); @@ -892,8 +908,6 @@ public class Game : GameWindow { if (activePhoto.Loaded) { DrawText($"{(scale * 100):F1}%", geometry.StatusBox.Min.X, geometry.StatusBox.Min.Y + statusPadding); } - - SwapBuffers(); } void DrawTexture(Texture texture, int x, int y) {