rustty/ui/
widget.rs

1use core::position::{Pos, Size, HasSize, HasPosition};
2use core::cellbuffer::{CellAccessor, Cell};
3use ui::layout::Alignable;
4
5pub struct Widget {
6    origin: Pos,
7    size: Size,
8    buf: Vec<Cell>,
9}
10
11impl Widget {
12    pub fn new(cols: usize, rows: usize) -> Widget {
13        Widget {
14            origin: (0, 0),
15            size: (cols, rows),
16            buf: vec![Cell::default(); cols * rows],
17        }
18    }
19
20    pub fn draw_into(&self, cells: &mut CellAccessor) {
21        let (cols, rows) = self.size();
22        let (x, y) = self.origin();
23        for ix in 0..cols {
24            let offset_x = x + ix;
25            for iy in 0..rows {
26                let offset_y = y + iy;
27                match cells.get_mut(offset_x, offset_y) {
28                    Some(cell) => {
29                        *cell = *self.get(ix, iy).unwrap();
30                    }
31                    None => (),
32                }
33            }
34        }
35    }
36}
37
38impl HasSize for Widget {
39    fn size(&self) -> Size {
40        self.size
41    }
42}
43
44impl CellAccessor for Widget {
45    fn cellvec(&self) -> &Vec<Cell> {
46        &self.buf
47    }
48
49    fn cellvec_mut(&mut self) -> &mut Vec<Cell> {
50        &mut self.buf
51    }
52}
53
54impl HasPosition for Widget {
55    fn origin(&self) -> Pos {
56        self.origin
57    }
58
59    fn set_origin(&mut self, new_origin: Pos) {
60        self.origin = new_origin;
61    }
62}
63
64impl Alignable for Widget {}