From c953fa2b47318d885da3db2d08efdb0a308af5ab Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Fri, 4 Aug 2023 00:17:11 -0400 Subject: [PATCH] refactor CropTool.HandleInput a bit --- Program.cs | 55 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Program.cs b/Program.cs index dafd6b7..a42843f 100644 --- a/Program.cs +++ b/Program.cs @@ -50,7 +50,7 @@ public class CameraInfo { } -public enum ToolState { +public enum ToolStatus { Active, Done, Canceled @@ -58,15 +58,15 @@ public enum ToolState { public interface ITool { - ToolState HandleInput(UiGeometry geometry, KeyboardState input, MouseState mouse, Game game); + ToolStatus HandleInput(KeyboardState input, MouseState mouse, Game game); string Status(); void Draw(UiGeometry geometry, Game game); } public class ViewTool : ITool { - public ToolState HandleInput(UiGeometry geometry, KeyboardState input, MouseState mouse, Game game) { - return ToolState.Active; + public ToolStatus HandleInput(KeyboardState input, MouseState mouse, Game game) { + return ToolStatus.Active; } public string Status() { @@ -84,6 +84,7 @@ public class CropTool : ITool { Photo activePhoto; Vector2i mouseDragStart; Vector2i mouseDragEnd; + bool dragging; string status = ""; public CropTool(Photo photo) { @@ -92,50 +93,52 @@ public class CropTool : ITool { mouseDragEnd = new(photo.CropRectangle.Right, photo.CropRectangle.Bottom); } - public ToolState HandleInput(UiGeometry geometry, KeyboardState input, MouseState mouse, Game game) { + public ToolStatus HandleInput(KeyboardState input, MouseState mouse, Game game) { Vector2i mousePosition = (Vector2i) mouse.Position; Vector2i imagePosition = game.ScreenToImage(mousePosition); if (mouse.IsButtonPressed(MouseButton.Button1)) { - if (geometry.PhotoBox.ContainsInclusive(mousePosition)) { + dragging = activePhoto.CropRectangle.Contains(imagePosition.X, imagePosition.Y); + } + + if (!dragging) { + if (mouse.IsButtonPressed(MouseButton.Button1)) { mouseDragStart = imagePosition; } - } - - if (mouse.IsButtonDown(MouseButton.Button1)) { - if (geometry.PhotoBox.ContainsInclusive(mousePosition)) { + if (mouse.IsButtonDown(MouseButton.Button1)) { mouseDragEnd = imagePosition; } + + var (left, right, top, bottom) = GetCrop(); + if (left != right && top != bottom) { + activePhoto.CropRectangle = Rectangle.FromLTRB(left, top, right, bottom); + } else { + activePhoto.CropRectangle = Rectangle.Empty; + } + } else { + // FIXME } - var (left, right, top, bottom) = GetCrop(); - Vector2i size = new(right - left, bottom - top); - - status = $"({left}, {top}) {size.X}x{size.Y}"; - - Rectangle crop = Rectangle.Empty; - if (size.X > 0 && size.Y > 0) { - crop = Rectangle.FromLTRB(left, top, right, bottom); - } - activePhoto.CropRectangle = crop; + Rectangle r = activePhoto.CropRectangle; + status = $"({r.Left}, {r.Top}) {r.Width}x{r.Height}"; if (input.IsKeyPressed(Keys.Enter)) { - return ToolState.Done; + return ToolStatus.Done; } if (input.IsKeyPressed(Keys.Escape)) { activePhoto.CropRectangle = Rectangle.Empty; - return ToolState.Canceled; + return ToolStatus.Canceled; } - return ToolState.Active; + return ToolStatus.Active; } // left, right, top, bottom (int, int, int, int) GetCrop() { // FIXME: this expects the start point in the top left and the end point // in the bottom right; some sign flipping needs to occur to make anchors - // in other direction work well. + // in other directions work well. Vector2i start = mouseDragStart; Vector2i end = mouseDragEnd; // FIXME: choose the aspect ratio based on the original image aspect ratio. @@ -553,10 +556,10 @@ public class Game : GameWindow { } // Delegate input to the active tool. - ToolState state = activeTool.HandleInput(geometry, KeyboardState, MouseState, this); + ToolStatus status = activeTool.HandleInput(KeyboardState, MouseState, this); // Change back to the default tool if the active tool is done. - if (state != ToolState.Active) { + if (status != ToolStatus.Active) { activeTool = viewTool; } }