mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #13248 from RickiNano/fix/ellipse-shift-shorter-side-13222
Shape tools: Shift draws the square or circle from the shorter side of the drag (#13222)
This commit is contained in:
5 files changed
+86
-6
No files matched your search
@@ -109,7 +109,7 @@ fn shift_and_alt_constrain_and_centre_the_star_like_the_polygon_tool() {
|
||||
app.ui.tool = Tool::Star;
|
||||
drag(&mut app, [100.0, 100.0], [140.0, 120.0], Modifiers::SHIFT | Modifiers::ALT);
|
||||
match live_shape(&app) {
|
||||
Some(LiveShape::Polygon { rect, .. }) => assert_eq!(rect, [60.0, 60.0, 80.0, 80.0], "a square box drawn from the centre"),
|
||||
Some(LiveShape::Polygon { rect, .. }) => assert_eq!(rect, [80.0, 80.0, 40.0, 40.0], "a square box of the shorter side drawn from the centre (#13222)"),
|
||||
other => panic!("a live star shape, got {other:?}"),
|
||||
}
|
||||
}
|
||||
@@ -238,6 +238,9 @@ mod pen_enter_deselects_tests;
|
||||
#[cfg(test)]
|
||||
mod line_shift_snap_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod ellipse_shift_circle_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod pen_mode_menu_tests;
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
//! #13222: ⇧ while dragging with the Ellipse tool makes a circle from the shorter side of the
|
||||
//! drag, as in the reference application, not the longer one; ⇧⌥ centres that circle on the
|
||||
//! press. The drag preview (`shape_bounds`) shows the same box as the layer the drag makes.
|
||||
|
||||
use egui::Modifiers;
|
||||
use photocraft_doc::{LayerContent, LiveShape};
|
||||
use serde_json::json;
|
||||
|
||||
use super::shape_bounds;
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::{ToolEvent, tool_event};
|
||||
use crate::state::Tool;
|
||||
|
||||
fn app() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 800, "height": 600})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.tool = Tool::EllipseShape;
|
||||
app
|
||||
}
|
||||
|
||||
/// A drag pressed without modifiers, with `mods` pressed after the start (so the press picks no
|
||||
/// path operation or eyedropper).
|
||||
fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) {
|
||||
tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(app, ToolEvent::Move { x: (from[0] + to[0]) / 2.0, y: (from[1] + to[1]) / 2.0, pressure: 1.0 }, mods);
|
||||
tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, mods);
|
||||
tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, mods);
|
||||
}
|
||||
|
||||
/// The active layer's live ellipse `[x, y, w, h]`.
|
||||
fn ellipse_rect(app: &PhotocraftApp) -> [f64; 4] {
|
||||
let st = app.session.active().unwrap();
|
||||
match &st.doc.layer(st.active_layer.unwrap()).unwrap().content {
|
||||
LayerContent::Shape(s) => match &s.live {
|
||||
Some(LiveShape::Ellipse { rect }) => *rect,
|
||||
other => panic!("a live ellipse, got {other:?}"),
|
||||
},
|
||||
_ => panic!("a shape layer"),
|
||||
}
|
||||
}
|
||||
|
||||
fn preview(app: &PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers) -> [f64; 4] {
|
||||
shape_bounds(&app.ui.tool_options, Tool::EllipseShape, from, to, mods).expect("a box")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_makes_a_circle_from_the_shorter_side() {
|
||||
let mut app = app();
|
||||
drag(&mut app, [400.0, 300.0], [600.0, 400.0], Modifiers::SHIFT);
|
||||
assert_eq!(ellipse_rect(&app), [400.0, 300.0, 100.0, 100.0]);
|
||||
assert_eq!(preview(&app, [400.0, 300.0], [600.0, 400.0], Modifiers::SHIFT), [400.0, 300.0, 500.0, 400.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_uses_the_shorter_side_when_the_drag_is_taller_than_wide() {
|
||||
let mut app = app();
|
||||
drag(&mut app, [400.0, 300.0], [450.0, 500.0], Modifiers::SHIFT);
|
||||
assert_eq!(ellipse_rect(&app), [400.0, 300.0, 50.0, 50.0]);
|
||||
assert_eq!(preview(&app, [400.0, 300.0], [450.0, 500.0], Modifiers::SHIFT), [400.0, 300.0, 450.0, 350.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_alt_centres_the_shorter_side_circle_on_the_press() {
|
||||
let mut app = app();
|
||||
let mods = Modifiers::SHIFT | Modifiers::ALT;
|
||||
drag(&mut app, [400.0, 300.0], [600.0, 400.0], mods);
|
||||
assert_eq!(ellipse_rect(&app), [300.0, 200.0, 200.0, 200.0]);
|
||||
assert_eq!(preview(&app, [400.0, 300.0], [600.0, 400.0], mods), [300.0, 200.0, 500.0, 400.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_keeps_the_drag_direction_up_and_left() {
|
||||
let app = app();
|
||||
assert_eq!(preview(&app, [400.0, 300.0], [200.0, 200.0], Modifiers::SHIFT), [300.0, 200.0, 400.0, 300.0]);
|
||||
}
|
||||
@@ -116,7 +116,8 @@ pub(super) fn shape_geometry(o: &ToolOptions, tool: Tool, start: [f64; 2], end:
|
||||
} else {
|
||||
let (mut w, mut h) = (end[0] - start[0], end[1] - start[1]);
|
||||
if mods.shift {
|
||||
let m = w.abs().max(h.abs());
|
||||
// The drag's shorter side sets the square, as in the reference application (#13222).
|
||||
let m = w.abs().min(h.abs());
|
||||
(w, h) = (m * w.signum(), m * h.signum());
|
||||
}
|
||||
let (x0, y0) = if mods.alt { (start[0] - w, start[1] - h) } else { (start[0], start[1]) };
|
||||
|
||||
@@ -491,7 +491,7 @@ fn shape_drag_creates_layers_with_modifiers() {
|
||||
finish_shape(&mut app, Tool::Rectangle, [10.0, 10.0], [60.0, 30.0], egui::Modifiers::SHIFT);
|
||||
let st = app.session.active().unwrap();
|
||||
let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds();
|
||||
assert_eq!((b.width(), b.height()), (50, 50), "shift = square");
|
||||
assert_eq!((b.width(), b.height()), (20, 20), "shift = square of the shorter side (#13222)");
|
||||
finish_shape(&mut app, Tool::EllipseShape, [100.0, 100.0], [120.0, 110.0], egui::Modifiers::ALT);
|
||||
let st = app.session.active().unwrap();
|
||||
let b = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds();
|
||||
@@ -694,9 +694,9 @@ fn shift_pressed_during_a_shape_drag_constrains_it() {
|
||||
let l = st.doc.layer(st.active_layer.unwrap()).unwrap();
|
||||
assert!(matches!(l.content, LayerContent::Shape(_)), "{tool:?} ({sides} sides): a shape layer");
|
||||
let b = l.surface().unwrap().content_bounds();
|
||||
// The drag is 60 × 30; constrained, the shape's box is 60 × 60 (a hexagon is narrower
|
||||
// than its box, so the height tells).
|
||||
assert!(b.height().abs_diff(60) <= 1 && b.width() <= 61, "{tool:?}: ⇧ mid-drag keeps proportions: {b:?}");
|
||||
// The drag is 60 × 30; constrained, the shape's box is 30 × 30, the shorter side (#13222)
|
||||
// (a hexagon is narrower than its box, so the height tells).
|
||||
assert!(b.height().abs_diff(30) <= 1 && b.width() <= 31, "{tool:?}: ⇧ mid-drag keeps proportions: {b:?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user