//! AniCraft engine (layer L3): the session, undo history and the command registry. //! //! Every user-visible action is a [`CommandSpec`]. The egui shell, the CLI, the JSON-lines //! control channel and future MCP server all call [`Session::execute`] with a command id and //! JSON params, so headless and interactive behaviour cannot drift apart. #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod anim; mod cmds_actions; mod cmds_bone; mod cmds_warp; pub mod cmds_brush; mod cmds_camera; mod parenting; mod cmds_draw; pub mod cmds_filter; pub mod cmds_ease; pub mod cmds_envelope; pub mod cmds_drop; pub mod cmds_erase; mod cmds_file; mod cmds_frames; mod cmds_group; mod cmds_modify; #[cfg(test)] mod origin_tests; #[cfg(test)] mod pivot_tests; mod cmds_motion; mod cmds_retime; mod cmds_hints; mod cmds_layer; pub mod cmds_library; pub mod cmds_macro; pub mod cmds_paint; mod cmds_path; pub mod cmds_raw; pub mod cmds_rawsub; mod cmds_rawpoints; pub mod brush_library; pub mod filter_library; pub mod cmds_recent; pub mod cmds_desktop; pub mod cmds_template; mod cmds_recovery; mod cmds_script; pub mod cmds_plugin; mod cmds_scene; pub mod cmds_sound; pub mod cmds_audio_keys; pub mod cmds_text; mod cmds_timeline; mod cmds_view; mod cmds_width; mod cmds_stroke; pub mod cmds_shape; pub mod cmds_swatch; pub mod confirm; pub mod editing; pub mod explorer; pub mod find; pub mod format; pub mod history; mod merge; pub mod multi; pub mod onion; pub mod recovery; pub mod render_service; pub mod selection; pub mod snap; pub mod transfer; pub mod params; pub mod plugin_export; pub mod player; pub mod publish; pub mod vfs; pub mod video; mod cmds_video; use anicraft_doc::{DisplayList, Document, Element, ElementId, ElementKind, Keyframe, Layer, LayerId, LayerKind, evaluate_scene}; #[cfg(test)] use anicraft_doc::evaluate_layer_elements; use anicraft_geom::{Affine, Rect, Vec2}; use serde_json::{Value, json}; use history::History; use std::cell::RefCell; use std::path::PathBuf; use std::sync::Arc; pub use anicraft_doc as doc; pub use anicraft_plugins as plugins; pub use anicraft_text as text; pub use anicraft_geom as geom; pub use anicraft_render as render; /// Whether a path is an Animate document (`.fla`, `.xfl`, or an XFL folder) that `file.open` /// imports. pub use cmds_file::is_animate; pub use cmds_modify::{Item as SelectedItem, transform_origins}; #[derive(Debug, thiserror::Error)] pub enum EngineError { #[error("unknown command '{0}'")] UnknownCommand(String), #[error("{0}")] Param(String), #[error("{0}")] State(String), #[error("file format: {0}")] Format(String), #[error("I/O: {0}")] Io(#[from] std::io::Error), #[error("render: {0}")] Render(#[from] anicraft_render::RenderError), #[error("{0}")] NeedsConfirmation(String), /// A running publish was cancelled (ADR 0060). #[error("cancelled")] Cancelled, } pub type Result = std::result::Result; /// Signature of a command implementation. pub type RunFn = fn(&mut Session, &Value) -> Result; /// Description of one command. #[derive(Clone, Copy)] pub struct CommandSpec { /// Stable id, e.g. `timeline.insert_keyframe`. pub id: &'static str, pub label: &'static str, /// Menu path, e.g. `Insert/Timeline`. Empty = not in a menu. pub menu: &'static str, pub shortcut: Option<&'static str>, /// Human-readable params description. pub params: &'static str, /// Whether it changes the document (and therefore records an undo step). pub mutates: bool, pub run: RunFn, } impl CommandSpec { pub fn to_json(&self) -> Value { json!({ "id": self.id, "label": self.label, "menu": self.menu, "shortcut": self.shortcut, "params": self.params, "mutates": self.mutates }) } } /// All commands, in menu order. pub fn commands() -> Vec { let mut v = Vec::new(); v.extend(cmds_file::specs()); v.extend(plugin_export::specs()); v.extend(cmds_envelope::specs()); v.extend(publish::specs()); v.extend(cmds_recent::specs()); v.extend(cmds_desktop::specs()); v.extend(cmds_drop::specs()); v.extend(cmds_recovery::specs()); v.extend(cmds_timeline::specs()); v.extend(cmds_layer::specs()); v.extend(cmds_camera::specs()); v.extend(parenting::specs()); v.extend(cmds_bone::specs()); v.extend(cmds_warp::specs()); v.extend(cmds_motion::specs()); v.extend(cmds_retime::specs()); v.extend(cmds_ease::specs()); v.extend(cmds_hints::specs()); v.extend(cmds_scene::specs()); v.extend(cmds_frames::specs()); v.extend(onion::specs()); v.extend(multi::specs()); v.extend(cmds_draw::specs()); v.extend(cmds_view::specs()); v.extend(cmds_group::specs()); v.extend(cmds_library::specs()); v.extend(merge::specs()); v.extend(cmds_modify::specs()); v.extend(cmds_path::specs()); v.extend(cmds_raw::specs()); v.extend(cmds_rawsub::specs()); v.extend(cmds_rawpoints::specs()); v.extend(editing::specs()); v.extend(find::specs()); v.extend(explorer::specs()); v.extend(cmds_brush::specs()); v.extend(cmds_filter::specs()); v.extend(cmds_erase::specs()); v.extend(cmds_paint::specs()); v.extend(cmds_swatch::specs()); v.extend(cmds_text::specs()); v.extend(cmds_width::specs()); v.extend(cmds_stroke::specs()); v.extend(cmds_shape::specs()); v.extend(cmds_plugin::specs()); v.extend(cmds_sound::specs()); v.extend(cmds_audio_keys::specs()); v.extend(cmds_video::specs()); v.extend(cmds_actions::specs()); v.extend(cmds_script::specs()); v.extend(cmds_macro::specs()); v } /// Second keys for commands, `(keys, command id)`: Animate's where the primary key is the /// platform's, and keys that differ by keyboard (a Mac's Delete key sends Backspace). pub const SHORTCUT_ALIASES: &[(&str, &str)] = &[ ("Backspace", "edit.delete"), ("Shift+,", "timeline.first_frame"), ("Shift+.", "timeline.last_frame"), // Snap to Objects is Animate's Ctrl+Shift+/ (ADR 0045). Where / and ? share a key (US, UK), // the key arrives as ?, so that spelling is bound too. ("Ctrl+Shift+?", "draw.snap_to_objects"), ]; pub fn find_command(id: &str) -> Option { // Built once: every command run looks itself up, and a saved command checks each of its // steps (ADR 0097), so this must not rebuild the registry per call. static INDEX: std::sync::OnceLock> = std::sync::OnceLock::new(); INDEX .get_or_init(|| { let mut m = std::collections::HashMap::new(); for c in commands() { // The first of a repeated id wins, as a search in registry order finds it. m.entry(c.id).or_insert(c); } m }) .get(id) .copied() } pub use history::{Recorded, Snapshot, StateId}; pub use selection::Selection; pub use snap::{Accuracy, SnapTo, ViewAids}; pub use cmds_layer::LAYER_COLORS; /// The command namespaces that change a timeline's layers and frames, refused while a group is /// open for editing (ADR 0073): a group is one layer and one frame. A name ending in `.` is a /// namespace, any other one command. const GROUP_REFUSES: &[&str] = &["timeline.", "layer.", "camera.", "bone.", "motion.", "tween.", "frames.", "sound.place", "sound.to_keyframes", "sound.lip_sync", "script.set", "script.clear"]; /// Last addressable frame (0-based); about 70 minutes at 24 fps. pub const MAX_FRAME: u32 = 100_000; /// The index of the topmost item of `dl` under `p` (stage space) that `ok` accepts (given its /// index). A bounding-box test rejects almost every item before any curve is flattened; /// masked-away parts cannot be picked. pub(crate) fn pick_index(dl: &DisplayList, p: Vec2, slop: f32, ok: impl Fn(usize, &anicraft_doc::DisplayItem) -> bool) -> Option { for (i, item) in dl.items.iter().enumerate().rev() { let hw = item.stroke.as_ref().map_or(0.0, |s| s.reach() * item.transform.mean_scale()); let bounds = item.path.bounds().transformed(&item.transform).inflate(hw + slop); // Nothing to hit: an item with no paint (a reference video's placeholder, ADR 0116). if item.fill.is_none() && item.stroke.is_none() { continue; } if !bounds.contains(p) || !ok(i, item) { continue; } if item.clip.is_some_and(|c| !dl.clip_contains(c, p)) { continue; } let inside = item.fill.is_some() && item.path.contains(p, &item.transform, item.fill_rule); if inside || item.path.distance_to_edge(p, &item.transform) <= hw + slop { return Some(i); } } None } /// Drawing defaults used by the drawing commands when params omit colours. #[derive(Clone, Debug)] pub struct ToolStyle { pub fill: anicraft_doc::Rgba, /// The fill swatch holds a gradient or a bitmap (set in the Colour panel or picked with the /// Eyedropper, ADR 0034 and 0074), in stage space; `fill` is then its representative colour. /// `None`: the solid `fill`. pub fill_paint: Option, /// Lock Fill (ADR 0074): a gradient or bitmap from the swatch is poured where it lies on the /// stage, so it runs on across shapes. Off, each shape gets it fitted to its own bounds. pub lock_fill: bool, pub stroke: anicraft_doc::Rgba, pub stroke_width: f32, pub use_fill: bool, pub use_stroke: bool, /// The width profile new strokes get (ADR 0018). pub stroke_profile: anicraft_doc::ProfilePreset, /// A picked width profile that is none of the presets (it wins over `stroke_profile`). pub custom_profile: Option, /// The caps, joins and dashes new strokes get (ADR 0046). pub line: anicraft_doc::StrokeStyle, /// Object Drawing: new shapes are drawing objects. Off, they merge with the raw shapes they /// touch (ADR 0031). On by default, so a script's draw command makes one element; the editor /// starts with it off, as Animate does. pub object_drawing: bool, /// Snap to Objects (Animate's magnet, ADR 0042): dragged points and selections snap to /// points, lines and fill edges nearby. On by default; off, the `snap` params do nothing. pub snap_objects: bool, /// What several selected objects turn, scale and skew about with Free Transform and /// Modify › Transform (ADR 0109). The whole selection's point by default, as in Animate. pub transform_origin: TransformOrigin, } /// What several selected objects are transformed about (ADR 0109, issue #296). #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum TransformOrigin { /// One point for the whole selection: the objects turn and scale as one arrangement. #[default] Selection, /// Each object about its own point: a symbol instance's or a group's registration point, /// and the centre of anything else (a shape, a drawing, a bitmap, text). Registration, /// Each object about the centre of its own box, wherever its registration point is. Center, } impl TransformOrigin { pub const ALL: [TransformOrigin; 3] = [TransformOrigin::Selection, TransformOrigin::Registration, TransformOrigin::Center]; pub fn key(self) -> &'static str { match self { TransformOrigin::Selection => "selection", TransformOrigin::Registration => "registration", TransformOrigin::Center => "center", } } pub fn from_key(key: &str) -> Option { TransformOrigin::ALL.into_iter().find(|m| m.key() == key) } /// The mode after this one (the cycling shortcut). pub fn next(self) -> TransformOrigin { match self { TransformOrigin::Selection => TransformOrigin::Registration, TransformOrigin::Registration => TransformOrigin::Center, TransformOrigin::Center => TransformOrigin::Selection, } } } impl Default for ToolStyle { fn default() -> Self { ToolStyle { fill: anicraft_doc::Rgba::hex(0x3d8bfd), fill_paint: None, lock_fill: false, stroke: anicraft_doc::Rgba::hex(0x1b2a4a), stroke_width: 2.0, use_fill: true, use_stroke: true, stroke_profile: anicraft_doc::ProfilePreset::Uniform, custom_profile: None, line: anicraft_doc::StrokeStyle::default(), object_drawing: true, snap_objects: true, transform_origin: TransformOrigin::Selection } } } impl ToolStyle { /// The fill swatch's paint, in stage space. pub fn fill_paint(&self) -> anicraft_doc::Paint { self.fill_paint.clone().unwrap_or(anicraft_doc::Paint::Solid(self.fill)) } /// The fill swatch's paint for a shape whose space is `to_local` from the stage and whose /// outline has the local `bounds`: with Lock Fill, the stage-space paint carried into the /// shape; without, a gradient or bitmap fitted to the bounds (as Animate pours them). pub fn fill_paint_for(&self, to_local: &anicraft_geom::Affine, bounds: &anicraft_geom::Rect) -> anicraft_doc::Paint { let paint = self.fill_paint(); if self.lock_fill || matches!(paint, anicraft_doc::Paint::Solid(_)) { paint.transformed(to_local) } else { paint.fitted(bounds) } } /// Sets the fill swatch to a solid colour (dropping a picked gradient or bitmap). pub fn set_fill_color(&mut self, c: anicraft_doc::Rgba) { self.fill = c; self.fill_paint = None; } /// The width profile new strokes get. pub fn profile(&self) -> Option { self.custom_profile.clone().or_else(|| self.stroke_profile.profile()) } /// A new stroke of `color` and `width` with the current profile and line style. pub fn new_stroke(&self, color: anicraft_doc::Rgba, width: f32) -> anicraft_doc::Stroke { anicraft_doc::Stroke::new(color, width).with_profile(self.profile()).with_style(self.line.clone()) } /// Sets the profile to a preset (dropping a picked custom one). pub fn set_profile(&mut self, p: anicraft_doc::ProfilePreset) { self.stroke_profile = p; self.custom_profile = None; } } /// An open document plus everything the editor tracks about it. /// A display list and the `(state, scene, frame, open symbol)` it was evaluated for. type CachedList = (StateId, usize, u32, Option<(editing::Opened, Option<(LayerId, ElementId)>, usize)>, Arc); pub struct Session { /// The document: a persistent tree, so cloning it (undo, render thread) is cheap. pub doc: Document, /// The scene being edited (index into `doc.scenes`). pub scene: usize, /// Symbols open for editing, outermost first (ADR 0045). While one is open, commands work on /// its timeline: `frame`, `layer` and `selection` are its. pub editing: Vec, /// Playhead within the scene (0-based). pub frame: u32, /// Active layer. pub layer: LayerId, /// Selected elements at the playhead, each with its layer (ADR 0044). pub selection: Selection, /// Bumped on every document change, undo and redo (UI invalidation). pub revision: u64, /// Identity of the current document content (equal ids ⇒ identical content). pub state: StateId, saved_state: Option, pub path: Option, pub style: ToolStyle, /// Whether the rulers, grid and guides show and snap (ADR 0076): the editor's, not the /// document's. pub aids: ViewAids, /// The style new text gets (the last one used). pub text_style: cmds_text::TextStyle, history: History, /// Open gesture (e.g. a drag): commands inside it coalesce into one undo step. gesture: Option, /// Display list of `(state, scene, frame)`, shared by hit testing, bounds and overlays. dl_cache: RefCell>, /// Copied elements (and the symbols they use), outside the document so undo leaves it alone. pub clipboard: cmds_modify::Clipboard, /// Copied frames (Copy Frames), separate from copied artwork. pub frame_clipboard: cmds_frames::FrameClipboard, /// A copied ease (Copy Ease, ADR 0081), for Paste Ease. pub ease_clipboard: Option, /// A command is editing a motion tween's target (auto-key, ADR 0016). motion_edit: Option, /// Layer parenting (ADR 0113): the layer spaces the running command started with, the /// keyframes it was handed in stage space, and the layers whose parent it set itself. rig_cmd: Option, rig_edits: Vec, pub(crate) rig_handled: Vec, /// Installed WebAssembly plug-ins (ADR 0017). They belong to the session, not the /// document: they survive New and Open. pub plugins: anicraft_plugins::Registry, /// A name for the undo step the running command records, set by the command /// (`plugin.run` names its step after the plug-in). pub(crate) step_label: Option, /// A marquee's cut that nothing has used yet (ADR 0041). provisional: Option, /// Fills and strokes turned off in this session, for turning them on again (#292, #393). pub(crate) paint_memory: PaintMemory, /// Raw shapes a paste left floating (`edit.paste {float: true}`, as the editor pastes). pub(crate) floating: Option, /// Set by `raw.select_rect` when it cut: each piece and the shape it was cut from, and the /// pieces inside the marquee. pub(crate) pending_cut: Option<(CutOrigin, Vec)>, /// The open gesture began on a provisional cut: the document with the cut. gesture_cut: Option, /// How a button open for editing shows its hit area over its other states (ADR 0057): the /// tint and opacity, or `None` for no overlay (the default; the app sets it from its theme). /// View state, like the playhead: never in the document or its undo history. pub hit_area: Option<(anicraft_doc::Rgba, f32)>, /// The last picture of an open symbol in place, by its key (ADR 0045). picture_cache: RefCell)>>, /// A publish this session runs: its Cancel and frame count, which exports check (ADR 0060). pub job: Option>, /// An import the editor prepared on another thread (ADR 0116): `file.import_video` asked /// for the same thing takes it instead of decoding again. Never in the document. pub staged_video: Option, /// Files this session's publishes wrote: publishing again replaces them without asking. pub published: std::collections::BTreeSet, /// The files opened and saved last, and pinned favourites (ADR 0059). They belong to the /// session, not the document: they survive New and Open. The desktop app keeps them in its /// preferences. pub recent: cmds_recent::RecentFiles, /// The user's own brush presets (ADR 0068). Like recent files they belong to the session /// and survive New and Open; the desktop app keeps them in its preferences. pub brushes: brush_library::BrushLibrary, /// The user's filter presets (ADR 0091). Like brushes they belong to the session and /// survive New and Open; the desktop app keeps them in its preferences. pub filter_presets: filter_library::FilterLibrary, /// Copied filters (Copy Filters, ADR 0091), for Paste Filters. pub filter_clipboard: Option>, /// The open envelope (ADR 0080): view state over the shapes it bends, like a gesture. pub(crate) envelope: Option, /// The commands run since the last undo step was recorded, which the next step keeps so it /// can be saved as a command (ADR 0097). rec: history::Recording, /// How many commands were recorded when the open gesture began (a revert goes back there). gesture_rec: usize, /// What the running command records instead of itself (`macro.run` records its steps). pub(crate) record_as: Option>, /// Which open document this is (ADR 0112): a number no other document in this process has, /// new with every New and Open. A copy remembers it, so a paste knows whether the artwork /// came from this document or another. pub origin: u64, /// Symbols pastes brought in from other documents, so the same one isn't brought twice. pub(crate) brought: Vec, } /// Each piece of a marquee's cut, with the shape it was cut from. pub(crate) type CutOrigin = Vec<(ElementId, ElementId)>; /// The fill and the stroke each shape had when it was turned off (the Fill and Stroke /// checkboxes in Properties), so turning it on again brings back that colour, gradient or line /// rather than a default (#292). A convenience of the session: it is not saved or undone. #[derive(Default)] pub(crate) struct PaintMemory { pub(crate) fills: std::collections::HashMap, pub(crate) strokes: std::collections::HashMap, } impl PaintMemory { /// Bounded: past this many shapes it starts over. const MAX: usize = 10_000; pub(crate) fn remember_fill(&mut self, id: ElementId, fill: anicraft_doc::Paint) { if self.fills.len() >= Self::MAX { self.fills.clear(); } self.fills.insert(id, fill); } pub(crate) fn remember_stroke(&mut self, id: ElementId, stroke: anicraft_doc::Stroke) { if self.strokes.len() >= Self::MAX { self.strokes.clear(); } self.strokes.insert(id, stroke); } } /// Raw shapes a paste put on top of the drawing without merging them (#391): each with its /// layer, and the frame they were pasted on. They merge when none of them is selected any more. #[derive(Clone, Debug, PartialEq)] pub struct Floating { pub(crate) items: Vec<(LayerId, ElementId)>, pub(crate) frame: u32, } /// A marquee's cut that nothing has used yet (ADR 0041). In Animate a marquee only selects: the /// drawing is cut where it crosses once the selection is moved or changed. Here the cut happens /// at once (the parts must be elements to be selected) and records no undo step. It is taken /// back as soon as the selection lets go of its pieces with nothing else changed; the first edit /// that uses it records one step from before the cut. struct Provisional { /// Before the cut. before: history::Snapshot, /// The document with the cut, and its state. cut: Document, state: history::StateId, /// Each piece and the shape it was cut from. origin: Vec<(ElementId, ElementId)>, /// The pieces inside the marquee(s): while one of them is selected, the cut is in use. inside: Vec, } impl Default for Session { fn default() -> Self { Session::new(Document::default()) } } impl Session { pub fn new(mut doc: Document) -> Session { doc.sanitize(); // Every scene has at least one layer to draw on. for i in 0..doc.scenes.len() { if doc.timeline(i).layers.is_empty() { let id = LayerId(doc.alloc_id()); doc.timeline_mut(i).layers.push(Arc::new(Layer::new(id, "Layer 1"))); } } let layer = doc.timeline(0).layers.first().map_or(LayerId(0), |l| l.id); let state = history::fresh_state(); Session { doc, scene: 0, editing: Vec::new(), frame: 0, layer, selection: Selection::default(), revision: 1, state, saved_state: Some(state), path: None, style: ToolStyle::default(), aids: ViewAids::default(), text_style: cmds_text::TextStyle::default(), history: History::default(), gesture: None, dl_cache: RefCell::new(None), clipboard: cmds_modify::Clipboard::default(), frame_clipboard: cmds_frames::FrameClipboard::default(), ease_clipboard: None, motion_edit: None, rig_cmd: None, rig_edits: Vec::new(), rig_handled: Vec::new(), plugins: anicraft_plugins::Registry::default(), step_label: None, provisional: None, paint_memory: PaintMemory::default(), floating: None, pending_cut: None, gesture_cut: None, hit_area: None, picture_cache: RefCell::new(None), job: None, staged_video: None, published: std::collections::BTreeSet::new(), recent: cmds_recent::RecentFiles::default(), brushes: brush_library::BrushLibrary::default(), filter_presets: filter_library::FilterLibrary::default(), filter_clipboard: None, envelope: None, rec: history::Recording::default(), gesture_rec: 0, record_as: None, origin: transfer::fresh_origin(), brought: Vec::new(), } } /// Whether the document differs from the last saved (or opened) state. Undoing back to /// the saved state makes it clean again. pub fn is_dirty(&self) -> bool { self.saved_state != Some(self.state) } /// Marks the current state as saved. pub fn mark_saved(&mut self) { self.saved_state = Some(self.state); } /// Marks the document as not saved anywhere yet (a restored autosave), so the close guard /// and the title treat it as unsaved work. pub fn mark_unsaved(&mut self) { self.saved_state = None; } /// The confirmation running `id` with `params` needs right now (ADR 0004). pub fn confirmation(&self, id: &str, params: &Value) -> Option { confirm::confirmation(self, id, params) } pub fn history(&self) -> &History { &self.history } /// Sets the undo memory budget (bytes); older steps are evicted beyond it. pub fn set_history_budget(&mut self, bytes: usize) { self.history.budget_bytes = bytes; } /// Starts coalescing: every mutating command until [`Session::end_gesture`] becomes a single /// undo step labelled `label` (used for drags, so a move is one step, not sixty). pub fn begin_gesture(&mut self, label: &str) { if self.gesture.is_some() { return; } // On a marquee's cut, the step (if the gesture makes one) starts from before the cut. let cut = self.live_cut().map(|p| (history::Snapshot { label: label.to_string(), ..p.before.clone() }, p.cut.clone())); self.gesture_rec = self.rec.cmds.len(); match cut { Some((before, doc)) => { self.gesture_cut = Some(doc); self.gesture = Some(before); } None => self.gesture = Some(self.snapshot(label)), } } /// Ends the open gesture, recording one undo step if the document changed. pub fn end_gesture(&mut self) { let cut = self.gesture_cut.take(); let Some(before) = self.gesture.take() else { return }; if let Some(cut) = &cut { if self.doc == *cut { // Nothing used the marquee's cut: it stays provisional. if let Some(p) = &self.provisional { self.state = p.state; } return; } self.provisional = None; } self.record(before); } /// Records `before` as the undo step to the current document, with the commands run since /// the last step; an unchanged document records nothing and forgets them. fn record(&mut self, mut before: Snapshot) { if self.doc != before.doc { before.commands = self.rec.take(); self.history.record(before, &self.doc); } else { self.rec.clear(); } } pub fn in_gesture(&self) -> bool { self.gesture.is_some() } /// Replaces the document (new/open), clearing history. pub fn replace_document(&mut self, doc: Document, path: Option) { let style = std::mem::take(&mut self.style); let rev = self.revision; let budget = self.history.budget_bytes; let clipboard = std::mem::take(&mut self.clipboard); let plugins = std::mem::take(&mut self.plugins); let recent = std::mem::take(&mut self.recent); let brushes = std::mem::take(&mut self.brushes); let filter_presets = std::mem::take(&mut self.filter_presets); let filter_clipboard = self.filter_clipboard.take(); *self = Session::new(doc); self.recent = recent; self.brushes = brushes; self.filter_presets = filter_presets; // Like copied artwork, copied filters carry across to the next document. self.filter_clipboard = filter_clipboard; // The clipboard survives New/Open, so artwork can move between documents. self.clipboard = clipboard; self.plugins = plugins; self.style = style; self.revision = rev + 1; self.path = path; self.history.budget_bytes = budget; } /// Drops what the session keeps only to draw faster (its display list and the picture of an /// open symbol in place): a document put aside while another is shown keeps none (ADR 0112). pub fn drop_caches(&self) { self.dl_cache.replace(None); self.picture_cache.replace(None); } /// Takes over from `from` what belongs to the editor rather than to one document (ADR /// 0112): the clipboards, the tool style, the plug-ins, recent files, brushes and filter /// presets, and how the rulers, grid and guides show. An editor with several documents open /// calls it when it turns to another one, so a copy made in one pastes into the next and the /// tools stay as they were. The documents, their undo histories, selections and playheads /// stay where they are; `from` is left with this session's (usually empty) share. pub fn take_shared(&mut self, from: &mut Session) { use std::mem::swap; swap(&mut self.style, &mut from.style); swap(&mut self.aids, &mut from.aids); swap(&mut self.text_style, &mut from.text_style); swap(&mut self.clipboard, &mut from.clipboard); swap(&mut self.frame_clipboard, &mut from.frame_clipboard); swap(&mut self.ease_clipboard, &mut from.ease_clipboard); swap(&mut self.filter_clipboard, &mut from.filter_clipboard); swap(&mut self.plugins, &mut from.plugins); swap(&mut self.recent, &mut from.recent); swap(&mut self.brushes, &mut from.brushes); swap(&mut self.filter_presets, &mut from.filter_presets); self.hit_area = from.hit_area; self.history.budget_bytes = from.history.budget_bytes; } /// Runs a command by id. Mutating commands are atomic: on error the document is restored. pub fn execute(&mut self, id: &str, params: &Value) -> Result { let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; // Irreversible loss (unsaved work, files on disk) needs explicit consent, whoever asks. if let Some(c) = self.confirmation(id, params) && c.irreversible && !confirm::confirmed(params) { return Err(EngineError::NeedsConfirmation(format!("{} {} (pass \"confirm\": true to proceed)", c.title, c.message))); } self.group_allows(&spec)?; self.settle_cut(id, params); if !spec.mutates { let r = (spec.run)(self, params); self.settle_cut(id, params); self.hold_group_frame(); self.settle_floating(); return r; } // An edit on a marquee's cut records its step from before the cut (ADR 0041). let cut = self.live_cut().map(|p| (history::Snapshot { label: spec.label.to_string(), ..p.before.clone() }, p.cut.clone())); let rollback = cut.as_ref().map(|_| self.snapshot(spec.label)); let mut before = match &cut { Some((b, _)) => b.clone(), None => self.snapshot(spec.label), }; // A command must never observe a display list of the state it is changing. self.dl_cache.replace(None); self.pending_cut = None; self.motion_edit = None; self.step_label = None; self.record_as = None; // Layers that follow a parent layer are edited in stage space (ADR 0113). let rig = self.rig_begin(); let result = (spec.run)(self, params); let record_as = self.record_as.take(); self.settle_rig(); // Edits to a motion tween's target become property keys (auto-key). if let Some(edit) = self.motion_edit.take() && result.is_ok() { cmds_motion::finish_edit(self, edit); } self.rig_end(rig); self.dl_cache.replace(None); if let Some(label) = self.step_label.take() { before.label = label; } let pending = self.pending_cut.take(); match result { Ok(v) => { let changed = match &cut { Some((_, doc)) => self.doc != *doc, // O(changed): unchanged subtrees compare by pointer. None => self.doc != before.doc, }; if changed { self.state = history::fresh_state(); self.revision += 1; match record_as { Some(steps) => steps.into_iter().for_each(|r| self.rec.push(r)), None => self.rec.push(Recorded::new(id, params)), } match pending.filter(|_| self.gesture.is_none()) { // A marquee's cut: provisional, no step (yet). Some((origin, inside)) => self.begin_cut(before, cut.is_some(), origin, inside), None => { if cut.is_some() { self.provisional = None; } if self.gesture.is_none() { self.record(before); } } } } self.fix_selection(); self.hold_group_frame(); self.settle_floating(); Ok(v) } Err(e) => { // Back to the cut, which stays provisional, or to before the command. self.restore(rollback.unwrap_or(before)); Err(e) } } } /// Merges floating pasted shapes into the drawing once the selection has let go of them /// (#391; in Animate pasted raw artwork stays apart until it is deselected). The merge is /// part of the step around it: undoing the paste brings back the drawing from before it, /// and undoing a command that deselected them brings them back floating and selected. fn settle_floating(&mut self) { let Some(f) = self.floating.as_ref() else { return }; if self.gesture.is_some() || f.items.iter().any(|&(layer, id)| self.selection.contains(layer, id)) { return; } let Some(f) = self.floating.take() else { return }; let (doc, frame, layer, selection) = (self.doc.clone(), self.frame, self.layer, self.selection.clone()); self.frame = f.frame; let mut layers: Vec = Vec::new(); for &(l, _) in &f.items { if !layers.contains(&l) && self.timeline().layer(l).is_some() { layers.push(l); } } let mut ok = true; for l in layers { let ids: Vec = f.items.iter().filter(|i| i.0 == l).map(|i| i.1).collect(); ok &= self.with_layer(l, |s| merge::settle(s, &ids)).is_ok(); } self.frame = frame; if self.timeline().layer(layer).is_some() { self.layer = layer; } self.selection = selection; if !ok { // They stay as they are, on top: a move merges them where they land. self.doc = doc; } else if self.doc != doc { self.state = history::fresh_state(); self.revision += 1; } self.dl_cache.replace(None); self.fix_selection(); } /// A group open for editing is one layer and one frame (ADR 0073): commands that change /// the timeline are refused inside it. fn group_allows(&self, spec: &CommandSpec) -> Result<()> { let id = spec.id; if spec.mutates && self.editing_group() && GROUP_REFUSES.iter().any(|ns| if ns.ends_with('.') { id.starts_with(ns) } else { id == *ns }) { return Err(EngineError::State("a group has one layer and one frame: close it (Edit › Edit Document) to change the timeline, or convert it to a symbol (F8) to animate inside it".into())); } Ok(()) } /// Inside a group (ADR 0073) the playhead stays on its only frame. fn hold_group_frame(&mut self) { if self.frame != 0 && self.editing_group() { self.frame = 0; self.fix_selection(); } } /// The marquee's cut, while nothing has changed since it. fn live_cut(&self) -> Option<&Provisional> { self.provisional.as_ref().filter(|p| p.state == self.state && self.gesture.is_none()) } /// Keeps a marquee's cut provisional. `before` is the document before it; `chained` when /// it cut a provisional cut again (Shift), whose pieces then lead back to the shapes the /// first cut took. fn begin_cut(&mut self, before: history::Snapshot, chained: bool, origin: Vec<(ElementId, ElementId)>, inside: Vec) { let (mut origin, mut inside) = (origin, inside); if chained && let Some(p) = self.provisional.take() { inside.extend(p.inside.iter().copied()); for o in &mut origin { if let Some(&(_, first)) = p.origin.iter().find(|(piece, _)| *piece == o.1) { o.1 = first; } } let new: Vec = origin.iter().map(|o| o.0).collect(); origin.extend(p.origin.into_iter().filter(|(piece, _)| !new.contains(piece))); } // A marquee alone leaves the document as it was saved. if self.saved_state == Some(before.state) { self.saved_state = Some(self.state); } self.provisional = Some(Provisional { before, cut: self.doc.clone(), state: self.state, origin, inside }); } /// Takes back a provisional cut that the selection has let go of (none of the pieces inside /// the marquee is selected), and before saving, undo, redo or a new marquee. fn settle_cut(&mut self, id: &str, params: &Value) { if self.gesture.is_none() && self.provisional.as_ref().is_some_and(|p| p.state != self.state) { self.provisional = None; } let Some(p) = self.live_cut() else { return }; let held = self.selection.ids().any(|s| p.inside.contains(&s)); let fresh_marquee = id == "raw.select_rect" && params.get("add").and_then(Value::as_bool) != Some(true); if !held || fresh_marquee || id.starts_with("file.") || id == "edit.undo" || id == "edit.redo" { self.heal_cut(); } } /// Puts the document back as it was before a provisional cut. Selected pieces become the /// shapes they were cut from. fn heal_cut(&mut self) { let Some(p) = self.provisional.take() else { return }; if p.state != self.state || self.gesture.is_some() { return; } let mut sel = Selection::default(); for (layer, id) in self.selection.iter() { let id = p.origin.iter().find(|(piece, _)| *piece == id).map_or(id, |o| o.1); sel.insert(layer, id); } if self.saved_state == Some(p.state) { self.saved_state = Some(p.before.state); } // The cut's commands went with it. self.rec.clear(); self.doc = p.before.doc; self.state = p.before.state; self.selection = sel; self.dl_cache.replace(None); self.revision += 1; self.fix_selection(); } /// Runs a command inside another one (a plug-in's requests during `plugin.run`): checked /// and atomic like [`Session::execute`], but it records no undo step of its own, so the /// outer command stays one step. pub(crate) fn execute_nested(&mut self, id: &str, params: &Value) -> Result { let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; if let Some(c) = self.confirmation(id, params) && c.irreversible { return Err(EngineError::NeedsConfirmation(format!("{} {}", c.title, c.message))); } if !spec.mutates { return (spec.run)(self, params); } self.group_allows(&spec)?; let before = self.snapshot(""); self.dl_cache.replace(None); // Each command's edits to a motion tween's target become its keys, as when it runs on // its own (the next one may work at another frame). let outer = self.motion_edit.take(); // What the outer command edited in stage space settles first: this one sees the // document as it is (ADR 0113). self.settle_rig(); let rig = self.rig_begin(); let result = (spec.run)(self, params); self.settle_rig(); if let Some(edit) = self.motion_edit.take() && result.is_ok() { cmds_motion::finish_edit(self, edit); } self.rig_end(rig); self.motion_edit = outer; self.dl_cache.replace(None); self.pending_cut = None; match result { Ok(v) => { // Later requests must see this change (display list, hit tests): new state. if self.doc != before.doc { self.state = history::fresh_state(); } self.fix_selection(); Ok(v) } Err(e) => { self.restore(before); Err(e) } } } /// Puts the document back where the open gesture started, keeping the gesture open. A /// live preview re-runs from the same start on every change, and Cancel leaves no step. pub fn revert_gesture(&mut self) { if self.gesture_cut.is_some() { // Back to before the marquee's cut: it is gone. self.provisional = None; } let Some(g) = &self.gesture else { return }; if g.doc == self.doc && g.selection == self.selection { return; } let back = Snapshot { label: String::new(), doc: g.doc.clone(), state: g.state, scene: g.scene, editing: g.editing.clone(), frame: g.frame, layer: g.layer, selection: g.selection.clone(), floating: self.floating.clone(), bytes: 0, commands: None }; self.rec.truncate(self.gesture_rec); self.restore(back); self.revision += 1; self.fix_selection(); } fn snapshot(&self, label: &str) -> Snapshot { Snapshot { label: label.to_string(), doc: self.doc.clone(), state: self.state, scene: self.scene, editing: self.editing.clone(), frame: self.frame, layer: self.layer, selection: self.selection.clone(), floating: self.floating.clone(), bytes: 0, commands: None } } fn restore(&mut self, s: Snapshot) { self.doc = s.doc; self.state = s.state; self.scene = s.scene; // Where the change was made: an undo inside a symbol opens it again (ADR 0045). self.editing = s.editing; self.frame = s.frame; self.layer = s.layer; self.selection = s.selection; self.floating = s.floating; self.dl_cache.replace(None); } pub fn can_undo(&self) -> bool { self.history.can_undo() || self.gesture.as_ref().is_some_and(|g| g.doc != self.doc) } pub fn can_redo(&self) -> bool { self.history.can_redo() } pub fn undo_label(&self) -> Option<&str> { self.history.undo_label() } pub fn history_len(&self) -> usize { self.history.undo_len() } pub fn undo(&mut self) -> Result { self.end_gesture(); self.heal_cut(); self.rec.clear(); let cur = self.snapshot(""); let s = self.history.undo(cur).ok_or_else(|| EngineError::State("nothing to undo".into()))?; let label = s.label.clone(); self.restore(s); self.revision += 1; self.fix_selection(); Ok(json!({ "undone": label })) } pub fn redo(&mut self) -> Result { self.end_gesture(); self.heal_cut(); self.rec.clear(); let cur = self.snapshot(""); let s = self.history.redo(cur).ok_or_else(|| EngineError::State("nothing to redo".into()))?; let label = s.label.clone(); self.restore(s); self.revision += 1; self.fix_selection(); Ok(json!({ "redone": label })) } /// Moves through history until `undo_len()` equals `target` (for a History panel click). pub fn travel_to(&mut self, target: usize) -> Result { let mut moved = 0usize; while self.history.undo_len() > target && self.history.can_undo() { self.undo()?; moved += 1; } while self.history.undo_len() < target && self.history.can_redo() { self.redo()?; moved += 1; } Ok(json!({ "steps": moved, "position": self.history.undo_len() })) } // ----- queries --------------------------------------------------------------------------- pub fn frame_count(&self) -> u32 { self.timeline().frame_count() } pub fn active_layer(&self) -> Option<&Layer> { self.timeline().layer(self.layer) } pub fn active_layer_index(&self) -> Option { self.timeline().layers.iter().position(|l| l.id == self.layer) } /// The current scene evaluated at the playhead, cached per `(state, scene, frame)`. pub fn display_list(&self) -> Arc { let open = self.editing.last().map(|l| (l.open, l.instance, self.editing.len())); if let Some((s, sc, f, o, dl)) = &*self.dl_cache.borrow() && *s == self.state && *sc == self.scene && *f == self.frame && *o == open { return dl.clone(); } // An open symbol (ADR 0045) or group (ADR 0073) is edited in its own space. let dl = Arc::new(if open.is_some() { anicraft_doc::evaluate_timeline(&self.doc, self.timeline(), self.frame) } else { evaluate_scene(&self.doc, self.scene, self.frame) }); self.dl_cache.replace(Some((self.state, self.scene, self.frame, open, dl.clone()))); dl } /// Elements on the active layer at the playhead (tweens applied). Shares the document's /// element `Arc`s on non-tweened frames. pub fn visible_elements(&self) -> Vec> { self.elements_on(self.layer) } /// Topmost element under `p` (stage space) on a visible, unlocked layer. A bounding-box /// test rejects almost every item before any curve is flattened. pub fn hit_test(&self, p: Vec2, slop: f32) -> Option<(LayerId, ElementId)> { // The timeline being edited: a symbol open in place picks in its own layers (ADR 0045). let tl = self.timeline(); let dl = self.display_list(); let i = pick_index(&dl, p, slop, |_, it| tl.layer(it.layer).is_some_and(|layer| !layer.locked && !tl.locked_by_folder(layer.id)))?; dl.items.get(i).map(|it| (it.layer, it.element)) } /// Stage → output for layer `id` at the playhead as the camera shows it (ADR 0082): the /// camera and the layer's depth then, the identity for an attached layer. `None` without a /// camera that does something, while a symbol is open, or when the camera has passed the /// layer. What editing through the camera maps the pointer back with. pub fn camera_view(&self, id: LayerId) -> Option { if !self.editing.is_empty() { return None; } let tl = self.doc.timeline(self.scene); let cam = tl.camera_at(self.frame, &self.doc.stage)?; cam.layer_view(&self.doc.stage, tl.layer(id)?, self.frame) } /// [`Session::hit_test`] through the scene's camera: the topmost element under output /// point `p` (the movie's pixels) on a visible, unlocked layer, `slop` in output pixels. /// Without a camera that does something this is the stage's hit test. pub fn hit_test_camera(&self, p: Vec2, slop: f32) -> Option<(LayerId, ElementId)> { if !self.editing.is_empty() || self.doc.timeline(self.scene).camera_at(self.frame, &self.doc.stage).is_none() { return self.hit_test(p, slop); } let tl = self.timeline(); let dl = anicraft_doc::evaluate_scene_camera(&self.doc, self.scene, self.frame); let i = pick_index(&dl, p, slop, |_, it| tl.layer(it.layer).is_some_and(|layer| !layer.locked && !tl.locked_by_folder(layer.id)))?; dl.items.get(i).map(|it| (it.layer, it.element)) } /// Stage-space bounds of a top-level element at the playhead. pub fn element_bounds(&self, id: ElementId) -> Rect { let mut r = Rect::EMPTY; for item in self.display_list().items.iter().filter(|i| i.element == id) { let pad = item.stroke.as_ref().map_or(0.0, anicraft_doc::Stroke::reach); r = r.union(&item.path.bounds().inflate(pad).transformed(&item.transform)); } r } pub fn set_frame(&mut self, frame: u32) { self.frame = frame.min(MAX_FRAME); self.fix_selection(); } // ----- mutation helpers used by commands ------------------------------------------------ pub(crate) fn layer_mut(&mut self, id: Option) -> Result<&mut Layer> { let id = id.unwrap_or(self.layer); crate::editing::edited_mut(&mut self.doc, self.scene, &self.editing)?.layer_mut(id).ok_or_else(|| EngineError::State(format!("no layer with id {}", id.0))) } /// The keyframe on the active layer that edits at the playhead should change. Creates a /// keyframe when the playhead is inside a tween (auto-key) or past the layer's end. pub(crate) fn editable_keyframe(&mut self) -> Result<&mut Keyframe> { self.editable_keyframe_in(KeyEdit::Artwork) } /// [`Session::editable_keyframe`] for `what`: the camera layer holds only the camera (ADR /// 0027) and an armature layer only its posed armature (ADR 0043); every other edit is /// refused there. pub(crate) fn editable_keyframe_in(&mut self, what: KeyEdit) -> Result<&mut Keyframe> { let frame = self.frame; // A layer that follows a parent layer is edited as the stage shows it (ADR 0113). if let Some(e) = self.rig_blocked(self.layer) { return Err(e); } let space = self.layer_space(self.layer); let tl = self.timeline(); if let Some(l) = tl.layer(self.layer) { if l.kind == LayerKind::Folder { return Err(EngineError::State(format!("'{}' is a folder; pick a layer inside it to draw on", l.name))); } if (l.kind == LayerKind::Camera) != (what == KeyEdit::Camera) { return Err(EngineError::State(if what == KeyEdit::Camera { "that is not the camera layer".into() } else { format!("'{}' is the camera layer: it holds the camera, not artwork. Pick another layer to draw on, or move the camera with the Camera tool", l.name) })); } if (l.kind == LayerKind::Armature) != (what == KeyEdit::Pose) { return Err(EngineError::State(if what == KeyEdit::Pose { "that is not an armature layer".into() } else { format!("'{}' is an armature layer: it holds the armature's instances, posed by their bones. Drag a bone (or an instance) to pose it, drag the root instance to move it all, or pick another layer to draw on", l.name) })); } if tl.locked_by_folder(l.id) { return Err(EngineError::State(format!("layer '{}' is in a locked folder", l.name))); } } // Through the fields, not `layer_mut`, so `motion_edit` can be set below. let (scene, active) = (self.scene, self.layer); let layer = crate::editing::edited_mut(&mut self.doc, scene, &self.editing)?.layer_mut(active).ok_or_else(|| EngineError::State(format!("no layer with id {}", active.0)))?; if layer.locked { return Err(EngineError::State(format!("layer '{}' is locked", layer.name))); } // Inside a motion tween edits key properties, never the timeline. let needs_key = match layer.keyframe_at(frame) { None => true, Some(k) => k.motion.is_none() && k.tween.is_some() && k.start != frame, }; if needs_key { // Inside a tween: capture the tweened state. On an empty frame: start blank. let copy = layer.keyframe_at(frame).is_some(); cmds_timeline::insert_keyframe_at(layer, frame, copy)?; } let idx = layer.keyframe_index_at(frame).ok_or_else(|| EngineError::State("no keyframe at the playhead".into()))?; let lid = layer.id; let kf = layer.keyframe_mut(idx).ok_or_else(|| EngineError::State("no keyframe at the playhead".into()))?; // In a motion tween the command edits the target as it looks at the playhead; the // session turns what changed into property keys when the command is done. if self.motion_edit.is_none() && let Some(edit) = cmds_motion::begin_edit(lid, kf, frame) { self.motion_edit = Some(edit); } Session::rig_conjugate(&mut self.rig_edits, lid, frame, space, kf); Ok(kf) } /// Selected elements in the editable keyframe (copy-on-write: only these are cloned). pub(crate) fn selected_elements_mut(&mut self) -> Result> { let sel: std::collections::HashSet = self.selection.on(self.layer).into_iter().collect(); if sel.is_empty() { return Err(EngineError::State("nothing is selected".into())); } let kf = self.editable_keyframe()?; Ok(kf.elements_mut_where(|id| sel.contains(&id))) } /// [`Session::selected_elements_mut`] for the properties an instance keeps however it is /// placed (colour effect, blending, filters). On an armature layer (ADR 0084) the selected /// members are edited in place on every pose of the armature, so the property holds as the /// armature moves and the bones and poses are untouched; elsewhere, as any edit. pub(crate) fn selected_props_mut(&mut self) -> Result> { let on_armature = self.timeline().layer(self.layer).is_some_and(|l| l.kind == LayerKind::Armature); if !on_armature { return self.selected_elements_mut(); } let sel: std::collections::HashSet = self.selection.on(self.layer).into_iter().collect(); if sel.is_empty() { return Err(EngineError::State("nothing is selected".into())); } if self.timeline().locked_by_folder(self.layer) { return Err(EngineError::State("the armature layer is in a locked folder".into())); } let layer = self.layer_mut(None)?; if layer.locked { return Err(EngineError::State(format!("layer '{}' is locked", layer.name))); } Ok(layer.keyframes.iter_mut().flat_map(|k| Arc::make_mut(k).elements_mut_where(|id| sel.contains(&id))).collect()) } /// JSON summary used by `doc.info` and the CLI. pub fn info(&self) -> Value { let layers: Vec = self .timeline() .layers .iter() .map(|l| { json!({ "id": l.id.0, "name": l.name, "visible": l.visible, "locked": l.locked, "volume": l.volume, "muted": l.muted, "keyframes": l.keyframes.iter().map(|k| json!({"start": k.start, "duration": k.duration, "elements": k.elements.len(), "tween": k.tween.as_ref().map(|t| t.easing.label()), "motion": k.motion.as_ref().map(|m| m.tracks.iter().map(|t| t.property.key()).collect::>()), "keep_area": k.motion.as_ref().map_or(k.tween.as_ref().is_some_and(|t| t.keep_area), |m| m.keep_area), "sound": k.sound.as_ref().map(|f| json!({"sound": f.sound.0, "sync": f.sync.key(), "repeat": f.repeat, "loop": f.looping, "effect": f.effect.key()})), "script": k.script.as_ref().map(anicraft_doc::FrameScript::describe)})).collect::>() }) }) .collect(); let symbols: Vec = self.doc.library.iter().map(|s| json!({"id": s.id.0, "name": s.name, "kind": format!("{:?}", s.kind), "frames": s.timeline.frame_count()})).collect(); let sounds: Vec = self.doc.sounds.iter().map(|x| json!({"id": x.id.0, "name": x.name, "format": x.format.label(), "rate": x.rate, "channels": x.channels, "seconds": x.seconds()})).collect(); json!({ "stage": {"width": self.doc.stage.width, "height": self.doc.stage.height, "fps": self.doc.stage.fps, "background": self.doc.stage.background.to_hex()}, "frames": self.frame_count(), "frame": self.frame, "scene": self.scene, "scenes": self.doc.scenes.iter().map(|s| json!({"name": s.name, "frames": s.timeline.frame_count()})).collect::>(), "movie_frames": self.doc.frame_count(), "layer": self.layer.0, "selection": self.selection.id_list(), "selection_layers": self.selected_layers().iter().map(|l| l.0).collect::>(), // The symbols open for editing, outermost first (ADR 0045); `layers` are the open one's. "editing": editing::editing_json(self)["editing"], "layers": layers, "library": symbols, "sounds": sounds, "revision": self.revision, "dirty": self.is_dirty(), "history": {"undo": self.history.undo_len(), "redo": self.history.redo_len(), "bytes": self.history.bytes(), "evicted": self.history.evicted}, "state": self.state, }) } } /// What [`Session::editable_keyframe_in`] is asked to edit. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum KeyEdit { /// Artwork: any layer but the camera and armature layers. Artwork, /// The camera, on the camera layer. Camera, /// A pose, on an armature layer. Pose, } /// A colour effect as commands report it (ADR 0056): its style and that style's values, in the /// units `element.set_color_effect` takes (percent; Advanced offsets −255..255). pub fn color_effect_json(fx: &anicraft_doc::ColorEffect) -> serde_json::Value { use anicraft_doc::ColorEffect; let r = |v: f32, k: f32| f64::from((v * k * 100.0).round() / 100.0); let mut v = serde_json::json!({"style": fx.style().key()}); match *fx { ColorEffect::None => {} ColorEffect::Alpha(a) => v["alpha"] = r(a, 100.0).into(), ColorEffect::Brightness(b) => v["brightness"] = r(b, 100.0).into(), ColorEffect::Tint { color, amount } => { v["tint"] = color.to_hex().into(); v["tint_amount"] = r(amount, 100.0).into(); } ColorEffect::Advanced(t) => { for (i, ch) in ["red", "green", "blue", "alpha"].iter().enumerate() { v[format!("{ch}_multiplier")] = r(t.mul.get(i).copied().unwrap_or(1.0), 100.0).into(); v[format!("{ch}_offset")] = r(t.add.get(i).copied().unwrap_or(0.0), 255.0).into(); } } } v } /// Short human label for an element (properties panel, library, CLI). pub fn element_label(doc: &Document, e: &Element) -> String { let kind = match &e.kind { ElementKind::Shape(_) => "Shape".to_string(), ElementKind::Symbol { symbol, .. } => match doc.symbol(*symbol) { Some(s) => format!("{}: {}", match s.kind { anicraft_doc::SymbolKind::Graphic => "Graphic", anicraft_doc::SymbolKind::MovieClip => "Movie clip", anicraft_doc::SymbolKind::Button => "Button", }, s.name), None => "Graphic: missing".to_string(), }, ElementKind::Camera => "Camera".to_string(), ElementKind::Group { .. } => "Group".to_string(), // An empty field (an input field waiting for the viewer) is named by its type. ElementKind::Text(t) if t.text.trim().is_empty() => match t.kind() { anicraft_doc::TextKind::Static => "Static text", anicraft_doc::TextKind::Dynamic => "Dynamic text", anicraft_doc::TextKind::Input => "Input text", } .to_string(), ElementKind::Text(t) => { let first: String = t.text.lines().next().unwrap_or("").chars().take(24).collect(); format!("Text: {first}{}", if t.text.chars().count() > first.chars().count() { "…" } else { "" }) } }; if e.name.is_empty() { kind } else { format!("{} ({kind})", e.name) } } #[cfg(test)] mod tests; #[cfg(test)] mod parenting_tests; #[cfg(test)] mod keep_area_tests; #[cfg(test)] mod motion_tests; #[cfg(test)] mod retime_tests; #[cfg(test)] mod ease_tests; #[cfg(test)] mod color_tests; #[cfg(test)] mod motion_path_tests; #[cfg(test)] mod motion_pin_tests; #[cfg(test)] mod plugin_tests; #[cfg(test)] mod macro_tests; #[cfg(test)] mod plugin_kinds_tests; #[cfg(test)] mod tests_sound; #[cfg(test)] mod tests_audio_keys; #[cfg(test)] mod video_tests; #[cfg(test)] mod hint_tests; #[cfg(test)] mod tests_actions; #[cfg(test)] mod tests_script; #[cfg(test)] mod tests_camera; #[cfg(test)] mod tests_bone; #[cfg(test)] mod tests_bone_spring; #[cfg(test)] mod tests_shape_bone; #[cfg(test)] mod tests_shape_weights; #[cfg(test)] mod tests_warp; #[cfg(test)] mod tests_filter; #[cfg(test)] mod tests_filter_tracks; #[cfg(test)] mod tests_filter_colour; #[cfg(test)] mod desktop_tests; #[cfg(test)] mod merge_tests; #[cfg(test)] mod raw_tests; #[cfg(test)] mod rawsub_tests; #[cfg(test)] mod rawpoints_tests; #[cfg(test)] mod editing_tests; #[cfg(test)] mod brush_tests; #[cfg(test)] mod brush_library_tests; #[cfg(test)] mod web_tests; #[cfg(test)] mod erase_tests; #[cfg(test)] mod eyedropper_tests; #[cfg(test)] mod gradient_tests; #[cfg(test)] mod selection_tests; #[cfg(test)] mod scale_grid_tests; #[cfg(test)] mod drop_tests; #[cfg(test)] mod template_tests; #[cfg(test)] mod timeline_drop_tests; #[cfg(test)] mod timeline_key_tests; #[cfg(test)] mod tests_bone_chain; #[cfg(test)] mod tests_mixed_bone; #[cfg(test)] mod view_tests; #[cfg(test)] mod layer_order_tests; #[cfg(test)] mod path_text_tests; #[cfg(test)] mod rich_text_tests; #[cfg(test)] mod group_tests; #[cfg(test)] mod swatch_tests; #[cfg(test)] mod parity_tests;