Skip to content
Merged
Show file tree
Hide file tree
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
33 changes: 20 additions & 13 deletions crates/engine/src/cmd/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,15 @@ fn change_extrusion(s: &mut Session, p: &Value, f: impl FnOnce(&mut Extrusion))
fn mask_inputs(input: &super::InputContext<'_>, p: &Value) -> Result<super::Inputs, String> {
let mut inputs = super::Inputs::single_layer(input, p)?;
if let Some((name, values)) = mask_name(p)? {
// Preserve known live validation; future names are bound by the planner and checked
// against the actual polygon or raster and target when the handler runs.
if input.map().layer_by_name(name).is_some() {
mask_layer(input.map(), name, values.is_some(), input.layer_id(p).ok())?;
// Preserve known live validation, binding the polygon or raster found; future names are
// bound by the planner and checked against the actual mask and target when the handler runs.
let map = input.map();
if map.layer_by_name(name).is_some() {
let mask = mask_layer(map, name, values.is_some(), input.layer_id(p).ok())?;
inputs.extend(super::Inputs::layers([(map, mask)]));
} else {
inputs.extend(super::Inputs::named(map, [name])?);
}
inputs.extend(super::Inputs::named(input.map(), [name])?);
}
Ok(inputs)
}
Expand Down Expand Up @@ -395,16 +398,20 @@ fn mask_name(p: &Value) -> Result<Option<(&str, Option<ValueMask>)>, String> {
}
}

/// Validates a live polygon (or with `values`, raster) mask, including the self-mask refusal.
/// Validates a live polygon (or with `values`, raster) mask, including the self-mask refusal: the
/// first layer of that name and kind, past any other named so, as the renderer finds it, which
/// also passes over the layer itself for a raster mask.
fn mask_layer<'a>(map: &'a rustgis_map::Map, name: &str, values: bool, id: Option<u32>) -> Result<&'a rustgis_map::Layer, String> {
let mask = map
.layer_by_name(name)
.filter(|l| if values { l.is_raster() } else { l.geometry_type() == GeometryType::Polygon })
.ok_or_else(|| format!("no {} layer '{name}'", if values { "raster" } else { "polygon" }))?;
if Some(mask.id) == id {
return Err("a layer can't mask itself".into());
let kind = |l: &&rustgis_map::Layer| if values { l.is_raster() } else { l.geometry_type() == GeometryType::Polygon };
let mut named = map.layers.iter().filter(|l| l.name.eq_ignore_ascii_case(name)).filter(kind);
let first = named.next().ok_or_else(|| format!("no {} layer '{name}'", if values { "raster" } else { "polygon" }))?;
if Some(first.id) != id {
return Ok(first);
}
match named.next() {
Some(other) if values => Ok(other),
_ => Err("a layer can't mask itself".into()),
}
Ok(mask)
}

/// Resolves and validates the literal mask against the actual execution map: a polygon layer's
Expand Down
27 changes: 27 additions & 0 deletions crates/engine/src/tests/raster_mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,30 @@ fn a_pasted_layer_named_as_its_mask_isnt_masked_by_itself() {
let pasted = s.map().layer_by_name(&format!("{dem} copy")).unwrap();
assert_eq!((pasted.raster_mask.clone(), r["unmasked"].clone()), (None, json!([format!("{dem} copy")])));
}

/// A raster mask names a raster even when a polygon layer of that name comes first: names can
/// collide after a rename, and the drawing finds the raster past it (#3360 review).
#[test]
fn layer_mask_finds_the_raster_past_a_polygon_of_that_name() {
let (mut s, dem) = with_dem();
let square = r#"{"type":"Polygon","coordinates":[[[10,45],[10.04,45],[10.04,45.04],[10,45.04],[10,45]]]}"#;
s.run("layer.addDataText", &json!({"name": "Hill", "fileName": "hill.geojson", "text": square})).unwrap();
s.run("layer.rename", &json!({"layer": "Hill", "name": dem})).unwrap();
s.run("layer.mask", &json!({"layer": "Cities", "mask": {"raster": dem, "min": 250}})).unwrap();
assert_eq!(masks(&s, "Cities").1.map(|m| m.raster), Some(dem));
}

/// A raster isn't refused as its own mask when another raster of that name follows it: the drawing
/// passes over the layer itself and takes that one (#3371 review).
#[test]
fn layer_mask_takes_another_raster_of_the_layers_own_name() {
let (mut s, dem) = with_dem();
s.run("layer.copy", &json!({"layer": dem})).unwrap();
s.run("layer.paste", &Value::Null).unwrap();
s.run("layer.rename", &json!({"layer": format!("{dem} copy"), "name": dem})).unwrap();
// The pasted copy, on top, is the first raster of that name.
let first = s.map().layers.iter().find(|l| l.is_raster()).unwrap().id;
s.run("layer.mask", &json!({"layer": first, "mask": {"raster": dem, "min": 250}})).unwrap();
let mask = s.map().layer(first).unwrap().raster_mask.clone().map(|m| m.raster);
assert_eq!(mask, Some(dem));
}
11 changes: 10 additions & 1 deletion crates/ui-egui/src/ribbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4574,6 +4574,12 @@ impl RasterMaskForm {
}
}

/// Where [`raster_mask_form`] keeps the draft of layer `layer` on map `map` (its
/// `instance_id`): a layer's id is its map's own, so another map's layer of that id has its own.
fn raster_mask_form_id(map: u64, layer: u32) -> egui::Id {
Comment thread
elpidioassinaturas marked this conversation as resolved.
Resolved
egui::Id::unique(("raster_mask_form", map, layer))
}

/// The form [`raster_mask_form`] shows, with the layer's mask it started from: the one `kept`
/// from the last frame while the layer's raster mask `mask` is still that one, else one from
/// `mask` (or a new one on `raster`), so an undo or another command shows in it.
Expand All @@ -4599,7 +4605,7 @@ pub(crate) fn raster_mask_form(ui: &mut Ui, app: &mut RustGisApp, layer: &rustgi
ui.label(egui::RichText::new(tl!("Add a raster to mask by its values.")).small().weak());
return;
}
let id = egui::Id::unique(("raster_mask_form", layer.id));
let id = raster_mask_form_id(app.session.map().instance_id(), layer.id);
let kept = ui.data(|d| d.get_temp::<(RasterMaskForm, Option<ValueMask>)>(id));
let (mut f, started) = raster_mask_draft(kept, layer.raster_mask.as_ref(), &rasters[0]);
ui.label(egui::RichText::new(tl!("Where a raster's values are in a range:")).small().weak());
Expand Down Expand Up @@ -5979,6 +5985,9 @@ mod tests {
/// again from the mask when an undo or another command changes it (#3147).
#[test]
fn the_raster_mask_form_starts_again_when_the_layers_mask_changes() {
// Layer ids are a map's own: the same id on two maps keeps two drafts.
assert_ne!(super::raster_mask_form_id(1, 7), super::raster_mask_form_id(2, 7));
assert_ne!(super::raster_mask_form_id(1, 7), super::raster_mask_form_id(1, 8));
let m = |min| ValueMask { raster: "Slope".into(), min: Some(min), max: None, inside: true, feather: 5.0 };
let (mut typed, started) = raster_mask_draft(None, Some(&m(25.0)), "Slope");
assert_eq!((typed.min.as_str(), typed.feather.as_str()), ("25", "5"));
Expand Down
Loading