use tui_vfx_types::{Cell, Color, Grid, Rect};
use crate::types::ShadowConfig;
pub struct SolidRenderer;
impl SolidRenderer {
pub fn render<G: Grid>(grid: &mut G, element_rect: Rect, config: &ShadowConfig, progress: f64) {
let shadow_color = config.color_at_progress(progress);
if shadow_color.a == 0 {
return;
}
let rect_x = element_rect.x as i32;
let rect_y = element_rect.y as i32;
let rect_w = element_rect.width as i32;
let rect_h = element_rect.height as i32;
let ox = config.offset_x as i32;
let oy = config.offset_y as i32;
let edges = config.edges;
if edges.has_right() && ox > 0 {
let start_x = (rect_x + rect_w).max(0) as usize;
let start_y = (rect_y + oy.max(0)).max(0) as usize;
let w = ox as usize;
let h = (rect_h - oy.abs().min(rect_h)).max(0) as usize;
Self::fill_region(grid, start_x, start_y, w, h, shadow_color);
}
if edges.has_bottom() && oy > 0 {
let start_x = (rect_x + ox.max(0)).max(0) as usize;
let start_y = (rect_y + rect_h).max(0) as usize;
let w = (rect_w - ox.abs().min(rect_w)).max(0) as usize;
let h = oy as usize;
Self::fill_region(grid, start_x, start_y, w, h, shadow_color);
}
if edges.has_left() && ox < 0 {
let start_x = (rect_x + ox).max(0) as usize;
let start_y = (rect_y + oy.max(0)).max(0) as usize;
let w = (-ox) as usize;
let h = (rect_h - oy.abs().min(rect_h)).max(0) as usize;
Self::fill_region(grid, start_x, start_y, w, h, shadow_color);
}
if edges.has_top() && oy < 0 {
let start_x = (rect_x + ox.max(0)).max(0) as usize;
let start_y = (rect_y + oy).max(0) as usize;
let w = (rect_w - ox.abs().min(rect_w)).max(0) as usize;
let h = (-oy) as usize;
Self::fill_region(grid, start_x, start_y, w, h, shadow_color);
}
if edges.has_right() && edges.has_bottom() && ox > 0 && oy > 0 {
let start_x = (rect_x + rect_w).max(0) as usize;
let start_y = (rect_y + rect_h).max(0) as usize;
Self::fill_region(
grid,
start_x,
start_y,
ox as usize,
oy as usize,
shadow_color,
);
}
if edges.has_left() && edges.has_top() && ox < 0 && oy < 0 {
let start_x = (rect_x + ox).max(0) as usize;
let start_y = (rect_y + oy).max(0) as usize;
Self::fill_region(
grid,
start_x,
start_y,
(-ox) as usize,
(-oy) as usize,
shadow_color,
);
}
}
fn fill_region<G: Grid>(grid: &mut G, x: usize, y: usize, w: usize, h: usize, color: Color) {
let cell = Cell::new(' ').with_bg(color).with_mod_alpha(Some(255));
for dy in 0..h {
for dx in 0..w {
let px = x + dx;
let py = y + dy;
if grid.in_bounds(px, py) {
grid.set(px, py, cell);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::ShadowEdges;
use tui_vfx_types::OwnedGrid;
#[test]
fn test_render_basic_shadow() {
let mut grid = OwnedGrid::new(20, 10);
let rect = Rect::new(5, 2, 8, 4);
let config = ShadowConfig::new(Color::BLACK.with_alpha(128))
.with_offset(2, 1)
.with_style(crate::types::ShadowStyle::Solid)
.with_edges(ShadowEdges::BOTTOM_RIGHT);
SolidRenderer::render(&mut grid, rect, &config, 1.0);
let cell = grid.get(13, 3).unwrap();
assert_ne!(cell.bg, Color::TRANSPARENT);
assert_eq!(cell.ch, ' ');
let cell = grid.get(7, 6).unwrap();
assert_ne!(cell.bg, Color::TRANSPARENT);
let cell = grid.get(13, 6).unwrap();
assert_ne!(cell.bg, Color::TRANSPARENT);
}
#[test]
fn test_zero_alpha_renders_nothing() {
let mut grid = OwnedGrid::new(20, 10);
let rect = Rect::new(5, 2, 8, 4);
let config = ShadowConfig::new(Color::BLACK.with_alpha(0));
SolidRenderer::render(&mut grid, rect, &config, 1.0);
for y in 0..10 {
for x in 0..20 {
let cell = grid.get(x, y).unwrap();
assert_eq!(cell.bg, Color::TRANSPARENT);
}
}
}
}