start work on compositing, effects

This commit is contained in:
Skyler Lehmkuhl
2025-12-08 09:30:22 -05:00
parent 420f3bf7b9
commit 7eb61ab0a8
31 changed files with 3172 additions and 150 deletions
@@ -98,6 +98,9 @@ impl Action for AddClipInstanceAction {
AnyLayer::Video(video_layer) => {
video_layer.clip_instances.push(self.clip_instance.clone());
}
AnyLayer::Effect(_) => {
return Err("Cannot add clip instances to effect layers".to_string());
}
}
self.executed = true;
@@ -130,6 +133,9 @@ impl Action for AddClipInstanceAction {
.clip_instances
.retain(|ci| ci.id != instance_id);
}
AnyLayer::Effect(_) => {
// Effect layers don't have clip instances, nothing to rollback
}
}
self.executed = false;
@@ -0,0 +1,232 @@
//! Add effect action
//!
//! Handles adding a new effect instance (as a ClipInstance) to an effect layer.
use crate::action::Action;
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::AnyLayer;
use uuid::Uuid;
/// Action that adds an effect instance to an effect layer
///
/// Effect instances are represented as ClipInstance objects where clip_id
/// references an EffectDefinition.
pub struct AddEffectAction {
/// ID of the layer to add the effect to
layer_id: Uuid,
/// The clip instance (effect) to add
instance: Option<ClipInstance>,
/// Index to insert at (None = append to end)
insert_index: Option<usize>,
/// ID of the created effect (set after execution)
created_effect_id: Option<Uuid>,
}
impl AddEffectAction {
/// Create a new add effect action
///
/// # Arguments
///
/// * `layer_id` - ID of the effect layer to add the effect to
/// * `instance` - The clip instance (referencing an effect definition) to add
pub fn new(layer_id: Uuid, instance: ClipInstance) -> Self {
Self {
layer_id,
instance: Some(instance),
insert_index: None,
created_effect_id: None,
}
}
/// Create a new add effect action that inserts at a specific index
///
/// # Arguments
///
/// * `layer_id` - ID of the effect layer to add the effect to
/// * `instance` - The clip instance (referencing an effect definition) to add
/// * `index` - Index to insert at
pub fn at_index(layer_id: Uuid, instance: ClipInstance, index: usize) -> Self {
Self {
layer_id,
instance: Some(instance),
insert_index: Some(index),
created_effect_id: None,
}
}
/// Get the ID of the created effect (after execution)
pub fn created_effect_id(&self) -> Option<Uuid> {
self.created_effect_id
}
/// Get the layer ID this effect was added to
pub fn layer_id(&self) -> Uuid {
self.layer_id
}
}
impl Action for AddEffectAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Take the instance (can only execute once without rollback)
let instance = self.instance.take()
.ok_or_else(|| "Effect already added (call rollback first)".to_string())?;
// Store the instance ID
let instance_id = instance.id;
// Find the effect layer
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
// Ensure it's an effect layer
let effect_layer = match layer {
AnyLayer::Effect(ref mut el) => el,
_ => return Err("Layer is not an effect layer".to_string()),
};
// Add or insert the effect
match self.insert_index {
Some(index) => {
effect_layer.insert_clip_instance(index, instance);
}
None => {
effect_layer.add_clip_instance(instance);
}
}
self.created_effect_id = Some(instance_id);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let instance_id = self.created_effect_id
.ok_or_else(|| "No effect to remove (not executed yet)".to_string())?;
// Find the effect layer
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
// Ensure it's an effect layer
let effect_layer = match layer {
AnyLayer::Effect(ref mut el) => el,
_ => return Err("Layer is not an effect layer".to_string()),
};
// Remove the instance and store it for potential re-execution
let removed = effect_layer.remove_clip_instance(&instance_id)
.ok_or_else(|| format!("Effect instance {} not found", instance_id))?;
// Store the instance back for potential redo
self.instance = Some(removed);
self.created_effect_id = None;
Ok(())
}
fn description(&self) -> String {
"Add effect".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::effect::{EffectCategory, EffectDefinition, EffectParameterDef};
use crate::effect_layer::EffectLayer;
use crate::layer::AnyLayer;
fn create_test_setup() -> (Document, Uuid, EffectDefinition) {
let mut document = Document::new("Test");
// Create effect layer
let effect_layer = EffectLayer::new("Effects");
let layer_id = effect_layer.layer.id;
document.root_mut().add_child(AnyLayer::Effect(effect_layer));
// Create effect definition
let def = EffectDefinition::new(
"Test Effect",
EffectCategory::Color,
"// shader code",
vec![EffectParameterDef::float_range("intensity", "Intensity", 1.0, 0.0, 2.0)],
);
(document, layer_id, def)
}
#[test]
fn test_add_effect() {
let (mut document, layer_id, def) = create_test_setup();
let instance = def.create_instance(0.0, 10.0);
let instance_id = instance.id;
let mut action = AddEffectAction::new(layer_id, instance);
action.execute(&mut document).unwrap();
// Verify effect was added
assert_eq!(action.created_effect_id(), Some(instance_id));
let layer = document.get_layer(&layer_id).unwrap();
if let AnyLayer::Effect(el) = layer {
assert_eq!(el.clip_instances.len(), 1);
assert_eq!(el.clip_instances[0].id, instance_id);
} else {
panic!("Expected effect layer");
}
}
#[test]
fn test_add_effect_rollback() {
let (mut document, layer_id, def) = create_test_setup();
let instance = def.create_instance(0.0, 10.0);
let mut action = AddEffectAction::new(layer_id, instance);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
// Verify effect was removed
let layer = document.get_layer(&layer_id).unwrap();
if let AnyLayer::Effect(el) = layer {
assert_eq!(el.clip_instances.len(), 0);
} else {
panic!("Expected effect layer");
}
}
#[test]
fn test_add_effect_at_index() {
let (mut document, layer_id, def) = create_test_setup();
// Add first effect
let instance1 = def.create_instance(0.0, 10.0);
let id1 = instance1.id;
let mut action1 = AddEffectAction::new(layer_id, instance1);
action1.execute(&mut document).unwrap();
// Add second effect
let instance2 = def.create_instance(0.0, 10.0);
let id2 = instance2.id;
let mut action2 = AddEffectAction::new(layer_id, instance2);
action2.execute(&mut document).unwrap();
// Insert third effect at index 1 (between first and second)
let instance3 = def.create_instance(0.0, 10.0);
let id3 = instance3.id;
let mut action3 = AddEffectAction::at_index(layer_id, instance3, 1);
action3.execute(&mut document).unwrap();
// Verify order: [id1, id3, id2]
let layer = document.get_layer(&layer_id).unwrap();
if let AnyLayer::Effect(el) = layer {
assert_eq!(el.clip_instances.len(), 3);
assert_eq!(el.clip_instances[0].id, id1);
assert_eq!(el.clip_instances[1].id, id3);
assert_eq!(el.clip_instances[2].id, id2);
} else {
panic!("Expected effect layer");
}
}
}
@@ -76,6 +76,7 @@ impl Action for AddLayerAction {
AnyLayer::Vector(_) => "Add vector layer",
AnyLayer::Audio(_) => "Add audio layer",
AnyLayer::Video(_) => "Add video layer",
AnyLayer::Effect(_) => "Add effect layer",
}
.to_string()
}
@@ -4,11 +4,13 @@
//! through the action system.
pub mod add_clip_instance;
pub mod add_effect;
pub mod add_layer;
pub mod add_shape;
pub mod move_clip_instances;
pub mod move_objects;
pub mod paint_bucket;
pub mod remove_effect;
pub mod set_document_properties;
pub mod set_instance_properties;
pub mod set_layer_properties;
@@ -18,11 +20,13 @@ pub mod transform_objects;
pub mod trim_clip_instances;
pub use add_clip_instance::AddClipInstanceAction;
pub use add_effect::AddEffectAction;
pub use add_layer::AddLayerAction;
pub use add_shape::AddShapeAction;
pub use move_clip_instances::MoveClipInstancesAction;
pub use move_objects::MoveShapeInstancesAction;
pub use paint_bucket::PaintBucketAction;
pub use remove_effect::RemoveEffectAction;
pub use set_document_properties::SetDocumentPropertiesAction;
pub use set_instance_properties::{InstancePropertyChange, SetInstancePropertiesAction};
pub use set_layer_properties::{LayerProperty, SetLayerPropertiesAction};
@@ -3,6 +3,7 @@
//! Handles moving one or more clip instances along the timeline.
use crate::action::Action;
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::AnyLayer;
use std::collections::HashMap;
@@ -50,10 +51,11 @@ impl Action for MoveClipInstancesAction {
// Find member's current position
if let Some(layer) = document.get_layer(member_layer_id) {
let clip_instances = match layer {
let clip_instances: &[ClipInstance] = match layer {
AnyLayer::Vector(vl) => &vl.clip_instances,
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
@@ -88,10 +90,11 @@ impl Action for MoveClipInstancesAction {
for (instance_id, old_start, new_start) in moves {
// Get the instance to calculate its duration
let clip_instances = match layer {
let clip_instances: &[ClipInstance] = match layer {
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Vector(vl) => &vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
let instance = clip_instances.iter()
@@ -138,6 +141,7 @@ impl Action for MoveClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
// Update timeline_start for each clip instance
@@ -162,6 +166,7 @@ impl Action for MoveClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
// Restore original timeline_start for each clip instance
@@ -0,0 +1,223 @@
//! Remove effect action
//!
//! Handles removing an effect instance (ClipInstance) from an effect layer.
use crate::action::Action;
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::AnyLayer;
use uuid::Uuid;
/// Action that removes an effect instance from an effect layer
pub struct RemoveEffectAction {
/// ID of the layer containing the effect
layer_id: Uuid,
/// ID of the effect instance to remove
instance_id: Uuid,
/// The removed instance (stored for undo)
removed_instance: Option<ClipInstance>,
/// Index where the instance was (for proper undo position)
removed_index: Option<usize>,
}
impl RemoveEffectAction {
/// Create a new remove effect action
///
/// # Arguments
///
/// * `layer_id` - ID of the effect layer containing the effect
/// * `instance_id` - ID of the clip instance to remove
pub fn new(layer_id: Uuid, instance_id: Uuid) -> Self {
Self {
layer_id,
instance_id,
removed_instance: None,
removed_index: None,
}
}
/// Get the layer ID
pub fn layer_id(&self) -> Uuid {
self.layer_id
}
/// Get the instance ID that was/will be removed
pub fn instance_id(&self) -> Uuid {
self.instance_id
}
}
impl Action for RemoveEffectAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Find the effect layer
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
// Ensure it's an effect layer
let effect_layer = match layer {
AnyLayer::Effect(ref mut el) => el,
_ => return Err("Layer is not an effect layer".to_string()),
};
// Find the index before removing
let index = effect_layer.clip_instance_index(&self.instance_id)
.ok_or_else(|| format!("Effect instance {} not found", self.instance_id))?;
// Remove the instance
let removed = effect_layer.remove_clip_instance(&self.instance_id)
.ok_or_else(|| format!("Effect instance {} not found", self.instance_id))?;
// Store for undo
self.removed_instance = Some(removed);
self.removed_index = Some(index);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let instance = self.removed_instance.take()
.ok_or_else(|| "No instance to restore (not executed yet)".to_string())?;
let index = self.removed_index
.ok_or_else(|| "No index stored (not executed yet)".to_string())?;
// Find the effect layer
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
// Ensure it's an effect layer
let effect_layer = match layer {
AnyLayer::Effect(ref mut el) => el,
_ => return Err("Layer is not an effect layer".to_string()),
};
// Insert the instance back at its original position
effect_layer.insert_clip_instance(index, instance);
self.removed_index = None;
Ok(())
}
fn description(&self) -> String {
"Remove effect".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::effect::{EffectCategory, EffectDefinition, EffectParameterDef};
use crate::effect_layer::EffectLayer;
use crate::layer::AnyLayer;
fn create_test_setup() -> (Document, Uuid, EffectDefinition) {
let mut document = Document::new("Test");
// Create effect layer
let effect_layer = EffectLayer::new("Effects");
let layer_id = effect_layer.layer.id;
document.root_mut().add_child(AnyLayer::Effect(effect_layer));
// Create effect definition
let def = EffectDefinition::new(
"Test Effect",
EffectCategory::Color,
"// shader code",
vec![EffectParameterDef::float_range("intensity", "Intensity", 1.0, 0.0, 2.0)],
);
(document, layer_id, def)
}
#[test]
fn test_remove_effect() {
let (mut document, layer_id, def) = create_test_setup();
// Add an effect first
let instance = def.create_instance(0.0, 10.0);
let instance_id = instance.id;
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
el.add_clip_instance(instance);
}
// Verify effect exists
if let Some(AnyLayer::Effect(el)) = document.get_layer(&layer_id) {
assert_eq!(el.clip_instances.len(), 1);
}
// Remove the effect
let mut action = RemoveEffectAction::new(layer_id, instance_id);
action.execute(&mut document).unwrap();
// Verify effect was removed
if let Some(AnyLayer::Effect(el)) = document.get_layer(&layer_id) {
assert_eq!(el.clip_instances.len(), 0);
}
}
#[test]
fn test_remove_effect_rollback() {
let (mut document, layer_id, def) = create_test_setup();
// Add an effect first
let instance = def.create_instance(0.0, 10.0);
let instance_id = instance.id;
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
el.add_clip_instance(instance);
}
// Remove and rollback
let mut action = RemoveEffectAction::new(layer_id, instance_id);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
// Verify effect was restored
if let Some(AnyLayer::Effect(el)) = document.get_layer(&layer_id) {
assert_eq!(el.clip_instances.len(), 1);
assert_eq!(el.clip_instances[0].id, instance_id);
}
}
#[test]
fn test_remove_preserves_order() {
let (mut document, layer_id, def) = create_test_setup();
// Add three effects
let instance1 = def.create_instance(0.0, 10.0);
let id1 = instance1.id;
let instance2 = def.create_instance(0.0, 10.0);
let id2 = instance2.id;
let instance3 = def.create_instance(0.0, 10.0);
let id3 = instance3.id;
if let Some(AnyLayer::Effect(el)) = document.get_layer_mut(&layer_id) {
el.add_clip_instance(instance1);
el.add_clip_instance(instance2);
el.add_clip_instance(instance3);
}
// Remove middle effect
let mut action = RemoveEffectAction::new(layer_id, id2);
action.execute(&mut document).unwrap();
// Verify order: [id1, id3]
if let Some(AnyLayer::Effect(el)) = document.get_layer(&layer_id) {
assert_eq!(el.clip_instances.len(), 2);
assert_eq!(el.clip_instances[0].id, id1);
assert_eq!(el.clip_instances[1].id, id3);
}
// Rollback - effect should be restored at index 1
action.rollback(&mut document).unwrap();
// Verify order: [id1, id2, id3]
if let Some(AnyLayer::Effect(el)) = document.get_layer(&layer_id) {
assert_eq!(el.clip_instances.len(), 3);
assert_eq!(el.clip_instances[0].id, id1);
assert_eq!(el.clip_instances[1].id, id2);
assert_eq!(el.clip_instances[2].id, id3);
}
}
}
@@ -40,6 +40,7 @@ impl Action for TransformClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => return Ok(()), // Effect layers don't have clip instances
};
// Apply new transforms
@@ -62,6 +63,7 @@ impl Action for TransformClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => return Ok(()), // Effect layers don't have clip instances
};
// Restore old transforms
@@ -3,6 +3,7 @@
//! Handles trimming one or more clip instances by adjusting trim_start and/or trim_end.
use crate::action::Action;
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::AnyLayer;
use std::collections::HashMap;
@@ -93,10 +94,11 @@ impl Action for TrimClipInstancesAction {
// Find member's current values
if let Some(layer) = document.get_layer(member_layer_id) {
let clip_instances = match layer {
let clip_instances: &[ClipInstance] = match layer {
AnyLayer::Vector(vl) => &vl.clip_instances,
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Effect(_) => continue,
};
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
@@ -127,10 +129,11 @@ impl Action for TrimClipInstancesAction {
// Find member's current trim_end
if let Some(layer) = document.get_layer(member_layer_id) {
let clip_instances = match layer {
let clip_instances: &[ClipInstance] = match layer {
AnyLayer::Vector(vl) => &vl.clip_instances,
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Effect(_) => continue,
};
if let Some(instance) = clip_instances.iter().find(|ci| ci.id == *member_instance_id) {
@@ -168,10 +171,11 @@ impl Action for TrimClipInstancesAction {
let mut clamped_layer_trims = Vec::new();
for (instance_id, trim_type, old, new) in trims {
let clip_instances = match layer {
let clip_instances: &[ClipInstance] = match layer {
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Vector(vl) => &vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
let instance = clip_instances.iter()
@@ -262,6 +266,7 @@ impl Action for TrimClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
// Apply trims
@@ -299,6 +304,7 @@ impl Action for TrimClipInstancesAction {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
AnyLayer::Effect(_) => continue, // Effect layers don't have clip instances
};
// Restore original trim values