From af73653bad8acfe360dee2ce31a6abdb659ca67f Mon Sep 17 00:00:00 2001 From: Tanish Manku Date: Thu, 12 Nov 2026 12:37:48 -0700 Subject: [PATCH] =?UTF-8?q?fix(stage):=20draw=20on=20the=20pasteboard,=20w?= =?UTF-8?q?ith=20View=20=E2=80=BA=20Clip=20Content=20Outside=20the=20Stage?= =?UTF-8?q?=20(M1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATTRIBUTION.md | 2 +- assets/icons/anicraft-clip-stage.svg | 11 +++ src/app.rs | 16 +++- src/icons.rs | 8 +- src/menus.rs | 3 + src/panels.rs | 5 ++ src/stage.rs | 130 +++++++++++++++++++++++---- 7 files changed, 155 insertions(+), 20 deletions(-) create mode 100644 assets/icons/anicraft-clip-stage.svg diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 6b35a877..36473082 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -19,7 +19,7 @@ shaders or SDK code). | Hack Regular (via `epaint_default_fonts`) | UI monospace font | Source Foundry Authors; Bitstream (Vera) | | MIT + Bitstream Vera licence, [`licenses/fonts/LICENSE-Hack.txt`](licenses/fonts/LICENSE-Hack.txt) | | Noto Emoji (via `epaint_default_fonts`) | Symbol fallback | Google | | SIL OFL 1.1, [`licenses/fonts/OFL-1.1-NotoEmoji.txt`](licenses/fonts/OFL-1.1-NotoEmoji.txt) | | emoji-icon-font (via `epaint_default_fonts`) | Symbol fallback | John Slegers | | MIT, [`licenses/fonts/LICENSE-emoji-icon-font.txt`](licenses/fonts/LICENSE-emoji-icon-font.txt) | -| `assets/icons/anicraft-*.svg` (25 files: align, distribute, layer, mask and masked layer, width tool, camera, brush, paint brush, eraser, eyedropper, muted sound, magnet, bone, bind, edit multiple frames, insert frame, keyframe and blank keyframe) | Align panel, layer-type, Width tool, Camera, Brush, Paint Brush, Eraser, Eyedropper, Bone and Bind tool icons, the timeline's mute toggle, Snap to Objects, Edit Multiple Frames, the timeline's Insert Frame / Keyframe / Blank Keyframe buttons | AniCraft contributors | Original work, drawn on Lucide's 24-unit grid and stroke style (the muted sound keeps the speaker of Lucide's `volume-2`, ISC, crossed out) | MIT OR Apache-2.0 | +| `assets/icons/anicraft-*.svg` (26 files: align, distribute, layer, mask and masked layer, width tool, camera, brush, paint brush, eraser, eyedropper, muted sound, magnet, bone, bind, edit multiple frames, insert frame, keyframe and blank keyframe, clip to the stage) | Align panel, layer-type, Width tool, Camera, Brush, Paint Brush, Eraser, Eyedropper, Bone and Bind tool icons, the timeline's mute toggle, Snap to Objects, Edit Multiple Frames, the timeline's Insert Frame / Keyframe / Blank Keyframe buttons, Clip Content Outside the Stage | AniCraft contributors | Original work, drawn on Lucide's 24-unit grid and stroke style (the muted sound keeps the speaker of Lucide's `volume-2`, ISC, crossed out) | MIT OR Apache-2.0 | | `crates/svg/fixtures/clips-masks-text.svg` | SVG import test drawing (clip paths, a mask, text) | AniCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/*.svg` except `anicraft-*` (33 files: tools, transport, layer toggles, folders, guide, image, text, sound, chevrons) | UI icons, stroked by `src/icons.rs` | Lucide Icons and Contributors (some derived from Feather by Cole Bemis) | (`icons/.svg`), copied unchanged from the PhotoCraft sibling project's set (`volume-2.svg` from Lucide 0.512.0) | ISC (Lucide) and MIT (Feather), [`licenses/icons/LICENSE-lucide.txt`](licenses/icons/LICENSE-lucide.txt) | | `assets/app-icon/` (`anicraft.svg`, `anicraft-small.svg` and every render: PNGs, `anicraft.ico`, `hicolor/`) | App icon (engraved rabbit): window, Dock, taskbar, `anicraft.exe`, Help › About | Project owner (ArtCraft team) | Original artwork drawn in ArtCraft for AniCraft, vectorised by `assets/app-icon/trace.py` and rendered by `scripts/icons.sh` | MIT OR Apache-2.0 ([`assets/app-icon/LICENSE.txt`](assets/app-icon/LICENSE.txt)) | diff --git a/assets/icons/anicraft-clip-stage.svg b/assets/icons/anicraft-clip-stage.svg new file mode 100644 index 00000000..fe7070f0 --- /dev/null +++ b/assets/icons/anicraft-clip-stage.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/app.rs b/src/app.rs index 6e91d716..84c2fc24 100644 --- a/src/app.rs +++ b/src/app.rs @@ -181,6 +181,9 @@ pub struct StageView { /// The canvas size and zoom of the last fit: while the view is still as fitted, it fits /// again when the canvas changes size (window resized, panels or interface scale changed). pub fitted: Option<(egui::Vec2, f32)>, + /// View › Clip Content Outside the Stage (#251): hide artwork on the pasteboard (it is still + /// there, and still exported only where it is on the stage). Off: it shows, as in Animate. + pub clip_to_stage: bool, } /// In-progress pointer gesture on the stage. @@ -558,7 +561,7 @@ impl App { updates: Default::default(), s: session, tool: Tool::Select, - view: StageView { zoom: 1.0, pan: egui::Vec2::ZERO, fit: true, fitted: None }, + view: StageView { zoom: 1.0, pan: egui::Vec2::ZERO, fit: true, fitted: None, clip_to_stage: false }, stage_cache: stage::Cache::default(), drag: Drag::None, hint_menu: None, @@ -1062,6 +1065,17 @@ impl App { self.view.zoom = 3.0; self.view.pan = egui::vec2(1200.0, 300.0); } + Some("pasteboard") | Some("clipped") => { + // Artwork half off the stage (#245); `clipped`: Clip Content Outside the Stage on + // (#251). Zoomed out so the pasteboard shows around the stage. + let w = self.s.doc.stage.width as f32; + self.run("draw.ellipse", json!({"x": -160.0, "y": 40.0, "w": 260.0, "h": 200.0})); + self.run("draw.rect", json!({"x": w - 80.0, "y": 260.0, "w": 240.0, "h": 120.0})); + self.view.fit = false; + self.view.zoom = 0.55; + self.view.pan = egui::Vec2::ZERO; + self.view.clip_to_stage = which == Some("clipped"); + } Some("prefs") => { self.show_prefs = true; self.scan_system_fonts(); diff --git a/src/icons.rs b/src/icons.rs index 7ae8507b..0b14fe81 100644 --- a/src/icons.rs +++ b/src/icons.rs @@ -74,11 +74,13 @@ pub enum Icon { InsertFrame, InsertKeyframe, InsertBlankKeyframe, + ClipStage, } impl Icon { #[cfg(test)] - pub const ALL: [Icon; 60] = [ + pub const ALL: [Icon; 61] = [ + Icon::ClipStage, Icon::InsertFrame, Icon::InsertKeyframe, Icon::InsertBlankKeyframe, @@ -231,6 +233,9 @@ impl Icon { Icon::InsertFrame => ("anicraft-insert-frame", false), Icon::InsertKeyframe => ("anicraft-insert-keyframe", false), Icon::InsertBlankKeyframe => ("anicraft-insert-blank-keyframe", false), + // The stage with crop marks at its corners (AniCraft original, on Lucide's grid): + // Clip Content Outside the Stage. + Icon::ClipStage => ("anicraft-clip-stage", false), } } } @@ -288,6 +293,7 @@ static FILES: &[(&str, &str)] = &[ ("anicraft-insert-frame", include_str!("../assets/icons/anicraft-insert-frame.svg")), ("anicraft-insert-keyframe", include_str!("../assets/icons/anicraft-insert-keyframe.svg")), ("anicraft-insert-blank-keyframe", include_str!("../assets/icons/anicraft-insert-blank-keyframe.svg")), + ("anicraft-clip-stage", include_str!("../assets/icons/anicraft-clip-stage.svg")), ("anicraft-camera", include_str!("../assets/icons/anicraft-camera.svg")), ("anicraft-bone", include_str!("../assets/icons/anicraft-bone.svg")), ("anicraft-bind", include_str!("../assets/icons/anicraft-bind.svg")), diff --git a/src/menus.rs b/src/menus.rs index 4e720886..b6025163 100644 --- a/src/menus.rs +++ b/src/menus.rs @@ -604,6 +604,9 @@ pub(crate) fn menu_contents(app: &mut App, ui: &mut egui::Ui, top: &str, cmds: & crate::onion::toggle_edit_multiple(app); } ui.menu_button(tr!("Onion Skin Options"), |ui| crate::onion::menu(app, ui)); + if ui_item(ui, tr!("Clip Content Outside the Stage"), "", Some(app.view.clip_to_stage)) { + app.view.clip_to_stage = !app.view.clip_to_stage; + } if ui_item(ui, tr!("Preview Through Camera"), "", Some(app.camera_preview)) { app.toggle_camera_preview(); } diff --git a/src/panels.rs b/src/panels.rs index 612e2395..79d086cf 100644 --- a/src/panels.rs +++ b/src/panels.rs @@ -189,6 +189,11 @@ pub fn edit_bar(app: &mut App, ui: &mut egui::Ui) { }) .response .set_name(tr!("Zoom")); + // Beside the zoom, as in Animate (#251): a toggle, on = the pasteboard is hidden. + let clip = app.view.clip_to_stage; + if crate::timeline::icon_button(ui, tr!("Clip content outside the stage"), Some(clip), Icon::ClipStage).clicked() { + app.view.clip_to_stage = !clip; + } if ui.selectable_label(app.onion.ghosts, tr!("Onion skin")).clicked() { crate::onion::toggle_ghosts(app); } diff --git a/src/stage.rs b/src/stage.rs index 8406102c..525c8f48 100644 --- a/src/stage.rs +++ b/src/stage.rs @@ -138,26 +138,58 @@ struct TileSpec { view: View, } -/// Tiles covering stage-device-pixel rect `[x0, x1) × [y0, y1)`, nearest to `centre` first. -fn tiles_for(z: f32, stage_px: (u32, u32), px: [f32; 4], margin: u32, bg: anicraft_engine::doc::Rgba) -> Vec { +/// Most tiles along one side: a screen's worth and the look-ahead ring, many times over. +const MAX_TILES_PER_AXIS: usize = 64; + +/// The tile spans `(offset, length)` in device pixels along one axis of a stage `len` long that +/// overlap `[lo, hi)`. The stage is cut into `TILE`s from its edge; with `pasteboard`, so is the +/// space before and after it (from the stage's edges outwards), so no tile straddles the edge. +fn spans(len: u32, lo: f32, hi: f32, pasteboard: bool) -> Vec<(i64, u32)> { + let t = i64::from(TILE); + let len = i64::from(len); + let (lo, hi) = (lo.floor().clamp(-1e9, 1e9) as i64, hi.ceil().clamp(-1e9, 1e9) as i64); + let mut out = Vec::new(); + if pasteboard && lo < 0 { + // Before the stage: [-(k+1)T, -kT), nearest the stage first. + let first = (-hi).max(0) / t; + let last = (-lo - 1) / t; + let first = first.max(last - MAX_TILES_PER_AXIS as i64 + 1); + out.extend((first..=last).rev().map(|k| (-(k + 1) * t, TILE))); + } + if hi > 0 && lo < len { + let first = lo.max(0) / t; + let last = (hi.min(len) - 1) / t; + out.extend((first..=last).take(MAX_TILES_PER_AXIS).map(|k| (k * t, (t.min(len - k * t)) as u32))); + } + if pasteboard && hi > len { + // After it: [len + kT, len + (k+1)T). + let first = (lo - len).max(0) / t; + let last = (hi - len - 1) / t; + out.extend((first..=last).take(MAX_TILES_PER_AXIS).map(|k| (len + k * t, TILE))); + } + out +} + +/// Tiles covering stage-device-pixel rect `[x0, x1) × [y0, y1)` (and `margin` tiles around it), +/// nearest to its centre first. The stage's tiles are filled with `bg`; with `pasteboard` the +/// view beyond the stage gets tiles too, transparent over the pasteboard (#245), so artwork +/// there shows while the stage keeps its exact colour and blending. +fn tiles_for(z: f32, stage_px: (u32, u32), px: [f32; 4], margin: u32, bg: anicraft_engine::doc::Rgba, pasteboard: bool) -> Vec { let (sw, sh) = stage_px; if sw == 0 || sh == 0 { return Vec::new(); } - let t = TILE as f32; - let last = (sw.div_ceil(TILE) - 1, sh.div_ceil(TILE) - 1); - let tx0 = ((px[0] / t).floor().max(0.0) as u32).saturating_sub(margin); - let ty0 = ((px[1] / t).floor().max(0.0) as u32).saturating_sub(margin); - let tx1 = (((px[2] - 1.0).max(0.0) / t).floor() as u32 + margin).min(last.0); - let ty1 = (((px[3] - 1.0).max(0.0) / t).floor() as u32 + margin).min(last.1); + let m = (margin * TILE) as f32; + let xs = spans(sw, px[0] - m, px[2] + m, pasteboard); + let ys = spans(sh, px[1] - m, px[3] + m, pasteboard); let (cx, cy) = ((px[0] + px[2]) / 2.0, (px[1] + px[3]) / 2.0); let mut out = Vec::new(); - for ty in ty0..=ty1 { - for tx in tx0..=tx1 { - let (ox, oy) = (tx * TILE, ty * TILE); - let (w, h) = (TILE.min(sw - ox), TILE.min(sh - oy)); - let view = View { transform: Affine::translate(-(ox as f32), -(oy as f32)).mul(&Affine::scale(z, z)), width: w, height: h, background: Some(bg) }; - let rect = [ox as f32 / z, oy as f32 / z, (ox + w) as f32 / z, (oy + h) as f32 / z]; + for &(oy, h) in &ys { + for &(ox, w) in &xs { + let inside = ox >= 0 && oy >= 0 && ox < i64::from(sw) && oy < i64::from(sh); + let (fx, fy) = (ox as f32, oy as f32); + let view = View { transform: Affine::translate(-fx, -fy).mul(&Affine::scale(z, z)), width: w, height: h, background: inside.then_some(bg) }; + let rect = [fx / z, fy / z, (fx + w as f32) / z, (fy + h as f32) / z]; out.push(TileSpec { rect, view }); } } @@ -803,7 +835,10 @@ pub fn show(app: &mut App, ui: &mut egui::Ui) { painter.rect_filled(stage_rect.translate(vec2(3.0, 4.0)), 0.0, T.stage_shadow); // Stage colour first, so a picture still rendering never shows as a hole. painter.rect_filled(stage_rect, 0.0, crate::theme::to_color32(app.s.doc.stage.background)); - let vis = stage_rect.intersect(canvas); + // What gets rendered: the stage, and the pasteboard around it unless View › Clip Content + // Outside the Stage is on (#251). Previewing through the camera shows what the movie shows. + let pasteboard = !app.view.clip_to_stage && !preview; + let vis = if pasteboard { canvas } else { stage_rect.intersect(canvas) }; if vis.width() >= 1.0 && vis.height() >= 1.0 { if app.stage_cache.service.is_none() { let ctx = ui.ctx().clone(); @@ -820,7 +855,7 @@ pub fn show(app: &mut App, ui: &mut egui::Ui) { let bg = app.s.doc.stage.background; let stage_px = ((size.x * z).ceil().max(1.0) as u32, (size.y * z).ceil().max(1.0) as u32); let px = [(vis.min.x - st_origin.x) * ppp, (vis.min.y - st_origin.y) * ppp, (vis.max.x - st_origin.x) * ppp, (vis.max.y - st_origin.y) * ppp]; - let visible = tiles_for(z, stage_px, px, 0, bg); + let visible = tiles_for(z, stage_px, px, 0, bg, pasteboard); let pic = app.s.stage_picture(); let editing = edit_view.is_some(); // Onion skin is off while a symbol is open in place (`stage_skin`, ADR 0045). @@ -899,7 +934,7 @@ pub fn show(app: &mut App, ui: &mut egui::Ui) { ahead.extend(jobs.iter().map(|(t, _)| make_job_for(raster, state, &app.s.doc, start + f, t.view, None).through_camera(preview))); } } else { - ahead.extend(tiles_for(z, stage_px, px, 1, bg).into_iter().filter(|t| !jobs.iter().any(|(v, _)| v.rect == t.rect)).map(|t| make_job_for(raster, state, &pic.doc, pic.frame, t.view, onion).through_camera(preview))); + ahead.extend(tiles_for(z, stage_px, px, 1, bg, pasteboard).into_iter().filter(|t| !jobs.iter().any(|(v, _)| v.rect == t.rect)).map(|t| make_job_for(raster, state, &pic.doc, pic.frame, t.view, onion).through_camera(preview))); } svc.prefetch(ahead); } @@ -1618,3 +1653,64 @@ mod xf_tests { assert_eq!(xf_zone(r, pos2(500.0, 500.0)), None); } } + +#[cfg(test)] +mod pasteboard_tests { + use super::*; + use anicraft_engine::doc::Rgba; + + const BG: Rgba = Rgba { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }; + + /// Every device pixel of `px` is in exactly one tile. + fn covered_once(tiles: &[TileSpec], z: f32, px: [f32; 4]) { + for y in (px[1] as i32..px[3] as i32).step_by(97) { + for x in (px[0] as i32..px[2] as i32).step_by(89) { + let (sx, sy) = ((x as f32 + 0.5) / z, (y as f32 + 0.5) / z); + let n = tiles.iter().filter(|t| sx >= t.rect[0] && sx < t.rect[2] && sy >= t.rect[1] && sy < t.rect[3]).count(); + assert_eq!(n, 1, "pixel ({x}, {y}) is in {n} tiles"); + } + } + } + + #[test] + fn the_pasteboard_gets_transparent_tiles_that_never_cross_the_stage_edge() { + // A 1280 × 720 stage at 100 %, seen with 700 px of pasteboard on each side (#245). + let (z, stage) = (1.0, (1280, 720)); + let view = [-700.0, -300.0, 1980.0, 1020.0]; + let tiles = tiles_for(z, stage, view, 0, BG, true); + covered_once(&tiles, z, view); + for t in &tiles { + let inside = t.rect[0] >= 0.0 && t.rect[1] >= 0.0 && t.rect[2] <= 1280.0 && t.rect[3] <= 720.0; + let outside = t.rect[2] <= 0.0 || t.rect[0] >= 1280.0 || t.rect[3] <= 0.0 || t.rect[1] >= 720.0; + assert!(inside ^ outside, "a tile straddles the stage edge: {:?}", t.rect); + // The stage keeps its colour (and blends against it); the pasteboard is see-through. + assert_eq!(t.view.background.is_some(), inside, "{:?}", t.rect); + assert!(t.view.width <= TILE && t.view.height <= TILE); + } + // The stage's own tiles are the same with or without the pasteboard. + let clipped = tiles_for(z, stage, view, 0, BG, false); + let rects = |ts: &[TileSpec], f: fn(&TileSpec) -> bool| { + let mut v: Vec<[i32; 4]> = ts.iter().filter(|t| f(t)).map(|t| t.rect.map(|c| c as i32)).collect(); + v.sort_unstable(); + v + }; + assert_eq!(rects(&tiles, |t| t.view.background.is_some()), rects(&clipped, |_| true)); + // Clipped (#251), nothing is drawn off the stage. + assert!(clipped.iter().all(|t| t.rect[0] >= 0.0 && t.rect[1] >= 0.0 && t.rect[2] <= 1280.0 && t.rect[3] <= 720.0)); + covered_once(&clipped, z, [0.0, 0.0, 1280.0, 720.0]); + } + + #[test] + fn pasteboard_tiles_work_far_away_zoomed_and_stay_bounded() { + // Zoomed in, looking only at the pasteboard, left of and below the stage. + let tiles = tiles_for(4.0, (2000, 1000), [-5000.0, 1500.0, -3000.0, 2600.0], 1, BG, true); + assert!(!tiles.is_empty()); + assert!(tiles.iter().all(|t| t.view.background.is_none())); + covered_once(&tiles, 4.0, [-5000.0, 1500.0, -3000.0, 2600.0]); + // A wild view (zoomed far out on a huge canvas) still makes a bounded number of tiles. + let n = tiles_for(1.0, (100, 100), [-1e12, -1e12, 1e12, 1e12], 1, BG, true).len(); + assert!(n <= (2 * MAX_TILES_PER_AXIS + 1).pow(2), "{n} tiles"); + // Clipped and looking away from the stage: nothing to draw. + assert!(tiles_for(1.0, (100, 100), [-900.0, -900.0, -10.0, -10.0], 0, BG, false).is_empty()); + } +}