draw crop rectangle when viewing a cropped photo

This commit is contained in:
Colin McMillen 2023-08-03 23:21:10 -04:00
parent 08862f52af
commit ff938693ff

View File

@ -126,8 +126,8 @@ public class CropTool : ITool {
}
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;
@ -167,27 +167,6 @@ public class CropTool : ITool {
}
public void Draw(UiGeometry geometry, Game game) {
if (activePhoto.CropRectangle == Rectangle.Empty) {
return;
}
Vector2i leftTop = game.ImageToScreen(activePhoto.CropRectangle.Left, activePhoto.CropRectangle.Top);
Vector2i rightBottom = game.ImageToScreen(activePhoto.CropRectangle.Right, activePhoto.CropRectangle.Bottom);
var (left, top) = leftTop;
var (right, bottom) = rightBottom;
Color4 shadeColor = new Color4(0, 0, 0, 0.75f);
game.DrawFilledBox(new Box2i(0, 0, left, geometry.PhotoBox.Max.Y), shadeColor);
game.DrawFilledBox(new Box2i(left, 0, geometry.PhotoBox.Max.X, top), shadeColor);
game.DrawFilledBox(new Box2i(left, bottom, geometry.PhotoBox.Max.X, geometry.PhotoBox.Max.Y), shadeColor);
game.DrawFilledBox(new Box2i(right, top, geometry.PhotoBox.Max.X, bottom), shadeColor);
game.DrawBox(new Box2i(left, top, right, bottom), 1, Color4.White);
game.DrawBox(new Box2i(left - 1, top - 1 , right + 1, bottom + 1), 1, Color4.Black);
game.DrawBox(new Box2i(left - 2, top - 2 , right + 2, bottom + 2), 1, Color4.White);
game.DrawHorizontalLine(left, Util.Lerp(top, bottom, 1.0 / 3), right, Color4.White);
game.DrawHorizontalLine(left, Util.Lerp(top, bottom, 2.0 / 3), right, Color4.White);
game.DrawVerticalLine(Util.Lerp(left, right, 1.0 / 3), top, bottom, Color4.White);
game.DrawVerticalLine(Util.Lerp(left, right, 2.0 / 3), top, bottom, Color4.White);
}
public string Status() {
@ -712,6 +691,8 @@ public class Game : GameWindow {
protected override void OnUpdateFrame(FrameEventArgs e) {
base.OnUpdateFrame(e);
Photo previousPhoto = photos[photoIndex];
KeyboardState input = KeyboardState;
bool shiftIsDown = input.IsKeyDown(Keys.LeftShift) || input.IsKeyDown(Keys.RightShift);
@ -860,6 +841,10 @@ public class Game : GameWindow {
}
// Handle tool switching.
if (photos[photoIndex] != previousPhoto) {
activeTool = viewTool;
}
if (activeTool == viewTool) {
if (input.IsKeyPressed(Keys.C)) {
activeTool = new CropTool(photos[photoIndex]);
@ -867,7 +852,6 @@ public class Game : GameWindow {
}
// Delegate input to the active tool.
activeTool.SetActivePhoto(photos[photoIndex]);
ToolState state = activeTool.HandleInput(geometry, KeyboardState, MouseState, this);
// Change back to the default tool if the active tool is done.
@ -1076,6 +1060,8 @@ public class Game : GameWindow {
Texture star = (activePhoto.Rating > i) ? STAR_FILLED : STAR_EMPTY;
DrawTexture(star, geometry.StarBoxes[i].Min.X, geometry.StarBoxes[i].Min.Y);
}
bool cropActive = activeTool.GetType().Name == "CropTool";
DrawCropRectangle(cropActive);
// Draw thumbnail boxes.
ribbonIndex = Math.Clamp(photoIndex - (geometry.ThumbnailBoxes.Count - 1) / 2, 0, Math.Max(0, photos.Count - geometry.ThumbnailBoxes.Count));
@ -1113,6 +1099,31 @@ public class Game : GameWindow {
}
}
void DrawCropRectangle(bool active) {
Photo activePhoto = photos[photoIndex];
if (activePhoto.CropRectangle == Rectangle.Empty) {
return;
}
Vector2i leftTop = ImageToScreen(activePhoto.CropRectangle.Left, activePhoto.CropRectangle.Top);
Vector2i rightBottom = ImageToScreen(activePhoto.CropRectangle.Right, activePhoto.CropRectangle.Bottom);
var (left, top) = leftTop;
var (right, bottom) = rightBottom;
Color4 shadeColor = new Color4(0, 0, 0, 0.75f);
DrawFilledBox(new Box2i(0, 0, left, geometry.PhotoBox.Max.Y), shadeColor);
DrawFilledBox(new Box2i(left, 0, geometry.PhotoBox.Max.X, top), shadeColor);
DrawFilledBox(new Box2i(left, bottom, geometry.PhotoBox.Max.X, geometry.PhotoBox.Max.Y), shadeColor);
DrawFilledBox(new Box2i(right, top, geometry.PhotoBox.Max.X, bottom), shadeColor);
DrawBox(new Box2i(left, top, right, bottom), 1, Color4.White);
if (active) {
DrawHorizontalLine(left, Util.Lerp(top, bottom, 1.0 / 3), right, Color4.White);
DrawHorizontalLine(left, Util.Lerp(top, bottom, 2.0 / 3), right, Color4.White);
DrawVerticalLine(Util.Lerp(left, right, 1.0 / 3), top, bottom, Color4.White);
DrawVerticalLine(Util.Lerp(left, right, 2.0 / 3), top, bottom, Color4.White);
}
}
public Vector2i ScreenToImage(int x, int y) {
int rx = (int) ((x - activeOffset.X) / activeScale);
rx = Math.Clamp(rx, 0, photos[photoIndex].Size.X);