oxedyne/fe2o3/fe2o3_tui/src/lib_tui/draw/canvas.rs
1.3 KiB, 7 runs
created by r1870400018:1173, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use crate::lib_tui::{ |
| 2 | style::Style, |
| 3 | render::{ |
| 4 | Drawable, |
| 5 | Drawer, |
| 6 | Renderer, |
| 7 | Sink, |
| 8 | When, |
| 9 | }, |
| 10 | }; |
| 11 | |
| 12 | use oxedyne_fe2o3_core::prelude::*; |
| 13 | use oxedyne_fe2o3_geom::{ |
| 14 | dim::Coord, |
| 15 | rect::AbsRect, |
| 16 | }; |
| 17 | |
| 18 | |
| 19 | #[derive(Clone, Debug, Default)] |
| 20 | pub struct CanvasConfig { |
| 21 | pub style: Style, |
| 22 | } |
| 23 | |
| 24 | #[derive(Clone, Debug)] |
| 25 | pub struct Canvas { |
| 26 | pub cfg: CanvasConfig, |
| 27 | pub view: AbsRect, |
| 28 | } |
| 29 | |
| 30 | impl Drawable for Canvas { |
| 31 | fn render<S: Sink, R: Renderer<S>>( |
| 32 | &mut self, |
| 33 | drawer: &mut Drawer<S, R>, |
| 34 | when: When, |
| 35 | ) |
| 36 | -> Outcome<()> |
| 37 | { |
| 38 | let (x, y, w, h) = self.view.tup(); |
| 39 | |
| 40 | res!(self.cfg.style.render(drawer, When::Later)); |
| 41 | for j in 0..h.as_index() { |
| 42 | res!(drawer.rend.set_cursor(Coord::new((x, y + j)), When::Later)); |
| 43 | res!(drawer.rend.print(&" ".to_string().repeat(w.as_usize()), When::Later)); |
| 44 | } |
| 45 | res!(drawer.rend.reset_style(When::Later)); |
| 46 | |
| 47 | if when == When::Now { |
| 48 | res!(drawer.rend.flush()); |
| 49 | } |
| 50 | |
| 51 | Ok(()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl Canvas { |
| 56 | pub fn new( |
| 57 | cfg: CanvasConfig, |
| 58 | view: AbsRect, |
| 59 | ) |
| 60 | -> Self |
| 61 | { |
| 62 | Self { |
| 63 | cfg, |
| 64 | view, |
| 65 | } |
| 66 | } |
| 67 | } |