Skip to content

Commit

Permalink
Simplify wrapping animations in decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
mrozycki committed Nov 20, 2024
1 parent 3263e63 commit 6644537
Show file tree
Hide file tree
Showing 41 changed files with 329 additions and 348 deletions.
7 changes: 7 additions & 0 deletions animation-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ pub struct AnimationError {

pub trait Animation {
type Parameters: GetSchema + DeserializeOwned + Serialize + Default + Clone;
type Wrapped: Animation<Parameters: GetSchema>;

fn new(points: Vec<(f64, f64, f64)>) -> Self;

fn new_wrapped(points: Vec<(f64, f64, f64)>) -> Self::Wrapped {
Self::Wrapped::new(points)
}

fn get_schema(&self) -> ConfigurationSchema
where
Expand Down
2 changes: 1 addition & 1 deletion animation-plugin-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn plugin(_attr: TokenStream, item: TokenStream) -> TokenStream {
Ok(Some(message)) => match message.payload {
JsonRpcMethod::Initialize { points } => {
animation = None;
animation = Some(<#name>::create(points));
animation = Some(<#name>::new_wrapped(points));
respond(message.id, ());
}
JsonRpcMethod::ParameterSchema => {
Expand Down
35 changes: 17 additions & 18 deletions animation-utils/src/decorators/brightness_controlled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,28 @@ impl<P: GetSchema> GetSchema for Parameters<P> {
}
}

pub struct BrightnessControlled<P: GetSchema, A: Animation<Parameters = P>> {
pub struct BrightnessControlled<A: Animation<Parameters: GetSchema>> {
animation: A,
parameters: Parameters<P>,
parameters: Parameters<A::Parameters>,
}

impl<A, P> Animation for BrightnessControlled<P, A>
impl<A> Animation for BrightnessControlled<A>
where
A: Animation<Parameters = P>,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;
type Parameters = Parameters<A::Parameters>;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
animation: A::new(points),
parameters: Parameters {
brightness_factor: 1.0,
inner: Default::default(),
},
}
}

fn update(&mut self, delta: f64) {
self.animation.update(delta)
Expand Down Expand Up @@ -68,15 +79,3 @@ where
self.animation.get_fps()
}
}

impl<P: GetSchema + Default, A: Animation<Parameters = P>> BrightnessControlled<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
parameters: Parameters {
brightness_factor: 1.0,
inner: Default::default(),
},
}
}
}
39 changes: 19 additions & 20 deletions animation-utils/src/decorators/off_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,31 @@ impl<P: GetSchema> GetSchema for Parameters<P> {
}
}

pub struct OffSwitch<P: GetSchema, A: Animation<Parameters = P>> {
pub struct OffSwitch<A: Animation<Parameters: GetSchema>> {
animation: A,
parameters: Parameters<P>,
parameters: Parameters<A::Parameters>,
energy: f64,
}

impl<A, P> Animation for OffSwitch<P, A>
impl<A> Animation for OffSwitch<A>
where
A: Animation<Parameters = P>,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;
type Parameters = Parameters<A::Parameters>;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
animation: A::new(points),
parameters: Parameters {
off_switch_delay: 1.0,
off_switch_state: Switch::On,
inner: Default::default(),
},
energy: 0.0,
}
}

fn update(&mut self, time_delta: f64) {
let energy_delta = if self.parameters.off_switch_delay == 0.0 {
Expand Down Expand Up @@ -113,17 +126,3 @@ where
self.animation.get_fps()
}
}

impl<P: GetSchema + Default, A: Animation<Parameters = P>> OffSwitch<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
parameters: Parameters {
off_switch_delay: 1.0,
off_switch_state: Switch::On,
inner: Default::default(),
},
energy: 0.0,
}
}
}
35 changes: 17 additions & 18 deletions animation-utils/src/decorators/speed_controlled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,28 @@ impl<P: GetSchema> GetSchema for Parameters<P> {
}
}

pub struct SpeedControlled<P: GetSchema, A: Animation<Parameters = P>> {
pub struct SpeedControlled<A: Animation<Parameters: GetSchema>> {
animation: A,
parameters: Parameters<P>,
parameters: Parameters<A::Parameters>,
}

impl<A, P> Animation for SpeedControlled<P, A>
impl<A> Animation for SpeedControlled<A>
where
A: Animation<Parameters = P>,
P: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<P>;
type Parameters = Parameters<A::Parameters>;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
animation: A::new(points),
parameters: Parameters {
speed_factor: 1.0,
inner: Default::default(),
},
}
}

fn update(&mut self, delta: f64) {
self.animation.update(delta * self.parameters.speed_factor);
Expand Down Expand Up @@ -69,15 +80,3 @@ where
(self.animation.get_fps() * self.parameters.speed_factor.abs()).clamp(0.0, 30.0)
}
}

