mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #12139 from hectorviov/fix/transform-relative-position
Free Transform: relative positioning toggle between X and Y (#11679)
This commit is contained in:
6 files changed
+277
-7
No files matched your search
@@ -858,6 +858,9 @@ mod transform_type_tests;
|
||||
#[cfg(test)]
|
||||
mod transform_reference_point_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod transform_relative_position_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod transform_several_tests;
|
||||
|
||||
|
||||
@@ -168,6 +168,10 @@ pub struct ToolOptions {
|
||||
/// transforms still turn and scale about it (#11098).
|
||||
#[serde(default)]
|
||||
pub transform_reference_point: bool,
|
||||
/// Transform options bar: X and Y show the reference point's offset from where the box started
|
||||
/// instead of its position (Photoshop's relative positioning, off by default, #11679).
|
||||
#[serde(default)]
|
||||
pub transform_relative_position: bool,
|
||||
/// Last interpolation selected in the Transform options bar; unset follows General's default.
|
||||
#[serde(default)]
|
||||
pub transform_interpolation: Option<photocraft_engine::prefs::Interpolation>,
|
||||
@@ -478,6 +482,7 @@ impl Default for ToolOptions {
|
||||
move_expand_group_on_click: true,
|
||||
move_show_layers_hover_bounds: true,
|
||||
transform_reference_point: false,
|
||||
transform_relative_position: false,
|
||||
transform_interpolation: None,
|
||||
crop_ratio: String::new(),
|
||||
crop_custom_ratio_width: 0.0,
|
||||
|
||||
@@ -184,7 +184,8 @@ pub fn option_keys(tool: Tool) -> &'static [&'static str] {
|
||||
move_expand_group_on_click,
|
||||
move_show_layers_hover_bounds,
|
||||
transform_interpolation,
|
||||
transform_reference_point
|
||||
transform_reference_point,
|
||||
transform_relative_position
|
||||
),
|
||||
Tool::RectMarquee | Tool::EllipseMarquee => reset!(feather, anti_alias, marquee_style, marquee_width, marquee_height, marquee_unit),
|
||||
// The single-pixel marquees are never anti-aliased: Feather is their only option.
|
||||
|
||||
@@ -270,6 +270,87 @@ fn reference_point_toggle(ui: &mut egui::Ui, on: &mut bool) -> bool {
|
||||
*on
|
||||
}
|
||||
|
||||
/// Where the reference point was when the session opened: the point of the original box that the
|
||||
/// box's transform has carried to the reference point now. A box that only scales or turns about
|
||||
/// its reference point leaves it in place; a moved box carries it along.
|
||||
fn reference_point_origin(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2]) -> [f64; 2] {
|
||||
if let Some(h) = Homography::rect_to_quad(rect, quad).and_then(|h| h.inverse()) {
|
||||
let (x, y) = h.apply(pivot[0], pivot[1]);
|
||||
if x.is_finite() && y.is_finite() {
|
||||
return [x, y];
|
||||
}
|
||||
}
|
||||
// A flattened box has no inverse. Fit the map's linear part about the box's centre and use
|
||||
// its pseudo-inverse: the collapsed direction adds nothing, so a box squashed about its
|
||||
// reference point reads no offset and one squashed toward an edge reads how far it moved.
|
||||
let (w, h) = (rect[2] - rect[0], rect[3] - rect[1]);
|
||||
let centre = [(rect[0] + rect[2]) / 2.0, (rect[1] + rect[3]) / 2.0];
|
||||
let mid = [quad.iter().map(|q| q[0]).sum::<f64>() / 4.0, quad.iter().map(|q| q[1]).sum::<f64>() / 4.0];
|
||||
let d = [pivot[0] - mid[0], pivot[1] - mid[1]];
|
||||
if !(w.abs() > f64::EPSILON && h.abs() > f64::EPSILON) {
|
||||
return [centre[0] + d[0], centre[1] + d[1]];
|
||||
}
|
||||
// Columns: where a unit step along the box's width and height goes.
|
||||
let ex = [(quad[1][0] - quad[0][0] + quad[2][0] - quad[3][0]) / (2.0 * w), (quad[1][1] - quad[0][1] + quad[2][1] - quad[3][1]) / (2.0 * w)];
|
||||
let ey = [(quad[3][0] - quad[0][0] + quad[2][0] - quad[1][0]) / (2.0 * h), (quad[3][1] - quad[0][1] + quad[2][1] - quad[1][1]) / (2.0 * h)];
|
||||
let det = ex[0] * ey[1] - ey[0] * ex[1];
|
||||
let norm2 = ex[0] * ex[0] + ex[1] * ex[1] + ey[0] * ey[0] + ey[1] * ey[1];
|
||||
let back = if det.abs() > 1e-9 * norm2.max(1e-12) {
|
||||
[(ey[1] * d[0] - ey[0] * d[1]) / det, (ex[0] * d[1] - ex[1] * d[0]) / det]
|
||||
} else if norm2 > 1e-12 {
|
||||
// Rank one: the pseudo-inverse is the transpose over the squared norm.
|
||||
[(ex[0] * d[0] + ex[1] * d[1]) / norm2, (ey[0] * d[0] + ey[1] * d[1]) / norm2]
|
||||
} else {
|
||||
[0.0, 0.0]
|
||||
};
|
||||
let o = [centre[0] + back[0], centre[1] + back[1]];
|
||||
if o[0].is_finite() && o[1].is_finite() { o } else { pivot }
|
||||
}
|
||||
|
||||
fn shown_at(rect: [f64; 4], quad: [[f64; 2]; 4], pivot: [f64; 2], relative: bool) -> [f64; 2] {
|
||||
if relative {
|
||||
let o = reference_point_origin(rect, quad, pivot);
|
||||
[pivot[0] - o[0], pivot[1] - o[1]]
|
||||
} else {
|
||||
pivot
|
||||
}
|
||||
}
|
||||
|
||||
/// X and Y as the options bar shows them: the reference point's position, or with relative
|
||||
/// positioning on (#11679) its offset from where it was when the session opened.
|
||||
pub(crate) fn position_shown(t: &TransformSession, relative: bool) -> [f64; 2] {
|
||||
shown_at(t.rect, t.quad, t.pivot, relative)
|
||||
}
|
||||
|
||||
/// Moves the box (and its reference point) so X and Y read `xy` (see [`position_shown`]).
|
||||
pub(crate) fn set_position(s: &mut TransformSession, relative: bool, xy: [f64; 2]) {
|
||||
let now = position_shown(s, relative);
|
||||
let (dx, dy) = (xy[0] - now[0], xy[1] - now[1]);
|
||||
s.quad = s.quad.map(|q| [q[0] + dx, q[1] + dy]);
|
||||
s.pivot = [s.pivot[0] + dx, s.pivot[1] + dy];
|
||||
}
|
||||
|
||||
/// Use Relative Positioning for Reference Point: the triangle button Photoshop puts between X and
|
||||
/// Y (#11679). On, X and Y are the reference point's offset from where the box started.
|
||||
fn relative_position_toggle(ui: &mut egui::Ui, on: &mut bool) {
|
||||
let tk = crate::theme::Tokens::get(ui.ctx());
|
||||
let (rect, resp) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), egui::Sense::click());
|
||||
if resp.clicked() {
|
||||
*on = !*on;
|
||||
}
|
||||
let painter = ui.painter();
|
||||
if *on || resp.hovered() {
|
||||
painter.rect_filled(rect, 3.0, if *on { tk.accent.gamma_multiply(0.35) } else { tk.card_border.gamma_multiply(0.5) });
|
||||
}
|
||||
let c = rect.center();
|
||||
let tri = vec![egui::pos2(c.x, c.y - 4.5), egui::pos2(c.x + 5.0, c.y + 4.0), egui::pos2(c.x - 5.0, c.y + 4.0)];
|
||||
let color = if *on { tk.accent } else { tk.text_dim };
|
||||
painter.add(egui::Shape::closed_line(tri, egui::Stroke::new(1.2, color)));
|
||||
let label = tl!("Use relative positioning for reference point");
|
||||
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, *on, label));
|
||||
resp.on_hover_text(label);
|
||||
}
|
||||
|
||||
fn anchor_grid_widget(ui: &mut egui::Ui, current_anchor: Option<usize>, enabled: bool) -> Option<usize> {
|
||||
let size = egui::vec2(16.0, 16.0);
|
||||
// Greyed and inert while Toggle Reference Point is off, as in Photoshop (#11098).
|
||||
@@ -314,18 +395,23 @@ pub(crate) fn position_size_fields(app: &mut PhotocraftApp, ui: &mut egui::Ui, t
|
||||
pivot = new_p;
|
||||
}
|
||||
ui.add_space(4.0);
|
||||
let mut px = pivot[0] as f32;
|
||||
let mut py = pivot[1] as f32;
|
||||
let relative = app.ui.tool_options.transform_relative_position;
|
||||
let shown = shown_at(t.rect, t.quad, pivot, relative);
|
||||
let mut px = shown[0] as f32;
|
||||
let mut py = shown[1] as f32;
|
||||
lbl(ui, "X:");
|
||||
let rx = crate::widgets::value_field(ui, &mut px, -POSITION_LIMIT_PX..=POSITION_LIMIT_PX, "px", 72.0).accessible_name("X");
|
||||
relative_position_toggle(ui, &mut app.ui.tool_options.transform_relative_position);
|
||||
lbl(ui, "Y:");
|
||||
let ry = crate::widgets::value_field(ui, &mut py, -POSITION_LIMIT_PX..=POSITION_LIMIT_PX, "px", 72.0).accessible_name("Y");
|
||||
if (rx.changed() || ry.changed())
|
||||
&& let Some(s) = app.ui.transform.as_mut()
|
||||
{
|
||||
let (dx, dy) = (px as f64 - s.pivot[0], py as f64 - s.pivot[1]);
|
||||
s.quad = s.quad.map(|q| [q[0] + dx, q[1] + dy]);
|
||||
s.pivot = [px as f64, py as f64];
|
||||
// An axis the user didn't edit keeps its full-precision value: the field's f32 would
|
||||
// nudge the box by its rounding.
|
||||
let x = if rx.changed() { f64::from(px) } else { shown[0] };
|
||||
let y = if ry.changed() { f64::from(py) } else { shown[1] };
|
||||
set_position(s, relative, [x, y]);
|
||||
}
|
||||
crate::widgets::vline(ui, 22.0);
|
||||
// W and H in the unit picked from their right-click menu: the scale in percent, or the box's
|
||||
|
||||
@@ -329,7 +329,7 @@ fn bar_harness(setup: impl FnOnce(&mut PhotocraftApp)) -> egui_kittest::Harness<
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.ui.transform = Some(session());
|
||||
setup(&mut app);
|
||||
let mut h = Harness::builder().with_size(vec2(900.0, 360.0)).build_ui_state(
|
||||
let mut h = Harness::builder().with_size(vec2(1000.0, 360.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
//! Relative positioning in the transform options bar (#11679): a toggle between X and Y, off by
|
||||
//! default; on, X and Y show the reference point's offset from where the box started and a typed
|
||||
//! value moves the box by that much; off, they show its position again.
|
||||
|
||||
use egui::accesskit::Role;
|
||||
use egui::vec2;
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::ToolEvent;
|
||||
|
||||
const TOGGLE: &str = "Use relative positioning for reference point";
|
||||
|
||||
/// A 200 x 120 document with a layer filled at 40,20 (60 x 40) and Free Transform open on it:
|
||||
/// the reference point starts at the box's centre, 70,40.
|
||||
fn transforming() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 120})).unwrap();
|
||||
app.sync_views();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("select.rect", json!({"x": 40, "y": 20, "width": 60, "height": 40})).unwrap();
|
||||
app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
crate::transform_tool::begin(&mut app, &egui::Context::default()).unwrap();
|
||||
app
|
||||
}
|
||||
|
||||
fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
|
||||
let mut h = Harness::builder().with_size(vec2(1600.0, 200.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
crate::panels::options_bar(app, ui);
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Studio);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
/// What the X and Y fields show.
|
||||
fn shown(h: &Harness<'_, PhotocraftApp>) -> (f64, f64) {
|
||||
let read = |name: &str| h.get_by_role_and_label(Role::SpinButton, name).accesskit_node().numeric_value().unwrap();
|
||||
(read("X"), read("Y"))
|
||||
}
|
||||
|
||||
/// Types `text` into the X field and commits it with Tab, as a user does.
|
||||
fn type_x(h: &mut Harness<'_, PhotocraftApp>, text: &str) {
|
||||
h.get_by_role_and_label(Role::SpinButton, "X").click();
|
||||
h.run_steps(1);
|
||||
h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A);
|
||||
h.run_steps(1);
|
||||
for ch in text.chars() {
|
||||
h.event(egui::Event::Text(ch.to_string()));
|
||||
h.run_steps(1);
|
||||
}
|
||||
h.key_press(egui::Key::Tab);
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
fn click_toggle(h: &mut Harness<'_, PhotocraftApp>) {
|
||||
h.get_by_label(TOGGLE).click();
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
fn quad(h: &Harness<'_, PhotocraftApp>) -> [[f64; 2]; 4] {
|
||||
h.state().ui.transform.as_ref().unwrap().quad
|
||||
}
|
||||
|
||||
fn moved(q: [[f64; 2]; 4], dx: f64) -> [[f64; 2]; 4] {
|
||||
q.map(|[x, y]| [x + dx, y])
|
||||
}
|
||||
|
||||
fn same(a: [[f64; 2]; 4], b: [[f64; 2]; 4]) -> bool {
|
||||
a.iter().flatten().zip(b.iter().flatten()).all(|(a, b)| (a - b).abs() < 1e-6)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relative_positioning_is_off_by_default() {
|
||||
let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
assert!(!app.ui.tool_options.transform_relative_position);
|
||||
// An older preferences file without the field reads it as off.
|
||||
let o: crate::state::ToolOptions = serde_json::from_value(json!({})).unwrap();
|
||||
assert!(!o.transform_relative_position);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_toggle_sits_between_x_and_y_and_turns_a_fresh_box_into_zero() {
|
||||
let mut h = harness(transforming());
|
||||
let x = h.get_by_role_and_label(Role::SpinButton, "X").rect();
|
||||
let y = h.get_by_role_and_label(Role::SpinButton, "Y").rect();
|
||||
let toggle = h.get_by_label(TOGGLE).rect();
|
||||
assert!(x.max.x <= toggle.min.x && toggle.max.x <= y.min.x, "X {x:?}, toggle {toggle:?}, Y {y:?}");
|
||||
assert_eq!(shown(&h), (70.0, 40.0), "off: the reference point's position");
|
||||
click_toggle(&mut h);
|
||||
assert!(h.state().ui.tool_options.transform_relative_position);
|
||||
assert_eq!(shown(&h), (0.0, 0.0), "on: no offset yet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_typed_x_moves_the_box_by_that_much_and_commits_as_free_transform() {
|
||||
let mut h = harness(transforming());
|
||||
click_toggle(&mut h);
|
||||
let before = quad(&h);
|
||||
type_x(&mut h, "20");
|
||||
assert!(same(quad(&h), moved(before, 20.0)), "the box moved 20 px right: {:?}", quad(&h));
|
||||
assert_eq!(shown(&h), (20.0, 0.0));
|
||||
// Typing the same offset again doesn't move it further.
|
||||
type_x(&mut h, "20");
|
||||
assert!(same(quad(&h), moved(before, 20.0)), "still 20 px: {:?}", quad(&h));
|
||||
let app = h.state_mut();
|
||||
crate::transform_tool::commit(app);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds().x0, 60, "the layer moved 20 px");
|
||||
assert_eq!(st.history.entries().last().map(ToString::to_string).as_deref(), Some("Free Transform"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_updates_the_offset_and_turning_it_off_shows_the_position_again() {
|
||||
let mut h = harness(transforming());
|
||||
click_toggle(&mut h);
|
||||
{
|
||||
let app = h.state_mut();
|
||||
let none = egui::Modifiers::NONE;
|
||||
crate::transform_tool::pointer(app, ToolEvent::Down { x: 50.0, y: 30.0, pressure: 1.0 }, none);
|
||||
crate::transform_tool::pointer(app, ToolEvent::Move { x: 55.0, y: 33.0, pressure: 1.0 }, none);
|
||||
crate::transform_tool::pointer(app, ToolEvent::Up { x: 55.0, y: 33.0 }, none);
|
||||
}
|
||||
h.run_steps(3);
|
||||
assert_eq!(shown(&h), (5.0, 3.0), "the offset follows the drag");
|
||||
click_toggle(&mut h);
|
||||
assert!(!h.state().ui.tool_options.transform_relative_position);
|
||||
assert_eq!(shown(&h), (75.0, 43.0), "off again: the reference point's position");
|
||||
}
|
||||
|
||||
/// Typing X leaves Y exactly where it was, even when Y is a fraction the field's f32 can't hold
|
||||
/// (CodeRabbit review on options_bar.rs:308): the field's rounding must not nudge the box.
|
||||
#[test]
|
||||
fn a_typed_x_leaves_a_fractional_y_exactly_where_it_was() {
|
||||
let mut app = transforming();
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = t.quad.map(|[x, y]| [x, y + 0.1]);
|
||||
t.pivot[1] += 0.1;
|
||||
let mut h = harness(app);
|
||||
let before = quad(&h);
|
||||
let pivot_y = h.state().ui.transform.as_ref().unwrap().pivot[1];
|
||||
type_x(&mut h, "90");
|
||||
let after = quad(&h);
|
||||
assert!(same(after, moved(before, 20.0)), "the box moved 20 px right: {after:?}");
|
||||
assert_eq!(after.map(|p| p[1]), before.map(|p| p[1]), "Y is untouched");
|
||||
assert_eq!(h.state().ui.transform.as_ref().unwrap().pivot[1], pivot_y);
|
||||
}
|
||||
|
||||
/// A box squashed flat has no inverse map, so the offset comes from the map's pseudo-inverse: a
|
||||
/// box squashed to zero width about its reference point reads 0 / 0, and one squashed onto its
|
||||
/// left edge, carrying the reference point with it, reads -30 / 0 (CodeRabbit pre-merge check).
|
||||
#[test]
|
||||
fn a_flattened_box_reads_the_reference_points_own_offset() {
|
||||
let mut app = transforming();
|
||||
app.ui.tool_options.transform_relative_position = true;
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = [[70.0, 20.0], [70.0, 20.0], [70.0, 60.0], [70.0, 60.0]];
|
||||
t.pivot = [70.0, 40.0];
|
||||
let mut h = harness(app);
|
||||
assert_eq!(shown(&h), (0.0, 0.0), "squashed about the reference point");
|
||||
let t = h.state_mut().ui.transform.as_mut().unwrap();
|
||||
t.quad = [[40.0, 20.0], [40.0, 20.0], [40.0, 60.0], [40.0, 60.0]];
|
||||
t.pivot = [40.0, 40.0];
|
||||
h.run_steps(2);
|
||||
assert_eq!(shown(&h), (-30.0, 0.0), "squashed onto the left edge");
|
||||
}
|
||||
Reference in new issue
Block a user