use super::view::{View, write_line_to_terminal};
use crate::core::draw::DrawBuffer;
use crate::core::event::Event;
use crate::core::geometry::Rect;
use crate::core::palette::Attr;
use crate::core::state::{GF_GROW_HI_X, GF_GROW_HI_Y, GrowFlags};
use crate::terminal::Terminal;
pub struct Background {
bounds: Rect,
pattern: char,
attr: Attr,
grow_mode: GrowFlags,
palette_chain: Option<crate::core::palette_chain::PaletteChainNode>,
}
impl Background {
pub fn new(bounds: Rect, pattern: char, attr: Attr) -> Self {
Self {
bounds,
pattern,
attr,
grow_mode: GF_GROW_HI_X | GF_GROW_HI_Y,
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 grow_mode(&self) -> GrowFlags {
self.grow_mode
}
fn set_grow_mode(&mut self, grow_mode: GrowFlags) {
self.grow_mode = grow_mode;
}
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::{Palette, palettes};
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()
}
}