allow resizing the crop box by dragging handles

This commit is contained in:
Colin McMillen 2023-09-15 01:42:01 -04:00
parent d72b804b92
commit 4cd0d4efc6

View File

@ -122,10 +122,12 @@ public class ViewTool : ITool {
public class CropTool : ITool { public class CropTool : ITool {
enum Mode { Sizing, Translating };
Photo photo; Photo photo;
Vector2i mouseDragStart; Vector2i mouseDragStart;
Vector2i mouseDragEnd; Vector2i mouseDragEnd;
bool dragging; Mode mode;
string status = ""; string status = "";
public CropTool(Photo photo) { public CropTool(Photo photo) {
@ -138,15 +140,38 @@ public class CropTool : ITool {
Vector2i mousePosition = (Vector2i) mouse.Position; Vector2i mousePosition = (Vector2i) mouse.Position;
Vector2i imagePosition = transform.ScreenToImage(mousePosition); Vector2i imagePosition = transform.ScreenToImage(mousePosition);
List<Vector2i> corners = Util.RectangleCorners(photo.CropRectangle);
Vector2i? oppositeCorner = null;
bool mouseNearHandle = false;
for (int i = 0; i < 4; i++) {
Vector2i corner = corners[i];
Vector2i handlePosition = transform.ImageToScreen(corner.X, corner.Y);
if (Vector2i.Subtract(mousePosition, handlePosition).ManhattanLength < 10) {
mouseNearHandle = true;
oppositeCorner = corners[(i + 2) % 4];
break;
}
}
bool mouseInRectangle = photo.CropRectangle.Contains(imagePosition.X, imagePosition.Y); bool mouseInRectangle = photo.CropRectangle.Contains(imagePosition.X, imagePosition.Y);
if (mouse.IsButtonPressed(MouseButton.Button1)) { if (mouse.IsButtonPressed(MouseButton.Button1)) {
dragging = mouseInRectangle; if (mouseNearHandle || !mouseInRectangle) {
mode = Mode.Sizing;
} else {
mode = Mode.Translating;
}
}
if (mouseNearHandle) {
game.Cursor = MouseCursor.Hand;
} else if (mouseInRectangle) {
game.Cursor = MouseCursor.Default;
} else {
game.Cursor = MouseCursor.Crosshair;
} }
game.Cursor = mouseInRectangle ? MouseCursor.Default : MouseCursor.Crosshair;
if (!dragging) { if (mode == Mode.Sizing) {
if (mouse.IsButtonPressed(MouseButton.Button1)) { if (mouse.IsButtonPressed(MouseButton.Button1)) {
mouseDragStart = imagePosition; mouseDragStart = oppositeCorner ?? imagePosition;
} }
if (mouse.IsButtonDown(MouseButton.Button1)) { if (mouse.IsButtonDown(MouseButton.Button1)) {
mouseDragEnd = imagePosition; mouseDragEnd = imagePosition;
@ -297,6 +322,16 @@ public static class Util {
return new Box2i(left, top, left + width, top + height); return new Box2i(left, top, left + width, top + height);
} }
// resulting items are ordered such that a corner's opposite is 2 indexes away.
public static List<Vector2i> RectangleCorners(Rectangle r) {
List<Vector2i> result = new(4);
result.Add(new(r.Left, r.Top));
result.Add(new(r.Right, r.Top));
result.Add(new(r.Right, r.Bottom));
result.Add(new(r.Left, r.Bottom));
return result;
}
public static Image<Rgba32> MakeImage(float width, float height) { public static Image<Rgba32> MakeImage(float width, float height) {
return new((int) Math.Ceiling(width), (int) Math.Ceiling(height)); return new((int) Math.Ceiling(width), (int) Math.Ceiling(height));
} }
@ -724,8 +759,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(@"c:\users\colin\pictures\photos\2023\09\06\jpg"); // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\09\06\jpg");
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\");
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\totte-output\2023\08\29"); string[] files = Directory.GetFiles(@"c:\users\colin\desktop\totte-output\2023\08\03");
string[] files = Directory.GetFiles(@"c:\users\colin\desktop\export"); // string[] files = Directory.GetFiles(@"c:\users\colin\desktop\export");
// 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\");