mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Crop: a turned Shift-corner drag snaps by sliding along its diagonal
On a turned frame a corner with a ratio (a Shift corner's diagonal or an options-bar preset) can only slide along its turned diagonal. Handle snapping moved the handle sideways onto the guide in canvas axes, and the ratio then put it back off the line: a Shift corner stopped short of the guide and lost its snap line, and a preset corner grew away from it. crop_handle now slides the corner along the diagonal onto the nearest line it reached. Upright frames keep the existing ratio solve. Tests: a turned frame snapping to a guide with Shift and with an 8:5 preset, in Classic Mode and the default mode; Shift on the top-right and bottom-left corners, which use the other diagonal.
This commit is contained in:
1 parent
6c0f4d50b5
commit
e2252cf3bb
3 files changed
+78
-8
No files matched your search
@@ -420,17 +420,33 @@ fn crop_handle(app: &PhotocraftApp, p: [f64; 2], mods: egui::Modifiers, targets:
|
||||
let original = at(rect);
|
||||
let travel = crate::crop_ui::turn([handle[0] - original[0], handle[1] - original[1]], [0.0, 0.0], -deg);
|
||||
let mut local = [(travel[0] + d[0]) / scale, (travel[1] + d[1]) / scale];
|
||||
if deg == 0.0
|
||||
&& hx != 0
|
||||
if hx != 0
|
||||
&& hy != 0
|
||||
&& let Some(k) = ratio
|
||||
{
|
||||
// A corner with a ratio has one degree of freedom. Put the other axis on the same
|
||||
// ratio, rather than letting the raw pointer's unused axis override the snapped edge.
|
||||
if lines.iter().any(|line| line.vertical) {
|
||||
local[1] = f64::from(hy) * ((rect[2] - rect[0] + grow * f64::from(hx) * local[0]) / k - (rect[3] - rect[1])) / grow;
|
||||
if deg == 0.0 {
|
||||
// A corner with a ratio has one degree of freedom. Put the other axis on the same
|
||||
// ratio, rather than letting the raw pointer's unused axis override the snapped edge.
|
||||
if lines.iter().any(|line| line.vertical) {
|
||||
local[1] = f64::from(hy) * ((rect[2] - rect[0] + grow * f64::from(hx) * local[0]) / k - (rect[3] - rect[1])) / grow;
|
||||
} else {
|
||||
local[0] = f64::from(hx) * ((rect[3] - rect[1] + grow * f64::from(hy) * local[1]) * k - (rect[2] - rect[0])) / grow;
|
||||
}
|
||||
} else {
|
||||
local[0] = f64::from(hx) * ((rect[3] - rect[1] + grow * f64::from(hy) * local[1]) * k - (rect[2] - rect[0])) / grow;
|
||||
// A turned corner with a ratio slides along its turned diagonal, which crosses the
|
||||
// guides at an angle: slide it onto the nearest line it reached. Moved sideways onto
|
||||
// the line instead, the ratio (a ⇧ corner's diagonal) put it back off the line.
|
||||
let along = [f64::from(hx) * k, f64::from(hy)];
|
||||
let v = crate::crop_ui::turn(along, [0.0, 0.0], deg);
|
||||
let slide = lines
|
||||
.iter()
|
||||
.filter_map(|line| {
|
||||
let (gap, step) = if line.vertical { (line.pos - handle[0], v[0]) } else { (line.pos - handle[1], v[1]) };
|
||||
(step.abs() > 1e-9).then(|| gap / step)
|
||||
})
|
||||
.min_by(|a, b| a.abs().total_cmp(&b.abs()));
|
||||
let Some(s) = slide else { return Some((p, Vec::new())) };
|
||||
local = [(travel[0] + s * along[0]) / scale, (travel[1] + s * along[1]) / scale];
|
||||
}
|
||||
}
|
||||
let d = crate::crop_ui::turn(local, [0.0, 0.0], deg);
|
||||
|
||||
@@ -406,6 +406,26 @@ fn handles_resize_with_shift_and_alt() {
|
||||
assert_eq!(app.ui.crop_rect, Some([40.0, 10.0, 100.0, 70.0]));
|
||||
}
|
||||
|
||||
/// The top-right and bottom-left corners follow the other diagonal, (120, −50) and (−120, 50):
|
||||
/// the drag projected onto it grows the frame by 2200 / 16900, as on the top-left corner. Along
|
||||
/// the top-left / bottom-right diagonal the top-right x1 would move to about 141.4.
|
||||
#[test]
|
||||
fn shift_resize_follows_the_diagonal_of_the_top_right_and_bottom_left_corners() {
|
||||
let mut app = app(SampleType::U8);
|
||||
app.ui.crop_rect = Some([20.0, 20.0, 140.0, 70.0]);
|
||||
drag(&mut app, &[[140.0, 20.0], [150.0, 0.0]], SHIFT);
|
||||
let r = app.ui.crop_rect.unwrap();
|
||||
assert_eq!((r[0], r[3]), (20.0, 70.0), "top-right: opposite corner fixed");
|
||||
assert!((r[2] - (140.0 + 2640.0 / 169.0)).abs() < 1e-9, "top-right: {r:?}");
|
||||
assert!((r[1] - (20.0 - 1100.0 / 169.0)).abs() < 1e-9, "top-right: {r:?}");
|
||||
app.ui.crop_rect = Some([20.0, 20.0, 140.0, 70.0]);
|
||||
drag(&mut app, &[[20.0, 70.0], [10.0, 90.0]], SHIFT);
|
||||
let r = app.ui.crop_rect.unwrap();
|
||||
assert_eq!((r[2], r[1]), (140.0, 20.0), "bottom-left: opposite corner fixed");
|
||||
assert!((r[0] - (20.0 - 2640.0 / 169.0)).abs() < 1e-9, "bottom-left: {r:?}");
|
||||
assert!((r[3] - (70.0 + 1100.0 / 169.0)).abs() < 1e-9, "bottom-left: {r:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_a_handle_past_the_other_side_flips_and_collapse_reverts() {
|
||||
let mut app = app(SampleType::U8);
|
||||
|
||||
@@ -73,6 +73,12 @@ fn commit(app: &mut PhotocraftApp) -> (u32, u32) {
|
||||
/// Drags the bottom-right corner by document vector `d` in steps through `ui.pointer`, holding
|
||||
/// `mods`, and commits.
|
||||
fn drag_corner_and_commit(app: &mut PhotocraftApp, d: [f64; 2], mods: Value) -> (u32, u32) {
|
||||
drag_corner(app, d, mods);
|
||||
commit(app)
|
||||
}
|
||||
|
||||
/// [`drag_corner_and_commit`] without the commit: the frame stays pending.
|
||||
fn drag_corner(app: &mut PhotocraftApp, d: [f64; 2], mods: Value) {
|
||||
let [x, y] = corner(app);
|
||||
let mut events = vec![json!({"kind": "down", "x": x, "y": y})];
|
||||
events.extend((1..=4).map(|i| {
|
||||
@@ -83,7 +89,6 @@ fn drag_corner_and_commit(app: &mut PhotocraftApp, d: [f64; 2], mods: Value) ->
|
||||
let mut params = mods;
|
||||
params["events"] = Value::Array(events);
|
||||
pointer(app, params);
|
||||
commit(app)
|
||||
}
|
||||
|
||||
fn assert_near(got: (u32, u32), want: (u32, u32), what: &str) {
|
||||
@@ -178,3 +183,32 @@ fn a_shift_corner_drag_snaps_where_its_handle_is() {
|
||||
assert_near(got, want, &format!("⇧ drag with a guide at x = {guide}"));
|
||||
}
|
||||
}
|
||||
|
||||
/// On a frame turned 30° the corner slides along the turned diagonal, direction (0.89, 1.67) for
|
||||
/// 8 : 5, so a vertical guide is met by sliding along it, not by moving the handle sideways.
|
||||
///
|
||||
/// - ⇧, free ratio: the measured drag leaves the handle at x ≈ 569.2, 4.2 px from a guide at
|
||||
/// x = 565. Sliding on to it shortens the frame by 12.7 % to 698.1 × 436.3. Moving the handle
|
||||
/// sideways and projecting that back onto the diagonal stopped it at x ≈ 568.3, off the guide,
|
||||
/// at 704 × 440.
|
||||
/// - An 8 : 5 preset (the larger extent wins, 720 × 450) leaves it at x ≈ 577.1, 4.1 px from a
|
||||
/// guide at x = 573: 712.5 × 445.3. Sideways, the larger extent grew the frame to 723 × 452,
|
||||
/// away from the guide.
|
||||
#[test]
|
||||
fn a_shift_corner_drag_on_a_turned_frame_snaps_along_its_diagonal() {
|
||||
for (ratio, guide, want) in [("", 565.0, (698, 436)), ("8:5", 573.0, (712, 445))] {
|
||||
for classic in [true, false] {
|
||||
let what = format!("ratio {ratio:?}, guide at x = {guide}, Classic Mode {classic}");
|
||||
let mut app = crop_app(Setup { classic, ratio, snap: true, ..Setup::default() });
|
||||
app.ui.crop_angle = 30.0;
|
||||
photocraft_ui_egui::crop_mode::sync_frame(&mut app);
|
||||
app.run("view.newGuide", json!({"orientation": "vertical", "position": guide})).expect("guide");
|
||||
drag_corner(&mut app, crop_ui::turn(DRAG, [0.0, 0.0], 30.0), json!({"shift": true}));
|
||||
let at = corner(&app);
|
||||
assert!((at[0] - guide).abs() < 1e-3, "{what}: the handle is at {at:?}, not on the guide");
|
||||
let r = app.ui.crop_rect.expect("a frame");
|
||||
assert!(((r[2] - r[0]) / (r[3] - r[1]) - 1.6).abs() < 1e-9, "{what}: 8 : 5 kept, {r:?}");
|
||||
assert_near(commit(&mut app), want, &what);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user