use crate::core::geometry::Rect;
use crate::core::event::Event;
use crate::core::draw::DrawBuffer;
use crate::core::palette::Attr;
use crate::terminal::Terminal;
use super::view::{View, write_line_to_terminal};
pub struct Background {
bounds: Rect,
pattern: char,
attr: Attr,
palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}
impl Background {
pub fn new(bounds: Rect, pattern: char, attr: Attr) -> Self {
Self {
bounds,
pattern,
attr,
palette_chain: None,
}
}
}
impl View for Background {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width_clamped() as usize;
let mut buf = DrawBuffer::new(width);
buf.move_char(0, self.pattern, self.attr, width);
for y in self.bounds.a.y..self.bounds.b.y {
write_line_to_terminal(terminal, self.bounds.a.x, y, &buf);
}
}
fn handle_event(&mut self, _event: &mut Event) {
}
fn set_palette_chain(&mut self, node: Option<crate::core::palette_chain::PaletteChainNode>) {
self.palette_chain = node;
}
fn get_palette_chain(&self) -> Option<&crate::core::palette_chain::PaletteChainNode> {
self.palette_chain.as_ref()
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_BACKGROUND))
}
}
pub struct BackgroundBuilder {
bounds: Option<Rect>,
pattern: char,
attr: Option<Attr>,
}
impl BackgroundBuilder {
pub fn new() -> Self {
Self { bounds: None, pattern: '░', attr: None }
}
#[must_use]
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = Some(bounds);
self
}
#[must_use]
pub fn pattern(mut self, pattern: char) -> Self {
self.pattern = pattern;
self
}
#[must_use]
pub fn attr(mut self, attr: Attr) -> Self {
self.attr = Some(attr);
self
}
pub fn build(self) -> Background {
let bounds = self.bounds.expect("Background bounds must be set");
let attr = self.attr.expect("Background attr must be set");
Background::new(bounds, self.pattern, attr)
}
pub fn build_boxed(self) -> Box<Background> {
Box::new(self.build())
}
}
impl Default for BackgroundBuilder {
fn default() -> Self {
Self::new()
}
}