mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-29 17:43:57 +01:00
feat(engine): Convert to Symbol takes a folder and a type (M2)
This commit is contained in:
1 parent
53c9c57402
commit
1e31d8440c
3 files changed
+64
-9
No files matched your search
@@ -39,7 +39,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
c("arrange.bring_forward", "Bring Forward", "Modify/Arrange", Some("Ctrl+Up"), "{} (within each layer)", true, |s, _| s.each_layer(true, |s| arrange_step(s, true))),
|
||||
c("arrange.send_backward", "Send Backward", "Modify/Arrange", Some("Ctrl+Down"), "{} (within each layer)", true, |s, _| s.each_layer(true, |s| arrange_step(s, false))),
|
||||
c("arrange.send_to_back", "Send to Back", "Modify/Arrange", Some("Ctrl+Shift+Down"), "{} (within each layer)", true, |s, _| s.each_layer(true, |s| arrange(s, false))),
|
||||
c("modify.convert_to_symbol", "Convert to Symbol…", "Modify", Some("F8"), "{name?} (a selection on several layers becomes one symbol with those layers inside; the instance lands on the topmost)", true, convert_to_symbol),
|
||||
c("modify.convert_to_symbol", "Convert to Symbol…", "Modify", Some("F8"), "{name? (default: the next free “Symbol n”; a name the folder holds gets a number), kind?: graphic|movie_clip|button (default: graphic), folder?: Library folder id (default: the top level)} → {symbol, instance, name, layer, folder} (a selection on several layers becomes one symbol with those layers inside; the instance lands on the topmost)", true, convert_to_symbol),
|
||||
c("modify.break_apart", "Break Apart", "Modify", Some("Ctrl+B"), "{}", true, |s, p| s.each_layer(false, |s| break_apart(s, p))),
|
||||
c("library.place", "Place on Stage", "", None, "{symbol | bitmap | sound: id, x?, y?} or {items: [{symbol | bitmap | sound: id}, ..], x?, y?} → {element, elements, sound?}: symbols and bitmaps centred on x, y (default: the stage centre; inside an open symbol, in its space) in the active layer's keyframe at the playhead, selected; a sound goes onto that keyframe, its span grown to fit (one sound per keyframe). One undo step", true, place),
|
||||
]
|
||||
@@ -648,8 +648,14 @@ pub(crate) fn convert_to_symbol(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let Some(&top) = layers.first() else {
|
||||
return Err(EngineError::State("select something to convert".into()));
|
||||
};
|
||||
// The next free “Symbol n” at the Library's top level, where new symbols go (ADR 0050).
|
||||
let name: String = opt_str(p, "name")?.map(|x| x.chars().take(128).collect()).unwrap_or_else(|| s.doc.numbered_name(None, "Symbol", s.doc.library.len() + 1));
|
||||
// Into the folder asked for (ADR 0053), else the top level; the name is free there: the
|
||||
// next free “Symbol n”, or the name asked for with a number when the folder has it already.
|
||||
let folder = crate::cmds_library::opt_folder(s, p, "folder")?;
|
||||
let kind = opt_str(p, "kind")?.map(crate::cmds_library::kind_of).transpose()?.unwrap_or(SymbolKind::Graphic);
|
||||
let name: String = match opt_str(p, "name")? {
|
||||
Some(n) => s.doc.unique_name(folder, n),
|
||||
None => s.doc.numbered_name(folder, "Symbol", s.doc.library.len() + 1),
|
||||
};
|
||||
let bounds = s.selection_bounds();
|
||||
let center = if bounds.is_empty() { Vec2::ZERO } else { bounds.center() };
|
||||
let sym_id = SymbolId(s.doc.alloc_id());
|
||||
@@ -694,10 +700,13 @@ pub(crate) fn convert_to_symbol(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
inner.push(Arc::new(layer));
|
||||
}
|
||||
s.doc.library.push(Arc::new(Symbol { id: sym_id, name: name.clone(), kind: SymbolKind::Graphic, linkage: None, timeline: Timeline { layers: inner } }));
|
||||
s.doc.library.push(Arc::new(Symbol { id: sym_id, name: name.clone(), kind, linkage: None, timeline: Timeline { layers: inner } }));
|
||||
if folder.is_some() {
|
||||
s.doc.place_entry(LibraryEntry::Item(LibraryItem::Symbol(sym_id)), folder);
|
||||
}
|
||||
s.layer = top;
|
||||
s.select_only([inst_id]);
|
||||
Ok(json!({"symbol": sym_id.0, "instance": inst_id.0, "name": name, "layer": top.0}))
|
||||
Ok(json!({"symbol": sym_id.0, "instance": inst_id.0, "name": name, "layer": top.0, "folder": folder.map(|f| f.0)}))
|
||||
}
|
||||
|
||||
fn break_apart(s: &mut Session, _: &Value) -> Result<Value> {
|
||||
|
||||
@@ -85,7 +85,7 @@ fn describe(e: LibraryEntry) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_folder(s: &Session, p: &Value, key: &str) -> Result<Option<FolderId>> {
|
||||
pub(crate) fn opt_folder(s: &Session, p: &Value, key: &str) -> Result<Option<FolderId>> {
|
||||
match p.get(key) {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(v) => {
|
||||
@@ -98,7 +98,17 @@ fn opt_folder(s: &Session, p: &Value, key: &str) -> Result<Option<FolderId>> {
|
||||
}
|
||||
}
|
||||
|
||||
fn kind_key(k: SymbolKind) -> &'static str {
|
||||
/// The symbol type `key` names (`graphic`, `movie_clip` or `button`).
|
||||
pub(crate) fn kind_of(key: &str) -> Result<SymbolKind> {
|
||||
match key {
|
||||
"graphic" => Ok(SymbolKind::Graphic),
|
||||
"movie_clip" | "movie clip" => Ok(SymbolKind::MovieClip),
|
||||
"button" => Ok(SymbolKind::Button),
|
||||
_ => Err(EngineError::Param(format!("unknown symbol type '{key}' (graphic, movie_clip or button)"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn kind_key(k: SymbolKind) -> &'static str {
|
||||
match k {
|
||||
SymbolKind::Graphic => "graphic",
|
||||
SymbolKind::MovieClip => "movie_clip",
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! Placing Library items (a drop on the stage, Place on Stage) and Swap Bitmap (ADR 0053).
|
||||
//! Placing Library items (a drop on the stage, Place on Stage), Swap Bitmap and Convert to Symbol
|
||||
//! into a folder (ADR 0053).
|
||||
|
||||
use crate::Session;
|
||||
use anicraft_doc::{BitmapId, Element, ElementKind, LibraryItem, Paint, SymbolId, sample};
|
||||
use anicraft_doc::{BitmapId, Element, ElementKind, FolderId, LibraryEntry, LibraryItem, Paint, SymbolId, SymbolKind, sample};
|
||||
use anicraft_geom::{Affine, Path, Vec2};
|
||||
use serde_json::{Value, json};
|
||||
use std::sync::Arc;
|
||||
@@ -181,3 +182,38 @@ fn swap_bitmap_resizes_a_placed_bitmap_and_keeps_a_filled_shape() {
|
||||
run(&mut s, "element.select", json!({"ids": [other]}));
|
||||
assert!(err(&mut s, "element.swap_bitmap", json!({"bitmap": big.0})).contains("filled with one"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn convert_to_symbol_goes_into_the_folder_asked_for_with_a_free_name() {
|
||||
let mut s = Session::new(sample::demo());
|
||||
let props = run(&mut s, "library.new_folder", json!({"name": "Props"}))["folder"].as_u64().unwrap();
|
||||
let entry = |v: &Value| LibraryEntry::Item(LibraryItem::Symbol(SymbolId(v["symbol"].as_u64().unwrap())));
|
||||
run(&mut s, "draw.rect", json!({"x": 10, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
let v = run(&mut s, "modify.convert_to_symbol", json!({"name": "Ball", "kind": "movie_clip", "folder": props}));
|
||||
assert_eq!(v["folder"], props);
|
||||
assert_eq!(v["name"], "Ball", "Props has no Ball yet (the top level's doesn't count)");
|
||||
assert_eq!(s.doc.parent_of(entry(&v)), Some(FolderId(props)));
|
||||
assert_eq!(s.doc.entry_path(entry(&v)), "Props/Ball");
|
||||
assert_eq!(s.doc.symbol(SymbolId(v["symbol"].as_u64().unwrap())).unwrap().kind, SymbolKind::MovieClip);
|
||||
// The same name again in that folder gets a number; no name gives the next free “Symbol n”.
|
||||
run(&mut s, "draw.rect", json!({"x": 50, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
assert_eq!(run(&mut s, "modify.convert_to_symbol", json!({"name": "ball", "folder": props}))["name"], "ball 2");
|
||||
run(&mut s, "draw.rect", json!({"x": 90, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
let v = run(&mut s, "modify.convert_to_symbol", json!({"folder": props}));
|
||||
assert!(v["name"].as_str().unwrap().starts_with("Symbol "));
|
||||
assert_eq!(s.doc.symbol(SymbolId(v["symbol"].as_u64().unwrap())).unwrap().kind, SymbolKind::Graphic, "graphic by default");
|
||||
// Without a folder: the top level, as before.
|
||||
run(&mut s, "draw.rect", json!({"x": 130, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
let v = run(&mut s, "modify.convert_to_symbol", json!({}));
|
||||
assert!(v["folder"].is_null());
|
||||
assert_eq!(s.doc.parent_of(entry(&v)), None);
|
||||
// Refusals change nothing.
|
||||
run(&mut s, "draw.rect", json!({"x": 170, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
let before = s.doc.clone();
|
||||
assert!(err(&mut s, "modify.convert_to_symbol", json!({"folder": 999_999})).contains("no folder"));
|
||||
assert!(err(&mut s, "modify.convert_to_symbol", json!({"kind": "sprite"})).contains("symbol type"));
|
||||
assert_eq!(s.doc, before);
|
||||
// The file keeps where it went.
|
||||
let back = crate::format::from_json(&crate::format::to_json(&s.doc).unwrap()).unwrap();
|
||||
assert_eq!(back.library_tree, s.doc.library_tree);
|
||||
}
|
||||
Reference in new issue
Block a user