kas_core/draw/
draw_rounded.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Drawing APIs — draw rounded
7
8use super::color::Rgba;
9use super::{Draw, DrawIface, DrawImpl, DrawSharedImpl, PassId};
10use crate::geom::{Quad, Vec2};
11
12/// Extended draw interface for [`DrawIface`] providing rounded drawing
13///
14/// All methods draw some feature.
15pub trait DrawRounded: Draw {
16    /// Draw a line with rounded ends and uniform colour
17    ///
18    /// This command draws a line segment between the points `p1` and `p2`.
19    /// Pixels within the given `radius` of this segment are drawn, resulting
20    /// in rounded ends and width `2 * radius`.
21    ///
22    /// Note that for rectangular, axis-aligned lines, [`DrawImpl::rect`] should be
23    /// preferred.
24    fn rounded_line(&mut self, p1: Vec2, p2: Vec2, radius: f32, col: Rgba);
25
26    /// Draw a circle or oval of uniform colour
27    ///
28    /// More generally, this shape is an axis-aligned oval which may be hollow.
29    ///
30    /// The `inner_radius` parameter gives the inner radius relative to the
31    /// outer radius: a value of `0.0` will result in the whole shape being
32    /// painted, while `1.0` will result in a zero-width line on the outer edge.
33    fn circle(&mut self, rect: Quad, inner_radius: f32, col: Rgba);
34
35    /// Draw a circle or oval with two colours
36    ///
37    /// More generally, this shape is an axis-aligned oval which may be hollow.
38    ///
39    /// Colour `col1` is used at the centre and `col2` at the edge with linear
40    /// blending. The edge is not anti-aliased.
41    ///
42    /// Note: this is drawn *before* other drawables, allowing it to be used
43    /// for shadows without masking.
44    fn circle_2col(&mut self, rect: Quad, col1: Rgba, col2: Rgba);
45
46    /// Draw a frame with rounded corners and uniform colour
47    ///
48    /// All drawing occurs within the `outer` rect and outside of the `inner`
49    /// rect. Corners are circular (or more generally, ovular), centered on the
50    /// inner corners.
51    ///
52    /// The `inner_radius` parameter gives the inner radius relative to the
53    /// outer radius: a value of `0.0` will result in the whole shape being
54    /// painted, while `1.0` will result in a zero-width line on the outer edge.
55    /// When `inner_radius > 0`, the frame will be visually thinner than the
56    /// allocated area.
57    fn rounded_frame(&mut self, outer: Quad, inner: Quad, inner_radius: f32, col: Rgba);
58
59    /// Draw a frame with rounded corners with two colours
60    ///
61    /// This is a variant of `rounded_frame` which blends between two colours,
62    /// `c1` at the inner edge and `c2` at the outer edge.
63    ///
64    /// Note: this is drawn *before* other drawables, allowing it to be used
65    /// for shadows without masking.
66    fn rounded_frame_2col(&mut self, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba);
67}
68
69impl<'a, DS: DrawSharedImpl> DrawRounded for DrawIface<'a, DS>
70where
71    DS::Draw: DrawRoundedImpl,
72{
73    #[inline]
74    fn rounded_line(&mut self, p1: Vec2, p2: Vec2, radius: f32, col: Rgba) {
75        self.draw.rounded_line(self.pass, p1, p2, radius, col);
76    }
77    #[inline]
78    fn circle(&mut self, rect: Quad, inner_radius: f32, col: Rgba) {
79        self.draw.circle(self.pass, rect, inner_radius, col);
80    }
81    #[inline]
82    fn circle_2col(&mut self, rect: Quad, col1: Rgba, col2: Rgba) {
83        self.draw.circle_2col(self.pass, rect, col1, col2);
84    }
85    #[inline]
86    fn rounded_frame(&mut self, outer: Quad, inner: Quad, inner_radius: f32, col: Rgba) {
87        self.draw
88            .rounded_frame(self.pass, outer, inner, inner_radius, col);
89    }
90    #[inline]
91    fn rounded_frame_2col(&mut self, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba) {
92        self.draw
93            .rounded_frame_2col(self.pass, outer, inner, c1, c2);
94    }
95}
96
97/// Implementation target for [`DrawRounded`]
98///
99/// This trait is an extension over [`DrawImpl`] providing rounded shapes.
100///
101/// The primitives provided by this trait are partially transparent.
102/// If the implementation buffers draw commands, it should draw these
103/// primitives after solid primitives.
104pub trait DrawRoundedImpl: DrawImpl {
105    /// Draw a line with rounded ends and uniform colour
106    fn rounded_line(&mut self, pass: PassId, p1: Vec2, p2: Vec2, radius: f32, col: Rgba);
107
108    /// Draw a circle or oval of uniform colour
109    fn circle(&mut self, pass: PassId, rect: Quad, inner_radius: f32, col: Rgba);
110
111    /// Draw a circle or oval with two colours
112    fn circle_2col(&mut self, pass: PassId, rect: Quad, col1: Rgba, col2: Rgba);
113
114    /// Draw a frame with rounded corners and uniform colour
115    fn rounded_frame(&mut self, pass: PassId, outer: Quad, inner: Quad, r1: f32, col: Rgba);
116
117    /// Draw a frame with rounded corners with two colours
118    fn rounded_frame_2col(&mut self, pass: PassId, outer: Quad, inner: Quad, c1: Rgba, c2: Rgba);
119}