feat: compose interactive world ui across nested 3d viewports
This commit is contained in:
@@ -4924,6 +4924,398 @@ fn scene3d_example_custom_geometry_and_cycle_diagnostics_use_real_source() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_ui_panel_renders_live_endor_and_ray_hits_respect_mesh_occlusion() {
|
||||
let (mut session, initial) = ReplSession::load(
|
||||
r##"
|
||||
class Tile: Button {
|
||||
tint: String
|
||||
function paint(self: Tile, mount: Mount, geometry: Bounds): DisplayList {
|
||||
fill=PaintCommand()
|
||||
fill.kind="roundedRect"
|
||||
fill.x=geometry.x
|
||||
fill.y=geometry.y
|
||||
fill.width=geometry.width
|
||||
fill.height=geometry.height
|
||||
fill.radius=0
|
||||
fill.color=self.tint
|
||||
return DisplayList(ObjectList(fill))
|
||||
}
|
||||
}
|
||||
count=ui.state(0)
|
||||
button=Tile("world UI","#FF0000")
|
||||
button.onClick(count.increment)
|
||||
panel=Panel3D(2,2,button)
|
||||
panel.uiWidth=100
|
||||
panel.uiHeight=100
|
||||
panel.rotationY=25
|
||||
scene=Scene3D(panel)
|
||||
scene.ambient=1
|
||||
view=Viewport3D(200,200,scene)
|
||||
page=Page()
|
||||
page.render(view)
|
||||
"##,
|
||||
"app.main",
|
||||
314,
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
initial.validate().unwrap();
|
||||
let button = initial
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.find(|mount| mount.interactive && mount.panel.is_some())
|
||||
.unwrap();
|
||||
assert_eq!((button.width, button.height), (100., 100.));
|
||||
let point = button
|
||||
.panel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.project_point(crate::geometry::Point::new(50., 50.))
|
||||
.unwrap();
|
||||
assert!((point.x - 100.).abs() < 0.001 && (point.y - 100.).abs() < 0.001);
|
||||
assert_eq!(initial.frame.hit_test(point).unwrap().id, button.id);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("button.mounts().at(0).toView(50,50).x", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"100"
|
||||
);
|
||||
let local = session
|
||||
.execute("button.mounts().at(0).toLocal(100,100).x", "app.main")
|
||||
.unwrap()
|
||||
.value
|
||||
.parse::<f32>()
|
||||
.unwrap();
|
||||
assert!((local - 50.).abs() < 0.001);
|
||||
let mut pixels = vec![0; 200 * 200];
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&initial.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
200,
|
||||
200,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pixels[100 * 200 + 100], 0xFF0000);
|
||||
let latest = session
|
||||
.execute("button.invalidate(\"paint\")", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap();
|
||||
let input = crate::events::Input {
|
||||
window: 1,
|
||||
session: latest.frame.session,
|
||||
layout_version: latest.frame.layout_version,
|
||||
mount: button.id,
|
||||
kind: "click".into(),
|
||||
point: Some([point.x, point.y]),
|
||||
press_point: Some([point.x, point.y]),
|
||||
delta: None,
|
||||
tab_drop: None,
|
||||
screen_position: None,
|
||||
};
|
||||
session.dispatch(&input).unwrap();
|
||||
assert_eq!(
|
||||
session.execute("count.value", "app.main").unwrap().value,
|
||||
"1"
|
||||
);
|
||||
let updated = session
|
||||
.execute("button.tint=\"#00FF00\"", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap();
|
||||
updated.validate().unwrap();
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&updated.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
200,
|
||||
200,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pixels[100 * 200 + 100], 0x00FF00);
|
||||
let blocked = session
|
||||
.execute(
|
||||
r##"
|
||||
blocker=Box3D(1.2,1.2,1.2,"#0000FF")
|
||||
blocker.z=1.5
|
||||
scene.children=ObjectList(panel,blocker)
|
||||
"##,
|
||||
"app.main",
|
||||
)
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap();
|
||||
blocked.validate().unwrap();
|
||||
let button = blocked
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.find(|mount| mount.panel.is_some() && mount.interactive)
|
||||
.unwrap();
|
||||
assert!(!button.contains(point));
|
||||
assert!(blocked.frame.hit_test(point).unwrap().scene3d_viewport);
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&blocked.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
200,
|
||||
200,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pixels[100 * 200 + 100], 0x0000FF);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_world_ui_can_embed_another_3d_viewport_and_preserve_ray_dispatch() {
|
||||
let (mut session, initial) = ReplSession::load(
|
||||
r##"
|
||||
class Actions {
|
||||
function select(self: Actions,event: Event): Unit { event.hit.mesh.color="#00FF00" }
|
||||
}
|
||||
cube=Box3D(2,2,2,"#0000FF")
|
||||
inner_scene=Scene3D(cube)
|
||||
inner_scene.ambient=1
|
||||
inner=Viewport3D(100,100,inner_scene)
|
||||
actions=Actions()
|
||||
inner.onClick(actions.select)
|
||||
panel=Panel3D(2,2,inner)
|
||||
panel.uiWidth=100
|
||||
panel.uiHeight=100
|
||||
panel.rotationY=20
|
||||
outer=Viewport3D(200,200,Scene3D(panel))
|
||||
page=Page()
|
||||
page.render(outer)
|
||||
"##,
|
||||
"app.main",
|
||||
315,
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
initial.validate().unwrap();
|
||||
let inner = initial
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.find(|mount| mount.scene3d_viewport && mount.panel.is_some())
|
||||
.unwrap();
|
||||
let point = crate::geometry::Point::new(100., 100.);
|
||||
assert_eq!(initial.frame.camera_at(point).unwrap().id, inner.id);
|
||||
assert_eq!(initial.frame.hit_test(point).unwrap().id, inner.id);
|
||||
let mut pixels = vec![0; 200 * 200];
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&initial.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
200,
|
||||
200,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pixels[100 * 200 + 100], 0x0000FF);
|
||||
let updated = session
|
||||
.dispatch(&crate::events::Input {
|
||||
window: 1,
|
||||
session: initial.frame.session,
|
||||
layout_version: initial.frame.layout_version,
|
||||
mount: inner.id,
|
||||
kind: "click".into(),
|
||||
point: Some([100., 100.]),
|
||||
press_point: Some([100., 100.]),
|
||||
delta: None,
|
||||
tab_drop: None,
|
||||
screen_position: None,
|
||||
})
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap();
|
||||
updated.validate().unwrap();
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&updated.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
200,
|
||||
200,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(pixels[100 * 200 + 100], 0x00FF00);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_ui_cycles_and_surface_nesting_are_rejected_before_publication() {
|
||||
let error = ReplSession::load(
|
||||
r##"
|
||||
panel=Panel3D(2,2,Button("cycle"))
|
||||
view=Viewport3D(200,200,Scene3D(panel))
|
||||
panel.content=view
|
||||
page=Page()
|
||||
page.render(view)
|
||||
"##,
|
||||
"app.main",
|
||||
316,
|
||||
1,
|
||||
)
|
||||
.err()
|
||||
.unwrap();
|
||||
assert!(
|
||||
format!("{error:#}").contains("cyclic Block composition"),
|
||||
"{error:#}"
|
||||
);
|
||||
let mut source = String::from("seed=Button(\"deep\")\n");
|
||||
for index in 0..9 {
|
||||
let child = if index == 0 {
|
||||
"seed".to_string()
|
||||
} else {
|
||||
format!("view{}", index - 1)
|
||||
};
|
||||
source.push_str(&format!("panel{index}=Panel3D(2,2,{child})\npanel{index}.uiWidth=100\npanel{index}.uiHeight=100\nview{index}=Viewport3D(100,100,Scene3D(panel{index}))\n"));
|
||||
}
|
||||
source.push_str("page=Page()\npage.render(view8)\n");
|
||||
let error = ReplSession::load(&source, "app.main", 317, 1)
|
||||
.err()
|
||||
.unwrap();
|
||||
assert!(
|
||||
format!("{error:#}").contains("3D UI viewport nesting exceeds 8"),
|
||||
"{error:#}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_ui_object_has_independent_mounts_and_texture_sizes_in_two_world_panels() {
|
||||
let (mut session, initial) = ReplSession::load(
|
||||
r##"
|
||||
shared=Button("shared UI")
|
||||
left=Panel3D(2,2,shared)
|
||||
left.x=-1.2
|
||||
left.uiWidth=100
|
||||
left.uiHeight=80
|
||||
right=Panel3D(2,2,shared)
|
||||
right.x=1.2
|
||||
right.uiWidth=200
|
||||
right.uiHeight=120
|
||||
view=Viewport3D(400,240,Scene3D(left,right))
|
||||
view.camera.z=6
|
||||
page=Page()
|
||||
page.render(view)
|
||||
"##,
|
||||
"app.main",
|
||||
318,
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
initial.validate().unwrap();
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("shared.mounts().count", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"2"
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("shared.mounts().at(0).bounds().width", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"100"
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("shared.mounts().at(1).bounds().width", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"200"
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("shared.mounts().at(0).bounds().height", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"80"
|
||||
);
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("shared.mounts().at(1).bounds().height", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"120"
|
||||
);
|
||||
let mounts: Vec<_> = initial
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.filter(|mount| mount.panel.is_some() && mount.interactive)
|
||||
.collect();
|
||||
let first = mounts[0]
|
||||
.panel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.project_point(crate::geometry::Point::new(50., 40.))
|
||||
.unwrap();
|
||||
let second = mounts[1]
|
||||
.panel
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.project_point(crate::geometry::Point::new(100., 60.))
|
||||
.unwrap();
|
||||
assert!(first.x < second.x);
|
||||
assert_eq!(initial.frame.hit_test(first).unwrap().id, mounts[0].id);
|
||||
assert_eq!(initial.frame.hit_test(second).unwrap().id, mounts[1].id);
|
||||
let mut pixels = vec![0; 400 * 240];
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&initial.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
400,
|
||||
240,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn world_ui_example_runs_with_window_constraints_and_programmable_actions() {
|
||||
let (mut session, document) = ReplSession::load_with_viewport(
|
||||
include_str!("../../../examples/world-ui.endor"),
|
||||
"app.main",
|
||||
319,
|
||||
1,
|
||||
Some(crate::layout::Size {
|
||||
width: 1024.,
|
||||
height: 800.,
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
document.validate().unwrap();
|
||||
assert!(
|
||||
document
|
||||
.frame
|
||||
.mounts
|
||||
.iter()
|
||||
.any(|mount| mount.panel.is_some() && mount.canvas_viewport)
|
||||
);
|
||||
session
|
||||
.execute("actions.turn()\nactions.fit()\nactions.reset()", "app.main")
|
||||
.unwrap()
|
||||
.document
|
||||
.unwrap()
|
||||
.validate()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
session
|
||||
.execute("panel.rotationY", "app.main")
|
||||
.unwrap()
|
||||
.value,
|
||||
"30"
|
||||
);
|
||||
assert_eq!(
|
||||
session.execute("view.camera.z", "app.main").unwrap().value,
|
||||
"7"
|
||||
);
|
||||
let mut pixels = vec![0; 1024 * 800];
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&document.display_list(1.).unwrap(),
|
||||
&mut pixels,
|
||||
1024,
|
||||
800,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scroll_axes_wrap_shared_text_and_clamp_only_the_enabled_offsets() {
|
||||
let (mut session, initial) = ReplSession::load(
|
||||
|
||||
Reference in New Issue
Block a user