add ability to draw a simple crop box

This commit is contained in:
Colin McMillen 2023-08-01 14:12:52 -04:00
parent 9cdef230f4
commit 1ad8b06be8

View File

@ -522,6 +522,10 @@ public class UiGeometry {
public static class Util { public static class Util {
public const float PI = (float) Math.PI; public const float PI = (float) Math.PI;
public static int Lerp(int start, int end, double fraction) {
return start + (int) ((end - start) * fraction);
}
public static Box2i MakeBox(int left, int top, int width, int height) { public static Box2i MakeBox(int left, int top, int width, int height) {
return new Box2i(left, top, left + width, top + height); return new Box2i(left, top, left + width, top + height);
} }
@ -633,6 +637,9 @@ public class Game : GameWindow {
readonly object loadedImagesLock = new(); readonly object loadedImagesLock = new();
int photoIndex = 0; int photoIndex = 0;
int ribbonIndex = 0; int ribbonIndex = 0;
Vector2i mouseDragStart;
Vector2i mouseDragEnd;
bool mouseDragging;
Shader shader = new(); Shader shader = new();
Matrix4 projection; Matrix4 projection;
float zoomLevel = 0f; float zoomLevel = 0f;
@ -671,6 +678,21 @@ public class Game : GameWindow {
photoIndex = ribbonIndex + i; photoIndex = ribbonIndex + i;
} }
} }
if (geometry.PhotoBox.ContainsInclusive(click)) {
mouseDragging = true;
mouseDragStart = click;
}
}
if (MouseState.IsButtonDown(MouseButton.Button1)) {
// FIXME: really this should be clipped to the active photo's drawable area, not the whole photobox.
mouseDragEnd = new Vector2i(
Math.Clamp((int) MouseState.Position.X, geometry.PhotoBox.Min.X, geometry.PhotoBox.Max.X),
Math.Clamp((int) MouseState.Position.Y, geometry.PhotoBox.Min.Y, geometry.PhotoBox.Max.Y));
} else {
mouseDragging = false;
} }
if (MouseState.IsButtonPressed(MouseButton.Button4)) { if (MouseState.IsButtonPressed(MouseButton.Button4)) {
@ -958,9 +980,28 @@ public class Game : GameWindow {
DrawText("No photos found.", 10, 10); DrawText("No photos found.", 10, 10);
} }
if (mouseDragging) {
DrawCropBox();
}
SwapBuffers(); SwapBuffers();
} }
void DrawCropBox() {
int left = Math.Min(mouseDragStart.X, mouseDragEnd.X);
int right = Math.Max(mouseDragStart.X, mouseDragEnd.X);
int top = Math.Min(mouseDragStart.Y, mouseDragEnd.Y);
int bottom = Math.Max(mouseDragStart.Y, mouseDragEnd.Y);
DrawBox(new Box2i(left, top, right, bottom), 1, Color4.White);
DrawBox(new Box2i(left - 1, top - 1 , right + 1, bottom + 1), 1, Color4.Black);
DrawBox(new Box2i(left - 2, top - 2 , right + 2, bottom + 2), 1, Color4.White);
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);
}
void DrawPhotos() { void DrawPhotos() {
Photo activePhoto = photos[photoIndex]; Photo activePhoto = photos[photoIndex];
Texture active = activePhoto.Texture(); Texture active = activePhoto.Texture();
@ -1029,6 +1070,14 @@ public class Game : GameWindow {
GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0);
} }
void DrawHorizontalLine(int left, int top, int right, Color4 color) {
DrawTexture(TEXTURE_WHITE, Util.MakeBox(left, top, right - left, 1), color);
}
void DrawVerticalLine(int left, int top, int bottom, Color4 color) {
DrawTexture(TEXTURE_WHITE, Util.MakeBox(left, top, 1, bottom - top), color);
}
void DrawBox(Box2i box, int thickness, Color4 color) { void DrawBox(Box2i box, int thickness, Color4 color) {
DrawTexture(TEXTURE_WHITE, Util.MakeBox(box.Min.X, box.Min.Y, box.Size.X, thickness), color); DrawTexture(TEXTURE_WHITE, Util.MakeBox(box.Min.X, box.Min.Y, box.Size.X, thickness), color);
DrawTexture(TEXTURE_WHITE, Util.MakeBox(box.Min.X, box.Min.Y, thickness, box.Size.Y), color); DrawTexture(TEXTURE_WHITE, Util.MakeBox(box.Min.X, box.Min.Y, thickness, box.Size.Y), color);