feat: separate 2d scenes viewports and explicit cameras
This commit is contained in:
@@ -33,6 +33,11 @@ pub enum Kind {
|
||||
Row,
|
||||
Column,
|
||||
Overlay,
|
||||
Scene2D,
|
||||
CanvasViewport {
|
||||
width: f32,
|
||||
height: f32,
|
||||
},
|
||||
Button,
|
||||
Page,
|
||||
}
|
||||
@@ -129,6 +134,7 @@ pub struct Data {
|
||||
pub border: crate::layout::Insets,
|
||||
pub border_color: String,
|
||||
pub clip_children: bool,
|
||||
pub camera: Option<ObjectRef>,
|
||||
pub scroll_axis: crate::scroll::Axis,
|
||||
pub position: [f32; 2],
|
||||
pub surface: crate::page::Surface,
|
||||
@@ -168,6 +174,7 @@ impl Data {
|
||||
border: crate::layout::Insets::default(),
|
||||
border_color: "#D8DCE2".into(),
|
||||
clip_children: false,
|
||||
camera: None,
|
||||
scroll_axis: crate::scroll::Axis::Both,
|
||||
position: [0.0, 0.0],
|
||||
surface: crate::page::Surface::new(crate::page::Mode::Window),
|
||||
@@ -187,6 +194,8 @@ impl Data {
|
||||
Kind::Row
|
||||
| Kind::Column
|
||||
| Kind::Overlay
|
||||
| Kind::Scene2D
|
||||
| Kind::CanvasViewport { .. }
|
||||
| Kind::Button
|
||||
| Kind::Page
|
||||
| Kind::Scroll { .. }
|
||||
@@ -205,6 +214,9 @@ impl ObjectPayload for Data {
|
||||
"tessera-block-data"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
if let Some(camera) = self.camera {
|
||||
visit(camera);
|
||||
}
|
||||
if let Some(properties) = self.properties {
|
||||
visit(properties);
|
||||
}
|
||||
@@ -403,6 +415,11 @@ pub fn native_properties(data: &Data) -> Vec<(&'static str, Value)> {
|
||||
));
|
||||
}
|
||||
match &data.kind {
|
||||
Kind::CanvasViewport { width, height } => fields.extend([
|
||||
("width", number(*width)),
|
||||
("height", number(*height)),
|
||||
("camera", data.camera.map_or(Value::Nil, Value::Object)),
|
||||
]),
|
||||
Kind::Page => fields.extend([
|
||||
("mode", Value::Text(data.surface.mode.name().into())),
|
||||
("panX", number(data.surface.pan.x)),
|
||||
@@ -461,6 +478,24 @@ pub fn visual_data(store: &ObjectStore, data: &Data) -> Result<Data, VmError> {
|
||||
{
|
||||
return Err(VmError::InvalidArgument("box minimum exceeds maximum"));
|
||||
}
|
||||
if matches!(data.kind, Kind::CanvasViewport { .. }) {
|
||||
crate::viewport::camera(
|
||||
store,
|
||||
data.camera.ok_or(VmError::InvalidArgument(
|
||||
"CanvasViewport requires a Camera2D",
|
||||
))?,
|
||||
)?;
|
||||
if data.children.len() != 1
|
||||
|| !matches!(
|
||||
resolve(store, data.children[0].block)?.1.kind,
|
||||
Kind::Scene2D
|
||||
)
|
||||
{
|
||||
return Err(VmError::InvalidArgument(
|
||||
"CanvasViewport requires exactly one Scene2D",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
@@ -501,6 +536,13 @@ pub fn apply_visual_property(data: &mut Data, name: &str, value: &Value) -> Resu
|
||||
return Ok(());
|
||||
}
|
||||
match name {
|
||||
"camera" if matches!(data.kind, Kind::CanvasViewport { .. }) => {
|
||||
let Value::Object(camera) = value else {
|
||||
return Err(bad());
|
||||
};
|
||||
data.camera = Some(*camera);
|
||||
return Ok(());
|
||||
}
|
||||
"paddingTop" | "paddingRight" | "paddingBottom" | "paddingLeft" => {
|
||||
let index = match name {
|
||||
"paddingTop" => 0,
|
||||
@@ -646,6 +688,8 @@ pub fn apply_visual_property(data: &mut Data, name: &str, value: &Value) -> Resu
|
||||
return Ok(());
|
||||
}
|
||||
match (&mut data.kind, name) {
|
||||
(Kind::CanvasViewport { width, .. }, "width") => *width = number()?,
|
||||
(Kind::CanvasViewport { height, .. }, "height") => *height = number()?,
|
||||
(Kind::Text { value: text, .. }, "text") => {
|
||||
let Value::Text(value) = value else {
|
||||
return Err(bad());
|
||||
|
||||
Reference in New Issue
Block a user