Clips in timeline

This commit is contained in:
Skyler Lehmkuhl
2025-11-28 05:53:11 -05:00
parent 1cb2aabc9c
commit bbeb85b3a3
24 changed files with 3613 additions and 424 deletions
@@ -0,0 +1,126 @@
//! Add layer action
//!
//! Handles adding a new layer to the document.
use crate::action::Action;
use crate::document::Document;
use crate::layer::{AnyLayer, VectorLayer};
use uuid::Uuid;
/// Action that adds a new layer to the document
pub struct AddLayerAction {
/// The layer to add
layer: AnyLayer,
/// ID of the created layer (set after execution)
created_layer_id: Option<Uuid>,
}
impl AddLayerAction {
/// Create a new add layer action with a vector layer
///
/// # Arguments
///
/// * `name` - The name for the new layer
pub fn new_vector(name: impl Into<String>) -> Self {
let layer = VectorLayer::new(name);
Self {
layer: AnyLayer::Vector(layer),
created_layer_id: None,
}
}
/// Create a new add layer action with any layer type
///
/// # Arguments
///
/// * `layer` - The layer to add
pub fn new(layer: AnyLayer) -> Self {
Self {
layer,
created_layer_id: None,
}
}
/// Get the ID of the created layer (after execution)
pub fn created_layer_id(&self) -> Option<Uuid> {
self.created_layer_id
}
}
impl Action for AddLayerAction {
fn execute(&mut self, document: &mut Document) {
// Add layer to the document's root
let layer_id = document.root_mut().add_child(self.layer.clone());
// Store the ID for rollback
self.created_layer_id = Some(layer_id);
}
fn rollback(&mut self, document: &mut Document) {
// Remove the created layer if it exists
if let Some(layer_id) = self.created_layer_id {
document.root_mut().remove_child(&layer_id);
// Clear the stored ID
self.created_layer_id = None;
}
}
fn description(&self) -> String {
match &self.layer {
AnyLayer::Vector(_) => "Add vector layer",
AnyLayer::Audio(_) => "Add audio layer",
AnyLayer::Video(_) => "Add video layer",
}
.to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_add_vector_layer() {
let mut document = Document::new("Test");
assert_eq!(document.root.children.len(), 0);
// Create and execute action
let mut action = AddLayerAction::new_vector("New Layer");
action.execute(&mut document);
// Verify layer was added
assert_eq!(document.root.children.len(), 1);
let layer = &document.root.children[0];
assert_eq!(layer.layer().name, "New Layer");
assert!(matches!(layer, AnyLayer::Vector(_)));
// Rollback
action.rollback(&mut document);
// Verify layer was removed
assert_eq!(document.root.children.len(), 0);
}
#[test]
fn test_add_layer_description() {
let action = AddLayerAction::new_vector("Test");
assert_eq!(action.description(), "Add vector layer");
}
#[test]
fn test_add_multiple_layers() {
let mut document = Document::new("Test");
let mut action1 = AddLayerAction::new_vector("Layer 1");
let mut action2 = AddLayerAction::new_vector("Layer 2");
action1.execute(&mut document);
action2.execute(&mut document);
assert_eq!(document.root.children.len(), 2);
assert_eq!(document.root.children[0].layer().name, "Layer 1");
assert_eq!(document.root.children[1].layer().name, "Layer 2");
}
}
@@ -5,13 +5,13 @@
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::object::Object;
use crate::object::ShapeInstance;
use crate::shape::Shape;
use uuid::Uuid;
/// Action that adds a shape and object to a vector layer
///
/// This action creates both a Shape (the path/geometry) and an Object
/// This action creates both a Shape (the path/geometry) and an ShapeInstance
/// (the instance with transform). Both are added to the layer.
pub struct AddShapeAction {
/// Layer ID to add the shape to
@@ -21,7 +21,7 @@ pub struct AddShapeAction {
shape: Shape,
/// The object to add (references the shape with transform)
object: Object,
object: ShapeInstance,
/// ID of the created shape (set after execution)
created_shape_id: Option<Uuid>,
@@ -38,7 +38,7 @@ impl AddShapeAction {
/// * `layer_id` - The layer to add the shape to
/// * `shape` - The shape to add
/// * `object` - The object instance referencing the shape
pub fn new(layer_id: Uuid, shape: Shape, object: Object) -> Self {
pub fn new(layer_id: Uuid, shape: Shape, object: ShapeInstance) -> Self {
Self {
layer_id,
shape,
@@ -110,7 +110,7 @@ mod tests {
let rect = Rect::new(0.0, 0.0, 100.0, 50.0);
let path = rect.to_path(0.1);
let shape = Shape::new(path).with_fill(ShapeColor::rgb(255, 0, 0));
let object = Object::new(shape.id).with_position(50.0, 50.0);
let object = ShapeInstance::new(shape.id).with_position(50.0, 50.0);
// Create and execute action
let mut action = AddShapeAction::new(layer_id, shape, object);
@@ -119,9 +119,9 @@ mod tests {
// Verify shape and object were added
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(layer.shapes.len(), 1);
assert_eq!(layer.objects.len(), 1);
assert_eq!(layer.shape_instances.len(), 1);
let added_object = &layer.objects[0];
let added_object = &layer.shape_instances[0];
assert_eq!(added_object.transform.x, 50.0);
assert_eq!(added_object.transform.y, 50.0);
} else {
@@ -134,7 +134,7 @@ mod tests {
// Verify shape and object were removed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(layer.shapes.len(), 0);
assert_eq!(layer.objects.len(), 0);
assert_eq!(layer.shape_instances.len(), 0);
}
}
@@ -149,7 +149,7 @@ mod tests {
let path = circle.to_path(0.1);
let shape = Shape::new(path)
.with_fill(ShapeColor::rgb(0, 255, 0));
let object = Object::new(shape.id);
let object = ShapeInstance::new(shape.id);
let mut action = AddShapeAction::new(layer_id, shape, object);
@@ -161,7 +161,7 @@ mod tests {
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(layer.shapes.len(), 1);
assert_eq!(layer.objects.len(), 1);
assert_eq!(layer.shape_instances.len(), 1);
}
}
@@ -174,7 +174,7 @@ mod tests {
let rect = Rect::new(0.0, 0.0, 50.0, 50.0);
let path = rect.to_path(0.1);
let shape = Shape::new(path);
let object = Object::new(shape.id);
let object = ShapeInstance::new(shape.id);
let mut action = AddShapeAction::new(layer_id, shape, object);
@@ -185,7 +185,7 @@ mod tests {
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Should have 2 shapes and 2 objects
assert_eq!(layer.shapes.len(), 2);
assert_eq!(layer.objects.len(), 2);
assert_eq!(layer.shape_instances.len(), 2);
}
}
}
@@ -3,12 +3,20 @@
//! This module contains all the concrete action types that can be executed
//! through the action system.
pub mod add_layer;
pub mod add_shape;
pub mod move_clip_instances;
pub mod move_objects;
pub mod paint_bucket;
pub mod transform_clip_instances;
pub mod transform_objects;
pub mod trim_clip_instances;
pub use add_layer::AddLayerAction;
pub use add_shape::AddShapeAction;
pub use move_objects::MoveObjectsAction;
pub use move_clip_instances::MoveClipInstancesAction;
pub use move_objects::MoveShapeInstancesAction;
pub use paint_bucket::PaintBucketAction;
pub use transform_objects::TransformObjectsAction;
pub use transform_clip_instances::TransformClipInstancesAction;
pub use transform_objects::TransformShapeInstancesAction;
pub use trim_clip_instances::{TrimClipInstancesAction, TrimData, TrimType};
@@ -0,0 +1,143 @@
//! Move clip instances action
//!
//! Handles moving one or more clip instances along the timeline.
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use std::collections::HashMap;
use uuid::Uuid;
/// Action that moves clip instances to new timeline positions
pub struct MoveClipInstancesAction {
/// Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start)
layer_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>>,
}
impl MoveClipInstancesAction {
/// Create a new move clip instances action
///
/// # Arguments
///
/// * `layer_moves` - Map of layer IDs to vectors of (clip_instance_id, old_timeline_start, new_timeline_start)
pub fn new(layer_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>>) -> Self {
Self { layer_moves }
}
}
impl Action for MoveClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
for (layer_id, moves) in &self.layer_moves {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Update timeline_start for each clip instance
for (clip_id, _old, new) in moves {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id)
{
clip_instance.timeline_start = *new;
}
}
}
}
fn rollback(&mut self, document: &mut Document) {
for (layer_id, moves) in &self.layer_moves {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Restore original timeline_start for each clip instance
for (clip_id, old, _new) in moves {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id)
{
clip_instance.timeline_start = *old;
}
}
}
}
fn description(&self) -> String {
let total_count: usize = self.layer_moves.values().map(|v| v.len()).sum();
if total_count == 1 {
"Move clip instance".to_string()
} else {
format!("Move {} clip instances", total_count)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clip::{Clip, ClipInstance, ClipType};
use crate::layer::VectorLayer;
#[test]
fn test_move_clip_instances_action() {
// Create a document with a test clip instance
let mut document = Document::new("Test");
let clip = Clip::new(ClipType::Vector, "Test Clip", None);
let clip_id = clip.id;
let mut vector_layer = VectorLayer::new("Layer 1");
vector_layer.clips.push(clip);
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.timeline_start = 1.0; // Start at 1 second
let instance_id = clip_instance.id;
vector_layer.clip_instances.push(clip_instance);
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
// Create move action: move from 1.0 to 5.0 seconds
let mut layer_moves = HashMap::new();
layer_moves.insert(layer_id, vec![(instance_id, 1.0, 5.0)]);
let mut action = MoveClipInstancesAction::new(layer_moves);
// Execute
action.execute(&mut document);
// Verify position changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.timeline_start, 5.0);
}
// Rollback
action.rollback(&mut document);
// Verify position restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.timeline_start, 1.0);
}
}
}
@@ -1,6 +1,6 @@
//! Move objects action
//! Move shape instances action
//!
//! Handles moving one or more objects to new positions.
//! Handles moving one or more shape instances to new positions.
use crate::action::Action;
use crate::document::Document;
@@ -9,31 +9,31 @@ use std::collections::HashMap;
use uuid::Uuid;
use vello::kurbo::Point;
/// Action that moves objects to new positions
pub struct MoveObjectsAction {
/// Layer ID containing the objects
/// Action that moves shape instances to new positions
pub struct MoveShapeInstancesAction {
/// Layer ID containing the shape instances
layer_id: Uuid,
/// Map of object IDs to their old and new positions
object_positions: HashMap<Uuid, (Point, Point)>, // (old_pos, new_pos)
shape_instance_positions: HashMap<Uuid, (Point, Point)>, // (old_pos, new_pos)
}
impl MoveObjectsAction {
/// Create a new move objects action
impl MoveShapeInstancesAction {
/// Create a new move shape instances action
///
/// # Arguments
///
/// * `layer_id` - The layer containing the objects
/// * `object_positions` - Map of object IDs to (old_position, new_position)
pub fn new(layer_id: Uuid, object_positions: HashMap<Uuid, (Point, Point)>) -> Self {
/// * `layer_id` - The layer containing the shape instances
/// * `shape_instance_positions` - Map of object IDs to (old_position, new_position)
pub fn new(layer_id: Uuid, shape_instance_positions: HashMap<Uuid, (Point, Point)>) -> Self {
Self {
layer_id,
object_positions,
shape_instance_positions,
}
}
}
impl Action for MoveObjectsAction {
impl Action for MoveShapeInstancesAction {
fn execute(&mut self, document: &mut Document) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
@@ -41,8 +41,8 @@ impl Action for MoveObjectsAction {
};
if let AnyLayer::Vector(vector_layer) = layer {
for (object_id, (_old, new)) in &self.object_positions {
vector_layer.modify_object_internal(object_id, |obj| {
for (shape_instance_id, (_old, new)) in &self.shape_instance_positions {
vector_layer.modify_object_internal(shape_instance_id, |obj| {
obj.transform.x = new.x;
obj.transform.y = new.y;
});
@@ -57,8 +57,8 @@ impl Action for MoveObjectsAction {
};
if let AnyLayer::Vector(vector_layer) = layer {
for (object_id, (old, _new)) in &self.object_positions {
vector_layer.modify_object_internal(object_id, |obj| {
for (shape_instance_id, (old, _new)) in &self.shape_instance_positions {
vector_layer.modify_object_internal(shape_instance_id, |obj| {
obj.transform.x = old.x;
obj.transform.y = old.y;
});
@@ -67,11 +67,11 @@ impl Action for MoveObjectsAction {
}
fn description(&self) -> String {
let count = self.object_positions.len();
let count = self.shape_instance_positions.len();
if count == 1 {
"Move object".to_string()
"Move shape instance".to_string()
} else {
format!("Move {} objects", count)
format!("Move {} shape instances", count)
}
}
}
@@ -80,40 +80,40 @@ impl Action for MoveObjectsAction {
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::object::Object;
use crate::object::ShapeInstance;
use crate::shape::Shape;
use vello::kurbo::{Circle, Shape as KurboShape};
#[test]
fn test_move_objects_action() {
fn test_move_shape_instances_action() {
// Create a document with a test object
let mut document = Document::new("Test");
let circle = Circle::new((100.0, 100.0), 50.0);
let path = circle.to_path(0.1);
let shape = Shape::new(path);
let object = Object::new(shape.id).with_position(50.0, 50.0);
let object = ShapeInstance::new(shape.id).with_position(50.0, 50.0);
let mut vector_layer = VectorLayer::new("Layer 1");
vector_layer.add_shape(shape);
let object_id = vector_layer.add_object(object);
let shape_instance_id = vector_layer.add_object(object);
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
// Create move action
let mut positions = HashMap::new();
positions.insert(
object_id,
shape_instance_id,
(Point::new(50.0, 50.0), Point::new(150.0, 200.0))
);
let mut action = MoveObjectsAction::new(layer_id, positions);
let mut action = MoveShapeInstancesAction::new(layer_id, positions);
// Execute
action.execute(&mut document);
// Verify position changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let obj = layer.get_object(&object_id).unwrap();
let obj = layer.get_object(&shape_instance_id).unwrap();
assert_eq!(obj.transform.x, 150.0);
assert_eq!(obj.transform.y, 200.0);
}
@@ -123,7 +123,7 @@ mod tests {
// Verify position restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let obj = layer.get_object(&object_id).unwrap();
let obj = layer.get_object(&shape_instance_id).unwrap();
assert_eq!(obj.transform.x, 50.0);
assert_eq!(obj.transform.y, 50.0);
}
@@ -8,7 +8,7 @@ use crate::curve_segment::CurveSegment;
use crate::document::Document;
use crate::gap_handling::GapHandlingMode;
use crate::layer::AnyLayer;
use crate::object::Object;
use crate::object::ShapeInstance;
use crate::planar_graph::PlanarGraph;
use crate::shape::ShapeColor;
use uuid::Uuid;
@@ -34,8 +34,8 @@ pub struct PaintBucketAction {
/// ID of the created shape (set after execution)
created_shape_id: Option<Uuid>,
/// ID of the created object (set after execution)
created_object_id: Option<Uuid>,
/// ID of the created shape instance (set after execution)
created_shape_instance_id: Option<Uuid>,
}
impl PaintBucketAction {
@@ -62,7 +62,7 @@ impl PaintBucketAction {
tolerance,
gap_mode,
created_shape_id: None,
created_object_id: None,
created_shape_instance_id: None,
}
}
}
@@ -74,10 +74,10 @@ impl Action for PaintBucketAction {
// Optimization: Check if we're clicking on an existing shape first
// This is much faster than building a planar graph
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer_mut(&self.layer_id) {
// Iterate through objects in reverse order (topmost first)
for object in vector_layer.objects.iter().rev() {
// Find the corresponding shape
if let Some(shape) = vector_layer.shapes.iter().find(|s| s.id == object.shape_id) {
// Iterate through shape instances in reverse order (topmost first)
for shape_instance in vector_layer.shape_instances.iter().rev() {
// Find the corresponding shape (O(1) HashMap lookup)
if let Some(shape) = vector_layer.shapes.get(&shape_instance.shape_id) {
// Skip shapes without fill color (e.g., lines with only stroke)
if shape.fill_color.is_none() {
continue;
@@ -92,8 +92,8 @@ impl Action for PaintBucketAction {
continue;
}
// Apply the object's transform to get the transformed path
let transform_affine = object.transform.to_affine();
// Apply the shape instance's transform to get the transformed path
let transform_affine = shape_instance.transform.to_affine();
// Transform the click point to shape's local coordinates (inverse transform)
let inverse_transform = transform_affine.inverse();
@@ -110,8 +110,8 @@ impl Action for PaintBucketAction {
// Store the shape ID before the immutable borrow ends
let shape_id = shape.id;
// Find mutable reference to the shape and update its fill
if let Some(shape_mut) = vector_layer.shapes.iter_mut().find(|s| s.id == shape_id) {
// Find mutable reference to the shape and update its fill (O(1) HashMap lookup)
if let Some(shape_mut) = vector_layer.shapes.get_mut(&shape_id) {
shape_mut.fill_color = Some(self.fill_color);
println!("Updated shape fill color");
}
@@ -154,20 +154,20 @@ impl Action for PaintBucketAction {
println!("DEBUG: Face shape created with fill_color: {:?}", face_shape.fill_color);
let face_object = Object::new(face_shape.id);
let face_shape_instance = ShapeInstance::new(face_shape.id);
// Store the created IDs for rollback
self.created_shape_id = Some(face_shape.id);
self.created_object_id = Some(face_object.id);
self.created_shape_instance_id = Some(face_shape_instance.id);
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer_mut(&self.layer_id) {
let shape_id_for_debug = face_shape.id;
vector_layer.add_shape_internal(face_shape);
vector_layer.add_object_internal(face_object);
vector_layer.add_object_internal(face_shape_instance);
println!("DEBUG: Added filled shape");
// Verify the shape still has the fill color after being added
if let Some(added_shape) = vector_layer.shapes.iter().find(|s| s.id == shape_id_for_debug) {
// Verify the shape still has the fill color after being added (O(1) HashMap lookup)
if let Some(added_shape) = vector_layer.shapes.get(&shape_id_for_debug) {
println!("DEBUG: After adding to layer, shape fill_color = {:?}", added_shape.fill_color);
}
}
@@ -180,7 +180,7 @@ impl Action for PaintBucketAction {
fn rollback(&mut self, document: &mut Document) {
// Remove the created shape and object if they exist
if let (Some(shape_id), Some(object_id)) = (self.created_shape_id, self.created_object_id) {
if let (Some(shape_id), Some(object_id)) = (self.created_shape_id, self.created_shape_instance_id) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
@@ -192,7 +192,7 @@ impl Action for PaintBucketAction {
}
self.created_shape_id = None;
self.created_object_id = None;
self.created_shape_instance_id = None;
}
}
@@ -219,11 +219,11 @@ fn extract_curves_from_all_shapes(
// Extract curves only from this vector layer
if let AnyLayer::Vector(vector_layer) = layer {
println!("Extracting curves from {} objects in layer", vector_layer.objects.len());
println!("Extracting curves from {} objects in layer", vector_layer.shape_instances.len());
// Extract curves from each object (which applies transforms to shapes)
for (obj_idx, object) in vector_layer.objects.iter().enumerate() {
// Find the shape for this object
let shape = match vector_layer.shapes.iter().find(|s| s.id == object.shape_id) {
for (obj_idx, object) in vector_layer.shape_instances.iter().enumerate() {
// Find the shape for this object (O(1) HashMap lookup)
let shape = match vector_layer.shapes.get(&object.shape_id) {
Some(s) => s,
None => continue,
};
@@ -313,12 +313,12 @@ mod tests {
let rect = Rect::new(0.0, 0.0, 100.0, 100.0);
let path = rect.to_path(0.1);
let shape = Shape::new(path);
let object = Object::new(shape.id);
let shape_instance = ShapeInstance::new(shape.id);
// Add the boundary shape
if let Some(AnyLayer::Vector(layer)) = document.get_layer_mut(&layer_id) {
layer.add_shape_internal(shape);
layer.add_object_internal(object);
layer.add_object_internal(shape_instance);
}
// Create and execute paint bucket action
@@ -336,7 +336,7 @@ mod tests {
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Should have original shape + filled shape
assert!(layer.shapes.len() >= 1);
assert!(layer.objects.len() >= 1);
assert!(layer.shape_instances.len() >= 1);
} else {
panic!("Layer not found or not a vector layer");
}
@@ -347,7 +347,7 @@ mod tests {
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Should only have original shape
assert_eq!(layer.shapes.len(), 1);
assert_eq!(layer.objects.len(), 1);
assert_eq!(layer.shape_instances.len(), 1);
}
}
@@ -0,0 +1,80 @@
//! Transform clip instances action
//!
//! Handles spatial transformation (move, scale, rotate) of clip instances on the stage.
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::object::Transform;
use std::collections::HashMap;
use uuid::Uuid;
/// Action that transforms clip instances spatially on the stage
pub struct TransformClipInstancesAction {
layer_id: Uuid,
/// Map of clip instance ID to (old transform, new transform)
clip_instance_transforms: HashMap<Uuid, (Transform, Transform)>,
}
impl TransformClipInstancesAction {
pub fn new(
layer_id: Uuid,
clip_instance_transforms: HashMap<Uuid, (Transform, Transform)>,
) -> Self {
Self {
layer_id,
clip_instance_transforms,
}
}
}
impl Action for TransformClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Apply new transforms
for (clip_id, (_old, new)) in &self.clip_instance_transforms {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id) {
clip_instance.transform = new.clone();
}
}
}
fn rollback(&mut self, document: &mut Document) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Restore old transforms
for (clip_id, (old, _new)) in &self.clip_instance_transforms {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id) {
clip_instance.transform = old.clone();
}
}
}
fn description(&self) -> String {
format!(
"Transform {} clip instance(s)",
self.clip_instance_transforms.len()
)
}
}
@@ -1,6 +1,6 @@
//! Transform objects action
//! Transform shape instances action
//!
//! Applies scale, rotation, and other transformations to objects with undo/redo support.
//! Applies scale, rotation, and other transformations to shape instances with undo/redo support.
use crate::action::Action;
use crate::document::Document;
@@ -9,32 +9,32 @@ use crate::object::Transform;
use std::collections::HashMap;
use uuid::Uuid;
/// Action to transform multiple objects
pub struct TransformObjectsAction {
/// Action to transform multiple shape instances
pub struct TransformShapeInstancesAction {
layer_id: Uuid,
/// Map of object ID to (old transform, new transform)
object_transforms: HashMap<Uuid, (Transform, Transform)>,
/// Map of shape instance ID to (old transform, new transform)
shape_instance_transforms: HashMap<Uuid, (Transform, Transform)>,
}
impl TransformObjectsAction {
impl TransformShapeInstancesAction {
/// Create a new transform action
pub fn new(
layer_id: Uuid,
object_transforms: HashMap<Uuid, (Transform, Transform)>,
shape_instance_transforms: HashMap<Uuid, (Transform, Transform)>,
) -> Self {
Self {
layer_id,
object_transforms,
shape_instance_transforms,
}
}
}
impl Action for TransformObjectsAction {
impl Action for TransformShapeInstancesAction {
fn execute(&mut self, document: &mut Document) {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (object_id, (_old, new)) in &self.object_transforms {
vector_layer.modify_object_internal(object_id, |obj| {
for (shape_instance_id, (_old, new)) in &self.shape_instance_transforms {
vector_layer.modify_object_internal(shape_instance_id, |obj| {
obj.transform = new.clone();
});
}
@@ -45,8 +45,8 @@ impl Action for TransformObjectsAction {
fn rollback(&mut self, document: &mut Document) {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (object_id, (old, _new)) in &self.object_transforms {
vector_layer.modify_object_internal(object_id, |obj| {
for (shape_instance_id, (old, _new)) in &self.shape_instance_transforms {
vector_layer.modify_object_internal(shape_instance_id, |obj| {
obj.transform = old.clone();
});
}
@@ -55,6 +55,6 @@ impl Action for TransformObjectsAction {
}
fn description(&self) -> String {
format!("Transform {} object(s)", self.object_transforms.len())
format!("Transform {} shape instance(s)", self.shape_instance_transforms.len())
}
}
@@ -0,0 +1,271 @@
//! Trim clip instances action
//!
//! Handles trimming one or more clip instances by adjusting trim_start and/or trim_end.
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use std::collections::HashMap;
use uuid::Uuid;
/// Type of trim operation
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TrimType {
/// Trim from the start (adjust trim_start and timeline_start)
TrimLeft,
/// Trim from the end (adjust trim_end)
TrimRight,
}
/// Action that trims clip instances
pub struct TrimClipInstancesAction {
/// Map of layer IDs to vectors of (clip_instance_id, trim_type, old_values, new_values)
/// For TrimLeft: (old_trim_start, old_timeline_start, new_trim_start, new_timeline_start)
/// For TrimRight: (old_trim_end, new_trim_end) - stored as Option<f64>
layer_trims: HashMap<Uuid, Vec<(Uuid, TrimType, TrimData, TrimData)>>,
}
/// Trim data that can represent either left or right trim values
#[derive(Debug, Clone)]
pub struct TrimData {
/// For TrimLeft: trim_start value
/// For TrimRight: trim_end value (Option because it can be None)
pub trim_value: Option<f64>,
/// For TrimLeft: timeline_start value (where the clip appears on timeline)
/// For TrimRight: unused (None)
pub timeline_start: Option<f64>,
}
impl TrimData {
/// Create TrimData for left trim
pub fn left(trim_start: f64, timeline_start: f64) -> Self {
Self {
trim_value: Some(trim_start),
timeline_start: Some(timeline_start),
}
}
/// Create TrimData for right trim
pub fn right(trim_end: Option<f64>) -> Self {
Self {
trim_value: trim_end,
timeline_start: None,
}
}
}
impl TrimClipInstancesAction {
/// Create a new trim clip instances action
pub fn new(layer_trims: HashMap<Uuid, Vec<(Uuid, TrimType, TrimData, TrimData)>>) -> Self {
Self { layer_trims }
}
}
impl Action for TrimClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
for (layer_id, trims) in &self.layer_trims {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Apply trims
for (clip_id, trim_type, _old, new) in trims {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id)
{
match trim_type {
TrimType::TrimLeft => {
if let (Some(new_trim), Some(new_timeline)) =
(new.trim_value, new.timeline_start)
{
clip_instance.trim_start = new_trim;
clip_instance.timeline_start = new_timeline;
}
}
TrimType::TrimRight => {
clip_instance.trim_end = new.trim_value;
}
}
}
}
}
}
fn rollback(&mut self, document: &mut Document) {
for (layer_id, trims) in &self.layer_trims {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
AnyLayer::Vector(vl) => &mut vl.clip_instances,
AnyLayer::Audio(al) => &mut al.clip_instances,
AnyLayer::Video(vl) => &mut vl.clip_instances,
};
// Restore original trim values
for (clip_id, trim_type, old, _new) in trims {
if let Some(clip_instance) = clip_instances.iter_mut().find(|ci| ci.id == *clip_id)
{
match trim_type {
TrimType::TrimLeft => {
if let (Some(old_trim), Some(old_timeline)) =
(old.trim_value, old.timeline_start)
{
clip_instance.trim_start = old_trim;
clip_instance.timeline_start = old_timeline;
}
}
TrimType::TrimRight => {
clip_instance.trim_end = old.trim_value;
}
}
}
}
}
}
fn description(&self) -> String {
let total_count: usize = self.layer_trims.values().map(|v| v.len()).sum();
if total_count == 1 {
"Trim clip instance".to_string()
} else {
format!("Trim {} clip instances", total_count)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::clip::{Clip, ClipInstance, ClipType};
use crate::layer::VectorLayer;
#[test]
fn test_trim_left_action() {
let mut document = Document::new("Test");
let clip = Clip::new(ClipType::Vector, "Test Clip", Some(10.0));
let clip_id = clip.id;
let mut vector_layer = VectorLayer::new("Layer 1");
vector_layer.clips.push(clip);
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.timeline_start = 0.0;
clip_instance.trim_start = 0.0;
let instance_id = clip_instance.id;
vector_layer.clip_instances.push(clip_instance);
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
// Create trim action: trim 2 seconds from left
let mut layer_trims = HashMap::new();
layer_trims.insert(
layer_id,
vec![(
instance_id,
TrimType::TrimLeft,
TrimData::left(0.0, 0.0),
TrimData::left(2.0, 2.0),
)],
);
let mut action = TrimClipInstancesAction::new(layer_trims);
// Execute
action.execute(&mut document);
// Verify trim applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_start, 2.0);
assert_eq!(instance.timeline_start, 2.0);
}
// Rollback
action.rollback(&mut document);
// Verify restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_start, 0.0);
assert_eq!(instance.timeline_start, 0.0);
}
}
#[test]
fn test_trim_right_action() {
let mut document = Document::new("Test");
let clip = Clip::new(ClipType::Vector, "Test Clip", Some(10.0));
let clip_id = clip.id;
let mut vector_layer = VectorLayer::new("Layer 1");
vector_layer.clips.push(clip);
let mut clip_instance = ClipInstance::new(clip_id);
clip_instance.trim_end = None; // Full duration
let instance_id = clip_instance.id;
vector_layer.clip_instances.push(clip_instance);
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
// Create trim action: trim to 8 seconds from right
let mut layer_trims = HashMap::new();
layer_trims.insert(
layer_id,
vec![(
instance_id,
TrimType::TrimRight,
TrimData::right(None),
TrimData::right(Some(8.0)),
)],
);
let mut action = TrimClipInstancesAction::new(layer_trims);
// Execute
action.execute(&mut document);
// Verify trim applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_end, Some(8.0));
}
// Rollback
action.rollback(&mut document);
// Verify restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let instance = layer
.clip_instances
.iter()
.find(|ci| ci.id == instance_id)
.unwrap();
assert_eq!(instance.trim_end, None);
}
}
}