Skip to main content

strat9_kernel/arch/x86_64/vga/
canvas.rs

1//! Canvas convenience drawing API : a thin wrapper around VgaWriter for drawing primitives.
2
3use super::{
4    api::*,
5    status_line::{draw_system_status_line, ui_draw_status_bar},
6    types::*,
7};
8
9pub struct Canvas {
10    fg: RgbColor,
11    bg: RgbColor,
12}
13
14impl Default for Canvas {
15    /// Builds a default instance.
16    fn default() -> Self {
17        Self {
18            fg: RgbColor::LIGHT_GREY,
19            bg: RgbColor::BLACK,
20        }
21    }
22}
23
24impl Canvas {
25    /// Creates a new instance.
26    pub const fn new(fg: RgbColor, bg: RgbColor) -> Self {
27        Self { fg, bg }
28    }
29
30    /// Sets fg.
31    pub fn set_fg(&mut self, fg: RgbColor) {
32        self.fg = fg;
33    }
34
35    /// Sets bg.
36    pub fn set_bg(&mut self, bg: RgbColor) {
37        self.bg = bg;
38    }
39
40    /// Sets colors.
41    pub fn set_colors(&mut self, fg: RgbColor, bg: RgbColor) {
42        self.fg = fg;
43        self.bg = bg;
44    }
45
46    /// Sets clip rect.
47    pub fn set_clip_rect(&self, x: usize, y: usize, w: usize, h: usize) {
48        set_clip_rect(x, y, w, h);
49    }
50
51    /// Performs the reset clip rect operation.
52    pub fn reset_clip_rect(&self) {
53        reset_clip_rect();
54    }
55
56    /// Performs the clear operation.
57    pub fn clear(&self) {
58        fill_rect(0, 0, width(), height(), self.bg);
59    }
60
61    /// Performs the pixel operation.
62    pub fn pixel(&self, x: usize, y: usize) {
63        draw_pixel(x, y, self.fg);
64    }
65
66    /// Performs the line operation.
67    pub fn line(&self, x0: isize, y0: isize, x1: isize, y1: isize) {
68        draw_line(x0, y0, x1, y1, self.fg);
69    }
70
71    /// Performs the rect operation.
72    pub fn rect(&self, x: usize, y: usize, w: usize, h: usize) {
73        draw_rect(x, y, w, h, self.fg);
74    }
75
76    /// Performs the fill rect operation.
77    pub fn fill_rect(&self, x: usize, y: usize, w: usize, h: usize) {
78        fill_rect(x, y, w, h, self.fg);
79    }
80
81    /// Performs the fill rect alpha operation.
82    pub fn fill_rect_alpha(&self, x: usize, y: usize, w: usize, h: usize, alpha: u8) {
83        fill_rect_alpha(x, y, w, h, self.fg, alpha);
84    }
85
86    /// Performs the text operation.
87    pub fn text(&self, x: usize, y: usize, text: &str) {
88        draw_text_at(x, y, text, self.fg, self.bg);
89    }
90
91    /// Performs the text opts operation.
92    pub fn text_opts(
93        &self,
94        x: usize,
95        y: usize,
96        text: &str,
97        align: TextAlign,
98        wrap: bool,
99        max_width: Option<usize>,
100    ) -> TextMetrics {
101        draw_text(
102            x,
103            y,
104            text,
105            TextOptions {
106                fg: self.fg,
107                bg: self.bg,
108                align,
109                wrap,
110                max_width,
111            },
112        )
113    }
114
115    /// Performs the measure text operation.
116    pub fn measure_text(&self, text: &str, max_width: Option<usize>, wrap: bool) -> TextMetrics {
117        measure_text(text, max_width, wrap)
118    }
119
120    /// Performs the blit rgb operation.
121    pub fn blit_rgb(&self, x: usize, y: usize, w: usize, h: usize, pixels: &[RgbColor]) -> bool {
122        blit_rgb(x, y, w, h, pixels)
123    }
124
125    /// Performs the blit rgb24 operation.
126    pub fn blit_rgb24(&self, x: usize, y: usize, w: usize, h: usize, bytes: &[u8]) -> bool {
127        blit_rgb24(x, y, w, h, bytes)
128    }
129
130    /// Performs the blit rgba operation.
131    pub fn blit_rgba(
132        &self,
133        x: usize,
134        y: usize,
135        w: usize,
136        h: usize,
137        bytes: &[u8],
138        global_alpha: u8,
139    ) -> bool {
140        blit_rgba(x, y, w, h, bytes, global_alpha)
141    }
142
143    /// Performs the blit sprite rgba operation.
144    pub fn blit_sprite_rgba(
145        &self,
146        x: usize,
147        y: usize,
148        sprite: SpriteRgba<'_>,
149        global_alpha: u8,
150    ) -> bool {
151        blit_sprite_rgba(x, y, sprite, global_alpha)
152    }
153
154    /// Performs the begin frame operation.
155    pub fn begin_frame(&self) -> bool {
156        begin_frame()
157    }
158
159    /// Performs the end frame operation.
160    pub fn end_frame(&self) {
161        end_frame();
162    }
163
164    /// Performs the ui clear operation.
165    pub fn ui_clear(&self, theme: UiTheme) {
166        ui_clear(theme);
167    }
168
169    /// Performs the ui panel operation.
170    pub fn ui_panel(
171        &self,
172        x: usize,
173        y: usize,
174        w: usize,
175        h: usize,
176        title: &str,
177        body: &str,
178        theme: UiTheme,
179    ) {
180        ui_draw_panel(x, y, w, h, title, body, theme);
181    }
182
183    /// Performs the ui status bar operation.
184    pub fn ui_status_bar(&self, left: &str, right: &str, theme: UiTheme) {
185        ui_draw_status_bar(left, right, theme);
186    }
187
188    /// Performs the system status line operation.
189    pub fn system_status_line(&self, theme: UiTheme) {
190        draw_system_status_line(theme);
191    }
192
193    /// Performs the layout screen operation.
194    pub fn layout_screen(&self) -> UiDockLayout {
195        UiDockLayout::from_screen()
196    }
197
198    /// Performs the ui label operation.
199    pub fn ui_label(&self, label: &UiLabel<'_>) {
200        ui_draw_label(label);
201    }
202
203    /// Performs the ui progress bar operation.
204    pub fn ui_progress_bar(&self, bar: UiProgressBar) {
205        ui_draw_progress_bar(bar);
206    }
207
208    /// Performs the ui table operation.
209    pub fn ui_table(&self, table: &UiTable) {
210        ui_draw_table(table);
211    }
212}