mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-19 17:12:19 +01:00
test(shell): a drop on the stage, Enter in the Library and the Convert to Symbol dialog, headless (M2)
This commit is contained in:
1 parent
b871683cb3
commit
ebd29c19eb
2 files changed
+141
-11
No files matched your search
+61
-11
@@ -131,22 +131,28 @@ pub fn window(app: &mut App, ctx: &egui::Context) {
|
||||
}
|
||||
});
|
||||
});
|
||||
if submit && let Some(d) = app.convert.take() {
|
||||
let name = d.name.trim();
|
||||
let mut params = json!({"kind": kind_key(d.kind), "folder": d.folder.map(|f| f.0)});
|
||||
if !name.is_empty() {
|
||||
params["name"] = json!(name);
|
||||
}
|
||||
app.convert_last = Last { folder: d.folder, kind: d.kind };
|
||||
if let Some(f) = d.folder {
|
||||
app.library.expanded.extend(app.s.doc.folder_chain(f));
|
||||
}
|
||||
app.request("modify.convert_to_symbol", params);
|
||||
if submit {
|
||||
accept(app);
|
||||
} else if cancel {
|
||||
app.convert = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// OK: converts with the dialog's choices and remembers the folder and type for next time.
|
||||
fn accept(app: &mut App) {
|
||||
let Some(d) = app.convert.take() else { return };
|
||||
let name = d.name.trim();
|
||||
let mut params = json!({"kind": kind_key(d.kind), "folder": d.folder.map(|f| f.0)});
|
||||
if !name.is_empty() {
|
||||
params["name"] = json!(name);
|
||||
}
|
||||
app.convert_last = Last { folder: d.folder, kind: d.kind };
|
||||
if let Some(f) = d.folder {
|
||||
app.library.expanded.extend(app.s.doc.folder_chain(f));
|
||||
}
|
||||
app.request("modify.convert_to_symbol", params);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -163,4 +169,48 @@ mod tests {
|
||||
assert_eq!(remembered(&s.doc, last), None, "a deleted folder falls back to the top level");
|
||||
assert_eq!(remembered(&s.doc, Last::default()), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ok_converts_into_the_folder_and_the_next_dialog_offers_it_again() {
|
||||
let ctx = egui::Context::default();
|
||||
let mut app = App::with_context(&ctx, Session::new(sample::demo()), Some(std::path::PathBuf::from("unused.png")));
|
||||
// Nothing selected: no dialog, the engine says why.
|
||||
app.run("edit.deselect", json!({}));
|
||||
open(&mut app);
|
||||
assert!(app.convert.is_none());
|
||||
let f = FolderId(app.run("library.new_folder", json!({"name": "Props"})).unwrap()["folder"].as_u64().unwrap());
|
||||
app.run("draw.rect", json!({"x": 10, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
open(&mut app);
|
||||
let d = app.convert.as_mut().expect("the dialog opens");
|
||||
assert_eq!(d.folder, None, "the top level the first time");
|
||||
assert!(d.name.starts_with("Symbol "));
|
||||
d.folder = Some(f);
|
||||
d.kind = SymbolKind::Button;
|
||||
d.name = " Lamp ".into();
|
||||
accept(&mut app);
|
||||
assert!(app.convert.is_none());
|
||||
let sym = app.s.doc.library.iter().find(|s| s.name == "Lamp").expect("converted, the name trimmed");
|
||||
assert_eq!(sym.kind, SymbolKind::Button);
|
||||
assert_eq!(app.s.doc.parent_of(LibraryEntry::Item(anicraft_engine::doc::LibraryItem::Symbol(sym.id))), Some(f));
|
||||
assert!(app.library.expanded.contains(&f), "the folder opens in the Library");
|
||||
// The next conversion offers Props and Button again; an empty name takes the default.
|
||||
app.run("draw.rect", json!({"x": 60, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
open(&mut app);
|
||||
let d = app.convert.as_mut().expect("the dialog opens");
|
||||
assert_eq!((d.folder, d.kind), (Some(f), SymbolKind::Button));
|
||||
d.name = String::new();
|
||||
accept(&mut app);
|
||||
assert_eq!(app.s.doc.library_children(Some(f)).len(), 2);
|
||||
// The dialog draws (the clash note too) without the document changing.
|
||||
app.run("draw.rect", json!({"x": 90, "y": 10, "w": 20, "h": 20, "object": true}));
|
||||
open(&mut app);
|
||||
if let Some(d) = app.convert.as_mut() {
|
||||
d.name = "Lamp".into();
|
||||
}
|
||||
let before = app.s.doc.clone();
|
||||
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| window(&mut app, ui.ctx()));
|
||||
out.textures_delta.clear();
|
||||
assert!(app.convert.is_some());
|
||||
assert_eq!(app.s.doc, before);
|
||||
}
|
||||
}
|
||||
@@ -969,6 +969,86 @@ mod tests {
|
||||
assert_eq!(names(&s, &mut v)[2..], [n(0, "Star"), n(0, "Ball copy")]);
|
||||
}
|
||||
|
||||
fn pointer(pos: egui::Pos2, pressed: Option<bool>) -> egui::RawInput {
|
||||
let mut events = vec![egui::Event::PointerMoved(pos)];
|
||||
if let Some(pressed) = pressed {
|
||||
events.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
}
|
||||
egui::RawInput { screen_rect: Some(Rect::from_min_size(pos2(0.0, 0.0), vec2(1600.0, 940.0))), events, ..Default::default() }
|
||||
}
|
||||
|
||||
fn app() -> (egui::Context, App) {
|
||||
let ctx = egui::Context::default();
|
||||
let app = App::with_context(&ctx, anicraft_engine::Session::new(anicraft_engine::doc::sample::demo()), Some(std::path::PathBuf::from("unused.png")));
|
||||
(ctx, app)
|
||||
}
|
||||
|
||||
fn frame(ctx: &egui::Context, app: &mut App, input: egui::RawInput) {
|
||||
let mut out = ctx.run_ui(input, |ui| app.frame(ui));
|
||||
out.textures_delta.clear();
|
||||
}
|
||||
|
||||
fn instances(app: &App, symbol: u64) -> Vec<anicraft_engine::geom::Vec2> {
|
||||
app.s.timeline().layers.iter().filter_map(|l| l.keyframe_at(app.s.frame)).flat_map(|k| &k.elements).filter(|e| matches!(e.kind, anicraft_engine::doc::ElementKind::Symbol { symbol: x, .. } if x.0 == symbol)).map(|e| e.transform.apply(anicraft_engine::geom::Vec2::ZERO)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_rows_on_the_stage_places_them_at_the_pointer_in_one_step() {
|
||||
let (ctx, mut app) = app();
|
||||
for _ in 0..3 {
|
||||
frame(&ctx, &mut app, pointer(pos2(5.0, 935.0), None));
|
||||
}
|
||||
let star = app.s.doc.library.iter().find(|x| x.name == "Star").unwrap().id;
|
||||
let before = instances(&app, star.0).len();
|
||||
let steps = app.s.history().undo_len();
|
||||
let canvas = app.stage_cache.canvas;
|
||||
assert!(canvas.width() > 100.0, "the stage was laid out");
|
||||
// The press starts outside the stage (as on a Library row), the rows ride along.
|
||||
frame(&ctx, &mut app, pointer(pos2(5.0, 935.0), Some(true)));
|
||||
egui::DragAndDrop::set_payload(&ctx, vec![LibraryEntry::Item(LibraryItem::Symbol(star)), LibraryEntry::Folder(FolderId(424_242))]);
|
||||
let at = canvas.center();
|
||||
for _ in 0..2 {
|
||||
frame(&ctx, &mut app, pointer(at, None));
|
||||
}
|
||||
assert_eq!(instances(&app, star.0).len(), before, "hovering places nothing");
|
||||
frame(&ctx, &mut app, pointer(at, Some(false)));
|
||||
let now = instances(&app, star.0);
|
||||
assert_eq!(now.len(), before + 1, "letting go places the symbol (folders are left out)");
|
||||
assert_eq!(app.s.history().undo_len(), steps + 1, "one undo step");
|
||||
assert!(!app.library.keys, "the stage has the keyboard again");
|
||||
// Where the pointer was, in stage space.
|
||||
let size = vec2(app.s.doc.stage.width as f32, app.s.doc.stage.height as f32);
|
||||
let origin = canvas.center() + app.view.pan - size * app.view.zoom * 0.5;
|
||||
let want = (at - origin) / app.view.zoom;
|
||||
let got = now.iter().find(|p| (p.x - want.x).abs() < 1.5 && (p.y - want.y).abs() < 1.5);
|
||||
assert!(got.is_some(), "{now:?} near {want:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_places_the_selected_rows_at_the_centre_of_the_view() {
|
||||
let (ctx, mut app) = app();
|
||||
for _ in 0..3 {
|
||||
frame(&ctx, &mut app, pointer(pos2(5.0, 935.0), None));
|
||||
}
|
||||
let star = app.s.doc.library.iter().find(|x| x.name == "Star").unwrap().id;
|
||||
let before = instances(&app, star.0).len();
|
||||
app.library.sel = vec![LibraryEntry::Item(LibraryItem::Symbol(star)), LibraryEntry::Folder(FolderId(424_242))];
|
||||
app.library.keys = true;
|
||||
let mut input = pointer(pos2(5.0, 935.0), None);
|
||||
input.events.push(egui::Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE });
|
||||
frame(&ctx, &mut app, input);
|
||||
let now = instances(&app, star.0);
|
||||
assert_eq!(now.len(), before + 1);
|
||||
let c = app.view_center_stage();
|
||||
assert!(now.iter().any(|p| (p.x - c.x).abs() < 0.01 && (p.y - c.y).abs() < 0.01), "{now:?} at {c:?}");
|
||||
// Inside a symbol open for editing, the centre of the view in its space.
|
||||
app.run("symbol.edit", json!({"symbol": star.0}));
|
||||
let ball = app.s.doc.library.iter().find(|x| x.name == "Ball").unwrap().id;
|
||||
place_items(&mut app, &[LibraryItem::Symbol(ball)], None);
|
||||
let (x, y) = view_centre(&app);
|
||||
assert!(instances(&app, ball.0).iter().any(|p| (p.x - x).abs() < 0.01 && (p.y - y).abs() < 0.01));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_search_shows_matches_inside_closed_folders() {
|
||||
let (s, _, _) = session();
|
||||
|
||||
Reference in new issue
Block a user