Change vector drawing primitive from shape to doubly-connected edge graph

This commit is contained in:
Skyler Lehmkuhl
2026-02-23 21:29:58 -05:00
parent eab116c930
commit 99f8dcfcf4
31 changed files with 2664 additions and 3774 deletions
@@ -1,111 +1,124 @@
//! Add shape action
//! Add shape action — inserts strokes into the DCEL.
//!
//! Handles adding a new shape to a vector layer's keyframe.
//! Converts a BezPath into cubic segments and inserts them via
//! `Dcel::insert_stroke()`. Undo is handled by snapshotting the DCEL.
use crate::action::Action;
use crate::dcel::{bezpath_to_cubic_segments, Dcel, DEFAULT_SNAP_EPSILON};
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::Shape;
use crate::shape::{ShapeColor, StrokeStyle};
use kurbo::BezPath;
use uuid::Uuid;
/// Action that adds a shape to a vector layer's keyframe
/// Action that inserts a drawn path into a vector layer's DCEL keyframe.
pub struct AddShapeAction {
/// Layer ID to add the shape to
layer_id: Uuid,
/// The shape to add (contains geometry, styling, transform, opacity)
shape: Shape,
/// Time of the keyframe to add to
time: f64,
/// ID of the created shape (set after execution)
created_shape_id: Option<Uuid>,
path: BezPath,
stroke_style: Option<StrokeStyle>,
stroke_color: Option<ShapeColor>,
fill_color: Option<ShapeColor>,
is_closed: bool,
description_text: String,
/// Snapshot of the DCEL before insertion (for undo).
dcel_before: Option<Dcel>,
}
impl AddShapeAction {
pub fn new(layer_id: Uuid, shape: Shape, time: f64) -> Self {
pub fn new(
layer_id: Uuid,
time: f64,
path: BezPath,
stroke_style: Option<StrokeStyle>,
stroke_color: Option<ShapeColor>,
fill_color: Option<ShapeColor>,
is_closed: bool,
) -> Self {
Self {
layer_id,
shape,
time,
created_shape_id: None,
path,
stroke_style,
stroke_color,
fill_color,
is_closed,
description_text: "Add shape".to_string(),
dcel_before: None,
}
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.description_text = desc.into();
self
}
}
impl Action for AddShapeAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return Ok(()),
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
if let AnyLayer::Vector(vector_layer) = layer {
let shape_id = self.shape.id;
vector_layer.add_shape_to_keyframe(self.shape.clone(), self.time);
self.created_shape_id = Some(shape_id);
let keyframe = vl.ensure_keyframe_at(self.time);
let dcel = &mut keyframe.dcel;
// Snapshot for undo
self.dcel_before = Some(dcel.clone());
let subpaths = bezpath_to_cubic_segments(&self.path);
for segments in &subpaths {
if segments.is_empty() {
continue;
}
let result = dcel.insert_stroke(
segments,
self.stroke_style.clone(),
self.stroke_color.clone(),
DEFAULT_SNAP_EPSILON,
);
// Apply fill to new faces if this is a closed shape with fill
if self.is_closed {
if let Some(ref fill) = self.fill_color {
for face_id in &result.new_faces {
dcel.face_mut(*face_id).fill_color = Some(fill.clone());
}
}
}
}
dcel.rebuild_spatial_index();
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(shape_id) = self.created_shape_id {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return Ok(()),
};
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
if let AnyLayer::Vector(vector_layer) = layer {
vector_layer.remove_shape_from_keyframe(&shape_id, self.time);
}
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
let keyframe = vl.ensure_keyframe_at(self.time);
keyframe.dcel = self
.dcel_before
.take()
.ok_or_else(|| "No DCEL snapshot for undo".to_string())?;
self.created_shape_id = None;
}
Ok(())
}
fn description(&self) -> String {
"Add shape".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::ShapeColor;
use vello::kurbo::{Rect, Shape as KurboShape};
#[test]
fn test_add_shape_action_rectangle() {
let mut document = Document::new("Test");
let vector_layer = VectorLayer::new("Layer 1");
let layer_id = document.root.add_child(AnyLayer::Vector(vector_layer));
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))
.with_position(50.0, 50.0);
let mut action = AddShapeAction::new(layer_id, shape, 0.0);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
let shapes = layer.shapes_at_time(0.0);
assert_eq!(shapes.len(), 1);
assert_eq!(shapes[0].transform.x, 50.0);
assert_eq!(shapes[0].transform.y, 50.0);
} else {
panic!("Layer not found or not a vector layer");
}
// Rollback
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(layer.shapes_at_time(0.0).len(), 0);
}
self.description_text.clone()
}
}
@@ -1,18 +1,13 @@
//! Convert to Movie Clip action
//!
//! Wraps selected shapes and/or clip instances into a new VectorClip
//! with is_group = false, giving it a real internal timeline.
//! Works with 1+ selected items (unlike Group which requires 2+).
//! Convert to Movie Clip action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::animation::{AnimationCurve, AnimationTarget, Keyframe, TransformProperty};
use crate::clip::{ClipInstance, VectorClip};
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::{AnyLayer, VectorLayer};
use crate::shape::Shape;
use uuid::Uuid;
use vello::kurbo::{Rect, Shape as KurboShape};
/// Action that converts selected items to a Movie Clip
/// TODO: Rewrite for DCEL
#[allow(dead_code)]
pub struct ConvertToMovieClipAction {
layer_id: Uuid,
time: f64,
@@ -20,7 +15,6 @@ pub struct ConvertToMovieClipAction {
clip_instance_ids: Vec<Uuid>,
instance_id: Uuid,
created_clip_id: Option<Uuid>,
removed_shapes: Vec<Shape>,
removed_clip_instances: Vec<ClipInstance>,
}
@@ -39,201 +33,18 @@ impl ConvertToMovieClipAction {
clip_instance_ids,
instance_id,
created_clip_id: None,
removed_shapes: Vec::new(),
removed_clip_instances: Vec::new(),
}
}
}
impl Action for ConvertToMovieClipAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Convert to Movie Clip is only supported on vector layers".to_string()),
};
// Collect shapes
let shapes_at_time = vl.shapes_at_time(self.time);
let mut collected_shapes: Vec<Shape> = Vec::new();
for id in &self.shape_ids {
if let Some(shape) = shapes_at_time.iter().find(|s| &s.id == id) {
collected_shapes.push(shape.clone());
}
}
// Collect clip instances
let mut collected_clip_instances: Vec<ClipInstance> = Vec::new();
for id in &self.clip_instance_ids {
if let Some(ci) = vl.clip_instances.iter().find(|ci| &ci.id == id) {
collected_clip_instances.push(ci.clone());
}
}
let total_items = collected_shapes.len() + collected_clip_instances.len();
if total_items < 1 {
return Err("Need at least 1 item to convert to movie clip".to_string());
}
// Compute combined bounding box
let mut combined_bbox: Option<Rect> = None;
for shape in &collected_shapes {
let local_bbox = shape.path().bounding_box();
let transform = shape.transform.to_affine();
let transformed_bbox = transform.transform_rect_bbox(local_bbox);
combined_bbox = Some(match combined_bbox {
Some(existing) => existing.union(transformed_bbox),
None => transformed_bbox,
});
}
for ci in &collected_clip_instances {
let content_bounds = if let Some(vector_clip) = document.get_vector_clip(&ci.clip_id) {
let clip_time = ((self.time - ci.timeline_start) * ci.playback_speed) + ci.trim_start;
vector_clip.calculate_content_bounds(document, clip_time)
} else if let Some(video_clip) = document.get_video_clip(&ci.clip_id) {
Rect::new(0.0, 0.0, video_clip.width, video_clip.height)
} else {
continue;
};
let ci_transform = ci.transform.to_affine();
let transformed_bbox = ci_transform.transform_rect_bbox(content_bounds);
combined_bbox = Some(match combined_bbox {
Some(existing) => existing.union(transformed_bbox),
None => transformed_bbox,
});
}
let bbox = combined_bbox.ok_or("Could not compute bounding box")?;
let center_x = (bbox.x0 + bbox.x1) / 2.0;
let center_y = (bbox.y0 + bbox.y1) / 2.0;
// Offset shapes relative to center
let mut clip_shapes: Vec<Shape> = collected_shapes.clone();
for shape in &mut clip_shapes {
shape.transform.x -= center_x;
shape.transform.y -= center_y;
}
let mut clip_instances_inside: Vec<ClipInstance> = collected_clip_instances.clone();
for ci in &mut clip_instances_inside {
ci.transform.x -= center_x;
ci.transform.y -= center_y;
}
// Create VectorClip with real timeline duration
let mut clip = VectorClip::new("Movie Clip", bbox.width(), bbox.height(), document.duration);
// is_group defaults to false — movie clips have real timelines
let clip_id = clip.id;
let mut inner_layer = VectorLayer::new("Layer 1");
for shape in clip_shapes {
inner_layer.add_shape_to_keyframe(shape, 0.0);
}
for ci in clip_instances_inside {
inner_layer.clip_instances.push(ci);
}
clip.layers.add_root(AnyLayer::Vector(inner_layer));
document.add_vector_clip(clip);
self.created_clip_id = Some(clip_id);
// Remove originals from the layer
let layer = document.get_layer_mut(&self.layer_id).unwrap();
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => unreachable!(),
};
self.removed_shapes.clear();
for id in &self.shape_ids {
if let Some(shape) = vl.remove_shape_from_keyframe(id, self.time) {
self.removed_shapes.push(shape);
}
}
self.removed_clip_instances.clear();
for id in &self.clip_instance_ids {
if let Some(pos) = vl.clip_instances.iter().position(|ci| &ci.id == id) {
self.removed_clip_instances.push(vl.clip_instances.remove(pos));
}
}
// Place the new ClipInstance
let instance = ClipInstance::with_id(self.instance_id, clip_id)
.with_position(center_x, center_y)
.with_name("Movie Clip");
vl.clip_instances.push(instance);
// Create default animation curves
let props_and_values = [
(TransformProperty::X, center_x),
(TransformProperty::Y, center_y),
(TransformProperty::Rotation, 0.0),
(TransformProperty::ScaleX, 1.0),
(TransformProperty::ScaleY, 1.0),
(TransformProperty::SkewX, 0.0),
(TransformProperty::SkewY, 0.0),
(TransformProperty::Opacity, 1.0),
];
for (prop, value) in props_and_values {
let target = AnimationTarget::Object {
id: self.instance_id,
property: prop,
};
let mut curve = AnimationCurve::new(target.clone(), value);
curve.set_keyframe(Keyframe::linear(0.0, value));
vl.layer.animation_data.set_curve(curve);
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, &self.shape_ids, &self.clip_instance_ids, self.instance_id);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
if let AnyLayer::Vector(vl) = layer {
// Remove animation curves
for prop in &[
TransformProperty::X, TransformProperty::Y,
TransformProperty::Rotation,
TransformProperty::ScaleX, TransformProperty::ScaleY,
TransformProperty::SkewX, TransformProperty::SkewY,
TransformProperty::Opacity,
] {
let target = AnimationTarget::Object {
id: self.instance_id,
property: *prop,
};
vl.layer.animation_data.remove_curve(&target);
}
// Remove the clip instance
vl.clip_instances.retain(|ci| ci.id != self.instance_id);
// Re-insert removed shapes
for shape in self.removed_shapes.drain(..) {
vl.add_shape_to_keyframe(shape, self.time);
}
// Re-insert removed clip instances
for ci in self.removed_clip_instances.drain(..) {
vl.clip_instances.push(ci);
}
}
// Remove the VectorClip from the document
if let Some(clip_id) = self.created_clip_id.take() {
document.remove_vector_clip(&clip_id);
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -1,42 +1,20 @@
//! Group action
//!
//! Groups selected shapes and/or clip instances into a new VectorClip
//! with a ClipInstance placed on the layer. Supports grouping shapes,
//! existing clip instances (groups), or a mix of both.
//! Group action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::animation::{AnimationCurve, AnimationTarget, Keyframe, TransformProperty};
use crate::clip::{ClipInstance, VectorClip};
use crate::clip::ClipInstance;
use crate::document::Document;
use crate::layer::{AnyLayer, VectorLayer};
use crate::shape::Shape;
use uuid::Uuid;
use vello::kurbo::{Rect, Shape as KurboShape};
/// Action that groups selected shapes and/or clip instances into a VectorClip
/// TODO: Rewrite for DCEL (group DCEL faces/edges into a sub-clip)
#[allow(dead_code)]
pub struct GroupAction {
/// Layer containing the items to group
layer_id: Uuid,
/// Time of the keyframe to operate on (for shape lookup)
time: f64,
/// Shape IDs to include in the group
shape_ids: Vec<Uuid>,
/// Clip instance IDs to include in the group
clip_instance_ids: Vec<Uuid>,
/// Pre-generated clip instance ID for the new group (so caller can update selection)
instance_id: Uuid,
/// Created clip ID (for rollback)
created_clip_id: Option<Uuid>,
/// Shapes removed from the keyframe (for rollback)
removed_shapes: Vec<Shape>,
/// Clip instances removed from the layer (for rollback, preserving original order)
removed_clip_instances: Vec<ClipInstance>,
}
@@ -55,227 +33,19 @@ impl GroupAction {
clip_instance_ids,
instance_id,
created_clip_id: None,
removed_shapes: Vec::new(),
removed_clip_instances: Vec::new(),
}
}
}
impl Action for GroupAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// --- Phase 1: Collect items and compute bounding box ---
let layer = document
.get_layer(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Group is only supported on vector layers".to_string()),
};
// Collect shapes
let shapes_at_time = vl.shapes_at_time(self.time);
let mut group_shapes: Vec<Shape> = Vec::new();
for id in &self.shape_ids {
if let Some(shape) = shapes_at_time.iter().find(|s| &s.id == id) {
group_shapes.push(shape.clone());
}
}
// Collect clip instances
let mut group_clip_instances: Vec<ClipInstance> = Vec::new();
for id in &self.clip_instance_ids {
if let Some(ci) = vl.clip_instances.iter().find(|ci| &ci.id == id) {
group_clip_instances.push(ci.clone());
}
}
let total_items = group_shapes.len() + group_clip_instances.len();
if total_items < 2 {
return Err("Need at least 2 items to group".to_string());
}
// Compute combined bounding box in parent (layer) space
let mut combined_bbox: Option<Rect> = None;
// Shape bounding boxes
for shape in &group_shapes {
let local_bbox = shape.path().bounding_box();
let transform = shape.transform.to_affine();
let transformed_bbox = transform.transform_rect_bbox(local_bbox);
combined_bbox = Some(match combined_bbox {
Some(existing) => existing.union(transformed_bbox),
None => transformed_bbox,
});
}
// Clip instance bounding boxes
for ci in &group_clip_instances {
let content_bounds = if let Some(vector_clip) = document.get_vector_clip(&ci.clip_id) {
let clip_time = ((self.time - ci.timeline_start) * ci.playback_speed) + ci.trim_start;
vector_clip.calculate_content_bounds(document, clip_time)
} else if let Some(video_clip) = document.get_video_clip(&ci.clip_id) {
Rect::new(0.0, 0.0, video_clip.width, video_clip.height)
} else {
continue;
};
let ci_transform = ci.transform.to_affine();
let transformed_bbox = ci_transform.transform_rect_bbox(content_bounds);
combined_bbox = Some(match combined_bbox {
Some(existing) => existing.union(transformed_bbox),
None => transformed_bbox,
});
}
let bbox = combined_bbox.ok_or("Could not compute bounding box")?;
let center_x = (bbox.x0 + bbox.x1) / 2.0;
let center_y = (bbox.y0 + bbox.y1) / 2.0;
// --- Phase 2: Build the VectorClip ---
// Offset shapes so positions are relative to the group center
let mut clip_shapes: Vec<Shape> = group_shapes.clone();
for shape in &mut clip_shapes {
shape.transform.x -= center_x;
shape.transform.y -= center_y;
}
// Offset clip instances similarly
let mut clip_instances_inside: Vec<ClipInstance> = group_clip_instances.clone();
for ci in &mut clip_instances_inside {
ci.transform.x -= center_x;
ci.transform.y -= center_y;
}
// Create VectorClip — groups are static (one frame), not time-based clips
let frame_duration = 1.0 / document.framerate;
let mut clip = VectorClip::new("Group", bbox.width(), bbox.height(), frame_duration);
clip.is_group = true;
let clip_id = clip.id;
let mut inner_layer = VectorLayer::new("Layer 1");
for shape in clip_shapes {
inner_layer.add_shape_to_keyframe(shape, 0.0);
}
for ci in clip_instances_inside {
inner_layer.clip_instances.push(ci);
}
clip.layers.add_root(AnyLayer::Vector(inner_layer));
// Add clip to document library
document.add_vector_clip(clip);
self.created_clip_id = Some(clip_id);
// --- Phase 3: Remove originals from the layer ---
let layer = document.get_layer_mut(&self.layer_id).unwrap();
let vl = match layer {
AnyLayer::Vector(vl) => vl,
_ => unreachable!(),
};
// Remove shapes
self.removed_shapes.clear();
for id in &self.shape_ids {
if let Some(shape) = vl.remove_shape_from_keyframe(id, self.time) {
self.removed_shapes.push(shape);
}
}
// Remove clip instances (preserve order for rollback)
self.removed_clip_instances.clear();
for id in &self.clip_instance_ids {
if let Some(pos) = vl.clip_instances.iter().position(|ci| &ci.id == id) {
self.removed_clip_instances.push(vl.clip_instances.remove(pos));
}
}
// --- Phase 4: Place the new group ClipInstance ---
let instance = ClipInstance::with_id(self.instance_id, clip_id)
.with_position(center_x, center_y)
.with_name("Group");
vl.clip_instances.push(instance);
// Register the group in the current keyframe's clip_instance_ids
if let Some(kf) = vl.keyframe_at_mut(self.time) {
if !kf.clip_instance_ids.contains(&self.instance_id) {
kf.clip_instance_ids.push(self.instance_id);
}
}
// --- Phase 5: Create default animation curves with initial keyframe ---
let props_and_values = [
(TransformProperty::X, center_x),
(TransformProperty::Y, center_y),
(TransformProperty::Rotation, 0.0),
(TransformProperty::ScaleX, 1.0),
(TransformProperty::ScaleY, 1.0),
(TransformProperty::SkewX, 0.0),
(TransformProperty::SkewY, 0.0),
(TransformProperty::Opacity, 1.0),
];
for (prop, value) in props_and_values {
let target = AnimationTarget::Object {
id: self.instance_id,
property: prop,
};
let mut curve = AnimationCurve::new(target.clone(), value);
curve.set_keyframe(Keyframe::linear(0.0, value));
vl.layer.animation_data.set_curve(curve);
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, &self.shape_ids, &self.clip_instance_ids, self.instance_id);
// TODO: Implement DCEL-aware grouping
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
if let AnyLayer::Vector(vl) = layer {
// Remove animation curves for the group's clip instance
for prop in &[
TransformProperty::X, TransformProperty::Y,
TransformProperty::Rotation,
TransformProperty::ScaleX, TransformProperty::ScaleY,
TransformProperty::SkewX, TransformProperty::SkewY,
TransformProperty::Opacity,
] {
let target = AnimationTarget::Object {
id: self.instance_id,
property: *prop,
};
vl.layer.animation_data.remove_curve(&target);
}
// Remove the group's clip instance
vl.clip_instances.retain(|ci| ci.id != self.instance_id);
// Remove the group ID from the keyframe
if let Some(kf) = vl.keyframe_at_mut(self.time) {
kf.clip_instance_ids.retain(|id| id != &self.instance_id);
}
// Re-insert removed shapes
for shape in self.removed_shapes.drain(..) {
vl.add_shape_to_keyframe(shape, self.time);
}
// Re-insert removed clip instances
for ci in self.removed_clip_instances.drain(..) {
vl.clip_instances.push(ci);
}
}
// Remove the VectorClip from the document
if let Some(clip_id) = self.created_clip_id.take() {
document.remove_vector_clip(&clip_id);
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -284,129 +54,3 @@ impl Action for GroupAction {
format!("Group {} objects", count)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::shape::ShapeColor;
use vello::kurbo::{Circle, Shape as KurboShape};
#[test]
fn test_group_shapes() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let circle1 = Circle::new((0.0, 0.0), 20.0);
let shape1 = Shape::new(circle1.to_path(0.1))
.with_fill(ShapeColor::rgb(255, 0, 0))
.with_position(50.0, 50.0);
let shape1_id = shape1.id;
let circle2 = Circle::new((0.0, 0.0), 20.0);
let shape2 = Shape::new(circle2.to_path(0.1))
.with_fill(ShapeColor::rgb(0, 255, 0))
.with_position(150.0, 50.0);
let shape2_id = shape2.id;
layer.add_shape_to_keyframe(shape1, 0.0);
layer.add_shape_to_keyframe(shape2, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let instance_id = Uuid::new_v4();
let mut action = GroupAction::new(
layer_id, 0.0,
vec![shape1_id, shape2_id],
vec![],
instance_id,
);
action.execute(&mut document).unwrap();
// Shapes removed, clip instance added
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.shapes_at_time(0.0).len(), 0);
assert_eq!(vl.clip_instances.len(), 1);
assert_eq!(vl.clip_instances[0].id, instance_id);
}
assert_eq!(document.vector_clips.len(), 1);
// Rollback
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.shapes_at_time(0.0).len(), 2);
assert_eq!(vl.clip_instances.len(), 0);
}
assert!(document.vector_clips.is_empty());
}
#[test]
fn test_group_mixed_shapes_and_clips() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
// Add a shape
let circle = Circle::new((0.0, 0.0), 20.0);
let shape = Shape::new(circle.to_path(0.1))
.with_fill(ShapeColor::rgb(255, 0, 0))
.with_position(50.0, 50.0);
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
// Add a clip instance (create a clip for it first)
let mut inner_clip = VectorClip::new("Inner", 40.0, 40.0, 1.0);
let inner_clip_id = inner_clip.id;
let mut inner_layer = VectorLayer::new("Inner Layer");
let inner_shape = Shape::new(Circle::new((20.0, 20.0), 15.0).to_path(0.1))
.with_fill(ShapeColor::rgb(0, 0, 255));
inner_layer.add_shape_to_keyframe(inner_shape, 0.0);
inner_clip.layers.add_root(AnyLayer::Vector(inner_layer));
document.add_vector_clip(inner_clip);
let ci = ClipInstance::new(inner_clip_id).with_position(150.0, 50.0);
let ci_id = ci.id;
layer.clip_instances.push(ci);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let instance_id = Uuid::new_v4();
let mut action = GroupAction::new(
layer_id, 0.0,
vec![shape_id],
vec![ci_id],
instance_id,
);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.shapes_at_time(0.0).len(), 0);
// Only the new group instance remains (the inner clip instance was grouped)
assert_eq!(vl.clip_instances.len(), 1);
assert_eq!(vl.clip_instances[0].id, instance_id);
}
// Two vector clips: the inner one + the new group
assert_eq!(document.vector_clips.len(), 2);
// Rollback
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.shapes_at_time(0.0).len(), 1);
assert_eq!(vl.clip_instances.len(), 1);
assert_eq!(vl.clip_instances[0].id, ci_id);
}
// Only the inner clip remains
assert_eq!(document.vector_clips.len(), 1);
}
#[test]
fn test_group_description() {
let action = GroupAction::new(
Uuid::new_v4(), 0.0,
vec![Uuid::new_v4(), Uuid::new_v4()],
vec![Uuid::new_v4()],
Uuid::new_v4(),
);
assert_eq!(action.description(), "Group 3 objects");
}
}
@@ -37,7 +37,7 @@ pub use add_clip_instance::AddClipInstanceAction;
pub use add_effect::AddEffectAction;
pub use add_layer::AddLayerAction;
pub use add_shape::AddShapeAction;
pub use modify_shape_path::ModifyShapePathAction;
pub use modify_shape_path::ModifyDcelAction;
pub use move_clip_instances::MoveClipInstancesAction;
pub use move_objects::MoveShapeInstancesAction;
pub use paint_bucket::PaintBucketAction;
@@ -1,223 +1,83 @@
//! Modify shape path action
//!
//! Handles modifying a shape's bezier path (for vector editing operations)
//! with undo/redo support.
//! Modify DCEL action — snapshot-based undo for DCEL editing
use crate::action::Action;
use crate::dcel::Dcel;
use crate::document::Document;
use crate::layer::AnyLayer;
use uuid::Uuid;
use vello::kurbo::BezPath;
/// Action that modifies a shape's path
/// Action that captures a before/after DCEL snapshot for undo/redo.
///
/// This action is used for vector editing operations like dragging vertices,
/// reshaping curves, or manipulating control points.
pub struct ModifyShapePathAction {
/// Layer containing the shape
/// Used by vertex editing, curve editing, and control point editing.
/// The caller provides both snapshots (taken before and after the edit).
pub struct ModifyDcelAction {
layer_id: Uuid,
/// Shape to modify
shape_id: Uuid,
/// Time of the keyframe containing the shape
time: f64,
/// The version index being modified (for shapes with multiple versions)
version_index: usize,
/// New path
new_path: BezPath,
/// Old path (stored after first execution for undo)
old_path: Option<BezPath>,
dcel_before: Option<Dcel>,
dcel_after: Option<Dcel>,
description_text: String,
}
impl ModifyShapePathAction {
/// Create a new action to modify a shape's path
pub fn new(layer_id: Uuid, shape_id: Uuid, time: f64, version_index: usize, new_path: BezPath) -> Self {
Self {
layer_id,
shape_id,
time,
version_index,
new_path,
old_path: None,
}
}
/// Create action with old path already known (for optimization)
pub fn with_old_path(
impl ModifyDcelAction {
pub fn new(
layer_id: Uuid,
shape_id: Uuid,
time: f64,
version_index: usize,
old_path: BezPath,
new_path: BezPath,
dcel_before: Dcel,
dcel_after: Dcel,
description: impl Into<String>,
) -> Self {
Self {
layer_id,
shape_id,
time,
version_index,
new_path,
old_path: Some(old_path),
dcel_before: Some(dcel_before),
dcel_after: Some(dcel_after),
description_text: description.into(),
}
}
}
impl Action for ModifyShapePathAction {
impl Action for ModifyDcelAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(&self.shape_id, self.time) {
if self.version_index >= shape.versions.len() {
return Err(format!(
"Version index {} out of bounds (shape has {} versions)",
self.version_index,
shape.versions.len()
));
}
let dcel_after = self.dcel_after.as_ref()
.ok_or("ModifyDcelAction: no dcel_after snapshot")?
.clone();
// Store old path if not already stored
if self.old_path.is_none() {
self.old_path = Some(shape.versions[self.version_index].path.clone());
}
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
// Apply new path
shape.versions[self.version_index].path = self.new_path.clone();
return Ok(());
}
if let AnyLayer::Vector(vl) = layer {
if let Some(kf) = vl.keyframe_at_mut(self.time) {
kf.dcel = dcel_after;
Ok(())
} else {
Err(format!("No keyframe at time {}", self.time))
}
} else {
Err("Not a vector layer".to_string())
}
Err(format!(
"Could not find shape {} in layer {}",
self.shape_id, self.layer_id
))
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(old_path) = &self.old_path {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(&self.shape_id, self.time) {
if self.version_index < shape.versions.len() {
shape.versions[self.version_index].path = old_path.clone();
return Ok(());
}
}
}
}
}
let dcel_before = self.dcel_before.as_ref()
.ok_or("ModifyDcelAction: no dcel_before snapshot")?
.clone();
Err(format!(
"Could not rollback shape path modification for shape {} in layer {}",
self.shape_id, self.layer_id
))
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
if let AnyLayer::Vector(vl) = layer {
if let Some(kf) = vl.keyframe_at_mut(self.time) {
kf.dcel = dcel_before;
Ok(())
} else {
Err(format!("No keyframe at time {}", self.time))
}
} else {
Err("Not a vector layer".to_string())
}
}
fn description(&self) -> String {
"Modify shape path".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::Shape as KurboShape;
fn create_test_path() -> BezPath {
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((100.0, 0.0));
path.line_to((100.0, 100.0));
path.line_to((0.0, 100.0));
path.close_path();
path
}
fn create_modified_path() -> BezPath {
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((150.0, 0.0));
path.line_to((150.0, 150.0));
path.line_to((0.0, 150.0));
path.close_path();
path
}
#[test]
fn test_modify_shape_path() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = Shape::new(create_test_path());
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
// Verify initial path
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
let bbox = shape.versions[0].path.bounding_box();
assert_eq!(bbox.width(), 100.0);
assert_eq!(bbox.height(), 100.0);
}
// Create and execute action
let new_path = create_modified_path();
let mut action = ModifyShapePathAction::new(layer_id, shape_id, 0.0, 0, new_path);
action.execute(&mut document).unwrap();
// Verify path changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
let bbox = shape.versions[0].path.bounding_box();
assert_eq!(bbox.width(), 150.0);
assert_eq!(bbox.height(), 150.0);
}
// Rollback
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
let bbox = shape.versions[0].path.bounding_box();
assert_eq!(bbox.width(), 100.0);
assert_eq!(bbox.height(), 100.0);
}
}
#[test]
fn test_invalid_version_index() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = Shape::new(create_test_path());
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let new_path = create_modified_path();
let mut action = ModifyShapePathAction::new(layer_id, shape_id, 0.0, 5, new_path);
let result = action.execute(&mut document);
assert!(result.is_err());
assert!(result.unwrap_err().contains("out of bounds"));
}
#[test]
fn test_description() {
let layer_id = Uuid::new_v4();
let shape_id = Uuid::new_v4();
let action = ModifyShapePathAction::new(layer_id, shape_id, 0.0, 0, create_test_path());
assert_eq!(action.description(), "Modify shape path");
self.description_text.clone()
}
}
@@ -247,7 +247,7 @@ mod tests {
let folder2_id = folder2_action.created_folder_id().unwrap();
// Create a clip in folder 1
let mut clip = crate::clip::AudioClip::new_sampled("Test Audio", 5.0, 0);
let mut clip = crate::clip::AudioClip::new_sampled("Test Audio", 0, 5.0);
clip.folder_id = Some(folder1_id);
let clip_id = clip.id;
document.audio_clips.insert(clip_id, clip);
@@ -1,19 +1,16 @@
//! Move shapes action
//!
//! Handles moving one or more shapes to new positions within a keyframe.
//! Move shapes action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use std::collections::HashMap;
use uuid::Uuid;
use vello::kurbo::Point;
/// Action that moves shapes to new positions within a keyframe
/// TODO: Replace with DCEL vertex translation
pub struct MoveShapeInstancesAction {
layer_id: Uuid,
time: f64,
/// Map of shape IDs to their old and new positions
shape_positions: HashMap<Uuid, (Point, Point)>,
}
@@ -28,37 +25,12 @@ impl MoveShapeInstancesAction {
}
impl Action for MoveShapeInstancesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, (_old, new)) in &self.shape_positions {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
shape.transform.x = new.x;
shape.transform.y = new.y;
}
}
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, &self.shape_positions);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, (old, _new)) in &self.shape_positions {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
shape.transform.x = old.x;
shape.transform.y = old.y;
}
}
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -1,44 +1,27 @@
//! Paint bucket fill action
//! Paint bucket fill action — STUB: needs DCEL rewrite
//!
//! This action performs a paint bucket fill operation starting from a click point,
//! using planar graph face detection to identify the region to fill.
//! With DCEL, paint bucket simply hit-tests faces and sets fill_color.
use crate::action::Action;
use crate::curve_segment::CurveSegment;
use crate::document::Document;
use crate::gap_handling::GapHandlingMode;
use crate::layer::AnyLayer;
use crate::planar_graph::PlanarGraph;
use crate::shape::ShapeColor;
use uuid::Uuid;
use vello::kurbo::Point;
/// Action that performs a paint bucket fill operation
/// TODO: Rewrite to use DCEL face hit-testing
pub struct PaintBucketAction {
/// Layer ID to add the filled shape to
layer_id: Uuid,
/// Time of the keyframe to operate on
time: f64,
/// Click point where fill was initiated
click_point: Point,
/// Fill color for the shape
fill_color: ShapeColor,
/// Tolerance for gap bridging (in pixels)
_tolerance: f64,
/// Gap handling mode
_gap_mode: GapHandlingMode,
/// ID of the created shape (set after execution)
created_shape_id: Option<Uuid>,
}
impl PaintBucketAction {
/// Create a new paint bucket action
pub fn new(
layer_id: Uuid,
time: f64,
@@ -60,93 +43,14 @@ impl PaintBucketAction {
}
impl Action for PaintBucketAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
println!("=== PaintBucketAction::execute ===");
// Optimization: Check if we're clicking on an existing shape first
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer_mut(&self.layer_id) {
// Iterate through shapes in the keyframe in reverse order (topmost first)
let shapes = vector_layer.shapes_at_time(self.time);
for shape in shapes.iter().rev() {
// Skip shapes without fill color
if shape.fill_color.is_none() {
continue;
}
use vello::kurbo::PathEl;
let is_closed = shape.path().elements().iter().any(|el| matches!(el, PathEl::ClosePath));
if !is_closed {
continue;
}
// Apply the shape's transform
let transform_affine = shape.transform.to_affine();
let inverse_transform = transform_affine.inverse();
let local_point = inverse_transform * self.click_point;
use vello::kurbo::Shape as KurboShape;
let winding = shape.path().winding(local_point);
if winding != 0 {
println!("Clicked on existing shape, changing fill color");
let shape_id = shape.id;
// Now get mutable access to change the fill
if let Some(shape_mut) = vector_layer.get_shape_in_keyframe_mut(&shape_id, self.time) {
shape_mut.fill_color = Some(self.fill_color);
}
return Ok(());
}
}
println!("No existing shape at click point, creating new fill region");
}
// Step 1: Extract curves from all shapes in the keyframe
let all_curves = extract_curves_from_keyframe(document, &self.layer_id, self.time);
println!("Extracted {} curves from all shapes", all_curves.len());
if all_curves.is_empty() {
println!("No curves found, returning");
return Ok(());
}
// Step 2: Build planar graph
println!("Building planar graph...");
let graph = PlanarGraph::build(&all_curves);
// Step 3: Trace the face containing the click point
println!("Tracing face from click point {:?}...", self.click_point);
if let Some(face) = graph.trace_face_from_point(self.click_point) {
println!("Successfully traced face containing click point!");
let face_path = graph.build_face_path(&face);
let face_shape = crate::shape::Shape::new(face_path)
.with_fill(self.fill_color);
self.created_shape_id = Some(face_shape.id);
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer_mut(&self.layer_id) {
vector_layer.add_shape_to_keyframe(face_shape, self.time);
println!("DEBUG: Added filled shape to keyframe");
}
} else {
println!("Click point is not inside any face!");
}
println!("=== Paint Bucket Complete ===");
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, self.click_point, self.fill_color);
// TODO: Hit-test DCEL faces, set face.fill_color
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(shape_id) = self.created_shape_id {
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer_mut(&self.layer_id) {
vector_layer.remove_shape_from_keyframe(&shape_id, self.time);
}
self.created_shape_id = None;
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
self.created_shape_id = None;
Ok(())
}
@@ -154,139 +58,3 @@ impl Action for PaintBucketAction {
"Paint bucket fill".to_string()
}
}
/// Extract curves from all shapes in the keyframe at the given time
fn extract_curves_from_keyframe(
document: &Document,
layer_id: &Uuid,
time: f64,
) -> Vec<CurveSegment> {
let mut all_curves = Vec::new();
let layer = match document.get_layer(layer_id) {
Some(l) => l,
None => return all_curves,
};
if let AnyLayer::Vector(vector_layer) = layer {
let shapes = vector_layer.shapes_at_time(time);
println!("Extracting curves from {} shapes in keyframe", shapes.len());
for (shape_idx, shape) in shapes.iter().enumerate() {
let transform_affine = shape.transform.to_affine();
let path = shape.path();
let mut current_point = Point::ZERO;
let mut subpath_start = Point::ZERO;
let mut segment_index = 0;
let mut curves_in_shape = 0;
for element in path.elements() {
if let Some(mut segment) = CurveSegment::from_path_element(
shape.id.as_u128() as usize,
segment_index,
element,
current_point,
) {
for control_point in &mut segment.control_points {
*control_point = transform_affine * (*control_point);
}
all_curves.push(segment);
segment_index += 1;
curves_in_shape += 1;
}
match element {
vello::kurbo::PathEl::MoveTo(p) => {
current_point = *p;
subpath_start = *p;
}
vello::kurbo::PathEl::LineTo(p) => current_point = *p,
vello::kurbo::PathEl::QuadTo(_, p) => current_point = *p,
vello::kurbo::PathEl::CurveTo(_, _, p) => current_point = *p,
vello::kurbo::PathEl::ClosePath => {
if let Some(mut segment) = CurveSegment::from_path_element(
shape.id.as_u128() as usize,
segment_index,
&vello::kurbo::PathEl::LineTo(subpath_start),
current_point,
) {
for control_point in &mut segment.control_points {
*control_point = transform_affine * (*control_point);
}
all_curves.push(segment);
segment_index += 1;
curves_in_shape += 1;
}
current_point = subpath_start;
}
}
}
println!(" Shape {}: Extracted {} curves", shape_idx, curves_in_shape);
}
}
all_curves
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::{Rect, Shape as KurboShape};
#[test]
fn test_paint_bucket_action_basic() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Layer 1");
// Create a simple rectangle shape (boundary for fill)
let rect = Rect::new(0.0, 0.0, 100.0, 100.0);
let path = rect.to_path(0.1);
let shape = Shape::new(path);
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
// Create and execute paint bucket action
let mut action = PaintBucketAction::new(
layer_id,
0.0,
Point::new(50.0, 50.0),
ShapeColor::rgb(255, 0, 0),
2.0,
GapHandlingMode::BridgeSegment,
);
action.execute(&mut document).unwrap();
// Verify a filled shape was created (or existing shape was recolored)
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert!(layer.shapes_at_time(0.0).len() >= 1);
} else {
panic!("Layer not found or not a vector layer");
}
// Test rollback
action.rollback(&mut document).unwrap();
}
#[test]
fn test_paint_bucket_action_description() {
let action = PaintBucketAction::new(
Uuid::new_v4(),
0.0,
Point::ZERO,
ShapeColor::rgb(0, 0, 255),
2.0,
GapHandlingMode::BridgeSegment,
);
assert_eq!(action.description(), "Paint bucket fill");
}
}
@@ -1,119 +1,42 @@
//! Region split action
//!
//! Commits a temporary region-based shape split permanently.
//! Replaces original shapes with their inside and outside portions.
//! Region split action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::Shape;
use uuid::Uuid;
use vello::kurbo::BezPath;
/// One shape split entry for the action
#[derive(Clone, Debug)]
struct SplitEntry {
/// The original shape (for rollback)
original_shape: Shape,
/// The inside portion shape
inside_shape: Shape,
/// The outside portion shape
outside_shape: Shape,
}
/// Action that commits a region split — replacing original shapes with
/// their inside and outside portions.
/// Action that commits a region split
/// TODO: Rewrite for DCEL edge splitting
pub struct RegionSplitAction {
layer_id: Uuid,
time: f64,
splits: Vec<SplitEntry>,
}
impl RegionSplitAction {
/// Create a new region split action.
///
/// Each tuple is (original_shape, inside_path, inside_id, outside_path, outside_id).
pub fn new(
layer_id: Uuid,
time: f64,
split_data: Vec<(Shape, BezPath, Uuid, BezPath, Uuid)>,
_split_data: Vec<(Shape, BezPath, Uuid, BezPath, Uuid)>,
) -> Self {
let splits = split_data
.into_iter()
.map(|(original, inside_path, inside_id, outside_path, outside_id)| {
let mut inside_shape = original.clone();
inside_shape.id = inside_id;
inside_shape.versions[0].path = inside_path;
let mut outside_shape = original.clone();
outside_shape.id = outside_id;
outside_shape.versions[0].path = outside_path;
SplitEntry {
original_shape: original,
inside_shape,
outside_shape,
}
})
.collect();
Self {
layer_id,
time,
splits,
}
}
}
impl Action for RegionSplitAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for split in &self.splits {
// Remove original
vector_layer.remove_shape_from_keyframe(&split.original_shape.id, self.time);
// Add inside and outside portions
vector_layer.add_shape_to_keyframe(split.inside_shape.clone(), self.time);
vector_layer.add_shape_to_keyframe(split.outside_shape.clone(), self.time);
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for split in &self.splits {
// Remove inside and outside portions
vector_layer.remove_shape_from_keyframe(&split.inside_shape.id, self.time);
vector_layer.remove_shape_from_keyframe(&split.outside_shape.id, self.time);
// Restore original
vector_layer.add_shape_to_keyframe(split.original_shape.clone(), self.time);
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
fn description(&self) -> String {
let count = self.splits.len();
if count == 1 {
"Region split shape".to_string()
} else {
format!("Region split {} shapes", count)
}
"Region split".to_string()
}
}
@@ -1,23 +1,15 @@
//! Remove shapes action
//!
//! Handles removing shapes from a vector layer's keyframe (for cut/delete).
//! Remove shapes action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::Shape;
use uuid::Uuid;
/// Action that removes shapes from a vector layer's keyframe
/// TODO: Replace with DCEL edge/face removal actions
pub struct RemoveShapesAction {
/// Layer ID containing the shapes
layer_id: Uuid,
/// Shape IDs to remove
shape_ids: Vec<Uuid>,
/// Time of the keyframe
time: f64,
/// Saved shapes for rollback
saved_shapes: Vec<Shape>,
}
impl RemoveShapesAction {
@@ -26,47 +18,17 @@ impl RemoveShapesAction {
layer_id,
shape_ids,
time,
saved_shapes: Vec::new(),
}
}
}
impl Action for RemoveShapesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
self.saved_shapes.clear();
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for shape_id in &self.shape_ids {
if let Some(shape) = vector_layer.remove_shape_from_keyframe(shape_id, self.time) {
self.saved_shapes.push(shape);
}
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, &self.shape_ids, self.time);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for shape in self.saved_shapes.drain(..) {
vector_layer.add_shape_to_keyframe(shape, self.time);
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -79,40 +41,3 @@ impl Action for RemoveShapesAction {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::BezPath;
#[test]
fn test_remove_shapes() {
let mut document = Document::new("Test");
let mut vector_layer = VectorLayer::new("Layer 1");
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((100.0, 100.0));
let shape = Shape::new(path);
let shape_id = shape.id;
vector_layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(vector_layer));
let mut action = RemoveShapesAction::new(layer_id, vec![shape_id], 0.0);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert!(vl.shapes_at_time(0.0).is_empty());
}
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.shapes_at_time(0.0).len(), 1);
}
}
}
@@ -1,12 +1,7 @@
//! Set shape instance properties action
//!
//! Handles changing individual properties on shapes (position, rotation, scale, etc.)
//! with undo/redo support. In the keyframe model, these operate on Shape's transform
//! and opacity fields within the active keyframe.
//! Set shape instance properties action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use uuid::Uuid;
/// Individual property change for a shape instance
@@ -23,8 +18,7 @@ pub enum InstancePropertyChange {
}
impl InstancePropertyChange {
/// Extract the f64 value from any variant
fn value(&self) -> f64 {
pub fn value(&self) -> f64 {
match self {
InstancePropertyChange::X(v) => *v,
InstancePropertyChange::Y(v) => *v,
@@ -39,22 +33,15 @@ impl InstancePropertyChange {
}
/// Action that sets a property on one or more shapes in a keyframe
/// TODO: Replace with DCEL-based property changes
pub struct SetInstancePropertiesAction {
/// Layer containing the shapes
layer_id: Uuid,
/// Time of the keyframe
time: f64,
/// Shape IDs to modify and their old values
shape_changes: Vec<(Uuid, Option<f64>)>,
/// Property to change
property: InstancePropertyChange,
}
impl SetInstancePropertiesAction {
/// Create a new action to set a property on a single shape
pub fn new(layer_id: Uuid, time: f64, shape_id: Uuid, property: InstancePropertyChange) -> Self {
Self {
layer_id,
@@ -64,7 +51,6 @@ impl SetInstancePropertiesAction {
}
}
/// Create a new action to set a property on multiple shapes
pub fn new_batch(layer_id: Uuid, time: f64, shape_ids: Vec<Uuid>, property: InstancePropertyChange) -> Self {
Self {
layer_id,
@@ -73,76 +59,15 @@ impl SetInstancePropertiesAction {
property,
}
}
fn get_value_from_shape(shape: &crate::shape::Shape, property: &InstancePropertyChange) -> f64 {
match property {
InstancePropertyChange::X(_) => shape.transform.x,
InstancePropertyChange::Y(_) => shape.transform.y,
InstancePropertyChange::Rotation(_) => shape.transform.rotation,
InstancePropertyChange::ScaleX(_) => shape.transform.scale_x,
InstancePropertyChange::ScaleY(_) => shape.transform.scale_y,
InstancePropertyChange::SkewX(_) => shape.transform.skew_x,
InstancePropertyChange::SkewY(_) => shape.transform.skew_y,
InstancePropertyChange::Opacity(_) => shape.opacity,
}
}
fn set_value_on_shape(shape: &mut crate::shape::Shape, property: &InstancePropertyChange, value: f64) {
match property {
InstancePropertyChange::X(_) => shape.transform.x = value,
InstancePropertyChange::Y(_) => shape.transform.y = value,
InstancePropertyChange::Rotation(_) => shape.transform.rotation = value,
InstancePropertyChange::ScaleX(_) => shape.transform.scale_x = value,
InstancePropertyChange::ScaleY(_) => shape.transform.scale_y = value,
InstancePropertyChange::SkewX(_) => shape.transform.skew_x = value,
InstancePropertyChange::SkewY(_) => shape.transform.skew_y = value,
InstancePropertyChange::Opacity(_) => shape.opacity = value,
}
}
}
impl Action for SetInstancePropertiesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let new_value = self.property.value();
// First pass: collect old values
if let Some(layer) = document.get_layer(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, old_value) in &mut self.shape_changes {
if old_value.is_none() {
if let Some(shape) = vector_layer.get_shape_in_keyframe(shape_id, self.time) {
*old_value = Some(Self::get_value_from_shape(shape, &self.property));
}
}
}
}
}
// Second pass: apply new values
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, _) in &self.shape_changes {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
Self::set_value_on_shape(shape, &self.property, new_value);
}
}
}
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, &self.shape_changes, &self.property);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, old_value) in &self.shape_changes {
if let Some(value) = old_value {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
Self::set_value_on_shape(shape, &self.property, *value);
}
}
}
}
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -165,144 +90,3 @@ impl Action for SetInstancePropertiesAction {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::BezPath;
fn make_shape_at(x: f64, y: f64) -> Shape {
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((10.0, 10.0));
Shape::new(path).with_position(x, y)
}
#[test]
fn test_set_x_position() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = make_shape_at(10.0, 20.0);
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let mut action = SetInstancePropertiesAction::new(
layer_id,
0.0,
shape_id,
InstancePropertyChange::X(50.0),
);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.transform.x, 50.0);
assert_eq!(s.transform.y, 20.0);
}
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.transform.x, 10.0);
}
}
#[test]
fn test_set_opacity() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = make_shape_at(0.0, 0.0);
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let mut action = SetInstancePropertiesAction::new(
layer_id,
0.0,
shape_id,
InstancePropertyChange::Opacity(0.5),
);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.opacity, 0.5);
}
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.opacity, 1.0);
}
}
#[test]
fn test_batch_set_scale() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape1 = make_shape_at(0.0, 0.0);
let shape1_id = shape1.id;
let shape2 = make_shape_at(10.0, 10.0);
let shape2_id = shape2.id;
layer.add_shape_to_keyframe(shape1, 0.0);
layer.add_shape_to_keyframe(shape2, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let mut action = SetInstancePropertiesAction::new_batch(
layer_id,
0.0,
vec![shape1_id, shape2_id],
InstancePropertyChange::ScaleX(2.0),
);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.get_shape_in_keyframe(&shape1_id, 0.0).unwrap().transform.scale_x, 2.0);
assert_eq!(vl.get_shape_in_keyframe(&shape2_id, 0.0).unwrap().transform.scale_x, 2.0);
}
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
assert_eq!(vl.get_shape_in_keyframe(&shape1_id, 0.0).unwrap().transform.scale_x, 1.0);
assert_eq!(vl.get_shape_in_keyframe(&shape2_id, 0.0).unwrap().transform.scale_x, 1.0);
}
}
#[test]
fn test_description() {
let layer_id = Uuid::new_v4();
let shape_id = Uuid::new_v4();
let action1 = SetInstancePropertiesAction::new(
layer_id, 0.0, shape_id,
InstancePropertyChange::X(0.0),
);
assert_eq!(action1.description(), "Set X position");
let action2 = SetInstancePropertiesAction::new(
layer_id, 0.0, shape_id,
InstancePropertyChange::Rotation(0.0),
);
assert_eq!(action2.description(), "Set rotation");
let action3 = SetInstancePropertiesAction::new_batch(
layer_id, 0.0,
vec![Uuid::new_v4(), Uuid::new_v4()],
InstancePropertyChange::Opacity(1.0),
);
assert_eq!(action3.description(), "Set opacity on 2 shapes");
}
}
@@ -1,12 +1,8 @@
//! Set shape properties action
//!
//! Handles changing shape properties (fill color, stroke color, stroke width)
//! with undo/redo support.
//! Set shape properties action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::{ShapeColor, StrokeStyle};
use crate::shape::ShapeColor;
use uuid::Uuid;
/// Property change for a shape
@@ -18,25 +14,16 @@ pub enum ShapePropertyChange {
}
/// Action that sets properties on a shape
/// TODO: Replace with DCEL face/edge property changes
pub struct SetShapePropertiesAction {
/// Layer containing the shape
layer_id: Uuid,
/// Shape to modify
shape_id: Uuid,
/// Time of the keyframe containing the shape
time: f64,
/// New property value
new_value: ShapePropertyChange,
/// Old property value (stored after first execution)
old_value: Option<ShapePropertyChange>,
}
impl SetShapePropertiesAction {
/// Create a new action to set a property on a shape
pub fn new(layer_id: Uuid, shape_id: Uuid, time: f64, new_value: ShapePropertyChange) -> Self {
Self {
layer_id,
@@ -47,85 +34,27 @@ impl SetShapePropertiesAction {
}
}
/// Create action to set fill color
pub fn set_fill_color(layer_id: Uuid, shape_id: Uuid, time: f64, color: Option<ShapeColor>) -> Self {
Self::new(layer_id, shape_id, time, ShapePropertyChange::FillColor(color))
}
/// Create action to set stroke color
pub fn set_stroke_color(layer_id: Uuid, shape_id: Uuid, time: f64, color: Option<ShapeColor>) -> Self {
Self::new(layer_id, shape_id, time, ShapePropertyChange::StrokeColor(color))
}
/// Create action to set stroke width
pub fn set_stroke_width(layer_id: Uuid, shape_id: Uuid, time: f64, width: f64) -> Self {
Self::new(layer_id, shape_id, time, ShapePropertyChange::StrokeWidth(width))
}
}
fn apply_property(shape: &mut crate::shape::Shape, change: &ShapePropertyChange) {
match change {
ShapePropertyChange::FillColor(color) => {
shape.fill_color = *color;
}
ShapePropertyChange::StrokeColor(color) => {
shape.stroke_color = *color;
}
ShapePropertyChange::StrokeWidth(width) => {
if let Some(ref mut style) = shape.stroke_style {
style.width = *width;
} else {
shape.stroke_style = Some(StrokeStyle {
width: *width,
..Default::default()
});
}
}
}
}
impl Action for SetShapePropertiesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(&self.shape_id, self.time) {
// Store old value if not already stored
if self.old_value.is_none() {
self.old_value = Some(match &self.new_value {
ShapePropertyChange::FillColor(_) => {
ShapePropertyChange::FillColor(shape.fill_color)
}
ShapePropertyChange::StrokeColor(_) => {
ShapePropertyChange::StrokeColor(shape.stroke_color)
}
ShapePropertyChange::StrokeWidth(_) => {
let width = shape
.stroke_style
.as_ref()
.map(|s| s.width)
.unwrap_or(1.0);
ShapePropertyChange::StrokeWidth(width)
}
});
}
apply_property(shape, &self.new_value);
}
}
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, &self.shape_id, self.time, &self.new_value);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(old_value) = &self.old_value.clone() {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(&self.shape_id, self.time) {
apply_property(shape, old_value);
}
}
}
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = &self.old_value;
Ok(())
}
@@ -137,115 +66,3 @@ impl Action for SetShapePropertiesAction {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::BezPath;
fn create_test_shape() -> Shape {
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((100.0, 0.0));
path.line_to((100.0, 100.0));
path.line_to((0.0, 100.0));
path.close_path();
let mut shape = Shape::new(path);
shape.fill_color = Some(ShapeColor::rgb(255, 0, 0));
shape.stroke_color = Some(ShapeColor::rgb(0, 0, 0));
shape.stroke_style = Some(StrokeStyle {
width: 2.0,
..Default::default()
});
shape
}
#[test]
fn test_set_fill_color() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = create_test_shape();
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
// Verify initial color
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(shape.fill_color.unwrap().r, 255);
}
// Create and execute action
let new_color = Some(ShapeColor::rgb(0, 255, 0));
let mut action = SetShapePropertiesAction::set_fill_color(layer_id, shape_id, 0.0, new_color);
action.execute(&mut document).unwrap();
// Verify color changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(shape.fill_color.unwrap().g, 255);
}
// Rollback
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(shape.fill_color.unwrap().r, 255);
}
}
#[test]
fn test_set_stroke_width() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let shape = create_test_shape();
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
// Create and execute action
let mut action = SetShapePropertiesAction::set_stroke_width(layer_id, shape_id, 0.0, 5.0);
action.execute(&mut document).unwrap();
// Verify width changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(shape.stroke_style.as_ref().unwrap().width, 5.0);
}
// Rollback
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let shape = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(shape.stroke_style.as_ref().unwrap().width, 2.0);
}
}
#[test]
fn test_description() {
let layer_id = Uuid::new_v4();
let shape_id = Uuid::new_v4();
let action1 =
SetShapePropertiesAction::set_fill_color(layer_id, shape_id, 0.0, Some(ShapeColor::rgb(0, 0, 0)));
assert_eq!(action1.description(), "Set fill color");
let action2 =
SetShapePropertiesAction::set_stroke_color(layer_id, shape_id, 0.0, Some(ShapeColor::rgb(0, 0, 0)));
assert_eq!(action2.description(), "Set stroke color");
let action3 = SetShapePropertiesAction::set_stroke_width(layer_id, shape_id, 0.0, 3.0);
assert_eq!(action3.description(), "Set stroke width");
}
}
@@ -1,19 +1,16 @@
//! Transform shapes action
//!
//! Applies scale, rotation, and other transformations to shapes in a keyframe.
//! Transform shapes action — STUB: needs DCEL rewrite
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::object::Transform;
use std::collections::HashMap;
use uuid::Uuid;
/// Action to transform multiple shapes in a keyframe
/// TODO: Replace with DCEL-based transforms (affine on vertices/edges)
pub struct TransformShapeInstancesAction {
layer_id: Uuid,
time: f64,
/// Map of shape ID to (old transform, new transform)
shape_transforms: HashMap<Uuid, (Transform, Transform)>,
}
@@ -32,29 +29,12 @@ impl TransformShapeInstancesAction {
}
impl Action for TransformShapeInstancesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, (_old, new)) in &self.shape_transforms {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
shape.transform = new.clone();
}
}
}
}
fn execute(&mut self, _document: &mut Document) -> Result<(), String> {
let _ = (&self.layer_id, self.time, &self.shape_transforms);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_id, (old, _new)) in &self.shape_transforms {
if let Some(shape) = vector_layer.get_shape_in_keyframe_mut(shape_id, self.time) {
shape.transform = old.clone();
}
}
}
}
fn rollback(&mut self, _document: &mut Document) -> Result<(), String> {
Ok(())
}
@@ -62,48 +42,3 @@ impl Action for TransformShapeInstancesAction {
format!("Transform {} shape(s)", self.shape_transforms.len())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layer::VectorLayer;
use crate::shape::Shape;
use vello::kurbo::BezPath;
#[test]
fn test_transform_shape() {
let mut document = Document::new("Test");
let mut layer = VectorLayer::new("Test Layer");
let mut path = BezPath::new();
path.move_to((0.0, 0.0));
path.line_to((100.0, 100.0));
let shape = Shape::new(path).with_position(10.0, 20.0);
let shape_id = shape.id;
layer.add_shape_to_keyframe(shape, 0.0);
let layer_id = document.root_mut().add_child(AnyLayer::Vector(layer));
let old_transform = Transform::with_position(10.0, 20.0);
let new_transform = Transform::with_position(100.0, 200.0);
let mut transforms = HashMap::new();
transforms.insert(shape_id, (old_transform, new_transform));
let mut action = TransformShapeInstancesAction::new(layer_id, 0.0, transforms);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.transform.x, 100.0);
assert_eq!(s.transform.y, 200.0);
}
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
let s = vl.get_shape_in_keyframe(&shape_id, 0.0).unwrap();
assert_eq!(s.transform.x, 10.0);
assert_eq!(s.transform.y, 20.0);
}
}
}