kas_theme/draw_shaded.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 — shaded drawing
7
8use kas::draw::color::Rgba;
9use kas::draw::{DrawIface, DrawImpl, DrawSharedImpl, PassId};
10use kas::geom::Quad;
11
12/// Extension trait providing shaded drawing over [`DrawIface`]
13///
14/// All methods draw some feature.
15///
16/// Methods are parameterised via a pair of normals, `(inner, outer)`, which
17/// specify the surface normal direction at inner and outer edges of the feature
18/// respectively (with interpolation between these edges). These have values
19/// from the closed range `[-1, 1]`, where -1 points towards the inside of the
20/// feature, 1 points away from the feature, and 0 is perpendicular to the
21/// screen towards the viewer.
22pub trait DrawShaded {
23 /// Add a shaded square to the draw buffer
24 ///
25 /// For shading purposes, the mid-point is considered the inner edge.
26 fn shaded_square(&mut self, rect: Quad, norm: (f32, f32), col: Rgba);
27
28 /// Add a shaded circle to the draw buffer
29 ///
30 /// For shading purposes, the mid-point is considered the inner edge.
31 fn shaded_circle(&mut self, rect: Quad, norm: (f32, f32), col: Rgba);
32
33 /// Add a shaded frame with square corners to the draw buffer
34 fn shaded_square_frame(
35 &mut self,
36 outer: Quad,
37 inner: Quad,
38 norm: (f32, f32),
39 outer_col: Rgba,
40 inner_col: Rgba,
41 );
42
43 /// Add a shaded frame with rounded corners to the draw buffer
44 fn shaded_round_frame(&mut self, outer: Quad, inner: Quad, norm: (f32, f32), col: Rgba);
45}
46
47impl<'a, DS: DrawSharedImpl> DrawShaded for DrawIface<'a, DS>
48where
49 DS::Draw: DrawShadedImpl,
50{
51 fn shaded_square(&mut self, rect: Quad, norm: (f32, f32), col: Rgba) {
52 self.draw.shaded_square(self.pass, rect, norm, col);
53 }
54
55 fn shaded_circle(&mut self, rect: Quad, norm: (f32, f32), col: Rgba) {
56 self.draw.shaded_circle(self.pass, rect, norm, col);
57 }
58
59 fn shaded_square_frame(
60 &mut self,
61 outer: Quad,
62 inner: Quad,
63 norm: (f32, f32),
64 outer_col: Rgba,
65 inner_col: Rgba,
66 ) {
67 self.draw
68 .shaded_square_frame(self.pass, outer, inner, norm, outer_col, inner_col);
69 }
70
71 fn shaded_round_frame(&mut self, outer: Quad, inner: Quad, norm: (f32, f32), col: Rgba) {
72 self.draw
73 .shaded_round_frame(self.pass, outer, inner, norm, col);
74 }
75}
76
77/// Drawing commands for shaded shapes
78///
79/// This trait is an extension over [`DrawImpl`] providing solid shaded shapes.
80///
81/// Some drawing primitives (the "round" ones) are partially transparent.
82/// If the implementation buffers draw commands, it should draw these
83/// primitives after solid primitives.
84///
85/// Methods are parameterised via a pair of normals, `(inner, outer)`. These may
86/// have values from the closed range `[-1, 1]`, where -1 points inwards,
87/// 0 is perpendicular to the screen towards the viewer, and 1 points outwards.
88#[cfg_attr(not(feature = "internal_doc"), doc(hidden))]
89#[cfg_attr(doc_cfg, doc(cfg(internal_doc)))]
90pub trait DrawShadedImpl: DrawImpl {
91 /// Add a shaded square to the draw buffer
92 fn shaded_square(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba);
93
94 /// Add a shaded circle to the draw buffer
95 fn shaded_circle(&mut self, pass: PassId, rect: Quad, norm: (f32, f32), col: Rgba);
96
97 /// Add a square shaded frame to the draw buffer.
98 fn shaded_square_frame(
99 &mut self,
100 pass: PassId,
101 outer: Quad,
102 inner: Quad,
103 norm: (f32, f32),
104 outer_col: Rgba,
105 inner_col: Rgba,
106 );
107
108 /// Add a rounded shaded frame to the draw buffer.
109 fn shaded_round_frame(
110 &mut self,
111 pass: PassId,
112 outer: Quad,
113 inner: Quad,
114 norm: (f32, f32),
115 col: Rgba,
116 );
117}