Browse Source

allow user to drag around the crop box

main
Colin McMillen 11 months ago
parent
commit
1c586a6197
  1. 42
      Program.cs

42
Program.cs

@ -81,14 +81,14 @@ public class ViewTool : ITool {
// FIXME: remove unneeded dependencies on "Game" or at least refactor them a bit.
public class CropTool : ITool {
Photo activePhoto;
Photo photo;
Vector2i mouseDragStart;
Vector2i mouseDragEnd;
bool dragging;
string status = "";
public CropTool(Photo photo) {
activePhoto = photo;
this.photo = photo;
mouseDragStart = new(photo.CropRectangle.Left, photo.CropRectangle.Top);
mouseDragEnd = new(photo.CropRectangle.Right, photo.CropRectangle.Bottom);
}
@ -98,7 +98,7 @@ public class CropTool : ITool {
Vector2i imagePosition = game.ScreenToImage(mousePosition);
if (mouse.IsButtonPressed(MouseButton.Button1)) {
dragging = activePhoto.CropRectangle.Contains(imagePosition.X, imagePosition.Y);
dragging = photo.CropRectangle.Contains(imagePosition.X, imagePosition.Y);
}
if (!dragging) {
@ -111,23 +111,41 @@ public class CropTool : ITool {
var (left, right, top, bottom) = GetCrop();
if (left != right && top != bottom) {
activePhoto.CropRectangle = Rectangle.FromLTRB(left, top, right, bottom);
photo.CropRectangle = Rectangle.FromLTRB(left, top, right, bottom);
} else {
activePhoto.CropRectangle = Rectangle.Empty;
photo.CropRectangle = Rectangle.Empty;
}
} else {
// FIXME
if (mouse.IsButtonDown(MouseButton.Button1)) {
Vector2 delta = mouse.Delta;
Vector2i imageDelta = game.ScreenToImageDelta((int) delta.X, (int) delta.Y);
photo.CropRectangle.Offset(imageDelta.X, imageDelta.Y);
if (photo.CropRectangle.Left < 0) {
photo.CropRectangle.Offset(-photo.CropRectangle.Left, 0);
}
if (photo.CropRectangle.Right > photo.Size.X) {
int overshoot = photo.CropRectangle.Right - photo.Size.X;
photo.CropRectangle.Offset(-overshoot, 0);
}
if (photo.CropRectangle.Top < 0) {
photo.CropRectangle.Offset(0, -photo.CropRectangle.Top);
}
if (photo.CropRectangle.Bottom > photo.Size.Y) {
int overshoot = photo.CropRectangle.Bottom - photo.Size.Y;
photo.CropRectangle.Offset(0, -overshoot);
}
}
}
Rectangle r = activePhoto.CropRectangle;
status = $"({r.Left}, {r.Top}) {r.Width}x{r.Height}";
Rectangle r = photo.CropRectangle;
status = $"({r.Left}, {r.Top}, {r.Right}, {r.Bottom}) {r.Width}x{r.Height}";
if (input.IsKeyPressed(Keys.Enter)) {
return ToolStatus.Done;
}
if (input.IsKeyPressed(Keys.Escape)) {
activePhoto.CropRectangle = Rectangle.Empty;
photo.CropRectangle = Rectangle.Empty;
return ToolStatus.Canceled;
}
@ -828,10 +846,14 @@ public class Game : GameWindow {
}
}
public Vector2i ScreenToImageDelta(int x, int y) {
return new((int) (x / activeScale), (int) (y / activeScale));
}
public Vector2i ScreenToImage(int x, int y) {
int rx = (int) ((x - activeOffset.X) / activeScale);
rx = Math.Clamp(rx, 0, photos[photoIndex].Size.X);
int ry = (int) ((y - activeOffset.Y) / activeScale);
rx = Math.Clamp(rx, 0, photos[photoIndex].Size.X);
ry = Math.Clamp(ry, 0, photos[photoIndex].Size.Y);
return new(rx, ry);
}

Loading…
Cancel
Save