Skip to content

Commit

Permalink
wip widget
Browse files Browse the repository at this point in the history
  • Loading branch information
junkdog committed Aug 5, 2024
1 parent bcd951e commit fdd5cf8
Show file tree
Hide file tree
Showing 24 changed files with 438 additions and 103 deletions.
4 changes: 4 additions & 0 deletions examples/common/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ impl Shader for OpenWindow {
self.pre_render_fx.as_mut().and_then(Effect::timer_mut)
}

fn timer(&self) -> Option<&EffectTimer> {
self.pre_render_fx.as_ref().and_then(Effect::timer)
}

fn cell_selection(&self) -> Option<CellFilter> {
self.pre_render_fx.as_ref().map(Effect::cell_selection).flatten()
}
Expand Down
14 changes: 13 additions & 1 deletion src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ratatui::layout;
use ratatui::layout::{Margin, Position, Rect};
use ratatui::style::Color;
use crate::{CellIterator, EffectTimer};
use crate::fxchart::EffectSpan;
use crate::shader::Shader;

/// Represents an effect that can be applied to terminal cells.
Expand Down Expand Up @@ -256,6 +257,10 @@ impl Shader for Effect {
self.shader.reverse()
}

fn timer(&self) -> Option<EffectTimer> {
self.shader.timer()
}

fn timer_mut(&mut self) -> Option<&mut EffectTimer> {
self.shader.timer_mut()
}
Expand All @@ -267,6 +272,13 @@ impl Shader for Effect {
fn reset(&mut self) {
self.shader.reset()
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan
where
Self: Sized + Clone,
{
self.shader.as_ref().as_effect_span(offset)
}
}


Expand All @@ -280,4 +292,4 @@ impl<S> IntoEffect for S
fn into_effect(self) -> Effect {
Effect::new(self)
}
}
}
90 changes: 0 additions & 90 deletions src/effect_chart.rs

This file was deleted.

13 changes: 13 additions & 0 deletions src/effect_timer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops::Mul;
use std::time::Duration;
use crate::interpolation::Interpolation;

Expand Down Expand Up @@ -137,6 +138,10 @@ impl EffectTimer {
self.interpolation.alpha(a)
}

pub(crate) fn duration(&self) -> Duration {
self.total
}

/// Processes the timer by reducing the remaining duration by the specified amount.
///
/// # Arguments
Expand Down Expand Up @@ -202,4 +207,12 @@ impl From<Duration> for EffectTimer {
fn from(duration: Duration) -> Self {
EffectTimer::new(duration, Interpolation::Linear)
}
}

impl Mul<u32> for EffectTimer {
type Output = (EffectTimer);

fn mul(self, rhs: u32) -> Self::Output {
EffectTimer::new(self.duration() * rhs, self.interpolation)
}
}
50 changes: 49 additions & 1 deletion src/fx/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use ratatui::buffer::Buffer;
use ratatui::layout::{Rect};
use crate::{CellIterator, EffectTimer};
use crate::effect::{Effect, CellFilter};
use crate::fxchart::EffectSpan;
use crate::Interpolation::Linear;
use crate::shader::Shader;

#[derive(Default, Clone)]
Expand Down Expand Up @@ -80,13 +82,31 @@ impl Shader for ParallelEffect {
None
}

fn timer(&self) -> Option<EffectTimer> {
self.effects.iter()
.map(|fx| fx.timer())
.filter(|t| t.is_some())
.map(|t| t.unwrap())
.map(|t| t.duration())
.max()
.map(|d| EffectTimer::new(d, Linear))
}

fn cell_selection(&self) -> Option<CellFilter> {
None
}

fn reset(&mut self) {
self.effects.iter_mut().for_each(Effect::reset)
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan {
let children = self.effects.iter()
.map(|e| e.as_effect_span(offset))
.collect();

EffectSpan::new(self, offset, children)
}
}

