Skip to main content

fission_core/ui/widgets/
transform.rs

1use crate::lowering::{LoweringContext, NodeBuilder};
2use crate::ui::traits::Lower;
3use crate::ui::Node;
4use fission_ir::{LayoutOp, NodeId, Op};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Transform {
9    pub id: Option<NodeId>,
10    pub transform: [f32; 16],
11    pub child: Box<Node>,
12}
13
14impl Default for Transform {
15    fn default() -> Self {
16        Self {
17            id: None,
18            transform: [
19                1.0, 0.0, 0.0, 0.0,
20                0.0, 1.0, 0.0, 0.0,
21                0.0, 0.0, 1.0, 0.0,
22                0.0, 0.0, 0.0, 1.0,
23            ],
24            child: Box::new(crate::ui::widgets::spacer::Spacer::default().into_node()),
25        }
26    }
27}
28
29impl Transform {
30    pub fn new(child: Node, transform: [f32; 16]) -> Self {
31        Self {
32            child: Box::new(child),
33            transform,
34            ..Default::default()
35        }
36    }
37
38    pub fn into_node(self) -> Node {
39        Node::Transform(self)
40    }
41}
42
43impl Lower for Transform {
44    fn lower(&self, cx: &mut LoweringContext) -> NodeId {
45        let id = self.id.unwrap_or_else(|| cx.next_node_id());
46        
47        cx.push_scope(id);
48        let child_id = self.child.lower(cx);
49        cx.pop_scope();
50        
51        let mut builder = NodeBuilder::new(id, Op::Layout(LayoutOp::Transform {
52            transform: self.transform,
53        }));
54        
55        builder.add_child(child_id);
56        builder.build(cx)
57    }
58}