impl<P: GetSchema + Default, A: Animation<Parameters = P>> SpeedControlled<P, A> {
pub fn new(animation: A) -> Self {
Self {
animation,
parameters: Parameters {
speed_factor: 1.0,
inner: Default::default(),
},
}
}
}
15 changes: 7 additions & 8 deletions animations/src/bin/audio_boom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ pub struct AudioVisualizer {
parameters: Parameters,
}

impl AudioVisualizer {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
SpeedControlled::new(BrightnessControlled::new(Self {
impl Animation for AudioVisualizer {
type Parameters = Parameters;
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points: points
.into_iter()
.map(|p| {
Expand All @@ -60,12 +63,8 @@ impl AudioVisualizer {
time: 0.0,
control_points: Vec::new(),
parameters: Default::default(),
}))
}
}
}

impl Animation for AudioVisualizer {
type Parameters = Parameters;

fn update(&mut self, delta: f64) {
self.time += delta;
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/audio_visualizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,17 @@ pub struct AudioVisualizer {
parameters: Parameters,
}

impl AudioVisualizer {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
SpeedControlled::new(BrightnessControlled::new(Self {
impl Animation for AudioVisualizer {
type Parameters = Parameters;
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points,
bands: vec![0.0],
parameters: Default::default(),
}))
}
}
}

impl Animation for AudioVisualizer {
type Parameters = Parameters;

fn on_event(&mut self, event: Event) {
if let Event::FftEvent { bands } = event {
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/audio_wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,11 @@ pub struct AudioVisualizer {
_stream: Stream,
}

impl AudioVisualizer {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
impl Animation for AudioVisualizer {
type Parameters = Parameters;
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
let buffer: Arc<Mutex<VecDeque<f32>>> = Arc::new(Mutex::new(vec![0.0; BUFFER_SIZE].into()));
let input_data_fn = {
let buffer = buffer.clone();
Expand All @@ -91,17 +94,13 @@ impl AudioVisualizer {
.unwrap();
input_stream.play().unwrap();

SpeedControlled::new(BrightnessControlled::new(Self {
Self {
points,
parameters: Default::default(),
buffer,
_stream: input_stream,
}))
}
}
}

impl Animation for AudioVisualizer {
type Parameters = Parameters;

fn update(&mut self, _: f64) {}

Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/barber_pole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ pub struct BarberPole {
parameters: Parameters,
}

impl BarberPole {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
SpeedControlled::new(BrightnessControlled::new(Self {
impl Animation for BarberPole {
type Parameters = Parameters;
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points_polar: points.into_iter().map(animation_utils::to_polar).collect(),
time: 0.0,
parameters: Default::default(),
}))
}
}
}

impl Animation for BarberPole {
type Parameters = Parameters;

fn update(&mut self, delta: f64) {
self.time += delta;
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/beats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ pub struct CircleBoom {
parameters: Parameters,
}

impl CircleBoom {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
OffSwitch::new(BrightnessControlled::new(Self {
impl Animation for CircleBoom {
type Parameters = Parameters;
type Wrapped = OffSwitch<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points,
beat: 0.0,
parameters: Default::default(),
}))
}
}
}

impl Animation for CircleBoom {
type Parameters = Parameters;

fn update(&mut self, delta: f64) {
self.beat += delta * self.parameters.bpm / 60.0;
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ pub struct Check {
time: f64,
}

impl Check {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
SpeedControlled::new(BrightnessControlled::new(Self {
impl Animation for Check {
type Parameters = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points_count: points.len(),
time: 0.0,
}))
}
}
}

impl Animation for Check {
type Parameters = ();

fn update(&mut self, delta: f64) {
self.time += delta;
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/circle_boom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,17 @@ pub struct CircleBoom {
parameters: Parameters,
}

impl CircleBoom {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
BrightnessControlled::new(Self {
impl Animation for CircleBoom {
type Parameters = Parameters;
type Wrapped = BrightnessControlled<Self>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points,
time: 0.0,
parameters: Default::default(),
})
}
}
}

impl Animation for CircleBoom {
type Parameters = Parameters;

fn update(&mut self, delta: f64) {
self.time += delta * self.parameters.bpm / 60.0;
Expand Down
15 changes: 7 additions & 8 deletions animations/src/bin/circle_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ pub struct CircleGrid {
parameters: Parameters,
}

impl CircleGrid {
pub fn create(points: Vec<(f64, f64, f64)>) -> impl Animation {
OffSwitch::new(BrightnessControlled::new(Self {
impl Animation for CircleGrid {
type Parameters = Parameters;
type Wrapped = OffSwitch<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Self {
points,
time: 0.0,
parameters: Default::default(),
}))
}
}
}

impl Animation for CircleGrid {
type Parameters = Parameters;

fn update(&mut self, delta: f64) {
self.time += delta;
Expand Down
Loading

0 comments on commit 6644537

Please sign in to comment.