mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
merge-f: F1: Perspective Crop takes either winding; Alt corner drag keeps the corners as they lie (#13485)
This commit is contained in:
@@ -22,8 +22,8 @@ use egui::{CornerRadius, Pos2, Rect, Response, Vec2};
|
||||
|
||||
use super::words::CropWords;
|
||||
|
||||
/// The quad of a perspective crop in document units, corners ordered top left, top right, bottom right, bottom left;
|
||||
/// `None` before one is drawn.
|
||||
/// The quad of a perspective crop in document units, its corners in the order of the crop's top left, top right,
|
||||
/// bottom right, bottom left; `None` before one is drawn.
|
||||
pub type PerspectiveQuad = Option<[Point; 4]>;
|
||||
|
||||
/// Fewest and most grid steps along a side.
|
||||
@@ -119,8 +119,9 @@ struct Report {
|
||||
/// - Shift on a corner drag keeps the axis the pointer moved less along; Alt on a corner of an upright quad moves the
|
||||
/// neighbouring corners too, so it stays a rectangle.
|
||||
/// - With a `shield` the document outside a valid quad is dimmed, under the grid.
|
||||
/// - A quad that is not convex or is wound differently from its box draws in the invalid colour without the grid and
|
||||
/// cannot be committed.
|
||||
/// - The corners stay where the user puts them: a quad wound counter-clockwise (a box flipped on one axis) applies
|
||||
/// mirrored, a box turned half way round applies upside down.
|
||||
/// - A quad that is not convex draws in the invalid colour without the grid and cannot be committed.
|
||||
/// - The quad and its corners are one Tab stop. Page Down and Page Up pick the whole quad or a corner; the arrows move
|
||||
/// the picked corner (1 document unit, Shift 10), or the whole quad when it is picked. Enter applies a valid quad.
|
||||
/// Esc cancels a drag, else discards.
|
||||
@@ -155,7 +156,7 @@ impl<'a> PerspectiveCropQuad<'a> {
|
||||
pub struct PerspectiveCropOutput {
|
||||
/// A drag (`Begin` on the first move, `Commit` on release; Esc cancels it) or an arrow key's `Set`.
|
||||
pub edit: EditResponse<PerspectiveQuad>,
|
||||
/// Enter while the quad is convex and wound like its box.
|
||||
/// Enter while the quad is convex ([`is_valid`]).
|
||||
pub apply: bool,
|
||||
/// Esc with no drag.
|
||||
pub discard: bool,
|
||||
@@ -201,7 +202,8 @@ fn upright(q: &[Point; 4]) -> bool {
|
||||
///
|
||||
/// - Shift keeps the press position's coordinate of the corner on the axis the drag moved less along.
|
||||
/// - Alt on a corner of an upright quad moves the two neighbouring corners one coordinate each, so it stays a
|
||||
/// rectangle, put in order when the corner passes the opposite one. A skewed quad's corner moves alone.
|
||||
/// rectangle. The corners stay as they lie when the corner passes the opposite one, so the box flips. A skewed
|
||||
/// quad's corner moves alone.
|
||||
fn dragged(drag: &Drag, to: Point, mods: Mods) -> [Point; 4] {
|
||||
match drag.grab {
|
||||
Grab::Corner { index, offset } => {
|
||||
@@ -222,7 +224,6 @@ fn dragged(drag: &Drag, to: Point, mods: Mods) -> [Point; 4] {
|
||||
let (in_row, in_column) = if index % 2 == 0 { (next, prev) } else { (prev, next) };
|
||||
quad[in_row].y = target.y;
|
||||
quad[in_column].x = target.x;
|
||||
return ordered(quad);
|
||||
}
|
||||
quad
|
||||
}
|
||||
@@ -433,10 +434,15 @@ pub fn ordered(points: [Point; 4]) -> [Point; 4] {
|
||||
v
|
||||
}
|
||||
|
||||
/// Whether `q` (top left, top right, bottom right, bottom left) is a usable perspective crop quad: finite, strictly
|
||||
/// convex with every corner angle above about 0.06 degrees, wound clockwise on screen (y down) and at least one unit^2
|
||||
/// in area.
|
||||
/// Whether `q` (top left, top right, bottom right, bottom left of the crop it maps onto) is a usable perspective crop
|
||||
/// quad: finite, strictly convex in either winding, every corner angle above about 0.06 degrees and at least one
|
||||
/// unit^2 in area. A quad wound counter-clockwise on screen (a box flipped on one axis) applies mirrored.
|
||||
pub fn is_valid(q: &[Point; 4]) -> bool {
|
||||
wound_clockwise(q) || wound_clockwise(&[q[0], q[3], q[2], q[1]])
|
||||
}
|
||||
|
||||
/// [`is_valid`] for a quad wound clockwise on screen (y down).
|
||||
fn wound_clockwise(q: &[Point; 4]) -> bool {
|
||||
if !finite(q) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -130,11 +130,6 @@ fn boxed(anchor: Point, to: Point) -> PerspectiveQuad {
|
||||
(x1 > x0 && y1 > y0).then_some([Point::new(x0, y0), Point::new(x1, y0), Point::new(x1, y1), Point::new(x0, y1)])
|
||||
}
|
||||
|
||||
/// Whether `q` is convex whichever way it is wound: the preview of corners clicked in any order.
|
||||
fn convex(q: &[Point; 4]) -> bool {
|
||||
is_valid(q) || is_valid(&[q[0], q[3], q[2], q[1]])
|
||||
}
|
||||
|
||||
/// What the host hands the perspective crop pass each frame.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
#[non_exhaustive]
|
||||
@@ -271,7 +266,7 @@ impl CanvasTool for PerspectiveCropPass<'_> {
|
||||
_ => None,
|
||||
};
|
||||
if let Some(quad) = preview {
|
||||
let ok = clicks.len() == 2 || convex(&quad);
|
||||
let ok = clicks.len() == 2 || is_valid(&quad);
|
||||
if ok && self.input.show_grid {
|
||||
let grid = grid_color(style);
|
||||
for [a, b] in grid_lines(view, &quad, grid_step(style)) {
|
||||
|
||||
@@ -12,7 +12,7 @@ use craft_ui::canvas::{Canvas, ToolHost};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::access::Role;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Event, Modifiers, PointerButton, Pos2, Vec2};
|
||||
use egui::{Event, Key, Modifiers, PointerButton, Pos2, Vec2};
|
||||
|
||||
const SIZE: Size = Size::new(400.0, 300.0);
|
||||
const SHIFT: Modifiers = Modifiers { shift: true, ..Modifiers::NONE };
|
||||
@@ -25,13 +25,16 @@ struct Rig {
|
||||
tool: Rc<RefCell<PerspectiveCropTool>>,
|
||||
/// The active document the pass is given; `None` keys nothing.
|
||||
document: Rc<Cell<Option<CropDocKey>>>,
|
||||
/// Whether the quad reported `apply` in any frame since the last [`Rig::applied`].
|
||||
apply: Rc<Cell<bool>>,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
fn new() -> Self {
|
||||
let tool = Rc::new(RefCell::new(PerspectiveCropTool::new()));
|
||||
let document = Rc::new(Cell::new(None));
|
||||
let (shown, active) = (Rc::clone(&tool), Rc::clone(&document));
|
||||
let apply = Rc::new(Cell::new(false));
|
||||
let (shown, active, applied) = (Rc::clone(&tool), Rc::clone(&document), Rc::clone(&apply));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut tool = shown.borrow_mut();
|
||||
let room = ui.available_rect_before_wrap();
|
||||
@@ -39,12 +42,13 @@ impl Rig {
|
||||
let input = PerspectiveInput { document: active.get(), ..Default::default() };
|
||||
let mut pass = tool.pass(input);
|
||||
let canvas = Canvas::new("Document").document(SIZE).view(view).show(ui, |ui, body| {
|
||||
let _ = pass.show_quad(ui, body.rect);
|
||||
let output = pass.show_quad(ui, body.rect);
|
||||
applied.set(applied.get() || output.apply);
|
||||
});
|
||||
let _ = ToolHost::new().run(ui, &canvas.output, &mut pass);
|
||||
});
|
||||
fixture.settle();
|
||||
Rig { fixture, tool, document }
|
||||
Rig { fixture, tool, document, apply }
|
||||
}
|
||||
|
||||
/// The screen point of document point `(x, y)`: the view shows (200, 150) at the canvas's centre.
|
||||
@@ -110,6 +114,15 @@ impl Rig {
|
||||
fn placing(&self) -> usize {
|
||||
self.tool.borrow().placing()
|
||||
}
|
||||
|
||||
/// Presses Enter and whether the quad applied.
|
||||
fn enter_applies(&mut self) -> bool {
|
||||
self.fixture.frame(Vec::new());
|
||||
self.apply.set(false);
|
||||
self.fixture.key(Key::Enter, Modifiers::NONE);
|
||||
self.fixture.frame(Vec::new());
|
||||
self.apply.take()
|
||||
}
|
||||
}
|
||||
|
||||
fn near(got: Option<[Point; 4]>, want: [(f64, f64); 4]) -> bool {
|
||||
@@ -197,8 +210,29 @@ fn perspective_box_and_corner_modifiers_and_clicks() {
|
||||
rig.drag((300.0, 220.0), (320.0, 230.0), ALT);
|
||||
assert!(near(rig.quad(), [(120.0, 80.0), (300.0, 80.0), (320.0, 230.0), (100.0, 220.0)]), "a skewed quad's corner moves alone: {:?}", rig.quad());
|
||||
rig.set(upright);
|
||||
rig.drag((300.0, 220.0), (50.0, 220.0), ALT);
|
||||
assert!(
|
||||
near(rig.quad(), [(100.0, 80.0), (50.0, 80.0), (50.0, 220.0), (100.0, 220.0)]),
|
||||
"Alt past the opposite corner along x keeps the corners as they lie, a mirrored box: {:?}",
|
||||
rig.quad()
|
||||
);
|
||||
assert!(rig.quad().is_some_and(|q| is_valid(&q)), "a mirrored box is valid");
|
||||
assert!(rig.enter_applies(), "Enter applies a mirrored box");
|
||||
rig.set(upright);
|
||||
rig.drag((300.0, 220.0), (50.0, 40.0), ALT);
|
||||
assert!(rig.quad().is_some_and(|q| is_valid(&q)), "Alt past the opposite corner stays a valid quad: {:?}", rig.quad());
|
||||
assert!(
|
||||
near(rig.quad(), [(100.0, 80.0), (50.0, 80.0), (50.0, 40.0), (100.0, 40.0)]),
|
||||
"Alt past the opposite corner on both axes keeps the corners as they lie, a box turned upside down: {:?}",
|
||||
rig.quad()
|
||||
);
|
||||
assert!(rig.enter_applies(), "Enter applies an upside-down box");
|
||||
// A bow-tie and a quad with three corners on a line are invalid in either winding and do not apply.
|
||||
for quad in [[(100.0, 80.0), (300.0, 220.0), (300.0, 80.0), (100.0, 220.0)], [(100.0, 80.0), (200.0, 80.0), (300.0, 80.0), (200.0, 220.0)]] {
|
||||
let points = quad.map(|(x, y)| Point::new(x, y));
|
||||
assert!(!is_valid(&points) && !is_valid(&[points[0], points[3], points[2], points[1]]), "{quad:?} is invalid");
|
||||
rig.set(quad);
|
||||
assert!(!rig.enter_applies(), "Enter does not apply {quad:?}");
|
||||
}
|
||||
|
||||
// A click on a corner with no drag changes nothing.
|
||||
rig.set(upright);
|
||||
|
||||
Reference in New Issue
Block a user