feat: separate 2d scenes viewports and explicit cameras

This commit is contained in:
2026-11-10 22:42:56 +08:00
parent 32d7ada3ab
commit 040b779f5f
29 changed files with 1309 additions and 70 deletions
+43
View File
@@ -38,12 +38,16 @@ pub const TYPE_NAMES: &[&str] = &[
"Block",
"ContainerBlock",
"ScrollBlock",
"ScrollView",
"TextBlock",
"RectBlock",
"Spacer",
"Row",
"Column",
"Overlay",
"Scene2D",
"CanvasViewport",
"Camera2D",
"Button",
"Page",
"Window",
@@ -145,6 +149,9 @@ enum Operation {
Row,
Column,
Overlay,
Scene2D,
CanvasViewport,
Camera2D,
Button,
Page,
SetText,
@@ -184,6 +191,9 @@ impl Operation {
Increment => (0, 0),
TextOf => (2, 4),
Scroll => (3, 3),
CanvasViewport => (3, 3),
Camera2D => (0, 3),
Scene2D => (0, MAX_UI_NODES),
Text => (1, 3),
Rect => (2, 4),
MoveTab | MoveWindow | OpenValue | Spacer | SetText | SetColor | Append | Remove
@@ -414,6 +424,31 @@ fn new_block(
new_typed_block(cx, data, session, None)
}
fn new_camera2d(
cx: &mut NativeContext<'_>,
session: &Shared,
requested: Option<ObjectRef>,
args: &[Value],
) -> Result<ObjectRef, VmError> {
let desktop = session
.lock()
.map_err(|_| invalid("UI session poisoned"))?
.desktop;
let definition = requested.unwrap_or(desktop_type(cx.store(), "Camera2D", desktop)?);
let camera = cx.allocate(crate::viewport::Camera2D)?;
cx.attach_type(camera, definition)?;
for (index, name, fallback) in [(0, "x", 0.0), (1, "y", 0.0), (2, "zoom", 1.0)] {
let value = args
.get(index)
.map(crate::scroll::coordinate)
.transpose()?
.unwrap_or(fallback);
cx.set_attr(camera, name, crate::native_visual::value(value))?;
}
crate::viewport::camera(cx.store(), camera)?;
Ok(camera)
}
fn new_typed_block(
cx: &mut NativeContext<'_>,
data: Data,
@@ -428,6 +463,8 @@ fn new_typed_block(
Kind::Row => "Row",
Kind::Column => "Column",
Kind::Overlay => "Overlay",
Kind::Scene2D => "Scene2D",
Kind::CanvasViewport { .. } => "CanvasViewport",
Kind::Button => "Button",
Kind::Page => "Page",
};
@@ -1197,6 +1234,9 @@ impl UiBridge {
if matches!(input.kind.as_str(), "page-pan" | "page-zoom") {
return self.dispatch_page_view(vm, input);
}
if matches!(input.kind.as_str(), "viewport-pan" | "viewport-zoom") {
return self.dispatch_viewport_view(vm, input);
}
if matches!(
input.kind.as_str(),
"tab-activate" | "tab-close" | "tab-detach" | "tab-drop"
@@ -1244,6 +1284,8 @@ impl UiBridge {
let delta = input
.delta
.ok_or_else(|| anyhow::anyhow!("scroll has no delta"))?;
let zoom = data.geometry.view.unwrap_or_default().zoom;
let delta = [delta[0] / zoom, delta[1] / zoom];
let method = vm.bind_attr(mount, "scrollBy")?;
let result = match method {
Value::Object(function) => vm.call_with_limits(
@@ -1849,6 +1891,7 @@ impl UiBridge {
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
crate::scroll::project(vm, &state.mounts, &root, &mut frame)?;
crate::viewport::project(&root, &mut frame)?;
}
// Native layout may replace the structural Frame; attach chrome only
// after either layout path has produced the final page geometry.