use crate::{
input::{Input, event::InputEvent},
storage::asset::{
curve::multiplied_curve::MultipliedCurve, project::Project, scene::instance::SceneInstance,
},
};
pub(crate) trait InputEventState {
fn is_new(&self) -> bool;
fn is_live(&self) -> bool;
fn dimmer(&self) -> f32;
}
impl InputEventState for InputEvent {
fn is_new(&self) -> bool {
Input::is_event_new(self)
}
fn is_live(&self) -> bool {
Input::event_live_value(self).is_some()
}
fn dimmer(&self) -> f32 {
Input::event_live_value(self)
.map(|value| f32::from(value) / 255.0)
.unwrap_or(0.0)
}
}
pub(crate) trait SceneInstanceInputs {
fn update_input_state(&mut self, beat_progression: f32);
}
impl SceneInstanceInputs for SceneInstance {
fn update_input_state(&mut self, beat_progression: f32) {
if let Some(event) = self.activation_input.as_ref()
&& event.is_new()
{
self.active = !self.active;
}
if let Some(event) = self.flash_input.as_ref() {
let flash_active = event.is_live();
if !self.flash && flash_active && self.set_offset_on_flash {
self.beat_progression_offset =
MultipliedCurve::new_multiplier(4.0 - beat_progression % 4.0);
}
self.flash = flash_active;
}
if let Some(event) = self.dimmer_input.as_ref() {
self.input_dimmer = event.dimmer()
}
}
}
pub(crate) trait ProjectInputs {
fn update_input_state(&mut self, beat_progression: f32);
fn tap_input_is_new(&self) -> bool;
fn blackout_input_is_new(&self) -> bool;
fn blackout_hold_input_is_live(&self) -> bool;
fn half_input_is_new(&self) -> bool;
fn double_input_is_new(&self) -> bool;
}
impl ProjectInputs for Project {
fn update_input_state(&mut self, beat_progression: f32) {
self.scenes_instances_grid
.values_mut()
.for_each(|scene_instance| scene_instance.update_input_state(beat_progression));
}
fn tap_input_is_new(&self) -> bool {
self.tap_input_events.iter().any(|event| event.is_new())
}
fn blackout_input_is_new(&self) -> bool {
self.blackout_input_events
.iter()
.any(|event| event.is_new())
}
fn blackout_hold_input_is_live(&self) -> bool {
self.blackout_hold_input_events
.iter()
.any(|event| event.is_live())
}
fn half_input_is_new(&self) -> bool {
self.half_input_events.iter().any(|event| event.is_new())
}
fn double_input_is_new(&self) -> bool {
self.double_input_events.iter().any(|event| event.is_new())
}
}