ori_core/context/
draw.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use std::{
    fmt::Display,
    ops::{Deref, DerefMut},
};

use crate::{
    canvas::{BorderRadius, BorderWidth, Canvas, Curve, FillRule, Mask, Paint, Stroke},
    layout::{Affine, Point, Rect, Size, Vector},
    text::{FontAttributes, Paragraph, TextAlign, TextWrap},
    view::{ViewId, ViewState},
    window::Window,
};

use super::BaseCx;

/// A context for drawing the view tree.
pub struct DrawCx<'a, 'b> {
    pub(crate) base: &'a mut BaseCx<'b>,
    pub(crate) view_state: &'a mut ViewState,
    pub(crate) transform: Affine,
    pub(crate) canvas: &'a mut Canvas,
    pub(crate) visible: Rect,
}

impl<'b> Deref for DrawCx<'_, 'b> {
    type Target = BaseCx<'b>;

    fn deref(&self) -> &Self::Target {
        self.base
    }
}

impl DerefMut for DrawCx<'_, '_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.base
    }
}

impl<'a, 'b> DrawCx<'a, 'b> {
    const EVERYTHING: Rect = Rect::new(Point::all(f32::NEG_INFINITY), Point::all(f32::INFINITY));

    /// Create a new draw context.
    pub fn new(
        base: &'a mut BaseCx<'b>,
        view_state: &'a mut ViewState,
        canvas: &'a mut Canvas,
    ) -> Self {
        let window_size = base.context::<Window>().size;
        let visible = Rect::min_size(Point::ZERO, window_size);

        Self {
            base,
            view_state,
            transform: Affine::IDENTITY,
            canvas,
            visible,
        }
    }

    /// Create a child context.
    pub fn child(&mut self) -> DrawCx<'_, 'b> {
        DrawCx {
            base: self.base,
            view_state: self.view_state,
            transform: self.transform,
            canvas: self.canvas,
            visible: self.visible,
        }
    }

    /// Check if a rect is visible.
    pub fn is_visible(&self, rect: Rect) -> bool {
        self.visible.intersects(rect)
    }

    /// Get the transform of the view.
    pub fn transform(&self) -> Affine {
        self.transform
    }

    /// Get the size of the view.
    pub fn size(&self) -> Size {
        self.view_state.size
    }

    /// Get the rect of the view in local space.
    pub fn rect(&self) -> Rect {
        Rect::min_size(Point::ZERO, self.size())
    }

    /// Get the canvas.
    pub fn canvas(&mut self) -> &mut Canvas {
        self.canvas
    }

    /// Draw a rectangle.
    pub fn fill_rect(&mut self, rect: Rect, paint: impl Into<Paint>) {
        if !self.is_visible(rect) {
            return;
        }

        self.canvas.rect(rect, paint.into());
    }

    /// Draw a trigger rectangle.
    pub fn trigger(&mut self, rect: Rect) {
        if !self.is_visible(rect) {
            return;
        }

        self.canvas.trigger(rect, self.id());
    }

    /// Fill a curve.
    pub fn fill(&mut self, curve: Curve, fill: FillRule, paint: impl Into<Paint>) {
        if !self.is_visible(curve.bounds()) {
            return;
        }

        self.canvas.fill(curve, fill, paint.into());
    }

    /// Stroke a curve.
    pub fn stroke(&mut self, curve: Curve, stroke: impl Into<Stroke>, paint: impl Into<Paint>) {
        let stroke = stroke.into();

        if !self.is_visible(curve.bounds().expand(stroke.width * 2.0)) {
            return;
        }

        self.canvas.stroke(curve, stroke, paint.into());
    }

    /// Draw some text.
    pub fn text(&mut self, text: impl Display, rect: Rect, font: FontAttributes) {
        let mut paragraph = Paragraph::new(1.2, TextAlign::Center, TextWrap::Word);
        paragraph.set_text(text, font);
        self.paragraph(&paragraph, rect);
    }

