rustty_oxide/core/
frame.rs

1use rustty::{Pos, Size, HasSize, HasPosition};
2use rustty::{CellAccessor, Cell};
3use core::alignable::{Alignable};
4
5/// The `Frame` struct is the building block for all future
6/// widgets inside of Oxide. Objects of `Frame` abstract away
7/// the actual creation and drawing of areas of a terminal,
8/// because this process is the same for all widgets. Every
9/// widget should contain one `Frame` type to be used to render
10/// text to the screen
11pub struct Frame {
12    origin: Pos,
13    size: Size,
14    buf: Vec<Cell>,
15}
16
17impl Frame {
18    /// Constructs a new Frame object with a width of `cols`
19    /// and height of `rows`
20    pub fn new(cols: usize, rows: usize) -> Frame {
21        Frame {
22            origin: (0, 0),
23            size: (cols, rows),
24            buf: vec![Cell::default(); cols * rows],
25        }
26    }
27
28    /// Draw the buffer contained inside of the base object to 
29    /// a valid object that implements CellAccessor.
30    pub fn draw_into(&self, cells: &mut CellAccessor) {
31        let (cols, rows) = self.size();
32        let (x, y) = self.origin();
33        for ix in 0..cols {
34            let offset_x = x + ix;
35            for iy in 0..rows {
36                let offset_y = y + iy;
37                match cells.get_mut(offset_x, offset_y) {
38                    Some(cell) => { *cell = *self.get(ix, iy).unwrap(); },
39                    None => (),
40                }
41            }
42        }
43    }
44
45    pub fn resize(&mut self, new_size: Size) {
46        let difference = (new_size.0 * self.size.0) - (new_size.1 * self.size.1);
47        self.buf.extend(vec![Cell::default(); difference]);
48        self.size = new_size;
49    }
50}
51
52impl HasSize for Frame {
53    fn size(&self) -> Size {
54        self.size
55    }
56}
57
58impl CellAccessor for Frame {
59    fn cellvec(&self) -> &Vec<Cell> {
60        &self.buf
61    }
62
63    fn cellvec_mut(&mut self) -> &mut Vec<Cell> {
64        &mut self.buf
65    }
66
67}
68
69impl HasPosition for Frame {
70    fn origin(&self) -> Pos {
71        self.origin
72    }
73
74    fn set_origin(&mut self, new_origin: Pos) {
75        self.origin = new_origin;
76    }
77}
78
79impl Alignable for Frame {}