makepad_widgets/
root.rs

1
2use {
3    crate::{
4        widget::*,
5        makepad_derive_widget::*,
6        makepad_draw::*,
7    }
8};
9
10live_design!{
11    link widgets;
12    
13    use link::widgets::*;
14    use link::designer::Designer;
15    
16    pub RootBase = {{Root}} {}
17    pub Root = <RootBase> {
18        design_window = <Designer> {}
19        xr_hands = <XrHands>{}
20    }
21}
22
23#[derive(Live, LiveRegisterWidget, WidgetRef)]
24pub struct Root {
25    #[rust] components: ComponentMap<LiveId, WidgetRef>,
26    #[rust(DrawList::new(cx))] xr_draw_list: DrawList,
27    #[live] xr_pass: Pass,
28}
29 
30impl LiveHook for Root {
31    fn apply_value_instance(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
32        let id = nodes[index].id;
33        match apply.from {
34            ApplyFrom::NewFromDoc {..} | ApplyFrom::UpdateFromDoc {..} => {
35                if nodes[index].origin.has_prop_type(LivePropType::Instance) {
36                    // only open the design window 
37                    if id == live_id!(design_window) && !cx.in_makepad_studio(){
38                        return nodes.skip_node(index);
39                    }
40                    if id == live_id!(xr_hands) && !cx.os_type().has_xr_mode(){
41                        return nodes.skip_node(index);
42                    }
43                    return self.components.get_or_insert(cx, id, | cx | {WidgetRef::new(cx)})
44                        .apply(cx, apply, index, nodes);
45                }
46                else {
47                    cx.apply_error_no_matching_field(live_error_origin!(), index, nodes);
48                }
49            }
50            _ => ()
51        }
52        nodes.skip_node(index)
53    }
54}
55
56
57impl WidgetNode for Root{
58    fn redraw(&mut self, cx: &mut Cx) {
59        for component in self.components.values_mut() {
60            component.redraw(cx);
61        }
62    }
63    
64    fn area(&self)->Area{Area::Empty}
65    
66    fn walk(&mut self, _cx:&mut Cx) -> Walk {Walk::default()}
67        
68    fn find_widgets(&self, path: &[LiveId], cached: WidgetCache, results:&mut WidgetSet){
69        if path.len() != 0{
70            if let Some(component) = self.components.get(&path[0]) {
71                if path.len() == 1{
72                    results.push(component.clone());
73                }
74                else{
75                    component.find_widgets(&path[1..], cached, results);
76                }
77            }
78        }
79        for component in self.components.values() {
80            component.find_widgets(path, cached, results);
81        }
82    }
83    
84    fn uid_to_widget(&self, uid:WidgetUid)->WidgetRef{
85        for component in self.components.values() {
86            let x = component.uid_to_widget(uid);
87            if !x.is_empty(){return x}
88        }
89        WidgetRef::empty()
90    }
91        
92}
93
94impl Widget for Root {
95    
96    fn handle_event(&mut self, cx: &mut Cx, event: &Event, scope: &mut Scope) {
97        if let Event::Draw(e) = event {
98            if cx.in_xr_mode(){
99                if  !e.xr_state.is_some(){
100                    return
101                }
102                let mut cx_draw = CxDraw::new(cx, e);
103                let cx = &mut Cx3d::new(&mut cx_draw);
104                // lets begin a 3D drawlist in the global context
105                self.xr_pass.set_as_xr_pass(cx);
106                cx.begin_pass(&self.xr_pass, Some(4.0));
107                self.xr_draw_list.begin_always(cx);
108                self.draw_3d_all(cx, scope);
109                self.xr_draw_list.end(cx);
110                cx.end_pass(&self.xr_pass);
111                return
112            }
113            else{
114                let mut cx_draw = CxDraw::new(cx, e);
115                let cx = &mut Cx2d::new(&mut cx_draw);
116                self.draw_all(cx, scope);
117                return
118            }
119        }
120        
121        for component in self.components.values_mut() {
122            component.handle_event(cx, event, scope);
123        }
124    }
125    
126    fn draw_3d(&mut self, cx: &mut Cx3d, scope:&mut Scope)->DrawStep{
127        for component in self.components.values(){
128            component.draw_3d_all(cx, scope);
129        }
130        DrawStep::done()
131    }
132    
133    fn draw_walk(&mut self, cx: &mut Cx2d, scope: &mut Scope, walk: Walk) -> DrawStep {
134        for component in self.components.values(){
135            component.draw_walk_all(cx, scope, walk);
136        }
137        DrawStep::done()
138    }
139}