    /// Draw a paragraph.
    pub fn paragraph(&mut self, paragraph: &Paragraph, rect: Rect) {
        let lines = self.fonts().layout(paragraph, rect.width());

        let mut bounds: Option<Rect> = None;

        for line in lines.iter() {
            let line_rect = Rect::new(
                Point::new(line.left, line.baseline - line.ascent),
                Point::new(line.left + line.width, line.baseline + line.descent),
            );

            if let Some(ref mut rect) = bounds {
                *rect = rect.union(line_rect);
            } else {
                bounds = Some(line_rect);
            }
        }

        (self.canvas).paragraph(paragraph.clone(), rect, bounds.unwrap_or_default());
    }

    /// Draw a rectangle with rounded corners and a border.
    pub fn quad(
        &mut self,
        rect: Rect,
        paint: impl Into<Paint>,
        border_radius: impl Into<BorderRadius>,
        border_width: impl Into<BorderWidth>,
        border_paint: impl Into<Paint>,
    ) {
        let radius = border_radius.into();
        let width = border_width.into();
        let rect = rect.round();

        let mut curve = Curve::new();
        curve.push_rect_with_radius(rect, radius);

        self.fill(curve, FillRule::NonZero, paint);

        let mut curve = Curve::new();
        curve.push_rect_with_borders(rect, radius, width);

        self.fill(curve, FillRule::NonZero, border_paint);
    }

    /// Draw a canvas.
    pub fn draw_canvas(&mut self, canvas: Canvas) {
        self.canvas.draw_canvas(canvas);
    }

    /// Draw an overlay, at `index`.
    pub fn overlay<T>(&mut self, index: i32, f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T) -> T {
        self.canvas.overlay(index, |canvas| {
            let mut cx = DrawCx {
                base: self.base,
                view_state: self.view_state,
                transform: Affine::IDENTITY,
                canvas,
                visible: Self::EVERYTHING,
            };

            f(&mut cx)
        })
    }

    /// Draw a layer, with a transform, mask, and view.
    pub fn layer<T>(
        &mut self,
        transform: Affine,
        mask: Option<Mask>,
        view: Option<ViewId>,
        f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T,
    ) -> T {
        let visible = match self.visible.is_infinite() {
            false => self.visible.transform(transform.inverse()),
            true => self.visible,
        };

        let visible = match mask {
            Some(ref mask) => visible.intersection(mask.curve.bounds()),
            None => visible,
        };

        self.canvas.layer(transform, mask, view, |canvas| {
            let mut cx = DrawCx {
                base: self.base,
                view_state: self.view_state,
                transform: self.transform * transform,
                canvas,
                visible,
            };

            f(&mut cx)
        })
    }

    /// Draw a hoverable layer.
    pub fn hoverable<T>(&mut self, f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T) -> T {
        self.layer(Affine::IDENTITY, None, Some(self.id()), f)
    }

    /// Draw a layer with a transform.
    pub fn transformed<T>(
        &mut self,
        transform: Affine,
        f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T,
    ) -> T {
        let visible = match self.visible.is_infinite() {
            false => self.visible.transform(transform.inverse()),
            true => self.visible,
        };

        self.canvas.layer(transform, None, None, |canvas| {
            let mut cx = DrawCx {
                base: self.base,
                view_state: self.view_state,
                transform: self.transform * transform,
                canvas,
                visible,
            };

            f(&mut cx)
        })
    }

    /// Draw a layer with a translation.
    pub fn translated<T>(
        &mut self,
        translation: Vector,
        f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T,
    ) -> T {
        self.transformed(Affine::translate(translation), f)
    }

    /// Draw a layer with a rotation.
    pub fn rotated<T>(&mut self, angle: f32, f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T) -> T {
        self.transformed(Affine::rotate(angle), f)
    }

    /// Draw a layer with a scale.
    pub fn scaled<T>(&mut self, scale: Vector, f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T) -> T {
        self.transformed(Affine::scale(scale), f)
    }

    /// Draw a layer with a mask.
    pub fn masked<T>(
        &mut self,
        mask: impl Into<Mask>,
        f: impl FnOnce(&mut DrawCx<'_, 'b>) -> T,
    ) -> T {
        self.layer(Affine::IDENTITY, Some(mask.into()), None, f)
    }
}