feat: render interactive manuscript from Endor through generic graphics
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
//! Integration acceptance through the actual Endor VM and published frames.
|
||||
use super::*;
|
||||
use crate::document::RenderCommand;
|
||||
|
||||
#[test]
|
||||
fn illustrated_manuscript_is_driven_by_real_endor_source_and_pointer_events() {
|
||||
let source = include_str!("../../../examples/illustrated-manuscript.endor");
|
||||
let (mut session, mut document) = ReplSession::load(source, "app.main", 1, 735).unwrap();
|
||||
fn evaluate(session: &mut ReplSession, document: &mut RenderDocument, source: &str) -> String {
|
||||
let result = session.execute(source, "app.main").unwrap();
|
||||
if let Some(next) = result.document {
|
||||
*document = next;
|
||||
}
|
||||
result.value
|
||||
}
|
||||
evaluate(&mut session, &mut document, "view.timer.cancel()");
|
||||
document.validate().unwrap();
|
||||
assert!(
|
||||
document
|
||||
.frame
|
||||
.commands
|
||||
.iter()
|
||||
.any(|c| matches!(c, RenderCommand::Image { .. }))
|
||||
);
|
||||
assert!(document.frame.mounts.iter().any(|m| m.pointer_events));
|
||||
let before = evaluate(&mut session, &mut document, "dragon.segments.at(0).x");
|
||||
let target = document
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.find(|m| m.pointer_events)
|
||||
.unwrap();
|
||||
let mut input = crate::events::Input {
|
||||
window: 1,
|
||||
session: 735,
|
||||
layout_version: document.frame.layout_version,
|
||||
mount: target.id,
|
||||
kind: "pointermove".into(),
|
||||
point: Some([
|
||||
target.x + target.width * 0.65,
|
||||
target.y + target.height * 0.55,
|
||||
]),
|
||||
press_point: None,
|
||||
delta: None,
|
||||
tab_drop: None,
|
||||
screen_position: None,
|
||||
};
|
||||
document = session
|
||||
.dispatch(&input)
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
for _ in 0..8 {
|
||||
document = session
|
||||
.execute("dragon.step()", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
}
|
||||
let after = evaluate(&mut session, &mut document, "dragon.segments.at(0).x");
|
||||
assert_ne!(before, after);
|
||||
input.layout_version = document.frame.layout_version;
|
||||
input.kind = "pointerdown".into();
|
||||
document = session
|
||||
.dispatch(&input)
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
for _ in 0..3 {
|
||||
document = session
|
||||
.execute("dragon.step()", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
}
|
||||
assert_ne!(
|
||||
evaluate(&mut session, &mut document, "dragon.flames.count"),
|
||||
"0"
|
||||
);
|
||||
let max_size = evaluate(&mut session, &mut document, "dragon.flames.at(0).size < 20");
|
||||
assert_eq!(max_size, "true");
|
||||
input.layout_version = document.frame.layout_version;
|
||||
input.kind = "pointercancel".into();
|
||||
session.dispatch(&input).unwrap();
|
||||
assert_eq!(evaluate(&mut session, &mut document, "dragon.pressed"), "0");
|
||||
for _ in 0..16 {
|
||||
document = session
|
||||
.execute("dragon.step()", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
}
|
||||
assert_eq!(
|
||||
evaluate(&mut session, &mut document, "dragon.flames.count"),
|
||||
"0"
|
||||
);
|
||||
let list = document.display_list(1.).unwrap();
|
||||
let mut pixels = vec![0; 1024 * 800];
|
||||
crate::software_renderer::SoftwareRenderer::render(&list, &mut pixels, 1024, 800).unwrap();
|
||||
assert!(pixels.contains(&0xf4eee0));
|
||||
}
|
||||
|
||||
fn execute(session: &mut ReplSession, document: &mut RenderDocument, source: &str) -> String {
|
||||
let result = session.execute(source, "app.main").unwrap();
|
||||
if let Some(next) = result.document {
|
||||
*document = next;
|
||||
}
|
||||
result.value
|
||||
}
|
||||
fn write_png(document: &RenderDocument, w: u32, h: u32, scale: f32, name: &str) {
|
||||
let list = document.display_list(scale).unwrap();
|
||||
let mut pixels = vec![0; w as usize * h as usize];
|
||||
crate::software_renderer::SoftwareRenderer::render(&list, &mut pixels, w, h).unwrap();
|
||||
assert!(pixels.iter().filter(|&&p| p == 0xf4eee0).count() > 1000);
|
||||
if let Ok(dir) = std::env::var("TESSERA_MANUSCRIPT_SNAPSHOTS") {
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let file =
|
||||
std::fs::File::create(std::path::Path::new(&dir).join(format!("endor-{name}.png")))
|
||||
.unwrap();
|
||||
let mut encoder = png::Encoder::new(file, w, h);
|
||||
encoder.set_color(png::ColorType::Rgb);
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
let rgb: Vec<_> = pixels
|
||||
.iter()
|
||||
.flat_map(|p| [(p >> 16) as u8, (p >> 8) as u8, *p as u8])
|
||||
.collect();
|
||||
encoder
|
||||
.write_header()
|
||||
.unwrap()
|
||||
.write_image_data(&rgb)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn manuscript_script_snapshots_and_owned_animation_lifecycle() {
|
||||
let source = format!(
|
||||
"{}\nfunction advanceTest():Unit {{dragon.lastMove=math.now() dragon.step()}}",
|
||||
include_str!("../../../examples/illustrated-manuscript.endor")
|
||||
);
|
||||
for (width, height, scale) in [(1000., 900., 1.), (420., 760., 2.)] {
|
||||
let (mut session, mut document) = ReplSession::load_with_viewport(
|
||||
&source,
|
||||
"app.main",
|
||||
1,
|
||||
736,
|
||||
Some(crate::layout::Size { width, height }),
|
||||
)
|
||||
.unwrap();
|
||||
// The actual owner-scoped Task calls script tick, not a native demo loop.
|
||||
let before = execute(&mut session, &mut document, "dragon.time");
|
||||
std::thread::sleep(std::time::Duration::from_millis(85));
|
||||
if let Some(next) = session.poll_tasks().unwrap() {
|
||||
document = next;
|
||||
}
|
||||
let after = execute(&mut session, &mut document, "dragon.time");
|
||||
assert_ne!(before, after);
|
||||
execute(&mut session, &mut document, "view.timer.cancel()");
|
||||
let (w, h) = ((width * scale) as u32, (height * scale) as u32);
|
||||
write_png(
|
||||
&document,
|
||||
w,
|
||||
h,
|
||||
scale,
|
||||
if width > 500. { "rest" } else { "narrow-rest" },
|
||||
);
|
||||
let mount = document
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.find(|m| m.pointer_events)
|
||||
.unwrap()
|
||||
.clone();
|
||||
let point = [mount.x + mount.width * 0.58, mount.y + mount.height * 0.50];
|
||||
let mut input = crate::events::Input {
|
||||
window: 1,
|
||||
session: 736,
|
||||
layout_version: document.frame.layout_version,
|
||||
mount: mount.id,
|
||||
kind: "pointermove".into(),
|
||||
point: Some(point),
|
||||
press_point: None,
|
||||
delta: None,
|
||||
tab_drop: None,
|
||||
screen_position: None,
|
||||
};
|
||||
document = session
|
||||
.dispatch(&input)
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
for _ in 0..12 {
|
||||
execute(&mut session, &mut document, "advanceTest()");
|
||||
}
|
||||
write_png(
|
||||
&document,
|
||||
w,
|
||||
h,
|
||||
scale,
|
||||
if width > 500. { "crossing" } else { "narrow" },
|
||||
);
|
||||
input.layout_version = document.frame.layout_version;
|
||||
input.kind = "pointerdown".into();
|
||||
document = session
|
||||
.dispatch(&input)
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap_or(document);
|
||||
// Hold long enough to reach the steady-state particle cap and exercise
|
||||
// the worst paint workload, then release/cancel through the event path.
|
||||
let start = std::time::Instant::now();
|
||||
for _ in 0..16 {
|
||||
execute(&mut session, &mut document, "advanceTest()");
|
||||
}
|
||||
eprintln!(
|
||||
"Endor manuscript width={width}: 16 steady-fire VM refreshes {:?}",
|
||||
start.elapsed()
|
||||
);
|
||||
assert_eq!(
|
||||
execute(&mut session, &mut document, "dragon.flames.count <= 12"),
|
||||
"true"
|
||||
);
|
||||
execute(
|
||||
&mut session,
|
||||
&mut document,
|
||||
"function verifyFire(): Bool { art=dragon.artwork(0,0,1) c=art.at(art.count-1) p=dragon.flames.at(dragon.flames.count-1) return math.abs(c.angle+p.art.axis-math.atan2(p.vy,p.vx))<0.0001 }",
|
||||
);
|
||||
assert_eq!(execute(&mut session, &mut document, "verifyFire()"), "true");
|
||||
execute(
|
||||
&mut session,
|
||||
&mut document,
|
||||
"function verifyFireSize(): Bool { art=dragon.artwork(0,0,1) c=art.at(art.count-1) return math.max(c.width,c.height)<=20 }",
|
||||
);
|
||||
assert_eq!(
|
||||
execute(&mut session, &mut document, "verifyFireSize()"),
|
||||
"true"
|
||||
);
|
||||
write_png(
|
||||
&document,
|
||||
w,
|
||||
h,
|
||||
scale,
|
||||
if width > 500. { "fire" } else { "narrow-fire" },
|
||||
);
|
||||
execute(&mut session, &mut document, "page.close()");
|
||||
let closed = execute(&mut session, &mut document, "dragon.time");
|
||||
std::thread::sleep(std::time::Duration::from_millis(85));
|
||||
session.poll_tasks().unwrap();
|
||||
assert_eq!(execute(&mut session, &mut document, "dragon.time"), closed);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user