1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::rc::Rc;

use pax_manifest::UniqueTemplateNodeIdentifier;

use crate::{
    api::{Rotation, Window},
    math::{Point2, Space, Transform2},
    ExpandedNode,
};

pub struct NodeInterface {
    inner: Rc<ExpandedNode>,
}

impl From<Rc<ExpandedNode>> for NodeInterface {
    fn from(expanded_node: Rc<ExpandedNode>) -> Self {
        Self {
            inner: expanded_node,
        }
    }
}

pub struct NodeLocal;

pub struct Properties {
    pub local_rotation: Rotation,
}

impl Space for NodeLocal {}

impl NodeInterface {
    pub fn global_id(&self) -> Option<UniqueTemplateNodeIdentifier> {
        let base = self.inner.instance_node.base();
        base.template_node_identifier.clone()
    }

    pub fn properties(&self) -> Properties {
        let cp = self.inner.get_common_properties();
        let rot = cp
            .borrow()
            .rotate
            .as_ref()
            .map(|p| p.get().clone())
            .unwrap_or(Rotation::default());
        Properties {
            local_rotation: rot,
        }
    }

    pub fn origin(&self) -> Option<Point2<Window>> {
        let common_props = self.inner.get_common_properties();
        let common_props = common_props.borrow();
        let lp = self.inner.layout_properties.borrow();
        let tab = &lp.as_ref()?.computed_tab;
        let p_anchor = Point2::new(
            common_props
                .anchor_x
                .as_ref()
                .map(|x| x.get().get_pixels(tab.bounds.0))
                .unwrap_or(0.0),
            common_props
                .anchor_y
                .as_ref()
                .map(|y| y.get().get_pixels(tab.bounds.1))
                .unwrap_or(0.0),
        );
        let origin_window = tab.transform * p_anchor;
        Some(origin_window)
    }

    pub fn transform(&self) -> Option<Transform2<Window, NodeLocal>> {
        let up_lp = self.inner.layout_properties.borrow_mut();
        if let Some(lp) = up_lp.as_ref() {
            Some(lp.computed_tab.transform.inverse())
        } else {
            None
        }
    }

    pub fn bounding_points(&self) -> Option<[Point2<Window>; 4]> {
        let lp = self.inner.layout_properties.borrow();
        if let Some(layout) = lp.as_ref() {
            Some(layout.computed_tab.corners())
        } else {
            None
        }
    }

    pub fn is_descendant_of(&self, node: &NodeInterface) -> bool {
        self.inner.is_descendant_of(&node.inner.id_chain)
    }
}