Skip to content

Commit

Permalink
Add custom triggers to animation API
Browse files Browse the repository at this point in the history
  • Loading branch information
mrozycki committed Dec 24, 2024
1 parent 6b4531b commit 707cb55
Show file tree
Hide file tree
Showing 46 changed files with 210 additions and 122 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions animation-api/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::schema::{ConfigurationSchema, GetSchema, ParameterValue};
use crate::schema::{ConfigurationSchema, GetEnumOptions, GetSchema, ParameterValue};

#[derive(Serialize, Deserialize, Debug, thiserror::Error)]
#[error("animation error: {message}")]
Expand All @@ -12,6 +12,7 @@ pub struct AnimationError {

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

fn new(points: Vec<(f64, f64, f64)>) -> Self;
Expand All @@ -24,7 +25,10 @@ pub trait Animation {
where
Self::Parameters: GetSchema,
{
Self::Parameters::schema()
ConfigurationSchema {
parameters: Self::Parameters::schema(),
custom_triggers: Self::CustomTriggers::enum_options(),
}
}

fn set_parameters(&mut self, _parameters: Self::Parameters) {}
Expand Down
4 changes: 2 additions & 2 deletions animation-api/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum Event {
)]
MidiMsg,
),
ManualEvent {
id: usize,
CustomTrigger {
trigger_id: String,
},
MouseMove {
ray_origin: [f32; 3],
Expand Down
19 changes: 14 additions & 5 deletions animation-api/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,34 @@ pub struct ParameterSchema {

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug, Default)]
pub struct ConfigurationSchema {
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub parameters: Vec<ParameterSchema>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub custom_triggers: Vec<EnumOption>,
}

pub trait GetSchema {
fn schema() -> ConfigurationSchema;
fn schema() -> Vec<ParameterSchema>;
}

impl GetSchema for () {
fn schema() -> ConfigurationSchema {
ConfigurationSchema {
parameters: Vec::new(),
}
fn schema() -> Vec<ParameterSchema> {
Vec::new()
}
}

pub trait GetEnumOptions {
fn enum_options() -> Vec<EnumOption>;
}

impl GetEnumOptions for () {
fn enum_options() -> Vec<EnumOption> {
Vec::new()
}
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
#[serde(untagged)]
pub enum ParameterValue {
Expand Down
8 changes: 2 additions & 6 deletions animation-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,8 @@ fn derive_schema_inner(
let ident = ast.ident;
errors.finish_with(quote! {
impl animation_api::schema::GetSchema for #ident {
fn schema() -> animation_api::schema::ConfigurationSchema {
animation_api::schema::ConfigurationSchema {
parameters: vec![
#fields
],
}
fn schema() -> Vec<animation_api::schema::ParameterSchema> {
vec![#fields]
}
}
})
Expand Down
10 changes: 6 additions & 4 deletions animation-utils/src/decorators/brightness_controlled.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use animation_api::schema::{ConfigurationSchema, GetSchema, ParameterSchema, ValueSchema};
use animation_api::schema::{GetEnumOptions, GetSchema, ParameterSchema, ValueSchema};
use animation_api::Animation;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
Expand All @@ -11,15 +11,15 @@ pub struct Parameters<P: GetSchema> {
}

impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
fn schema() -> Vec<ParameterSchema> {
let mut parameters = vec![ParameterSchema {
id: "brightness_factor".to_owned(),
name: "Brightness".to_owned(),
description: None,
value: ValueSchema::Percentage,
}];
parameters.extend(P::schema().parameters);
ConfigurationSchema { parameters }
parameters.extend(P::schema());
parameters
}
}

Expand All @@ -32,8 +32,10 @@ impl<A> Animation for BrightnessControlled<A>
where
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A::CustomTriggers: GetEnumOptions + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<A::Parameters>;
type CustomTriggers = A::CustomTriggers;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
16 changes: 6 additions & 10 deletions animation-utils/src/decorators/off_switch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use animation_api::schema::{
ConfigurationSchema, GetEnumOptions, GetSchema, ParameterSchema, ValueSchema,
};
use animation_api::schema::{GetEnumOptions, GetSchema, ParameterSchema, ValueSchema};
use animation_api::Animation;
use animation_macros::EnumSchema;
use serde::de::DeserializeOwned;
Expand All @@ -26,7 +24,7 @@ pub struct Parameters<P: GetSchema> {
}

impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
fn schema() -> Vec<ParameterSchema> {
let mut parameters = vec![
ParameterSchema {
id: "off_switch_state".to_owned(),
Expand All @@ -47,8 +45,8 @@ impl<P: GetSchema> GetSchema for Parameters<P> {
},
},
];
parameters.extend(P::schema().parameters);
ConfigurationSchema { parameters }
parameters.extend(P::schema());
parameters
}
}

Expand All @@ -62,8 +60,10 @@ impl<A> Animation for OffSwitch<A>
where
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A::CustomTriggers: GetEnumOptions + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<A::Parameters>;
type CustomTriggers = A::CustomTriggers;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down Expand Up @@ -105,10 +105,6 @@ where
.into()
}

fn get_schema(&self) -> ConfigurationSchema {
Self::Parameters::schema()
}

fn set_parameters(&mut self, parameters: Self::Parameters) {
self.animation.set_parameters(parameters.inner.clone());
self.parameters = parameters;
Expand Down
14 changes: 6 additions & 8 deletions animation-utils/src/decorators/speed_controlled.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use animation_api::schema::{ConfigurationSchema, GetSchema, ParameterSchema, ValueSchema};
use animation_api::schema::{GetEnumOptions, GetSchema, ParameterSchema, ValueSchema};
use animation_api::Animation;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
Expand All @@ -12,15 +12,15 @@ pub struct Parameters<P: GetSchema> {
}

impl<P: GetSchema> GetSchema for Parameters<P> {
fn schema() -> ConfigurationSchema {
fn schema() -> Vec<ParameterSchema> {
let mut parameters = vec![ParameterSchema {
id: "speed_factor".to_owned(),
name: "Speed Factor".to_owned(),
description: None,
value: ValueSchema::Speed,
}];
parameters.extend(P::schema().parameters);
ConfigurationSchema { parameters }
parameters.extend(P::schema());
parameters
}
}

Expand All @@ -33,8 +33,10 @@ impl<A> Animation for SpeedControlled<A>
where
A: Animation,
A::Parameters: GetSchema + Default + Clone + Serialize + DeserializeOwned,
A::CustomTriggers: GetEnumOptions + Clone + Serialize + DeserializeOwned,
{
type Parameters = Parameters<A::Parameters>;
type CustomTriggers = A::CustomTriggers;
type Wrapped = Self;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand All @@ -59,10 +61,6 @@ where
self.animation.render()
}

fn get_schema(&self) -> ConfigurationSchema {
Self::Parameters::schema()
}

fn set_parameters(&mut self, parameters: Self::Parameters) {
self.animation.set_parameters(parameters.inner.clone());
self.parameters = parameters;
Expand Down
1 change: 1 addition & 0 deletions animations/audio-boom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct AudioBoom {

impl Animation for AudioBoom {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/audio-visualizer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct AudioVisualizer {

impl Animation for AudioVisualizer {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/audio-wave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct AudioWave {

impl Animation for AudioWave {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/barber-pole/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct BarberPole {

impl Animation for BarberPole {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/beats/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct Beats {

impl Animation for Beats {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = OffSwitch<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/circle-boom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct CircleBoom {

impl Animation for CircleBoom {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = BrightnessControlled<Self>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/circle-grid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub struct CircleGrid {

impl Animation for CircleGrid {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = OffSwitch<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/circle-wave/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct CircleWave {

impl Animation for CircleWave {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = BrightnessControlled<Self>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/classic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub struct Classic {

impl Animation for Classic {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/doom-fire/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub struct DoomFireAnimation {

impl Animation for DoomFireAnimation {
type Parameters = Parameters;
type CustomTriggers = ();
type Wrapped = SpeedControlled<BrightnessControlled<Self>>;

fn new(points: Vec<(f64, f64, f64)>) -> Self {
Expand Down
1 change: 1 addition & 0 deletions animations/draw/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lightfx = { path = "../../lightfx" }

serde = "1.0.215"
nalgebra = "0.33.2"
serde_plain = "1.0.2"

[dependencies.animation-wasm-bindings]
path = "../../animation-wasm-bindings"
Expand Down
Loading

0 comments on commit 707cb55

Please sign in to comment.