From ee67c7761fd56cbb517015b8f755cfc3bcb8a54f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 14 Nov 2026 12:51:06 +0100 Subject: [PATCH] Support gravity pivots when resizing crop content --- crates/midra-layout/src/gui.rs | 14 ++++-- crates/midra-layout/src/lib.rs | 80 +++++++++++++++++++++++++----- crates/midra-layout/tests/pivot.rs | 69 ++++++++++++++++++++++++++ docs/midra-layout.md | 2 +- docs/midralayout/changelog.md | 2 + 5 files changed, 149 insertions(+), 18 deletions(-) diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index 780d1a0..71b9b77 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -2096,7 +2096,7 @@ impl Editor { ); let pivot_rect = Rect::from_min_size(ink.point(tools_x + 76.0, tools_y), Vec2::splat(32.0)); if edit_tool_icon(ui, pivot_rect, None, "Pivot", self.pivot_tool, self.effective_pivot(modifiers)) - .on_hover_text("Pivot resize — click to toggle. Hold Ctrl for the same effect; if selected, Ctrl temporarily disables it. Combines with either grid. Drag the pivot: snaps to 9 layer anchors and enabled grids/edges; Alt bypasses anchors. Double-click to center. Applies to layer handles.") + .on_hover_text("Pivot resize — click to toggle. Hold Ctrl for the same effect; if selected, Ctrl temporarily disables it. Combines with either grid. Drag the pivot: snaps to 9 layer anchors and enabled grids/edges; Alt bypasses anchors. Double-click to center. Applies to layer and crop/zoom handles.") .clicked() { self.pivot_tool = !self.pivot_tool; @@ -2450,8 +2450,7 @@ impl Editor { ResizeHandle::Top | ResizeHandle::Bottom => egui::CursorIcon::ResizeVertical, }); } - let pivot_visible = - edit_pointer && self.effective_pivot(ui.input(|i| i.modifiers)) && !self.edit_content; + let pivot_visible = edit_pointer && self.effective_pivot(ui.input(|i| i.modifiers)); let pivot_hover = if pivot_visible { pointer.and_then(|p| self.pivot_hit(p, origin, scale)) } else { @@ -5023,12 +5022,13 @@ mod tests { #[test] fn crop_handle_gesture_zooms_content_without_resizing_layer() { - for (zoom, handle_index) in [0.6, 1.0] + for (zoom, handle_index, pivot_tool) in [0.6, 1.0] .into_iter() - .flat_map(|zoom| (0..8).map(move |i| (zoom, i))) + .flat_map(|zoom| (0..8).flat_map(move |i| [false, true].map(|pivot| (zoom, i, pivot)))) { let mut app = Editor { edit_content: true, + pivot_tool, keep_aspect_ratio: false, zoom, ..Default::default() @@ -5059,6 +5059,10 @@ mod tests { .zoom > 1.0 ); + if pivot_tool { + let mapping = app.layout.formats().mapping(1, before[1]).unwrap(); + assert!(mapping.framing.offset.iter().all(|v| v.abs() < 1e-5)); + } app.layout.undo(); assert_eq!(*app.layout.formats(), formats); } diff --git a/crates/midra-layout/src/lib.rs b/crates/midra-layout/src/lib.rs index c86cbf8..ebb3071 100644 --- a/crates/midra-layout/src/lib.rs +++ b/crates/midra-layout/src/lib.rs @@ -825,7 +825,7 @@ impl Layout { self.content_drag = Some((i, self.snapshot(), point)); } } - /// Zoom content from a corner or side, fixing the opposite anchor and visible layer box. + /// Zoom content from a corner or side, fixing the opposite anchor (or enabled pivot) and layer box. pub fn start_content_resize(&mut self, index: usize, handle: ResizeHandle, point: [f64; 2]) { self.finish_drag(); if !self.selectable(index) @@ -1422,14 +1422,14 @@ impl Layout { /// Set a gesture's pivot relative to the initiating layer; partners mirror it by resize mode. pub fn set_resize_pivot(&mut self, relative: [f64; 2]) { if relative.iter().all(|v| v.is_finite() && v.abs() <= 1e6) - && let Some(resize) = &mut self.resize + && let Some(resize) = self.resize.as_mut().or(self.content_resize.as_mut()) { resize.pivot = Some(relative); } } /// Toggle pivot anchoring during a gesture, always recomputing from its initial geometry. pub fn use_resize_pivot(&mut self, active: bool) { - if let Some(resize) = &mut self.resize { + if let Some(resize) = self.resize.as_mut().or(self.content_resize.as_mut()) { resize.use_pivot = active; } } @@ -1453,15 +1453,71 @@ impl Layout { width: input[0] * m.scale, height: input[1] * m.scale, }; - let desired = resized_geometry( - full, - r.handle, - [point[0] - r.start[0], point[1] - r.start[1]], - true, - ); - let zoom = (m.framing.zoom * desired.width / full.width).clamp(1.0, 100.0); - let (scale, limits) = formats::limits(input, [g.width, g.height], zoom)?; - let resized = anchored_resize(full, r.handle, input[0] * scale, input[1] * scale); + let (zoom, resized) = if r.use_pivot { + let pivot = r.anchor(i); + let handle = ResizeHandle::positions(full) + .into_iter() + .find(|(h, _)| *h == r.handle) + .unwrap() + .1; + let axes = [ + !matches!(r.handle, ResizeHandle::Top | ResizeHandle::Bottom), + !matches!(r.handle, ResizeHandle::Left | ResizeHandle::Right), + ]; + let vector = [handle[0] - pivot[0], handle[1] - pivot[1]]; + let denominator: f64 = + (0..2).filter(|&a| axes[a]).map(|a| vector[a].powi(2)).sum(); + let desired = if denominator > 1e-18 { + 1.0 + (0..2) + .filter(|&a| axes[a]) + .map(|a| (point[a] - r.start[a]) * vector[a]) + .sum::() + / denominator + } else { + 1.0 + }; + let mut lower = 1.0 / m.framing.zoom; + let mut upper = 100.0 / m.framing.zoom; + // Bound scaling about the pivot so content still covers the box. + // Clamping offsets instead would move the source pixel under the pivot. + for (a, b) in [ + (full.x - pivot[0], g.x - pivot[0]), + (pivot[0] - full.x - full.width, pivot[0] - g.x - g.width), + (full.y - pivot[1], g.y - pivot[1]), + (pivot[1] - full.y - full.height, pivot[1] - g.y - g.height), + ] { + if a > 1e-9 { + upper = upper.min(b / a); + } else if a < -1e-9 { + lower = lower.max(b / a); + } + } + // The starting crop is valid; include it despite boundary roundoff. + let factor = desired.clamp(lower.min(1.0), upper.max(1.0)); + ( + (m.framing.zoom * factor).clamp(1.0, 100.0), + Geometry { + x: pivot[0] + (full.x - pivot[0]) * factor, + y: pivot[1] + (full.y - pivot[1]) * factor, + width: full.width * factor, + height: full.height * factor, + }, + ) + } else { + let desired = resized_geometry( + full, + r.handle, + [point[0] - r.start[0], point[1] - r.start[1]], + true, + ); + let zoom = (m.framing.zoom * desired.width / full.width).clamp(1.0, 100.0); + let (scale, _) = formats::limits(input, [g.width, g.height], zoom)?; + ( + zoom, + anchored_resize(full, r.handle, input[0] * scale, input[1] * scale), + ) + }; + let (_, limits) = formats::limits(input, [g.width, g.height], zoom)?; let center = resized.center(); let layer_center = g.center(); let offset = std::array::from_fn(|axis| { diff --git a/crates/midra-layout/tests/pivot.rs b/crates/midra-layout/tests/pivot.rs index ebe1597..30e583f 100644 --- a/crates/midra-layout/tests/pivot.rs +++ b/crates/midra-layout/tests/pivot.rs @@ -338,3 +338,72 @@ fn grouped_resize_keeps_each_stored_pivot_fixed() { } } } + +#[test] +fn crop_zoom_keeps_source_pixel_under_saved_pivot_fixed_and_never_exposes_empty_space() { + use midra_layout::formats::Framing; + for relative in [[0.5, 0.5], [0.2, 0.7], [-0.25, 1.25], [0.0, 0.0]] { + let mut layout = Layout::default(); + let i = 1; + let g = layout.layers()[i]; + layout.set_layer_pivots(&[(i, relative)]).unwrap(); + layout + .set_framing( + i, + Framing { + zoom: 2.0, + offset: [20.0, -10.0], + }, + ) + .unwrap(); + let before = layout.formats().clone(); + let mapping = before.mapping(i, g).unwrap(); + let crop = before.advanced[i]; + let input = before.input(i).unwrap(); + let full = Geometry { + x: g.x - crop.left * mapping.scale, + y: g.y - crop.top * mapping.scale, + width: input[0] * mapping.scale, + height: input[1] * mapping.scale, + }; + let fixed = pivot(g, relative); + let source = [ + (fixed[0] - full.x) / mapping.scale, + (fixed[1] - full.y) / mapping.scale, + ]; + for (handle, start) in ResizeHandle::positions(full) { + for factor in [0.001, 0.8, 1.2, 1000.0] { + layout.start_content_resize(i, handle, start); + layout.use_resize_pivot(true); + let end = std::array::from_fn(|a| fixed[a] + (start[a] - fixed[a]) * factor); + layout.drag_to(end); + let m = layout.formats().mapping(i, g).unwrap(); + let c = layout.formats().advanced[i]; + near(g.x - c.left * m.scale + source[0] * m.scale, fixed[0]); + near(g.y - c.top * m.scale + source[1] * m.scale, fixed[1]); + assert!([c.left, c.right, c.top, c.bottom].iter().all(|v| *v >= 0.0)); + assert_eq!(layout.layers()[i], g); + let resized = layout.formats().clone(); + // Modifier changes recompute from the gesture baseline without drift. + layout.use_resize_pivot(false); + layout.drag_to(end); + layout.use_resize_pivot(true); + layout.drag_to(end); + assert_eq!(layout.formats(), &resized); + layout.finish_drag(); + if resized != before { + layout.undo(); + assert_eq!(layout.formats(), &before); + layout.redo(); + assert_eq!(layout.formats(), &resized); + } + layout.start_content_resize(i, handle, start); + layout.use_resize_pivot(true); + layout.drag_to(end); + layout.cancel_drag(); + assert_eq!(layout.formats(), &resized); + layout.set_framing(i, mapping.framing).unwrap(); + } + } + } +} diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 40a3a4b..2fcb3e6 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -392,7 +392,7 @@ while resizing; unequal sizes retain proportional scaling. Aspect-ratio lock, snapping (including edge contact), Alt inversion and Undo still apply. A handle on the pivot's axis cannot resize that axis; move the pivot or use another handle. Layers do not flip when dragged through the pivot; minimum sizes still apply. -**Ctrl+Shift remains the 100 px grid** (or temporarily disables a selected 100 px grid); it uses pivot anchoring if Pivot is selected. Pivot positions are saved per layer in the project, survive selection changes and reopening, and follow layers when moved. Placement and reset support Undo/Redo. They do not affect numeric sizing or Adjust layer crop mode. +**Ctrl+Shift remains the 100 px grid** (or temporarily disables a selected 100 px grid); it uses pivot anchoring if Pivot is selected. Pivot positions are saved per layer in the project, survive selection changes and reopening, and follow layers when moved. Placement and reset support Undo/Redo. They do not affect numeric sizing. In **Adjust layer crop** mode, Ctrl/Pivot scales the image with its source pixel under the gravity point fixed; the visible layer box stays unchanged. Crop limits stop scaling before revealing empty space, including with outside pivots. ### Resizing with corner handles diff --git a/docs/midralayout/changelog.md b/docs/midralayout/changelog.md index 7f3b19c..c9cdef3 100644 --- a/docs/midralayout/changelog.md +++ b/docs/midralayout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- Ctrl/Pivot now works on crop/zoom handles, keeping the source pixel under the saved gravity point fixed; crop limits prevent empty borders. + - Group grid snapping tools and snapping options in a toolbar dropdown; the label shows the active grid. Pivot remains independently accessible. - Save gravity points per layer across selection changes and reopening; placement/reset supports Undo. Project v9 migrates older files to centered pivots.