Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 50 additions & 12 deletions crates/ui-touch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ pub fn chrome(app: &mut RustGisApp, ui: &mut Ui) {
// The shape being drawn, kept while Edit is on but drawn only in Add; the feature
// being edited, and where the cross snapped to.
let state = form(&ctx);
// A streamed layer's features put in place by the map view this frame, after the
// sheet looked for the snap: one more frame looks again, the cross held still.
// Only while there is a layer to capture into, whose sheet then looks again.
if capture_layer(app, &state).is_some() && state.snap_for.is_some_and(|f| f.shapes != shapes(app)) {
ctx.request_repaint();
}
if !state.edit_mode {
sketch(app, &painter, map.center(), &t, &ctx);
}
Expand Down Expand Up @@ -345,7 +351,8 @@ struct Form {
/// Where the cross's place snapped to on the last frame, with snapping on.
snap: Option<edit::Snapped>,
/// What [`Form::snap`] was found for: it is looked for again only when the cross moved, the
/// map zoomed, the vertex being moved changed or the project did.
/// map zoomed, the vertex being moved changed, the session changed or a layer's shapes did
/// (a streamed layer's features put in place).
snap_for: Option<SnapFor>,
/// The height of the window the sheet was last drawn in: when it changes (the soft keyboard
/// came or went), the field being typed in is scrolled back into view.
Expand Down Expand Up @@ -385,17 +392,25 @@ impl Form {
}
}

/// What the cross's place was snapped for: its place, the vertex being moved, the project's state,
/// the map's resolution and whether snapping was on.
/// What the cross's place was snapped for: its place, the vertex being moved, the session's
/// revision, the layers' geometry revisions summed (a streamed layer's features are put in place
/// by the map view without a change to the session), the map's resolution and whether snapping
/// was on.
#[derive(Clone, Copy, Debug, PartialEq)]
struct SnapFor {
at: Option<(f64, f64)>,
moving: Option<(u32, usize, rustgis_ui_egui::rustgis_engine::data::VertexId)>,
state: u64,
revision: u64,
shapes: u64,
res: f64,
on: bool,
}

/// The map's layers' geometry revisions, summed: it moves whenever a layer's shapes change.
fn shapes(app: &RustGisApp) -> u64 {
app.session.map().layers.iter().fold(0u64, |sum, l| sum.wrapping_add(l.geometry_rev))
}

fn form(ctx: &egui::Context) -> Form {
ctx.data(|d| d.get_temp(egui::Id::unique("touch_capture"))).unwrap_or_default()
}
Expand Down Expand Up @@ -774,7 +789,8 @@ fn capture(app: &mut RustGisApp, ui: &mut Ui, t: &Tokens) {
// on changed, not on every frame.
let at = cross_place(app);
let map = app.session.map();
let wants = SnapFor { at, moving, state: app.session.state(), res: map.camera.res, on: map.editing.snapping.enabled };
let wants =
SnapFor { at, moving, revision: app.session.revision, shapes: shapes(app), res: map.camera.res, on: map.editing.snapping.enabled };
if state.snap_for != Some(wants) {
state.snap = at.and_then(|at| edit::snap(app, at, moving));
state.snap_for = Some(wants);
Expand Down Expand Up @@ -1049,11 +1065,12 @@ fn edit_sheet(
}
return;
};
let Some(shape) = edit::shape(app, target.id, row) else {
// The shape is borrowed for each look at it, not copied each frame: the buttons change it.
let Some(status) = edit::shape_ref(app, target.id, row).map(|shape| edit::status(target.kind, shape, editing.vertex)) else {
state.editing = None;
return;
};
ui.label(RichText::new(edit::status(target.kind, &shape, editing.vertex)).size(14.0).color(t.text));
ui.label(RichText::new(status).size(14.0).color(t.text));
let off = || tl!("Nothing was changed: the cross is off the map. Move the map under it.");
if target.kind == GeometryType::Point {
if action(ui, t, tl!("Move Point Here"), width, true, true) {
Expand All @@ -1068,8 +1085,8 @@ fn edit_sheet(
let has = editing.vertex.is_some();
if action(ui, t, tl!("Pick Vertex at Cross"), width, !has, true) {
let middle = app.map.view_rect.map(|(rect, _)| rect.center());
match middle.and_then(|at| edit::vertex_near(app, &shape, at)) {
Some(v) => state.editing = Some(editing.picking(Some(v), Some(&shape))),
match middle.and_then(|at| edit::vertex_near(app, edit::shape_ref(app, target.id, row)?, at)) {
Some(v) => state.editing = Some(editing.picking(Some(v), edit::shape_ref(app, target.id, row))),
None => app.session.notify(tl!("No vertex is near the cross. Move the map so the cross is on one."), true),
}
}
Expand All @@ -1084,14 +1101,15 @@ fn edit_sheet(
let at = if act == "delete" { None } else { cross };
if act != "delete" && at.is_none() {
app.session.notify(off(), true);
} else if at.is_some_and(|at| edit::doubles(app, &shape, v, act, at)) {
} else if let Some(at) = at
&& edit::shape_ref(app, target.id, row).is_some_and(|shape| edit::doubles(app, shape, v, act, at))
{
app.session.notify(
tl!("Nothing was changed: the cross is on the vertex next to it, and two in one place make an edge of no length. Move the map."),
true,
);
} else if let Some(next) = edit::vertex(app, target.id, row, v, act, at) {
let now = edit::shape(app, target.id, row);
state.editing = Some(editing.picking(next, now.as_ref()));
state.editing = Some(editing.picking(next, edit::shape_ref(app, target.id, row)));
}
}
}
Expand Down Expand Up @@ -1525,6 +1543,26 @@ mod tests {
assert!(form(&ctx).calendar.is_some());
}

#[test]
fn the_snap_is_looked_for_again_once_a_layers_shapes_change() {
let ctx = egui::Context::default();
let mut app = RustGisApp::with_sample();
target(&mut app, GeometryType::Polyline);
frame(&mut app, &ctx, Sheet::Capture);
assert!(form(&ctx).snap_for.is_some());
// A stale snap the cache holds, then features read in the background: it is let go.
let stale = edit::Snapped { lon: 1.0, lat: 2.0, kind: "vertex" };
set_form(&ctx, Form { snap: Some(stale), ..form(&ctx) });
frame(&mut app, &ctx, Sheet::Capture);
// Nothing changed since the snap was found: the one held is kept.
assert_eq!(form(&ctx).snap, Some(stale));
// A layer's shapes put in place, as the map view does with a streamed layer's features.
let id = app.session.map().layers[0].id;
app.session.map_mut().layer_mut(id).unwrap().touch_geometry();
frame(&mut app, &ctx, Sheet::Capture);
assert_ne!(form(&ctx).snap, Some(stale));
Comment thread
giswqs marked this conversation as resolved.
Resolved
}

#[test]
fn a_vertex_put_on_the_one_next_to_it_is_found_and_a_place_apart_is_not() {
let ctx = egui::Context::default();
Expand Down
Loading