Skip to content

Commit

Permalink
sliding window
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Aug 30, 2024
1 parent f59a334 commit f0543ae
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/fx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod translate_buffer;
mod hsl_shift;
mod shader_fn;
mod slide;
mod moving_window;
mod sliding_window_alpha;
mod offscreen_buffer;
mod prolong;

Expand Down
56 changes: 0 additions & 56 deletions src/fx/moving_window.rs

This file was deleted.

17 changes: 8 additions & 9 deletions src/fx/slide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bon::builder;
use ratatui::layout::Rect;
use ratatui::style::Color;

use crate::fx::moving_window::{horizontal_gradient, vertical_gradient, window_alpha_fn};
use crate::fx::sliding_window_alpha::SlidingWindowAlpha;
use crate::fx::Direction;
use crate::{CellFilter, CellIterator, EffectTimer, Shader};

Expand Down Expand Up @@ -50,16 +50,15 @@ impl Shader for SlideCell {
fn execute(&mut self, alpha: f32, area: Rect, cells: CellIterator) {
let direction = self.direction;

let gradient = match direction {
Direction::LeftToRight | Direction::RightToLeft =>
horizontal_gradient(area, alpha, self.gradient_length),
Direction::UpToDown | Direction::DownToUp =>
vertical_gradient(area, alpha, self.gradient_length),
};
let window_alpha = window_alpha_fn(direction, gradient);
let window_alpha = SlidingWindowAlpha::builder()
.direction(direction)
.progress(alpha)
.area(area)
.gradient_len(self.gradient_length)
.build();

cells.for_each(|(pos, cell)| {
match window_alpha(pos) {
match window_alpha.alpha(pos) {
0.0 => {},
1.0 => {
cell.set_char(' ');
Expand Down
100 changes: 100 additions & 0 deletions src/fx/sliding_window_alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::ops::Range;
use ratatui::layout::{Position, Rect};
use crate::fx::Direction;

pub struct SlidingWindowAlpha {
alpha_fn: fn(Position, Range<f32>) -> f32,
gradient: Range<f32>,
}

#[bon::bon]
impl SlidingWindowAlpha {
#[builder(finish_fn = build)]
pub fn builder(
direction: Direction,
area: Rect,
progress: f32,
gradient_len: u16,
) -> Self {
let alpha_fn = match direction {
Direction::UpToDown => slide_up,
Direction::DownToUp => slide_down,
Direction::LeftToRight => slide_left,
Direction::RightToLeft => slide_right,
};

let gradient = match direction {
Direction::LeftToRight | Direction::RightToLeft =>
horizontal_gradient(area, progress, gradient_len),
Direction::UpToDown | Direction::DownToUp =>
vertical_gradient(area, progress, gradient_len),
};

Self { alpha_fn, gradient }
}

pub fn alpha(&self, position: Position) -> f32 {
(self.alpha_fn)(position, self.gradient.clone())
}
}

fn horizontal_gradient(area: Rect, alpha: f32, gradient_len: u16) -> Range<f32> {
let gradient_len = gradient_len as f32;
let x_start = (area.x as f32 - gradient_len) + ((area.width as f32 + gradient_len) * alpha);
let x_end = x_start + gradient_len;

x_start..x_end
}

fn vertical_gradient(area: Rect, progress: f32, gradient_len: u16) -> Range<f32> {
let gradient_len = gradient_len as f32;
let y_start = (area.y as f32 - gradient_len) + ((area.height as f32 + gradient_len) * progress);
let y_end = y_start + gradient_len;

y_start..y_end
}

fn slide_up(
position: Position,
gradient: Range<f32>,
) -> f32 {
match position.y as f32 {
y if gradient.contains(&y) => 1.0 - (y - gradient.start) / (gradient.end - gradient.start),
y if y < gradient.start => 1.0,
_ => 0.0,
}
}

fn slide_down(
position: Position,
gradient: Range<f32>,
) -> f32 {
match position.y as f32 {
y if gradient.contains(&y) => (y - gradient.start) / (gradient.end - gradient.start),
y if y >= gradient.end => 1.0,
_ => 0.0,
}
}

fn slide_right(
position: Position,
gradient: Range<f32>,
) -> f32 {
match position.x as f32 {
x if gradient.contains(&x) => (x - gradient.start) / (gradient.end - gradient.start),
x if x >= gradient.end => 1.0,
_ => 0.0,
}
}

fn slide_left(
position: Position,
gradient: Range<f32>,
) -> f32 {
match position.x as f32 {
x if gradient.contains(&x) => 1.0 - (x - gradient.start) / (gradient.end - gradient.start),
x if x < gradient.start => 1.0,
_ => 0.0,
}
}

17 changes: 8 additions & 9 deletions src/fx/sweep_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Interpolation::CircOut;
use crate::{CellIterator, ColorMapper};
use crate::CellFilter;
use crate::effect_timer::EffectTimer;
use crate::fx::moving_window::{horizontal_gradient, vertical_gradient, window_alpha_fn};
use crate::fx::sliding_window_alpha::SlidingWindowAlpha;
use crate::interpolation::{Interpolatable, Interpolation};
use crate::shader::Shader;

Expand Down Expand Up @@ -72,20 +72,19 @@ impl Shader for SweepIn {

fn execute(&mut self, alpha: f32, area: Rect, cell_iter: CellIterator) {
let direction = self.direction;
let gradient = match direction {
Direction::LeftToRight | Direction::RightToLeft =>
horizontal_gradient(area, alpha, self.gradient_length),
Direction::UpToDown | Direction::DownToUp =>
vertical_gradient(area, alpha, self.gradient_length),
};

let window_alpha = window_alpha_fn(direction, gradient);
let window_alpha = SlidingWindowAlpha::builder()
.direction(direction)
.progress(alpha)
.area(area)
.gradient_len(self.gradient_length)
.build();

let mut fg_mapper = ColorMapper::default();
let mut bg_mapper = ColorMapper::default();

cell_iter.for_each(|(pos, cell)| {
match window_alpha(pos) {
match window_alpha.alpha(pos) {
0.0 => {
cell.set_fg(self.faded_color);
cell.set_bg(self.faded_color);
Expand Down

0 comments on commit f0543ae

Please sign in to comment.