feat: compose interactive world ui across nested 3d viewports
This commit is contained in:
@@ -107,6 +107,7 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
|
||||
("OrthographicCamera", "Object", Some("orthographicCamera")),
|
||||
("OrbitController", "Object", Some("orbitController")),
|
||||
("Viewport3D", "Block", Some("viewport3D")),
|
||||
("Panel3D", "Object", Some("panel3D")),
|
||||
("RayHit3D", "Object", None),
|
||||
("Button", "ContainerBlock", Some("button")),
|
||||
("Page", "ContainerBlock", Some("page")),
|
||||
@@ -377,10 +378,12 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
|
||||
("OrbitController", "update", Operation::CameraControl),
|
||||
("Mesh3D", "translate", Operation::Translate3D),
|
||||
("Group3D", "translate", Operation::Translate3D),
|
||||
("Panel3D", "translate", Operation::Translate3D),
|
||||
("PerspectiveCamera", "translate", Operation::Translate3D),
|
||||
("OrthographicCamera", "translate", Operation::Translate3D),
|
||||
("Mesh3D", "rotate", Operation::Rotate3D),
|
||||
("Group3D", "rotate", Operation::Rotate3D),
|
||||
("Panel3D", "rotate", Operation::Rotate3D),
|
||||
("Block", "mounts", Operation::Mounts),
|
||||
("Block", "invalidate", Operation::Invalidate),
|
||||
("Block", "onClick", Operation::OnClick),
|
||||
|
||||
@@ -51,9 +51,18 @@ impl UiBridge {
|
||||
let delta = input
|
||||
.delta
|
||||
.ok_or_else(|| anyhow::anyhow!("3D camera delta required"))?;
|
||||
let local_delta = if input.kind == "scene-zoom" {
|
||||
[0., 0.]
|
||||
} else {
|
||||
let Some(delta) = geometry.logical_delta(input.point, delta) else {
|
||||
return Ok(());
|
||||
};
|
||||
delta
|
||||
};
|
||||
let point = input
|
||||
.point
|
||||
.map(|p| outer.inverse(crate::geometry::Point::new(p[0], p[1])))
|
||||
.and_then(|p| geometry.view_point(crate::geometry::Point::new(p[0], p[1]), false))
|
||||
.map(|p| outer.inverse(p))
|
||||
.unwrap_or(crate::geometry::Point::new(
|
||||
geometry.x + inset.left,
|
||||
geometry.y + inset.top,
|
||||
@@ -66,8 +75,8 @@ impl UiBridge {
|
||||
)?;
|
||||
vm.set_attr(gesture, "kind", Value::Text(input.kind.clone()))?;
|
||||
for (name, value) in [
|
||||
("dx", delta[0] / outer.zoom),
|
||||
("dy", delta[1] / outer.zoom),
|
||||
("dx", local_delta[0]),
|
||||
("dy", local_delta[1]),
|
||||
("factor", delta[0]),
|
||||
("width", (geometry.width - inset.horizontal()).max(0.)),
|
||||
("height", (geometry.height - inset.vertical()).max(0.)),
|
||||
@@ -141,7 +150,6 @@ impl UiBridge {
|
||||
let delta = input
|
||||
.delta
|
||||
.ok_or_else(|| anyhow::anyhow!("viewport input has no delta"))?;
|
||||
let outer = geometry.view.unwrap_or_default();
|
||||
let (mut x, mut y, mut zoom) = (old.x, old.y, old.zoom);
|
||||
if input.kind == "viewport-zoom" {
|
||||
let pointer = input
|
||||
@@ -152,7 +160,9 @@ impl UiBridge {
|
||||
geometry.contains(pointer),
|
||||
"zoom anchor is outside viewport"
|
||||
);
|
||||
let pointer = outer.inverse(pointer);
|
||||
let pointer = geometry
|
||||
.logical_point(pointer)
|
||||
.ok_or_else(|| anyhow::anyhow!("zoom anchor cannot be projected"))?;
|
||||
let node = block::snapshot(vm.object_store(), block)?;
|
||||
let inset = crate::layout::insets(&node);
|
||||
let local = [
|
||||
@@ -170,8 +180,11 @@ impl UiBridge {
|
||||
x += local[0] / old.zoom - local[0] / zoom;
|
||||
y += local[1] / old.zoom - local[1] / zoom;
|
||||
} else {
|
||||
x += delta[0] / outer.zoom / zoom;
|
||||
y += delta[1] / outer.zoom / zoom;
|
||||
let Some(delta) = geometry.logical_delta(input.point, delta) else {
|
||||
return Ok(());
|
||||
};
|
||||
x += delta[0] / zoom;
|
||||
y += delta[1] / zoom;
|
||||
}
|
||||
x = x.clamp(-MAX_DIMENSION as f32, MAX_DIMENSION as f32);
|
||||
y = y.clamp(-MAX_DIMENSION as f32, MAX_DIMENSION as f32);
|
||||
|
||||
@@ -30,6 +30,7 @@ impl UiNode {
|
||||
| UiKind::Canvas { children, .. }
|
||||
| UiKind::Scene2D { children, .. }
|
||||
| UiKind::CanvasViewport { children, .. }
|
||||
| UiKind::Viewport3D { children, .. }
|
||||
| UiKind::Row { children, .. }
|
||||
| UiKind::Button { children, .. }
|
||||
| UiKind::Scroll { children, .. } => children,
|
||||
@@ -87,14 +88,18 @@ impl UiNode {
|
||||
height: b,
|
||||
view: c,
|
||||
controls: g,
|
||||
panels: i,
|
||||
..
|
||||
},
|
||||
UiKind::Viewport3D {
|
||||
width: d,
|
||||
height: e,
|
||||
view: f,
|
||||
controls: h,
|
||||
panels: j,
|
||||
..
|
||||
},
|
||||
) => a == d && b == e && c == f && g == h,
|
||||
) => a == d && b == e && c == f && g == h && i == j && same_children(),
|
||||
(
|
||||
UiKind::CanvasViewport {
|
||||
width: a,
|
||||
@@ -199,6 +204,8 @@ pub enum UiKind {
|
||||
height: f32,
|
||||
view: Arc<crate::scene3d::View>,
|
||||
controls: bool,
|
||||
panels: Vec<crate::layout::Size>,
|
||||
children: Vec<Arc<UiNode>>,
|
||||
},
|
||||
Scene2D {
|
||||
positions: Vec<[f32; 2]>,
|
||||
|
||||
Reference in New Issue
Block a user