#[cfg(feature = "dsl")]
use compact_str::{CompactString, ToCompactString};
use ratatui_core::layout::{Position, Rect};
#[cfg(feature = "dsl")]
use crate::dsl::DslFormat;
use crate::{
pattern::{InstancedPattern, Pattern, PreparedPattern},
SimpleRng,
};
#[derive(Clone, Debug, Copy, Default, PartialEq)]
pub struct CoalescePattern {
rng: SimpleRng,
}
impl CoalescePattern {
pub fn new() -> Self {
Self::default()
}
}
impl Pattern for CoalescePattern {
type Context = (f32, SimpleRng);
fn for_frame(self, alpha: f32, _area: Rect) -> PreparedPattern<Self::Context, Self>
where
Self: Sized,
{
PreparedPattern { pattern: self, context: (alpha, self.rng) }
}
}
impl InstancedPattern for PreparedPattern<(f32, SimpleRng), CoalescePattern> {
fn map_alpha(&mut self, _pos: Position) -> f32 {
let threshold = self.context.1.gen_f32();
let global_alpha = self.context.0;
if global_alpha <= threshold {
0.0
} else {
let progress = (global_alpha - threshold) / (1.0 - threshold);
progress.clamp(0.0, 1.0)
}
}
}
impl From<SimpleRng> for CoalescePattern {
fn from(rng: SimpleRng) -> Self {
Self { rng }
}
}
#[cfg(feature = "dsl")]
impl DslFormat for CoalescePattern {
fn dsl_format(&self) -> CompactString {
"CoalescePattern::new()".to_compact_string()
}
}