use crate::draw::command::DrawCommand;
use crate::draw::renderer::Renderer;
use crate::draw::sw::blur::{alpha_for_radius, iir_blur_inplace};
use crate::ecs::{Entity, World};
use crate::types::{Point, Rect};
use crate::widget::view::{View, ViewCtx};
pub struct BackgroundBlur {
pub radius: u8,
}
impl BackgroundBlur {
pub fn new(radius: u8) -> Self {
Self { radius }
}
}
fn background_blur_render(
renderer: &mut dyn Renderer,
world: &World,
entity: Entity,
rect: &Rect,
ctx: &mut ViewCtx,
) {
let Some(bg) = world.get::<BackgroundBlur>(entity) else {
return;
};
if bg.radius == 0 {
return;
}
let sample_rect = ctx.transform.apply_rect_bbox(*rect);
let Some(mut tmp) = renderer.sample_target_region(&sample_rect) else {
return;
};
iir_blur_inplace(&mut tmp, alpha_for_radius(bg.radius));
renderer.draw(
&DrawCommand::Blit {
pos: Point::new(rect.x, rect.y),
size: Point::new(rect.w, rect.h),
transform: ctx.transform,
quad: ctx.quad,
texture: &tmp,
},
ctx.clip,
);
}
pub fn view() -> View {
View::new("BackgroundBlur", 60, background_blur_render)
}