use crate::elements::{
view::{utils, ColChar, ViewElement},
Pixel, Vec2D,
};
use super::CanShade;
#[derive(Debug, Clone)]
pub struct PixelContainer {
pub pixels: Vec<Pixel>,
}
impl PixelContainer {
#[must_use]
pub const fn new() -> Self {
Self { pixels: vec![] }
}
pub fn push(&mut self, pixel: Pixel) {
self.pixels.push(pixel);
}
pub fn append(&mut self, pixels: &mut Vec<Pixel>) {
self.pixels.append(pixels);
}
pub fn append_points(&mut self, points: &[Vec2D], fill_char: ColChar) {
self.append(&mut utils::points_to_pixels(points, fill_char));
}
pub fn plot(&mut self, pos: Vec2D, c: ColChar) {
self.push(Pixel::new(pos, c));
}
pub fn blit<E: ViewElement>(&mut self, element: &E) {
let mut active_pixels = element.active_pixels();
self.append(&mut active_pixels);
}
#[must_use]
pub fn shade_with(&self, shader: &mut Box<dyn CanShade>) -> Self {
let shaded_pixels: Vec<Pixel> = self
.active_pixels()
.iter()
.map(|p| shader.shade(*p))
.collect();
Self::from(shaded_pixels.as_slice())
}
}
impl From<&[Pixel]> for PixelContainer {
fn from(pixels: &[Pixel]) -> Self {
Self {
pixels: pixels.to_vec(),
}
}
}
impl From<&[(Vec2D, ColChar)]> for PixelContainer {
fn from(pixels: &[(Vec2D, ColChar)]) -> Self {
Self {
pixels: pixels.iter().map(|x| Pixel::from(*x)).collect(),
}
}
}
impl From<(&[Vec2D], ColChar)> for PixelContainer {
fn from(value: (&[Vec2D], ColChar)) -> Self {
Self {
pixels: value
.0
.iter()
.map(|pos| Pixel::new(*pos, value.1))
.collect(),
}
}
}
impl ViewElement for PixelContainer {
fn active_pixels(&self) -> Vec<Pixel> {
self.pixels.clone()
}
}