rustty_oxide/core/
frame.rs1use rustty::{Pos, Size, HasSize, HasPosition};
2use rustty::{CellAccessor, Cell};
3use core::alignable::{Alignable};
4
5pub struct Frame {
12 origin: Pos,
13 size: Size,
14 buf: Vec<Cell>,
15}
16
17impl Frame {
18 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 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 {}