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
use crate::makepad_draw::*;


live_design!{
    import makepad_draw::shader::std::*;
    
    DrawRect = {{DrawRect}} {
        fn pixel(self) -> vec4 {
            let sdf = Sdf2d::viewport(self.pos * self.rect_size)
            sdf.rect(0., 0., self.rect_size.x, self.rect_size.y);
            sdf.stroke(self.color, 1.0);
            return sdf.result;
            //return vec4(self.color.xyz * self.color.w, self.color.w)
        }
        draw_depth: 20.0
    }
    
    DebugView = {{DebugView}} {  
        label: {
            text_style: {
                font_size: 6
            },
            color: #a
        }
    }
}

#[derive(Live, LiveHook)]
#[repr(C)]
pub struct DrawRect {
    #[deref] draw_super: DrawQuad,
    #[live] color: Vec4,
}


#[derive(Live, LiveHook)]
pub struct DebugView {
    #[live] draw_list: DrawList2d,
    #[live] rect: DrawRect,
    #[live] label: DrawText
}

impl DebugView {
    pub fn handle_event(&mut self, cx: &mut Cx, _event: &Event) {
        if cx.debug.has_data() {
            self.draw_list.redraw(cx);
        }
    }
    
    pub fn draw(&mut self, cx: &mut Cx2d) {
        if !self.draw_list.begin(cx, Walk::default()).is_redrawing() {
            return
        }
        let debug = cx.debug.clone();
        let rects = debug.take_rects();
        for (rect, color) in rects {
            self.rect.color = color;
            self.rect.draw_abs(cx, rect);
        }
        
        let points = debug.take_points();
        let point_size = dvec2(1.0, 1.0);
        for (point, color) in points {
            self.rect.color = color;
            let rect = Rect {pos: point - 0.5 * point_size, size: point_size};
            self.rect.draw_abs(cx, rect);
        }
        
        let labels = debug.take_labels();
        let point_size = dvec2(1.0, 1.0);
        for (point, color, label) in labels {
            self.rect.color = color;
            let rect = Rect {pos: point - 0.5 * point_size, size: point_size};
            self.rect.draw_abs(cx, rect);
            self.label.draw_abs(cx, point, &label);
        }
        
        self.draw_list.end(cx);
        
    }
}