handle "filter result size = 0" without crashing

This commit is contained in:
Colin McMillen 2023-07-28 11:52:07 -04:00
parent 1bedac471d
commit 75b80507bd

View File

@ -647,7 +647,11 @@ public class Game : GameWindow {
} }
// Make sure the photoIndex is actually valid. // Make sure the photoIndex is actually valid.
if (photos.Count == 0) {
photoIndex = 0;
} else {
photoIndex = Math.Clamp(photoIndex, 0, photos.Count - 1); photoIndex = Math.Clamp(photoIndex, 0, photos.Count - 1);
}
// Handle presses of the "rating" keys -- 0-5 and `. // Handle presses of the "rating" keys -- 0-5 and `.
// A normal press just sets the rating of the current photo. // A normal press just sets the rating of the current photo.
@ -683,9 +687,11 @@ public class Game : GameWindow {
if (shifted) { if (shifted) {
FilterByRating(rating); FilterByRating(rating);
} else { } else {
if (photos.Count > 0) {
photos[photoIndex].Rating = rating; photos[photoIndex].Rating = rating;
} }
} }
}
if (input.IsKeyPressed(Keys.Q)) { if (input.IsKeyPressed(Keys.Q)) {
zoomLevel = 0f; zoomLevel = 0f;
@ -714,7 +720,7 @@ public class Game : GameWindow {
void FilterByRating(int rating) { void FilterByRating(int rating) {
Console.WriteLine("filter to " + 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(); photos = allPhotos.Where(p => p.Rating >= rating).ToList();
// Move photoIndex to wherever the previously active photo was, or if it // Move photoIndex to wherever the previously active photo was, or if it
// was filtered out, to whichever unfiltered photo comes before it. This // 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(@"c:\users\colin\pictures\photos\2023\07\14\");
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // 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\Pictures\photos\2018\06\23");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); 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\many-birds\");
for (int i = 0; i < files.Count(); i++) { for (int i = 0; i < files.Count(); i++) {
string file = files[i]; string file = files[i];
@ -843,6 +849,16 @@ public class Game : GameWindow {
GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject);
GL.ActiveTexture(TextureUnit.Texture0); GL.ActiveTexture(TextureUnit.Texture0);
if (photos.Count > 0) {
DrawPhotos();
} else {
DrawText("No photos found.", 10, 10);
}
SwapBuffers();
}
void DrawPhotos() {
Photo activePhoto = photos[photoIndex]; Photo activePhoto = photos[photoIndex];
Texture active = activePhoto.Texture(); Texture active = activePhoto.Texture();
@ -892,8 +908,6 @@ public class Game : GameWindow {
if (activePhoto.Loaded) { if (activePhoto.Loaded) {
DrawText($"{(scale * 100):F1}%", geometry.StatusBox.Min.X, geometry.StatusBox.Min.Y + statusPadding); DrawText($"{(scale * 100):F1}%", geometry.StatusBox.Min.X, geometry.StatusBox.Min.Y + statusPadding);
} }
SwapBuffers();
} }
void DrawTexture(Texture texture, int x, int y) { void DrawTexture(Texture texture, int x, int y) {