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, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
20            ],
21            child: Box::new(crate::ui::widgets::spacer::Spacer::default().into_node()),
22        }
23    }
24}
25
26impl Transform {
27    pub fn new(child: Node, transform: [f32; 16]) -> Self {
28        Self {
29            child: Box::new(child),
30            transform,
31            ..Default::default()
32        }
33    }
34
35    pub fn into_node(self) -> Node {
36        Node::Transform(self)
37    }
38}
39
40impl Lower for Transform {
41    fn lower(&self, cx: &mut LoweringContext) -> NodeId {
42        let id = self.id.unwrap_or_else(|| cx.next_node_id());
43
44        cx.push_scope(id);
45        let child_id = self.child.lower(cx);
46        cx.pop_scope();
47
48        let mut builder = NodeBuilder::new(
49            id,
50            Op::Layout(LayoutOp::Transform {
51                transform: self.transform,
52            }),
53        );
54
55        builder.add_child(child_id);
56        builder.build(cx)
57    }
58}