Skip to main content

strat9_kernel/arch/x86_64/vga/
canvas.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Canvas convenience drawing API : a thin wrapper around VgaWriter for drawing primitives.
24
25use super::{
26    api::*,
27    status_line::{draw_system_status_line, ui_draw_status_bar},
28    types::*,
29};
30
31pub struct Canvas {
32    fg: RgbColor,
33    bg: RgbColor,
34}
35
36impl Default for Canvas {
37    /// Builds a default instance.
38    fn default() -> Self {
39        Self {
40            fg: RgbColor::LIGHT_GREY,
41            bg: RgbColor::BLACK,
42        }
43    }
44}
45
46impl Canvas {
47    /// Creates a new instance.
48    pub const fn new(fg: RgbColor, bg: RgbColor) -> Self {
49        Self { fg, bg }
50    }
51
52    /// Sets fg.
53    pub fn set_fg(&mut self, fg: RgbColor) {
54        self.fg = fg;
55    }
56
57    /// Sets bg.
58    pub fn set_bg(&mut self, bg: RgbColor) {
59        self.bg = bg;
60    }
61
62    /// Sets colors.
63    pub fn set_colors(&mut self, fg: RgbColor, bg: RgbColor) {
64        self.fg = fg;
65        self.bg = bg;
66    }
67
68    /// Sets clip rect.
69    pub fn set_clip_rect(&self, x: usize, y: usize, w: usize, h: usize) {
70        set_clip_rect(x, y, w, h);
71    }
72
73    /// Performs the reset clip rect operation.
74    pub fn reset_clip_rect(&self) {
75        reset_clip_rect();
76    }
77
78    /// Performs the clear operation.
79    pub fn clear(&self) {
80        fill_rect(0, 0, width(), height(), self.bg);
81    }
82
83    /// Performs the pixel operation.
84    pub fn pixel(&self, x: usize, y: usize) {
85        draw_pixel(x, y, self.fg);
86    }
87
88    /// Performs the line operation.
89    pub fn line(&self, x0: isize, y0: isize, x1: isize, y1: isize) {
90        draw_line(x0, y0, x1, y1, self.fg);
91    }
92
93    /// Performs the rect operation.
94    pub fn rect(&self, x: usize, y: usize, w: usize, h: usize) {
95        draw_rect(x, y, w, h, self.fg);
96    }
97
98    /// Performs the fill rect operation.
99    pub fn fill_rect(&self, x: usize, y: usize, w: usize, h: usize) {
100        fill_rect(x, y, w, h, self.fg);
101    }
102
103    /// Performs the fill rect alpha operation.
104    pub fn fill_rect_alpha(&self, x: usize, y: usize, w: usize, h: usize, alpha: u8) {
105        fill_rect_alpha(x, y, w, h, self.fg, alpha);
106    }
107
108    /// Performs the text operation.
109    pub fn text(&self, x: usize, y: usize, text: &str) {
110        draw_text_at(x, y, text, self.fg, self.bg);
111    }
112
113    /// Performs the text opts operation.
114    pub fn text_opts(
115        &self,
116        x: usize,
117        y: usize,
118        text: &str,
119        align: TextAlign,
120        wrap: bool,
121        max_width: Option<usize>,
122    ) -> TextMetrics {
123        draw_text(
124            x,
125            y,
126            text,
127            TextOptions {
128                fg: self.fg,
129                bg: self.bg,
130                align,
131                wrap,
132                max_width,
133            },
134        )
135    }
136
137    /// Performs the measure text operation.
138    pub fn measure_text(&self, text: &str, max_width: Option<usize>, wrap: bool) -> TextMetrics {
139        measure_text(text, max_width, wrap)
140    }
141
142    /// Performs the blit rgb operation.
143    pub fn blit_rgb(&self, x: usize, y: usize, w: usize, h: usize, pixels: &[RgbColor]) -> bool {
144        blit_rgb(x, y, w, h, pixels)
145    }
146
147    /// Performs the blit rgb24 operation.
148    pub fn blit_rgb24(&self, x: usize, y: usize, w: usize, h: usize, bytes: &[u8]) -> bool {
149        blit_rgb24(x, y, w, h, bytes)
150    }
151
152    /// Performs the blit rgba operation.
153    pub fn blit_rgba(
154        &self,
155        x: usize,
156        y: usize,
157        w: usize,
158        h: usize,
159        bytes: &[u8],
160        global_alpha: u8,
161    ) -> bool {
162        blit_rgba(x, y, w, h, bytes, global_alpha)
163    }
164
165    /// Performs the blit sprite rgba operation.
166    pub fn blit_sprite_rgba(
167        &self,
168        x: usize,
169        y: usize,
170        sprite: SpriteRgba<'_>,
171        global_alpha: u8,
172    ) -> bool {
173        blit_sprite_rgba(x, y, sprite, global_alpha)
174    }
175
176    /// Performs the begin frame operation.
177    pub fn begin_frame(&self) -> bool {
178        begin_frame()
179    }
180
181    /// Performs the end frame operation.
182    pub fn end_frame(&self) {
183        end_frame();
184    }
185
186    /// Performs the ui clear operation.
187    pub fn ui_clear(&self, theme: UiTheme) {
188        ui_clear(theme);
189    }
190
191    /// Performs the ui panel operation.
192    pub fn ui_panel(
193        &self,
194        x: usize,
195        y: usize,
196        w: usize,
197        h: usize,
198        title: &str,
199        body: &str,
200        theme: UiTheme,
201    ) {
202        ui_draw_panel(x, y, w, h, title, body, theme);
203    }
204
205    /// Performs the ui status bar operation.
206    pub fn ui_status_bar(&self, left: &str, right: &str, theme: UiTheme) {
207        ui_draw_status_bar(left, right, theme);
208    }
209
210    /// Performs the system status line operation.
211    pub fn system_status_line(&self, theme: UiTheme) {
212        draw_system_status_line(theme);
213    }
214
215    /// Performs the layout screen operation.
216    pub fn layout_screen(&self) -> UiDockLayout {
217        UiDockLayout::from_screen()
218    }
219
220    /// Performs the ui label operation.
221    pub fn ui_label(&self, label: &UiLabel<'_>) {
222        ui_draw_label(label);
223    }
224
225    /// Performs the ui progress bar operation.
226    pub fn ui_progress_bar(&self, bar: UiProgressBar) {
227        ui_draw_progress_bar(bar);
228    }
229
230    /// Performs the ui table operation.
231    pub fn ui_table(&self, table: &UiTable) {
232        ui_draw_table(table);
233    }
234}