mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-21 09:26:43 +00:00
Move: estimate exactly what moved_target shifts (a group's contents, a mask-only drag leaves them); tighten the sparse test
This commit is contained in:
2 files changed
+46
-10
No files matched your search
@@ -72,16 +72,25 @@ fn shift_bytes(s: &photocraft_raster::Surface) -> u64 {
|
||||
tiles.saturating_mul(t * t).saturating_mul(s.format().bytes_per_pixel() as u64)
|
||||
}
|
||||
|
||||
/// Whether the pixel memory a live drag frame writes for the layers `ids` of `doc` exceeds `limit`:
|
||||
/// what `moved_target` shifts, that is a layer's content and linked mask, or only its mask when
|
||||
/// `mask_target` drags an unlinked mask alone.
|
||||
/// Whether the pixel memory a live drag frame writes for the move targets `ids` of `doc` exceeds
|
||||
/// `limit`, counting what `moved_target` shifts: a target whose unlinked mask is dragged alone
|
||||
/// (`mask_target`) copies that mask only and its descendants stay put; any other target shifts its
|
||||
/// content and linked mask, and every layer inside it likewise (a group's contents move whole).
|
||||
fn exceeds(doc: &Document, ids: &[LayerId], limit: u64, mask_target: bool) -> bool {
|
||||
let mut total = 0u64;
|
||||
for l in ids.iter().filter_map(|id| doc.layer(*id)) {
|
||||
let mask_alone = mask_target && photocraft_engine::layer_multi_cmds::moves_mask_alone(l);
|
||||
let content = if mask_alone { 0 } else { l.surface().map_or(0, shift_bytes) };
|
||||
let mask = l.mask.as_ref().filter(|m| mask_alone || m.linked).map_or(0, |m| shift_bytes(&m.surface));
|
||||
total = total.saturating_add(content).saturating_add(mask);
|
||||
for id in ids {
|
||||
let Some(root) = doc.layer(*id) else { continue };
|
||||
let moving = if mask_target && photocraft_engine::layer_multi_cmds::moves_mask_alone(root) {
|
||||
total = total.saturating_add(root.mask.as_ref().map_or(0, |m| shift_bytes(&m.surface)));
|
||||
Vec::new()
|
||||
} else {
|
||||
with_descendants(doc, &[*id])
|
||||
};
|
||||
for l in moving.iter().filter_map(|d| doc.layer(*d)) {
|
||||
let content = l.surface().map_or(0, shift_bytes);
|
||||
let mask = l.mask.as_ref().filter(|m| m.linked).map_or(0, |m| shift_bytes(&m.surface));
|
||||
total = total.saturating_add(content).saturating_add(mask);
|
||||
}
|
||||
if total > limit {
|
||||
return true;
|
||||
}
|
||||
@@ -180,7 +189,7 @@ pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc<Do
|
||||
let bounds = ids.iter().try_fold(Rect::EMPTY, |acc, id| Some(acc.union(&photocraft_compose::change_bounds(doc.layer(*id)?, all)?)));
|
||||
let canvas = doc.bounds();
|
||||
let moving = with_descendants(&doc, &ids);
|
||||
let too_big = exceeds(&doc, &moving, LIVE_LIMIT, mask_target);
|
||||
let too_big = exceeds(&doc, &ids, LIVE_LIMIT, mask_target);
|
||||
let bounds = bounds.map(|b| moving.iter().filter_map(|id| shape_reach(doc.layer(*id)?, canvas)).fold(b, |acc, r| acc.union(&r)));
|
||||
app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, moving, bounds, canvas, offsets: Vec::new(), shown: None, floating: false, too_big });
|
||||
}
|
||||
|
||||
@@ -251,7 +251,7 @@ fn the_estimate_counts_reachable_target_tiles_and_what_moved_target_copies() {
|
||||
})
|
||||
.unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(super::exceeds(&st.doc, &[id], super::LIVE_LIMIT / 4, false), "isolated tiles each write up to four");
|
||||
assert!(super::exceeds(&st.doc, &[id], super::LIVE_LIMIT, false), "isolated tiles each write up to four");
|
||||
// Only the mask is copied when an unlinked mask is dragged alone: the huge content isn't charged.
|
||||
app.session.execute("layer.layerMask.revealAll", json!({})).unwrap();
|
||||
app.session.execute("layer.layerMask.linked", json!({"linked": false})).unwrap();
|
||||
@@ -260,6 +260,33 @@ fn the_estimate_counts_reachable_target_tiles_and_what_moved_target_copies() {
|
||||
assert!(!super::exceeds(&st.doc, &[id], super::LIVE_LIMIT / 4, true), "dragging the unlinked mask alone copies the mask only");
|
||||
}
|
||||
|
||||
/// A group moves its contents whole; a group's unlinked mask dragged alone leaves them where they are.
|
||||
#[test]
|
||||
fn the_estimate_follows_a_group_into_its_contents() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap();
|
||||
app.session.execute("layer.new.layer", json!({})).unwrap();
|
||||
let inner = app.session.active().unwrap().active_layer.unwrap();
|
||||
app.session
|
||||
.edit("paint", |doc, a| {
|
||||
let surface = doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap();
|
||||
let tile = surface.solid_tile(&[1.0, 0.0, 0.0, 1.0]);
|
||||
surface.put_tiles((0..600).map(|i| (photocraft_geom::TileCoord::new(i * 10, i * 10), tile.clone())));
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let group = app.session.execute("layer.new.group", json!({})).unwrap()["layer"].as_u64().unwrap();
|
||||
app.session.execute("layer.moveTo", json!({"layer": inner.0, "target": group, "position": "into"})).unwrap();
|
||||
let group = photocraft_doc::LayerId(group);
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(super::exceeds(&st.doc, &[group], super::LIVE_LIMIT, false), "the group's contents are shifted with it");
|
||||
app.session.execute("layer.layerMask.revealAll", json!({"layer": group.0})).unwrap();
|
||||
app.session.execute("layer.layerMask.linked", json!({"layer": group.0, "linked": false})).unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
assert!(st.doc.layer(group).is_some_and(|l| l.mask.as_ref().is_some_and(|m| !m.linked)), "the group has an unlinked mask");
|
||||
assert!(!super::exceeds(&st.doc, &[group], super::LIVE_LIMIT, true), "an unlinked group mask dragged alone leaves the contents");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounding_box_preference_turns_the_live_drag_off() {
|
||||
assert!(live(false), "by default the layer follows the pointer");
|
||||
|
||||
Reference in new issue
Block a user