ori_core/views/
painter.rs

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use ori_macro::Build;

use crate::{
    canvas::{Curve, FillRule, Paint},
    context::{BuildCx, DrawCx, EventCx, LayoutCx, RebuildCx},
    event::Event,
    layout::{Size, Space},
    rebuild::Rebuild,
    view::View,
};

/// Create a new [`Painter`] view.
pub fn painter<T>(draw: impl FnMut(&mut DrawCx, &mut T) + 'static) -> Painter<T> {
    Painter::new(draw)
}

/// Create a new [`Painter`] view that draws a circle.
pub fn circle<T>(radius: f32, paint: impl Into<Paint>) -> Painter<T> {
    Painter::new({
        let paint = paint.into();

        move |cx, _| {
            cx.fill(
                Curve::circle(cx.rect().center(), radius),
                FillRule::NonZero,
                paint.clone(),
            );
        }
    })
    .size(Size::all(radius * 2.0))
}

/// Create a new [`Painter`] view that draws an ellipse.
pub fn ellipse<T>(size: Size, paint: impl Into<Paint>) -> Painter<T> {
    Painter::new({
        let paint = paint.into();

        move |cx, _| {
            cx.fill(Curve::ellipse(cx.rect()), FillRule::NonZero, paint.clone());
        }
    })
    .size(size)
}

/// Create a new [`Painter`] view that draws a rectangle.
pub fn rect<T>(size: Size, paint: impl Into<Paint>) -> Painter<T> {
    Painter::new({
        let paint = paint.into();

        move |cx, _| {
            cx.fill(Curve::rect(cx.rect()), FillRule::NonZero, paint.clone());
        }
    })
    .size(size)
}

/// A view that draws something.
///
/// The painter takes up as much space as possible.
#[derive(Build, Rebuild)]
pub struct Painter<T> {
    /// The draw function.
    #[allow(clippy::type_complexity)]
    pub draw: Box<dyn FnMut(&mut DrawCx, &mut T)>,

    /// The size of the view.
    pub size: Option<Size>,
}

impl<T> Painter<T> {
    /// Create a new [`Painter`] view.
    pub fn new(draw: impl FnMut(&mut DrawCx, &mut T) + 'static) -> Self {
        Self {
            draw: Box::new(draw),
            size: None,
        }
    }
}

impl<T> View<T> for Painter<T> {
    type State = ();

    fn build(&mut self, _cx: &mut BuildCx, _data: &mut T) -> Self::State {}

    fn rebuild(&mut self, _state: &mut Self::State, cx: &mut RebuildCx, _data: &mut T, old: &Self) {
        Rebuild::rebuild(self, cx, old);
    }

    fn event(
        &mut self,
        _state: &mut Self::State,
        _cx: &mut EventCx,
        _data: &mut T,
        _event: &Event,
    ) -> bool {
        false
    }

    fn layout(
        &mut self,
        _state: &mut Self::State,
        _cx: &mut LayoutCx,
        _data: &mut T,
        space: Space,
    ) -> Size {
        match self.size {
            Some(size) => space.fit(size),
            None => space.max,
        }
    }

    fn draw(&mut self, _state: &mut Self::State, cx: &mut DrawCx, data: &mut T) {
        (self.draw)(cx, data);
    }
}