makepad_widgets/
vectorline.rs

1use {
2    crate::{
3        makepad_derive_widget::*,
4        makepad_draw::*,
5        widget::*
6    }
7};
8
9live_design! {
10    VectorLine = {{VectorLine}} {
11        width: Fill,
12        height: Fill,
13        line_align: Top,
14        color: #f,
15        line_width: 10
16    }
17}
18
19#[derive(Copy, Clone, Debug, Live, LiveHook)]
20#[live_ignore]
21pub enum LineAlign {
22    Free,
23    Left,
24    #[pick]
25    Top,
26    DiagonalBottomLeftTopRight,
27    DiagonalTopLeftBottomRight,
28    Right,
29    Bottom,
30    VerticalCenter,
31    HorizontalCenter,
32}
33
34#[derive(Live, LiveHook, Widget)]
35pub struct VectorLine {
36    #[animator]
37    animator: Animator,
38    #[walk]
39    walk: Walk,
40    #[live]
41    draw_ls: DrawLine,
42    #[redraw] #[rust]
43    area: Area,
44    #[live(15.0)]
45    line_width: f64,
46    #[live]
47    color: Vec4,
48    #[live(true)]
49    contained: bool,
50    #[live(LineAlign::Top)]
51    line_align: LineAlign,
52    #[rust(dvec2(350., 10.))]
53    line_start: DVec2,
54    #[rust(dvec2(1000., 1440.))]
55    line_end: DVec2,
56}
57
58#[derive(Clone, DefaultNone)]
59pub enum LineAction {
60    None,
61    Pressed,
62    Clicked,
63    Released,
64}
65
66impl Widget for VectorLine {
67    fn handle_event(
68        &mut self,
69        cx: &mut Cx,
70        event: &Event,
71        _scope: &mut Scope,
72    ) {
73        self.animator_handle_event(cx, event);
74    }
75
76    fn draw_walk(&mut self, cx: &mut Cx2d, _scope: &mut Scope, walk: Walk) -> DrawStep {
77        // lets draw a bunch of quads
78        let fullrect = cx.walk_turtle_with_area(&mut self.area, walk);
79
80        let mut rect = fullrect;
81        let hw = self.line_width / 2.;
82        if self.contained == false {
83            rect.size.x += self.line_width;
84            rect.size.y += self.line_width;
85            rect.pos.x -= hw;
86            rect.pos.y -= hw;
87        }
88        // self.line_width = 10.5;
89
90        let mut line_start = self.line_start;
91        let mut line_end = self.line_end;
92
93        match self.line_align {
94            LineAlign::Top => {
95                line_start = dvec2(rect.pos.x + hw, rect.pos.y + hw);
96                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + hw);
97            }
98            LineAlign::Bottom => {
99                line_start = dvec2(rect.pos.x + hw, rect.pos.y + rect.size.y - hw);
100                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + rect.size.y - hw);
101            }
102            LineAlign::Right => {
103                line_start = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + hw);
104                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + rect.size.y - hw);
105            }
106            LineAlign::Left => {
107                line_start = dvec2(rect.pos.x + hw, rect.pos.y + hw);
108                line_end = dvec2(rect.pos.x + hw, rect.pos.y + rect.size.y - hw);
109            }
110            LineAlign::HorizontalCenter => {
111                line_start = dvec2(rect.pos.x + hw, rect.pos.y + rect.size.y / 2.);
112                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + rect.size.y / 2.);
113            }
114            LineAlign::VerticalCenter => {
115                line_start = dvec2(rect.pos.x + rect.size.x / 2., rect.pos.y + hw);
116                line_end = dvec2(rect.pos.x + rect.size.x / 2., rect.pos.y + rect.size.y - hw);
117            }
118            LineAlign::DiagonalTopLeftBottomRight => {
119                line_start = dvec2(rect.pos.x + hw, rect.pos.y + hw);
120                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + rect.size.y - hw);
121            }
122            LineAlign::DiagonalBottomLeftTopRight => {
123                line_start = dvec2(rect.pos.x + hw, rect.pos.y + rect.size.y - hw);
124                line_end = dvec2(rect.pos.x + rect.size.x - hw, rect.pos.y + hw);
125            }
126            _ => {}
127        }
128
129        self.draw_ls
130            .draw_line_abs(cx, line_start, line_end, self.color, self.line_width);
131
132        DrawStep::done()
133    }
134}