Skip to main content

retroglyph_core/surface/
styled.rs

1use crate::color::Style;
2use crate::grid::{Grid, Offset, Pos, Rect, Size};
3
4use super::Surface;
5
6/// A [`Surface`](crate::surface::Surface) with a [`Style`](crate::color::Style) bound in, returned by [`Surface::with_style`](crate::surface::Surface::with_style).
7///
8/// Every draw call omits the `style` argument the underlying [`Surface`](crate::surface::Surface) method would otherwise
9/// need, using the bound style instead. Reach back to the underlying surface (e.g. to call
10/// [`Surface::print_line`](crate::surface::Surface::print_line), whose per-span styles make a bound style meaningless) via
11/// [`StyledSurface::surface`](crate::surface::StyledSurface::surface).
12pub struct StyledSurface<'s, 'a> {
13    pub(super) surface: &'s mut Surface<'a>,
14    pub(super) style: Style,
15}
16
17impl<'a> StyledSurface<'_, 'a> {
18    /// The style every draw call on this view uses.
19    #[must_use]
20    pub const fn style(&self) -> Style {
21        self.style
22    }
23
24    /// Borrows the underlying [`Surface`](crate::surface::Surface) directly, for calls that need an explicit style (e.g.
25    /// [`Surface::print_line`](crate::surface::Surface::print_line)) or a capability [`StyledSurface`](crate::surface::StyledSurface) doesn't expose.
26    pub const fn surface(&mut self) -> &mut Surface<'a> {
27        self.surface
28    }
29
30    /// [`Surface::put`](crate::surface::Surface::put) using this view's bound style.
31    pub fn put(&mut self, pos: impl Into<Pos>, ch: char) {
32        self.surface.put(pos, ch, self.style);
33    }
34
35    /// [`Surface::print`](crate::surface::Surface::print) using this view's bound style.
36    pub fn print(&mut self, pos: impl Into<Pos>, text: &str) {
37        self.surface.print(pos, text, self.style);
38    }
39
40    /// [`Surface::fill_rect`](crate::surface::Surface::fill_rect) using this view's bound style.
41    pub fn fill_rect(&mut self, rect: Rect, ch: char) {
42        self.surface.fill_rect(rect, ch, self.style);
43    }
44
45    /// [`Surface::put_span`](crate::surface::Surface::put_span) using this view's bound style.
46    pub fn put_span<S: AsRef<str>>(&mut self, pos: impl Into<Pos>, rows: &[S]) -> Option<()> {
47        self.surface.put_span(pos, rows, self.style)
48    }
49
50    /// [`Surface::put_span_uniform`](crate::surface::Surface::put_span_uniform) using this view's bound style.
51    pub fn put_span_uniform(
52        &mut self,
53        pos: impl Into<Pos>,
54        size: impl Into<Size>,
55        anchor: char,
56        fill: char,
57    ) -> Option<()> {
58        self.surface
59            .put_span_uniform(pos, size, anchor, fill, self.style)
60    }
61
62    /// [`Surface::put_offset`](crate::surface::Surface::put_offset) using this view's bound style.
63    pub fn put_offset(&mut self, pos: impl Into<Pos>, offset: impl Into<Offset>, ch: char) {
64        self.surface.put_offset(pos, offset, ch, self.style);
65    }
66
67    /// [`Surface::put_signed`](crate::surface::Surface::put_signed) using this view's bound style.
68    pub fn put_signed(&mut self, pos: (i32, i32), ch: char) {
69        self.surface.put_signed(pos, ch, self.style);
70    }
71
72    /// [`Surface::blit`](crate::surface::Surface::blit). Takes no style: `grid`'s own tiles carry their own.
73    pub fn blit(&mut self, grid: &Grid, x: u16, y: u16) {
74        self.surface.blit(grid, x, y);
75    }
76
77    /// [`Surface::clear`](crate::surface::Surface::clear). Takes no style: cleared cells reset to [`Tile::default`](crate::tile::Tile::default).
78    pub fn clear(&mut self) {
79        self.surface.clear();
80    }
81
82    /// [`Surface::clear_region`](crate::surface::Surface::clear_region). Takes no style: cleared cells reset to
83    /// [`Tile::default`](crate::tile::Tile::default).
84    pub fn clear_region(&mut self, rect: Rect) {
85        self.surface.clear_region(rect);
86    }
87
88    /// [`Surface::width`](crate::surface::Surface::width) of the underlying surface.
89    #[must_use]
90    pub const fn width(&self) -> u16 {
91        self.surface.width()
92    }
93
94    /// [`Surface::height`](crate::surface::Surface::height) of the underlying surface.
95    #[must_use]
96    pub const fn height(&self) -> u16 {
97        self.surface.height()
98    }
99
100    /// [`Surface::area`](crate::surface::Surface::area) of the underlying surface.
101    #[must_use]
102    pub const fn area(&self) -> Rect {
103        self.surface.area()
104    }
105
106    /// [`Surface::clip_rect`](crate::surface::Surface::clip_rect) of the underlying surface.
107    #[must_use]
108    pub const fn clip_rect(&self) -> Rect {
109        self.surface.clip_rect()
110    }
111
112    /// [`Surface::area`](crate::surface::Surface::area) of the underlying surface, translated to its own local coordinate
113    /// space (`self.area().at_origin()`).
114    #[must_use]
115    pub const fn local_area(&self) -> Rect {
116        self.surface.area().at_origin()
117    }
118}