impl Shader for SequentialEffect {
Expand Down Expand Up @@ -143,10 +163,38 @@ impl Shader for SequentialEffect {

fn timer_mut(&mut self) -> Option<&mut EffectTimer> { None }

fn timer(&self) -> Option<EffectTimer> {
let duration: Duration = self.effects.iter()
.map(|fx| fx.timer())
.filter(|t| t.is_some())
.map(|t| t.unwrap().duration())
.sum();

if duration.is_zero() {
None
} else {
Some(EffectTimer::new(duration, Linear))
}
}

fn cell_selection(&self) -> Option<CellFilter> { None }

fn reset(&mut self) {
self.current = 0;
self.effects.iter_mut().for_each(Effect::reset)
}
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan {
let mut acc = Duration::from_secs(0);
let children = self.effects.iter()
.map(|e| {
let span = e.as_effect_span(offset + acc);
acc += e.timer().map(|t| t.duration()).unwrap_or_default();
span
})
.collect();

EffectSpan::new(self, offset, children)
}
}

4 changes: 4 additions & 0 deletions src/fx/dissolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Shader for Dissolve {
self.cell_filter = strategy
}

fn timer(&self) -> Option<EffectTimer> {
Some(self.timer.clone())
}

fn timer_mut(&mut self) -> Option<&mut EffectTimer> {
Some(&mut self.timer)
}
Expand Down
4 changes: 4 additions & 0 deletions src/fx/fade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl Shader for FadeColors {
Some(&mut self.timer)
}

fn timer(&self) -> Option<EffectTimer> {
Some(self.timer.clone())
}

fn cell_selection(&self) -> Option<CellFilter> {
Some(self.cell_filter.clone())
}
Expand Down
5 changes: 0 additions & 5 deletions src/fx/flattenable.rs

This file was deleted.

4 changes: 4 additions & 0 deletions src/fx/hsl_shift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl Shader for HslShift {
Some(&mut self.timer)
}

fn timer(&self) -> Option<EffectTimer> {
Some(self.timer.clone())
}

fn cell_selection(&self) -> Option<CellFilter> {
Some(self.cell_filter.clone())
}
Expand Down
3 changes: 1 addition & 2 deletions src/fx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::fx::temporary::{IntoTemporaryEffect, TemporaryEffect};

mod ansi256;
mod consume_tick;
mod containers;
pub(crate) mod containers;
mod dissolve;
mod fade;
mod glitch;
Expand All @@ -46,7 +46,6 @@ mod shader_fn;
mod slide;
mod moving_window;
mod offscreen_buffer;
mod flattenable;

/// Creates a custom effect using a user-defined function.
///
Expand Down
5 changes: 5 additions & 0 deletions src/fx/never_complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use crate::{CellIterator, EffectTimer};
use crate::effect::{Effect, CellFilter};
use crate::fxchart::EffectSpan;
use crate::shader::Shader;

#[derive(Clone)]
Expand Down Expand Up @@ -54,4 +55,8 @@ impl Shader for NeverComplete {
fn reset(&mut self) {
self.effect.reset();
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan {
EffectSpan::new(self, offset, vec![self.effect.as_effect_span(offset)])
}
}
5 changes: 5 additions & 0 deletions src/fx/offscreen_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::time::Duration;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use crate::{CellFilter, CellIterator, Effect, Shader};
use crate::fxchart::EffectSpan;

#[derive(Clone)]
pub struct OffscreenBuffer {
Expand Down Expand Up @@ -58,4 +59,8 @@ impl Shader for OffscreenBuffer {
fn set_cell_selection(&mut self, filter: CellFilter) {
self.fx.set_cell_selection(filter);
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan {
EffectSpan::new(self, offset, vec![self.fx.as_effect_span(offset)])
}
}
9 changes: 9 additions & 0 deletions src/fx/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ratatui::layout::Rect;
use std::time::Duration;

use crate::{CellFilter, CellIterator, Effect, EffectTimer, Shader};
use crate::fxchart::EffectSpan;

#[derive(Clone)]
pub struct PingPong {
Expand Down Expand Up @@ -77,6 +78,14 @@ impl Shader for PingPong {
None
}

fn timer(&self) -> Option<EffectTimer> {
self.fx.timer().as_ref().map(|t| t.clone() * 2)
}

fn as_effect_span(&self, offset: Duration) -> EffectSpan {
EffectSpan::new(self, offset, vec![self.fx.as_effect_span(offset)])
}

fn cell_selection(&self) -> Option<CellFilter> {
Some(self.strategy.clone())
}
Expand Down
Loading

0 comments on commit fdd5cf8

Please sign in to comment.