use rustty::{Pos, Size, HasSize, HasPosition};
use rustty::{CellAccessor, Cell};
use core::alignable::{Alignable};
pub struct Frame {
origin: Pos,
size: Size,
buf: Vec<Cell>,
}
impl Frame {
pub fn new(cols: usize, rows: usize) -> Frame {
Frame {
origin: (0, 0),
size: (cols, rows),
buf: vec![Cell::default(); cols * rows],
}
}
pub fn draw_into(&self, cells: &mut CellAccessor) {
let (cols, rows) = self.size();
let (x, y) = self.origin();
for ix in 0..cols {
let offset_x = x + ix;
for iy in 0..rows {
let offset_y = y + iy;
match cells.get_mut(offset_x, offset_y) {
Some(cell) => { *cell = *self.get(ix, iy).unwrap(); },
None => (),
}
}
}
}
pub fn resize(&mut self, new_size: Size) {
let difference = (new_size.0 * self.size.0) - (new_size.1 * self.size.1);
self.buf.extend(vec![Cell::default(); difference]);
self.size = new_size;
}
}
impl HasSize for Frame {
fn size(&self) -> Size {
self.size
}
}
impl CellAccessor for Frame {
fn cellvec(&self) -> &Vec<Cell> {
&self.buf
}
fn cellvec_mut(&mut self) -> &mut Vec<Cell> {
&mut self.buf
}
}
impl HasPosition for Frame {
fn origin(&self) -> Pos {
self.origin
}
fn set_origin(&mut self, new_origin: Pos) {
self.origin = new_origin;
}
}
impl Alignable for Frame {}