mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Merge pull request #10914 from dotCooCoo/fix/whole-pixel-pan-workaround
Canvas: pans move the view by whole pixels of the art's frame
This commit is contained in:
3 files changed
+424
-6
No files matched your search
@@ -146,6 +146,8 @@ struct Body {
|
||||
edited: Option<CanvasView>,
|
||||
/// The Quick Pen tool took this pass's wheel for its arc size ([`quick_pen_wheel::arc_size`]).
|
||||
arc_view: Option<CanvasView>,
|
||||
/// What this pass's pan by whole device pixels left ([`pan_on_device_pixels`]).
|
||||
pan: Option<PanRest>,
|
||||
}
|
||||
|
||||
/// Window `win`'s document in the framework canvas, filling the rest of `ui`. True when the canvas
|
||||
@@ -184,6 +186,8 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
if refit && queued.is_empty() {
|
||||
queued.push(ViewRequest::Place(ViewCommands::FitArtboard));
|
||||
}
|
||||
// A view the app asks for this pass (a fit, a zoom command) keeps its exact center.
|
||||
let requested = !queued.is_empty();
|
||||
let mut cv = view.canvas(scaling, area);
|
||||
let given = cv;
|
||||
// A zoom percentage (Actual Size) counts from the density of the display that shows this
|
||||
@@ -265,8 +269,16 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
pass.view = Some(arc_view);
|
||||
}
|
||||
}
|
||||
// The canvas's own pans (the wheel, a middle-button drag, keys) move the view by a step from where it was.
|
||||
let pan = (!measuring).then(|| {
|
||||
let rest = doc_windows::view(app, win)
|
||||
.filter(|_| !requested)
|
||||
.and_then(|v| v.pan_rest.filter(|rest| rest.kept == Some((v.center, v.zoom, v.rotation))));
|
||||
pan_on_device_pixels(rest, ui.ctx().input(|i| i.max_texture_side), &mut space, pass.as_mut())
|
||||
});
|
||||
body = with_canvas_space(ui, space, |ui| show_body(app, ui, &space, pass.as_ref(), win, kind, bar_shapes));
|
||||
body.arc_view = arc_turn.then_some(arc_view);
|
||||
body.pan = pan;
|
||||
}
|
||||
});
|
||||
let output = shown.output;
|
||||
@@ -279,7 +291,7 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
rulers::apply(app, ui, out);
|
||||
}
|
||||
let pressed = matches!(output.press.phase, PressPhase::Down { .. });
|
||||
let Body { reply, context_at, context_key, edited, arc_view } = body;
|
||||
let Body { reply, context_at, context_key, edited, arc_view, pan } = body;
|
||||
let request = reply
|
||||
.as_ref()
|
||||
.and_then(|r| r.context.as_ref())
|
||||
@@ -288,13 +300,17 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
let jumped = edited.is_some();
|
||||
// Presentation Mode keeps its view through a drag of the middle button, which the canvas pans on its own, and a
|
||||
// wheel turn the Quick Pen tool takes for its arc size leaves other view requests in place.
|
||||
let view_now = if presenting && ui.input(|i| i.pointer.middle_down()) {
|
||||
given
|
||||
} else {
|
||||
edited.or(reply.and_then(|r| r.view)).or(arc_view).or(output.view).unwrap_or(cv)
|
||||
};
|
||||
let keeps_given = presenting && ui.input(|i| i.pointer.middle_down());
|
||||
let pan = pan.filter(|_| !keeps_given);
|
||||
let view_now =
|
||||
if keeps_given { given } else { edited.or(reply.and_then(|r| r.view)).or(arc_view).or(pan.map(|p| p.view)).or(output.view).unwrap_or(cv) };
|
||||
// Pointer drags of the Hand, compass and scroll bars request absolute views without carrying a rounding remainder.
|
||||
let absolute_drag = (edited.is_some() || app.session.tool_id() == "hand" || tool_adapter::view_tool_held(app))
|
||||
&& ui.input(|i| i.pointer.any_down() || i.pointer.any_released());
|
||||
let (view_now, pan) = settled_on_device_pixels(pan, view_now, !absolute_drag);
|
||||
if let Some(view) = doc_windows::view_mut(app, win) {
|
||||
view.set_canvas(&view_now, scaling);
|
||||
view.pan_rest = pan.map(|rest| PanRest { kept: Some((view.center, view.zoom, view.rotation)), ..rest });
|
||||
// Fitted once, the view stays where it is moved.
|
||||
view.fitted |= refit;
|
||||
// An edit of the compass or a scroll bar reaches the next pass, which stops a running ease.
|
||||
@@ -318,6 +334,100 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
pressed
|
||||
}
|
||||
|
||||
/// What a canvas pan by whole device pixels ([`pan_step`]) left: the view the canvas settled on, the device pixels a
|
||||
/// point it was shown at, and the part of a device pixel along each screen axis the pan has not moved yet (at most half
|
||||
/// a pixel each way).
|
||||
///
|
||||
/// Temporary: storytold/craft-libs#5411 moves the canvas's pans and wheel steps by whole device pixels. Remove this,
|
||||
/// [`crate::state::View::pan_rest`] and their callers with the craft-libs pin bump that brings it in.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) struct PanRest {
|
||||
view: CanvasView,
|
||||
pixels_per_point: f32,
|
||||
owed: (f64, f64),
|
||||
/// The app's center, zoom and rotation after keeping the settled view.
|
||||
kept: Option<(vectorcraft_geom::Point, f64, f64)>,
|
||||
}
|
||||
|
||||
impl PanRest {
|
||||
/// A rest of nothing after `view`, shown at `pixels_per_point`.
|
||||
fn none(view: CanvasView, pixels_per_point: f32) -> Self {
|
||||
Self { view, pixels_per_point, owed: (0.0, 0.0), kept: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// `view`, when it differs from `from` only in its center: the view that moves the document from where `from` draws it
|
||||
/// by a whole number of device pixels at `pixels_per_point` along each screen axis, the number nearest to how far
|
||||
/// `view` moves it plus `owed` device pixels, with what is left of a pixel. Exactly half a pixel left stays owed. None
|
||||
/// when `view` differs from `from` in more than its center, or `pixels_per_point` or the views are not usable.
|
||||
pub(crate) fn pan_step(from: CanvasView, view: CanvasView, owed: (f64, f64), pixels_per_point: f32) -> Option<(CanvasView, (f64, f64))> {
|
||||
let ppp = f64::from(pixels_per_point);
|
||||
let mut moved = from;
|
||||
moved.center = view.center;
|
||||
if moved != view || !(ppp.is_finite() && ppp > 0.0 && view.zoom.is_finite() && view.zoom > 0.0) {
|
||||
return None;
|
||||
}
|
||||
let (a, b) = (from.affine() * vectorcraft_geom::Point::ZERO, view.affine() * vectorcraft_geom::Point::ZERO);
|
||||
let total = ((b.x - a.x) * ppp + owed.0, (b.y - a.y) * ppp + owed.1);
|
||||
let nearest = |t: f64| t.signum() * (t.abs() - 0.5).ceil().max(0.0);
|
||||
let whole = (nearest(total.0), nearest(total.1));
|
||||
let shift = egui::vec2((a.x + whole.0 / ppp - b.x) as f32, (a.y + whole.1 / ppp - b.y) as f32);
|
||||
if !(shift.x.is_finite() && shift.y.is_finite()) {
|
||||
return None;
|
||||
}
|
||||
let mut shown = view;
|
||||
if shift != egui::Vec2::ZERO {
|
||||
shown.center = view.center - view.unmap_vec(shift);
|
||||
}
|
||||
(shown.center.x.is_finite() && shown.center.y.is_finite()).then_some((shown, (total.0 - whole.0, total.1 - whole.1)))
|
||||
}
|
||||
|
||||
/// Moves the view the canvas settled on in this pass (`space`'s target) from `rest.view` by whole pixels of the art's
|
||||
/// frame ([`pan_step`], [`raster::frame_ppp`] with the display's largest texture side `max_side`), adding the part of a
|
||||
/// pixel `rest` owes, and gives the tool host the same view in `pass`. The render worker then moves the frame it kept
|
||||
/// ([`raster::render_job`]) instead of rendering the whole canvas for a pan by part of a pixel. Nothing is rounded while
|
||||
/// a zoom eases, when the app changed the view since `rest`, or when the app asks for a view this pass (`rest` is None).
|
||||
/// Returns the rest to keep.
|
||||
fn pan_on_device_pixels(rest: Option<PanRest>, max_side: usize, space: &mut CanvasSpace, pass: Option<&mut ToolPass>) -> PanRest {
|
||||
let target = space.target();
|
||||
// The pixels of the art's frame, which are fewer than the device's when the frame would be wider than `max_side`.
|
||||
let ppp = raster::frame_ppp(target.rect.size(), space.pixels_per_point, max_side);
|
||||
let step = rest.filter(|rest| !space.easing() && rest.pixels_per_point == ppp).and_then(|rest| {
|
||||
let mut from = rest.view;
|
||||
// Match zooms separated only by the UI scaling round trip.
|
||||
if (from.zoom - target.zoom).abs() <= 1e-12 * from.zoom.abs().max(target.zoom.abs()) {
|
||||
from.zoom = target.zoom;
|
||||
}
|
||||
pan_step(from, target, rest.owed, ppp)
|
||||
});
|
||||
let Some((view, owed)) = step else {
|
||||
return PanRest::none(target, ppp);
|
||||
};
|
||||
if view != target {
|
||||
space.set_view(view);
|
||||
if let Some(pass) = pass {
|
||||
pass.space = Some(*space);
|
||||
pass.view = Some(view);
|
||||
}
|
||||
}
|
||||
PanRest { view, pixels_per_point: ppp, owed, kept: None }
|
||||
}
|
||||
|
||||
/// `view`, the view the app keeps after this pass, moved from the view of `rest` by whole device pixels when it differs
|
||||
/// from it in its center alone (a tool, a scroll bar or the compass panned it after [`pan_on_device_pixels`]), and the
|
||||
/// rest to keep. Without `carry` (an absolute drag) the rest neither adds nor keeps a part of a pixel.
|
||||
fn settled_on_device_pixels(rest: Option<PanRest>, view: CanvasView, carry: bool) -> (CanvasView, Option<PanRest>) {
|
||||
let Some(rest) = rest else { return (view, None) };
|
||||
if rest.view == view {
|
||||
return (view, Some(PanRest { owed: if carry { rest.owed } else { (0.0, 0.0) }, ..rest }));
|
||||
}
|
||||
let ppp = rest.pixels_per_point;
|
||||
match pan_step(rest.view, view, if carry { rest.owed } else { (0.0, 0.0) }, ppp) {
|
||||
Some((view, owed)) => (view, Some(PanRest { view, owed: if carry { owed } else { (0.0, 0.0) }, ..rest })),
|
||||
None => (view, Some(PanRest::none(view, ppp))),
|
||||
}
|
||||
}
|
||||
|
||||
/// The body of the canvas in order: controls that take presses first, the tool host, the selection's marks and the
|
||||
/// display-only widgets over it, the picture last.
|
||||
fn show_body(
|
||||
|
||||
@@ -6268,6 +6268,309 @@ fn a_whole_pixel_pan() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Wheel steps of part of a device pixel, at a zoom and center that put the document's origin between device pixels,
|
||||
/// move the frame the render worker kept: every frame that may copy from the last one does.
|
||||
#[test]
|
||||
fn wheel_steps_of_part_of_a_pixel_move_the_kept_frame() {
|
||||
keeping_color_settings(|| {
|
||||
for rulers in [false, true] {
|
||||
for scaling in [1.0, 1.75] {
|
||||
wheel_steps_of_part_of_a_pixel(rulers, scaling);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn wheel_steps_of_part_of_a_pixel(rulers: bool, scaling: f64) {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.prefs.ui_scaling = scaling;
|
||||
app.ui.view.rulers = rulers;
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
for x in [20, 160, 300] {
|
||||
app.session.execute("shape.rectangle", &json!({"x": x, "y": 40, "width": 80, "height": 200})).unwrap();
|
||||
}
|
||||
app.canvas.worker_started = true;
|
||||
let ctx = crate::testing::ui_context();
|
||||
for _ in 0..3 {
|
||||
app_step(&mut app, &ctx, vec![]);
|
||||
}
|
||||
{
|
||||
let v = app.view_mut().unwrap();
|
||||
let zoom = 1.37;
|
||||
let scale = f64::from(scaling as f32);
|
||||
if scaling != 1.0 {
|
||||
assert_ne!((zoom * scale) / scale, zoom, "the UI scaling round trip changes the canvas zoom");
|
||||
}
|
||||
v.zoom = zoom * scale;
|
||||
v.center = Point::new(200.3, 150.7);
|
||||
}
|
||||
app_step(&mut app, &ctx, vec![]);
|
||||
let (worker, jobs, done) = crate::render_worker::Worker::test_channel();
|
||||
app.canvas.worker = Some(worker);
|
||||
app.canvas.last_ms = 50.0;
|
||||
let at = app.canvas_view().unwrap().rect.center();
|
||||
let wheel = egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: vec2(0.3, -0.4),
|
||||
phase: egui::TouchPhase::Move,
|
||||
modifiers: egui::Modifiers::default(),
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
};
|
||||
let start = app.canvas_view().unwrap().center;
|
||||
let mut r = vectorcraft_render::Renderer::new();
|
||||
let (mut kept, mut copying) = (None, 0);
|
||||
for _ in 0..10 {
|
||||
app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at), wheel.clone()]);
|
||||
while let Ok(job) = jobs.try_recv() {
|
||||
let may = job.may_copy && kept.is_some();
|
||||
let frame = render_job(&mut r, &mut kept, &job);
|
||||
copying += usize::from(may);
|
||||
if may {
|
||||
assert!(frame.reused, "each new frame of the pan copies the kept frame's pixels, rulers: {rulers}, UI scaling: {scaling}");
|
||||
}
|
||||
let (img, reused) = (frame.img, frame.reused);
|
||||
done.send(crate::render_worker::Done { key: job.key, doc: job.doc, opts: job.opts, img, ms: 50.0, moved: 0, reused, view_sized: false })
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
assert_ne!(app.canvas_view().unwrap().center, start, "the wheel pans the view");
|
||||
assert!(copying >= 3, "the pan asks for new frames: {copying}");
|
||||
}
|
||||
|
||||
/// Pans by part of a device pixel, each from the view the last one settled on, move the document by whole device pixels
|
||||
/// at the view's zoom and rotation, and the parts left over add up: after each step the document is within half a device
|
||||
/// pixel of where the pans together put it. A change of more than the center is not a pan.
|
||||
#[test]
|
||||
fn pans_by_part_of_a_pixel_move_by_whole_device_pixels_and_add_up() {
|
||||
let rect = egui::Rect::from_min_size(pos2(10.5, 7.25), vec2(640.0, 480.0));
|
||||
for (zoom, rotation, ppp) in [(1.37, 0.0, 1.0), (0.613, 30.0, 2.0), (7.9, -75.0, 1.25)] {
|
||||
let start = CanvasView::new(rect, Point::new(203.17, -48.6), zoom).rotation(rotation);
|
||||
let origin = |v: &CanvasView| v.affine() * Point::ZERO;
|
||||
let (mut view, mut owed) = (start, (0.0, 0.0));
|
||||
// Each step moves the document 0.3 points right and 0.45 points up on screen.
|
||||
let step = vec2(0.3, -0.45);
|
||||
for n in 1..=12 {
|
||||
let mut next = view;
|
||||
next.center = view.center - view.unmap_vec(step);
|
||||
let (shown, left) = super::pan_step(view, next, owed, ppp).expect("a pan");
|
||||
let ppp = f64::from(ppp);
|
||||
let (a, b) = (origin(&view), origin(&shown));
|
||||
let wanted = origin(&start) + vectorcraft_geom::Vec2::new(f64::from(step.x), f64::from(step.y)) * f64::from(n);
|
||||
for (from, to, want) in [(a.x, b.x, wanted.x), (a.y, b.y, wanted.y)] {
|
||||
let moved = (to - from) * ppp;
|
||||
assert!((moved - moved.round()).abs() < 1e-6, "{zoom} {rotation}° step {n}: moved {moved} device pixels");
|
||||
assert!(((to - want) * ppp).abs() <= 0.5 + 1e-6, "{zoom} {rotation}° step {n}: {} device pixels short", (want - to) * ppp);
|
||||
}
|
||||
assert_eq!((shown.zoom, shown.rotation, shown.rect), (start.zoom, start.rotation, start.rect));
|
||||
(view, owed) = (shown, left);
|
||||
}
|
||||
let mut zoomed = view;
|
||||
zoomed.zoom = zoom * 1.1;
|
||||
assert_eq!(super::pan_step(view, zoomed, owed, ppp), None, "a zoom is not a pan");
|
||||
}
|
||||
}
|
||||
|
||||
/// A drag (a button held) sets where the view goes: each pass's view stays within half a device pixel of the view the
|
||||
/// drag asked for, however many passes it lasts, and moves by whole device pixels from the last one.
|
||||
#[test]
|
||||
fn a_drag_s_views_stay_within_half_a_pixel_of_the_pointer() {
|
||||
let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0));
|
||||
let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.37);
|
||||
let mut rest = Some(super::PanRest::none(start, 1.0));
|
||||
for n in 1..=20 {
|
||||
// The drag asks for the view at its press moved 0.35 points a pass.
|
||||
let mut asked = start;
|
||||
asked.center = start.center - start.unmap_vec(vec2(0.35, 0.35) * n as f32);
|
||||
let last = rest.unwrap().view;
|
||||
let (view, next) = super::settled_on_device_pixels(rest, asked, false);
|
||||
let [a, b, c] = [last, view, asked].map(|v| v.affine() * Point::ZERO);
|
||||
for (from, to, want) in [(a.x, b.x, c.x), (a.y, b.y, c.y)] {
|
||||
assert!(((to - from) - (to - from).round()).abs() < 1e-6, "pass {n}: whole pixels from the last view");
|
||||
assert!((to - want).abs() <= 0.5 + 1e-6, "pass {n}: {} device pixels from the drag", want - to);
|
||||
}
|
||||
rest = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// A drag's rounding is not owed to the pans after it: a drag by 0.4 device pixels leaves the view where it was and
|
||||
/// nothing owed, so a later step of 0.2 device pixels does not move the view either.
|
||||
#[test]
|
||||
fn a_drag_leaves_nothing_owed_to_later_pans() {
|
||||
let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0));
|
||||
let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.0);
|
||||
let mut asked = start;
|
||||
asked.center = start.center - start.unmap_vec(vec2(0.4, 0.0));
|
||||
let (view, rest) = super::settled_on_device_pixels(Some(super::PanRest::none(start, 1.0)), asked, false);
|
||||
assert_eq!(view.center, start.center, "a drag by 0.4 device pixels rounds to no move");
|
||||
assert_eq!(rest.map(|rest| rest.owed), Some((0.0, 0.0)), "the drag leaves nothing owed");
|
||||
let mut stepped = view;
|
||||
stepped.center = view.center - view.unmap_vec(vec2(0.2, 0.0));
|
||||
let (after, _) = super::settled_on_device_pixels(rest, stepped, true);
|
||||
assert_eq!(after.center, view.center, "a step of 0.2 device pixels alone does not move the view");
|
||||
}
|
||||
|
||||
/// A drag pass that leaves the view where it was clears what earlier pans left owed, so a later step of 0.2 device
|
||||
/// pixels after 0.4 owed does not move the view by a whole pixel.
|
||||
#[test]
|
||||
fn a_drag_that_leaves_the_view_in_place_clears_what_is_owed() {
|
||||
let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0));
|
||||
let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.0);
|
||||
let owing = super::PanRest { owed: (0.4, 0.0), ..super::PanRest::none(start, 1.0) };
|
||||
let (view, rest) = super::settled_on_device_pixels(Some(owing), start, false);
|
||||
assert_eq!(view, start, "the drag keeps the view");
|
||||
assert_eq!(rest.map(|rest| rest.owed), Some((0.0, 0.0)), "the drag leaves nothing owed");
|
||||
let mut stepped = view;
|
||||
stepped.center = view.center - view.unmap_vec(vec2(0.2, 0.0));
|
||||
let (after, _) = super::settled_on_device_pixels(rest, stepped, true);
|
||||
assert_eq!(after.center, view.center, "a step of 0.2 device pixels alone does not move the view");
|
||||
// A pass that carries the rest keeps what is owed when the view stays where it was.
|
||||
let (_, kept) = super::settled_on_device_pixels(Some(owing), start, true);
|
||||
assert_eq!(kept.map(|rest| rest.owed), Some((0.4, 0.0)), "a carrying pass keeps what is owed");
|
||||
}
|
||||
|
||||
/// Steps a tool takes from where the view was (a drag's autoscroll, the Hand's glide) keep the parts of a pixel they
|
||||
/// leave: after each pass the view is within half a device pixel of all the steps together.
|
||||
#[test]
|
||||
fn a_tool_s_steps_keep_the_parts_of_a_pixel_they_leave() {
|
||||
let rect = egui::Rect::from_min_size(pos2(0.0, 0.0), vec2(640.0, 480.0));
|
||||
let start = CanvasView::new(rect, Point::new(100.0, 80.0), 1.37);
|
||||
let mut rest = Some(super::PanRest::none(start, 1.25));
|
||||
for n in 1..=12 {
|
||||
let last = rest.unwrap().view;
|
||||
// Each pass the tool moves the view it was given 2.4 device pixels left.
|
||||
let mut asked = last;
|
||||
asked.center = last.center - last.unmap_vec(vec2(-2.4 / 1.25, 0.0));
|
||||
let (view, next) = super::settled_on_device_pixels(rest, asked, true);
|
||||
let moved = (view.affine() * Point::ZERO).x - (start.affine() * Point::ZERO).x;
|
||||
assert!((moved * 1.25 + 2.4 * f64::from(n)).abs() <= 0.5 + 1e-5, "pass {n}: moved {} device pixels", moved * 1.25);
|
||||
rest = next;
|
||||
}
|
||||
}
|
||||
|
||||
/// A Hand drag by parts of a device pixel, at a zoom and center that put the document's origin between device pixels,
|
||||
/// ends where the pointer let go: the document is within half a device pixel of where the drag puts it, after the
|
||||
/// release too.
|
||||
#[test]
|
||||
fn a_hand_drag_by_parts_of_a_pixel_ends_where_the_pointer_lets_go() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
app.select_tool("hand");
|
||||
let ctx = crate::testing::ui_context();
|
||||
for _ in 0..3 {
|
||||
app_step(&mut app, &ctx, vec![]);
|
||||
}
|
||||
{
|
||||
let v = app.view_mut().unwrap();
|
||||
v.zoom = 1.37;
|
||||
v.center = Point::new(200.3, 150.7);
|
||||
}
|
||||
app_step(&mut app, &ctx, vec![]);
|
||||
let start = app.canvas_view().unwrap();
|
||||
let at = start.rect.center();
|
||||
let end = at + vec2(17.7, 12.35);
|
||||
app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(at), press(at, true)]);
|
||||
for to in [at + vec2(13.7, 9.35), end] {
|
||||
app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]);
|
||||
}
|
||||
let origin = |v: &CanvasView| v.affine() * Point::ZERO;
|
||||
let wanted = origin(&start) + vectorcraft_geom::Vec2::new(f64::from(end.x - at.x), f64::from(end.y - at.y));
|
||||
let near = |app: &VectorcraftApp, when: &str| {
|
||||
let got = origin(&app.canvas_view().unwrap());
|
||||
assert!((got.x - wanted.x).abs() <= 0.5 + 1e-6 && (got.y - wanted.y).abs() <= 0.5 + 1e-6, "{when}: {got:?}, the drag puts it at {wanted:?}");
|
||||
};
|
||||
near(&app, "dragging");
|
||||
app_step(&mut app, &ctx, vec![press(end, false)]);
|
||||
app_step(&mut app, &ctx, vec![]);
|
||||
near(&app, "released");
|
||||
}
|
||||
|
||||
/// A middle-button drag at 1.25 device pixels a point moves the view by whole device pixels and keeps the parts of a
|
||||
/// pixel each move leaves: after moves of one point each the document is within half a device pixel of the pointer.
|
||||
#[test]
|
||||
fn a_middle_button_drag_keeps_the_parts_of_a_pixel_it_moves() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
let step = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
|
||||
let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0));
|
||||
let mut raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() };
|
||||
raw.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(1.25);
|
||||
crate::testing::app_frame(app, &ctx, raw);
|
||||
};
|
||||
for _ in 0..3 {
|
||||
step(&mut app, vec![]);
|
||||
}
|
||||
{
|
||||
let v = app.view_mut().unwrap();
|
||||
v.zoom = 1.37;
|
||||
v.center = Point::new(200.3, 150.7);
|
||||
}
|
||||
step(&mut app, vec![]);
|
||||
assert_eq!(ctx.pixels_per_point(), 1.25);
|
||||
let start = app.canvas_view().unwrap();
|
||||
let at = start.rect.center();
|
||||
let middle =
|
||||
|at: Pos2, pressed: bool| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Middle, pressed, modifiers: Default::default() };
|
||||
step(&mut app, vec![egui::Event::PointerMoved(at), middle(at, true)]);
|
||||
let mut to = at;
|
||||
for _ in 0..8 {
|
||||
to += vec2(1.0, 0.0);
|
||||
step(&mut app, vec![egui::Event::PointerMoved(to)]);
|
||||
}
|
||||
let origin = |v: &CanvasView| v.affine() * Point::ZERO;
|
||||
let (got, from) = (origin(&app.canvas_view().unwrap()), origin(&start));
|
||||
let moved = (got.x - from.x) * 1.25;
|
||||
assert!((moved - f64::from(to.x - at.x) * 1.25).abs() <= 0.5 + 1e-6, "moved {moved} device pixels for {} points", to.x - at.x);
|
||||
assert!(((got.y - from.y) * 1.25).abs() <= 0.5 + 1e-6, "no move down");
|
||||
step(&mut app, vec![middle(to, false)]);
|
||||
}
|
||||
|
||||
/// A fit the app asks for after a pan by parts of a device pixel, at the zoom the pan kept, puts the view at the fitted
|
||||
/// center exactly: the pan's leftover part of a pixel does not round it.
|
||||
#[test]
|
||||
fn a_fit_after_a_pan_by_part_of_a_pixel_keeps_its_exact_center() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
let step = |app: &mut VectorcraftApp, events: Vec<egui::Event>| {
|
||||
let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0));
|
||||
let mut raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() };
|
||||
raw.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(1.25);
|
||||
crate::testing::app_frame(app, &ctx, raw);
|
||||
};
|
||||
for _ in 0..3 {
|
||||
step(&mut app, vec![]);
|
||||
}
|
||||
let fitted = app.canvas_view().unwrap();
|
||||
let at = fitted.rect.center();
|
||||
// The pans start from a view whose document origin is between device pixels of the fitted one.
|
||||
{
|
||||
let v = app.view_mut().unwrap();
|
||||
v.center = Point::new(v.center.x + 0.37, v.center.y + 0.21);
|
||||
}
|
||||
step(&mut app, vec![]);
|
||||
let wheel = egui::Event::MouseWheel {
|
||||
unit: egui::MouseWheelUnit::Point,
|
||||
delta: vec2(0.3, -0.45),
|
||||
phase: egui::TouchPhase::Move,
|
||||
modifiers: egui::Modifiers::default(),
|
||||
source: egui::MouseWheelSource::Unknown,
|
||||
};
|
||||
for _ in 0..3 {
|
||||
step(&mut app, vec![egui::Event::PointerMoved(at), wheel.clone()]);
|
||||
}
|
||||
let panned = app.canvas_view().unwrap();
|
||||
assert_ne!(panned.center, fitted.center, "the wheel pans the view");
|
||||
assert_eq!(panned.zoom, fitted.zoom, "the pan keeps the zoom");
|
||||
assert!(app.view().unwrap().pan_rest.is_some(), "the pan keeps a rest");
|
||||
app.view_mut().unwrap().fitted = false;
|
||||
step(&mut app, vec![]);
|
||||
let refit = app.canvas_view().unwrap();
|
||||
assert_eq!(refit.zoom, fitted.zoom, "the fit gives the same zoom");
|
||||
let (got, want) = (refit.affine() * Point::ZERO, fitted.affine() * Point::ZERO);
|
||||
assert!((got.x - want.x).abs() < 1e-6 && (got.y - want.y).abs() < 1e-6, "the fit is not rounded: {got:?}, fitted at {want:?}");
|
||||
}
|
||||
|
||||
/// Installing, replacing or removing a plug-in draws a document with a plug-in effect again, and a
|
||||
/// pan from the frame drawn before the change renders whole instead of copying its pixels. The key
|
||||
/// of a document without a plug-in effect does not follow the plug-ins. The other tests share the
|
||||
|
||||
@@ -104,6 +104,10 @@ pub struct View {
|
||||
/// How the canvas reaches the view the app last set ([`Self::set_canvas`] callers).
|
||||
#[serde(skip)]
|
||||
pub moved: Option<ViewMove>,
|
||||
/// What the canvas's last pan left to move ([`crate::canvas::PanRest`]). Temporary, removed with the craft-libs pin
|
||||
/// bump that brings in storytold/craft-libs#5411.
|
||||
#[serde(skip)]
|
||||
pub(crate) pan_rest: Option<crate::canvas::PanRest>,
|
||||
}
|
||||
|
||||
impl Default for View {
|
||||
@@ -122,6 +126,7 @@ impl Default for View {
|
||||
rect: egui::Rect::NOTHING,
|
||||
request: None,
|
||||
moved: None,
|
||||
pan_rest: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user