Work on region select

This commit is contained in:
Skyler Lehmkuhl
2026-02-21 06:04:54 -05:00
parent 469849a0d6
commit 2222e68a3e
16 changed files with 2163 additions and 6 deletions
@@ -31,6 +31,7 @@ pub mod remove_shapes;
pub mod set_keyframe;
pub mod group_shapes;
pub mod convert_to_movie_clip;
pub mod region_split;
pub use add_clip_instance::AddClipInstanceAction;
pub use add_effect::AddEffectAction;
@@ -60,3 +61,4 @@ pub use remove_shapes::RemoveShapesAction;
pub use set_keyframe::SetKeyframeAction;
pub use group_shapes::GroupAction;
pub use convert_to_movie_clip::ConvertToMovieClipAction;
pub use region_split::RegionSplitAction;
@@ -0,0 +1,119 @@
//! Region split action
//!
//! Commits a temporary region-based shape split permanently.
//! Replaces original shapes with their inside and outside portions.
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.
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)>,
) -> 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);
}
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);
}
Ok(())
}
fn description(&self) -> String {
let count = self.splits.len();
if count == 1 {
"Region split shape".to_string()
} else {
format!("Region split {} shapes", count)
}
}
}
@@ -5,10 +5,11 @@
use crate::clip::ClipInstance;
use crate::layer::VectorLayer;
use crate::region_select;
use crate::shape::Shape;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use vello::kurbo::{Affine, Point, Rect, Shape as KurboShape};
use vello::kurbo::{Affine, BezPath, Point, Rect, Shape as KurboShape};
/// Result of a hit test operation
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -119,6 +120,66 @@ pub fn hit_test_objects_in_rect(
hits
}
/// Classification of shapes relative to a clipping region
#[derive(Debug, Clone)]
pub struct ShapeRegionClassification {
/// Shapes entirely inside the region
pub fully_inside: Vec<Uuid>,
/// Shapes whose paths cross the region boundary
pub intersecting: Vec<Uuid>,
/// Shapes with no overlap with the region
pub fully_outside: Vec<Uuid>,
}
/// Classify shapes in a layer relative to a clipping region.
///
/// Uses bounding box fast-rejection, then checks path-region intersection
/// and containment for accurate classification.
pub fn classify_shapes_by_region(
layer: &VectorLayer,
time: f64,
region: &BezPath,
parent_transform: Affine,
) -> ShapeRegionClassification {
let mut result = ShapeRegionClassification {
fully_inside: Vec::new(),
intersecting: Vec::new(),
fully_outside: Vec::new(),
};
let region_bbox = region.bounding_box();
for shape in layer.shapes_at_time(time) {
let combined_transform = parent_transform * shape.transform.to_affine();
let bbox = shape.path().bounding_box();
let transformed_bbox = combined_transform.transform_rect_bbox(bbox);
// Fast rejection: if bounding boxes don't overlap, fully outside
if region_bbox.intersect(transformed_bbox).area() <= 0.0 {
result.fully_outside.push(shape.id);
continue;
}
// Transform the shape path to world space for accurate testing
let world_path = {
let mut p = shape.path().clone();
p.apply_affine(combined_transform);
p
};
// Check if the path crosses the region boundary
if region_select::path_intersects_region(&world_path, region) {
result.intersecting.push(shape.id);
} else if region_select::path_fully_inside_region(&world_path, region) {
result.fully_inside.push(shape.id);
} else {
result.fully_outside.push(shape.id);
}
}
result
}
/// Get the bounding box of a shape in screen space
pub fn get_shape_bounds(
shape: &Shape,
@@ -489,14 +489,14 @@ impl VectorLayer {
/// Add a shape to the keyframe at the given time.
/// Creates a keyframe if none exists at that time.
pub(crate) fn add_shape_to_keyframe(&mut self, shape: Shape, time: f64) {
pub fn add_shape_to_keyframe(&mut self, shape: Shape, time: f64) {
let kf = self.ensure_keyframe_at(time);
kf.shapes.push(shape);
}
/// Remove a shape from the keyframe at the given time.
/// Returns the removed shape if found.
pub(crate) fn remove_shape_from_keyframe(&mut self, shape_id: &Uuid, time: f64) -> Option<Shape> {
pub fn remove_shape_from_keyframe(&mut self, shape_id: &Uuid, time: f64) -> Option<Shape> {
let kf = self.keyframe_at_mut(time)?;
let idx = kf.shapes.iter().position(|s| &s.id == shape_id)?;
Some(kf.shapes.remove(idx))
@@ -42,3 +42,4 @@ pub mod file_types;
pub mod file_io;
pub mod export;
pub mod clipboard;
pub mod region_select;
File diff suppressed because it is too large Load Diff
@@ -2,8 +2,10 @@
//!
//! Tracks selected shape instances, clip instances, and shapes for editing operations.
use crate::shape::Shape;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use vello::kurbo::BezPath;
/// Selection state for the editor
///
@@ -212,6 +214,43 @@ impl Selection {
}
}
/// Represents a temporary region-based split of shapes.
///
/// When a region select is active, shapes that cross the region boundary
/// are temporarily split into "inside" and "outside" parts. The inside
/// parts are selected. If the user performs an operation, the split is
/// committed; if they deselect, the original shapes are restored.
#[derive(Clone, Debug)]
pub struct RegionSelection {
/// The clipping region as a closed BezPath (polygon or rect)
pub region_path: BezPath,
/// Layer containing the affected shapes
pub layer_id: Uuid,
/// Keyframe time
pub time: f64,
/// Per-shape split results
pub splits: Vec<ShapeSplit>,
/// Shape IDs that were fully inside the region (not split, just selected)
pub fully_inside_ids: Vec<Uuid>,
/// Whether the split has been committed (via an operation on the selection)
pub committed: bool,
}
/// One shape's split result from a region selection
#[derive(Clone, Debug)]
pub struct ShapeSplit {
/// The original shape (stored for reverting)
pub original_shape: Shape,
/// UUID for the "inside" portion shape
pub inside_shape_id: Uuid,
/// The clipped path inside the region
pub inside_path: BezPath,
/// UUID for the "outside" portion shape
pub outside_shape_id: Uuid,
/// The clipped path outside the region
pub outside_path: BezPath,
}
#[cfg(test)]
mod tests {
use super::*;
@@ -33,6 +33,23 @@ pub enum Tool {
BezierEdit,
/// Text tool - add and edit text
Text,
/// Region select tool - select sub-regions of shapes by clipping
RegionSelect,
}
/// Region select mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegionSelectMode {
/// Rectangular region selection
Rectangle,
/// Freehand lasso region selection
Lasso,
}
impl Default for RegionSelectMode {
fn default() -> Self {
Self::Rectangle
}
}
/// Tool state tracking for interactive operations
@@ -117,6 +134,17 @@ pub enum ToolState {
parameter_t: f64, // Parameter where the drag started (0.0-1.0)
},
/// Drawing a region selection rectangle
RegionSelectingRect {
start: Point,
current: Point,
},
/// Drawing a freehand lasso region selection
RegionSelectingLasso {
points: Vec<Point>,
},
/// Editing a control point (BezierEdit tool only)
EditingControlPoint {
shape_id: Uuid, // Which shape is being edited
@@ -179,6 +207,7 @@ impl Tool {
Tool::Polygon => "Polygon",
Tool::BezierEdit => "Bezier Edit",
Tool::Text => "Text",
Tool::RegionSelect => "Region Select",
}
}
@@ -196,6 +225,7 @@ impl Tool {
Tool::Polygon => "polygon.svg",
Tool::BezierEdit => "bezier_edit.svg",
Tool::Text => "text.svg",
Tool::RegionSelect => "region_select.svg",
}
}
@@ -213,6 +243,7 @@ impl Tool {
Tool::Polygon,
Tool::BezierEdit,
Tool::Text,
Tool::RegionSelect,
]
}
@@ -230,6 +261,7 @@ impl Tool {
Tool::Polygon => "G",
Tool::BezierEdit => "A",
Tool::Text => "T",
Tool::RegionSelect => "S",
}
}
}