Group shapes

This commit is contained in:
Skyler Lehmkuhl
2026-02-20 11:13:56 -05:00
parent 7e2f63b62d
commit 3ba6dcb3d2
24 changed files with 2092 additions and 2176 deletions
@@ -1566,31 +1566,17 @@ impl EditorApp {
_ => return,
};
// Gather selected shape instances and their shape definitions
let selected_instances: Vec<_> = vector_layer
.shape_instances
.iter()
.filter(|si| self.selection.contains_shape_instance(&si.id))
.cloned()
// Gather selected shapes (they now contain their own transforms)
let selected_shapes: Vec<_> = self.selection.shapes().iter()
.filter_map(|id| vector_layer.shapes.get(id).cloned())
.collect();
if selected_instances.is_empty() {
if selected_shapes.is_empty() {
return;
}
let mut shapes = Vec::new();
let mut seen_shape_ids = std::collections::HashSet::new();
for inst in &selected_instances {
if seen_shape_ids.insert(inst.shape_id) {
if let Some(shape) = vector_layer.shapes.get(&inst.shape_id) {
shapes.push((inst.shape_id, shape.clone()));
}
}
}
let content = ClipboardContent::Shapes {
shapes,
instances: selected_instances,
shapes: selected_shapes,
};
self.clipboard_manager.copy(content);
@@ -1641,47 +1627,18 @@ impl EditorApp {
}
self.selection.clear_clip_instances();
} else if !self.selection.shape_instances().is_empty() {
} else if !self.selection.shapes().is_empty() {
let active_layer_id = match self.active_layer_id {
Some(id) => id,
None => return,
};
let document = self.action_executor.document();
let layer = match document.get_layer(&active_layer_id) {
Some(l) => l,
None => return,
};
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return,
};
// Collect shape instance IDs and their shape IDs
let instance_ids: Vec<Uuid> = self.selection.shape_instances().to_vec();
let mut shape_ids: Vec<Uuid> = Vec::new();
let mut shape_id_set = std::collections::HashSet::new();
for inst in &vector_layer.shape_instances {
if instance_ids.contains(&inst.id) {
if shape_id_set.insert(inst.shape_id) {
// Only remove shape definition if no other instances reference it
let other_refs = vector_layer
.shape_instances
.iter()
.any(|si| si.shape_id == inst.shape_id && !instance_ids.contains(&si.id));
if !other_refs {
shape_ids.push(inst.shape_id);
}
}
}
}
let shape_ids: Vec<Uuid> = self.selection.shapes().to_vec();
let action = lightningbeam_core::actions::RemoveShapesAction::new(
active_layer_id,
shape_ids,
instance_ids,
self.playback_time,
);
if let Err(e) = self.action_executor.execute(Box::new(action)) {
@@ -1797,16 +1754,13 @@ impl EditorApp {
self.selection.add_clip_instance(id);
}
}
ClipboardContent::Shapes {
shapes,
instances,
} => {
ClipboardContent::Shapes { shapes } => {
let active_layer_id = match self.active_layer_id {
Some(id) => id,
None => return,
};
// Add shapes and instances to the active vector layer
// Add shapes to the active vector layer's keyframe
let document = self.action_executor.document_mut();
let layer = match document.get_layer_mut(&active_layer_id) {
Some(l) => l,
@@ -1821,19 +1775,17 @@ impl EditorApp {
}
};
let new_instance_ids: Vec<Uuid> = instances.iter().map(|i| i.id).collect();
let new_shape_ids: Vec<Uuid> = shapes.iter().map(|s| s.id).collect();
for (id, shape) in shapes {
vector_layer.shapes.insert(id, shape);
}
for inst in instances {
vector_layer.shape_instances.push(inst);
let kf = vector_layer.ensure_keyframe_at(self.playback_time);
for shape in shapes {
kf.shapes.push(shape);
}
// Select pasted shapes
self.selection.clear_shape_instances();
for id in new_instance_ids {
self.selection.add_shape_instance(id);
self.selection.clear_shapes();
for id in new_shape_ids {
self.selection.add_shape(id);
}
}
}
@@ -2268,8 +2220,26 @@ impl EditorApp {
// Modify menu
MenuAction::Group => {
println!("Menu: Group");
// TODO: Implement group
if let Some(layer_id) = self.active_layer_id {
let shape_ids: Vec<uuid::Uuid> = self.selection.shape_instances().to_vec();
let clip_ids: Vec<uuid::Uuid> = self.selection.clip_instances().to_vec();
if shape_ids.len() + clip_ids.len() >= 2 {
let instance_id = uuid::Uuid::new_v4();
let action = lightningbeam_core::actions::GroupAction::new(
layer_id,
self.playback_time,
shape_ids,
clip_ids,
instance_id,
);
if let Err(e) = self.action_executor.execute(Box::new(action)) {
eprintln!("Failed to group: {}", e);
} else {
self.selection.clear();
self.selection.add_clip_instance(instance_id);
}
}
}
}
MenuAction::SendToBack => {
println!("Menu: Send to Back");
@@ -2397,7 +2367,6 @@ impl EditorApp {
use lightningbeam_core::clip::VectorClip;
use lightningbeam_core::layer::{VectorLayer, AnyLayer};
use lightningbeam_core::shape::{Shape, ShapeColor};
use lightningbeam_core::object::ShapeInstance;
use kurbo::{Circle, Rect, Shape as KurboShape};
// Generate unique name based on existing clip count
@@ -2413,19 +2382,16 @@ impl EditorApp {
let circle_path = Circle::new((100.0, 100.0), 50.0).to_path(0.1);
let mut circle_shape = Shape::new(circle_path);
circle_shape.fill_color = Some(ShapeColor::rgb(255, 0, 0));
let circle_id = circle_shape.id;
layer.add_shape(circle_shape);
// Create a blue rectangle shape
let rect_path = Rect::new(200.0, 50.0, 350.0, 150.0).to_path(0.1);
let mut rect_shape = Shape::new(rect_path);
rect_shape.fill_color = Some(ShapeColor::rgb(0, 0, 255));
let rect_id = rect_shape.id;
layer.add_shape(rect_shape);
// Add shape instances
layer.shape_instances.push(ShapeInstance::new(circle_id));
layer.shape_instances.push(ShapeInstance::new(rect_id));
// Add shapes to keyframe at time 0.0
let kf = layer.ensure_keyframe_at(0.0);
kf.shapes.push(circle_shape);
kf.shapes.push(rect_shape);
// Add the layer to the clip
test_clip.layers.add_root(AnyLayer::Vector(layer));
@@ -2444,9 +2410,36 @@ impl EditorApp {
}
// Timeline menu
MenuAction::NewKeyframe => {
println!("Menu: New Keyframe");
// TODO: Implement new keyframe
MenuAction::NewKeyframe | MenuAction::AddKeyframeAtPlayhead => {
if let Some(layer_id) = self.active_layer_id {
let document = self.action_executor.document();
// Determine which selected objects are shape instances vs clip instances
let mut shape_ids = Vec::new();
let mut clip_ids = Vec::new();
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
for &id in self.selection.shape_instances() {
if vl.get_shape_in_keyframe(&id, self.playback_time).is_some() {
shape_ids.push(id);
}
}
for &id in self.selection.clip_instances() {
if vl.clip_instances.iter().any(|ci| ci.id == id) {
clip_ids.push(id);
}
}
}
// For vector layers, always create a shape keyframe (even without clip selection)
if document.get_layer(&layer_id).map_or(false, |l| matches!(l, AnyLayer::Vector(_))) || !clip_ids.is_empty() {
let action = lightningbeam_core::actions::SetKeyframeAction::new(
layer_id,
self.playback_time,
clip_ids,
);
if let Err(e) = self.action_executor.execute(Box::new(action)) {
eprintln!("Failed to set keyframe: {}", e);
}
}
}
}
MenuAction::NewBlankKeyframe => {
println!("Menu: New Blank Keyframe");
@@ -2460,10 +2453,7 @@ impl EditorApp {
println!("Menu: Duplicate Keyframe");
// TODO: Implement duplicate keyframe
}
MenuAction::AddKeyframeAtPlayhead => {
println!("Menu: Add Keyframe at Playhead");
// TODO: Implement add keyframe at playhead
}
// AddKeyframeAtPlayhead handled above together with NewKeyframe
MenuAction::AddMotionTween => {
println!("Menu: Add Motion Tween");
// TODO: Implement add motion tween
@@ -3312,16 +3302,14 @@ impl EditorApp {
use lightningbeam_core::shape::Shape;
let shape = Shape::new(path).with_image_fill(asset_info.clip_id);
// Create shape instance at document center
use lightningbeam_core::object::ShapeInstance;
let shape_instance = ShapeInstance::new(shape.id)
.with_position(center_x, center_y);
// Set position on shape directly
let shape = shape.with_position(center_x, center_y);
// Create and execute action
let action = lightningbeam_core::actions::AddShapeAction::new(
layer_id,
shape,
shape_instance,
self.playback_time,
);
let _ = self.action_executor.execute(Box::new(action));
} else {
@@ -4446,6 +4434,25 @@ impl eframe::App for EditorApp {
// Empty cache fallback if generator not initialized
let empty_thumbnail_cache: HashMap<Uuid, Vec<u8>> = HashMap::new();
// Sync clip instance transforms from animation data at current playback time.
// This ensures selection boxes, hit testing, and interactive editing see the
// animated transform values, not just the base values on the ClipInstance struct.
{
let time = self.playback_time;
let document = self.action_executor.document_mut();
for layer in document.root.children.iter_mut() {
if let lightningbeam_core::layer::AnyLayer::Vector(vl) = layer {
for ci in &mut vl.clip_instances {
let (t, opacity) = vl.layer.animation_data.eval_clip_instance_transform(
ci.id, time, &ci.transform, ci.opacity,
);
ci.transform = t;
ci.opacity = opacity;
}
}
}
}
// Create render context
let mut ctx = RenderContext {
tool_icon_cache: &mut self.tool_icon_cache,
@@ -440,84 +440,82 @@ fn generate_vector_thumbnail(clip: &VectorClip, bg_color: egui::Color32) -> Vec<
// Iterate through layers and render shapes
for layer_node in clip.layers.iter() {
if let AnyLayer::Vector(vector_layer) = &layer_node.data {
// Render each shape instance
for shape_instance in &vector_layer.shape_instances {
if let Some(shape) = vector_layer.shapes.get(&shape_instance.shape_id) {
// Get the path (frame 0)
let kurbo_path = shape.path();
// Render each shape at time 0.0 (frame 0)
for shape in vector_layer.shapes_at_time(0.0) {
// Get the path (frame 0)
let kurbo_path = shape.path();
// Convert kurbo BezPath to tiny-skia PathBuilder
let mut path_builder = PathBuilder::new();
for el in kurbo_path.iter() {
match el {
PathEl::MoveTo(p) => {
let x = (p.x * scale + offset_x) as f32;
let y = (p.y * scale + offset_y) as f32;
path_builder.move_to(x, y);
}
PathEl::LineTo(p) => {
let x = (p.x * scale + offset_x) as f32;
let y = (p.y * scale + offset_y) as f32;
path_builder.line_to(x, y);
}
PathEl::QuadTo(p1, p2) => {
let x1 = (p1.x * scale + offset_x) as f32;
let y1 = (p1.y * scale + offset_y) as f32;
let x2 = (p2.x * scale + offset_x) as f32;
let y2 = (p2.y * scale + offset_y) as f32;
path_builder.quad_to(x1, y1, x2, y2);
}
PathEl::CurveTo(p1, p2, p3) => {
let x1 = (p1.x * scale + offset_x) as f32;
let y1 = (p1.y * scale + offset_y) as f32;
let x2 = (p2.x * scale + offset_x) as f32;
let y2 = (p2.y * scale + offset_y) as f32;
let x3 = (p3.x * scale + offset_x) as f32;
let y3 = (p3.y * scale + offset_y) as f32;
path_builder.cubic_to(x1, y1, x2, y2, x3, y3);
}
PathEl::ClosePath => {
path_builder.close();
}
// Convert kurbo BezPath to tiny-skia PathBuilder
let mut path_builder = PathBuilder::new();
for el in kurbo_path.iter() {
match el {
PathEl::MoveTo(p) => {
let x = (p.x * scale + offset_x) as f32;
let y = (p.y * scale + offset_y) as f32;
path_builder.move_to(x, y);
}
PathEl::LineTo(p) => {
let x = (p.x * scale + offset_x) as f32;
let y = (p.y * scale + offset_y) as f32;
path_builder.line_to(x, y);
}
PathEl::QuadTo(p1, p2) => {
let x1 = (p1.x * scale + offset_x) as f32;
let y1 = (p1.y * scale + offset_y) as f32;
let x2 = (p2.x * scale + offset_x) as f32;
let y2 = (p2.y * scale + offset_y) as f32;
path_builder.quad_to(x1, y1, x2, y2);
}
PathEl::CurveTo(p1, p2, p3) => {
let x1 = (p1.x * scale + offset_x) as f32;
let y1 = (p1.y * scale + offset_y) as f32;
let x2 = (p2.x * scale + offset_x) as f32;
let y2 = (p2.y * scale + offset_y) as f32;
let x3 = (p3.x * scale + offset_x) as f32;
let y3 = (p3.y * scale + offset_y) as f32;
path_builder.cubic_to(x1, y1, x2, y2, x3, y3);
}
PathEl::ClosePath => {
path_builder.close();
}
}
}
if let Some(ts_path) = path_builder.finish() {
// Draw fill if present
if let Some(fill_color) = &shape.fill_color {
if let Some(ts_path) = path_builder.finish() {
// Draw fill if present
if let Some(fill_color) = &shape.fill_color {
let mut paint = Paint::default();
paint.set_color(shape_color_to_tiny_skia(fill_color));
paint.anti_alias = true;
pixmap.fill_path(
&ts_path,
&paint,
tiny_skia::FillRule::Winding,
TsTransform::identity(),
None,
);
}
// Draw stroke if present
if let Some(stroke_color) = &shape.stroke_color {
if let Some(stroke_style) = &shape.stroke_style {
let mut paint = Paint::default();
paint.set_color(shape_color_to_tiny_skia(fill_color));
paint.set_color(shape_color_to_tiny_skia(stroke_color));
paint.anti_alias = true;
pixmap.fill_path(
let stroke = tiny_skia::Stroke {
width: (stroke_style.width * scale) as f32,
..Default::default()
};
pixmap.stroke_path(
&ts_path,
&paint,
tiny_skia::FillRule::Winding,
&stroke,
TsTransform::identity(),
None,
);
}
// Draw stroke if present
if let Some(stroke_color) = &shape.stroke_color {
if let Some(stroke_style) = &shape.stroke_style {
let mut paint = Paint::default();
paint.set_color(shape_color_to_tiny_skia(stroke_color));
paint.anti_alias = true;
let stroke = tiny_skia::Stroke {
width: (stroke_style.width * scale) as f32,
..Default::default()
};
pixmap.stroke_path(
&ts_path,
&paint,
&stroke,
TsTransform::identity(),
None,
);
}
}
}
}
}
@@ -117,82 +117,78 @@ impl InfopanelPane {
let mut first = true;
for instance_id in &info.instance_ids {
if let Some(instance) = vector_layer.get_object(instance_id) {
info.shape_ids.push(instance.shape_id);
if let Some(shape) = vector_layer.get_shape_in_keyframe(instance_id, *shared.playback_time) {
info.shape_ids.push(*instance_id);
if first {
// First instance - set initial values
info.x = Some(instance.transform.x);
info.y = Some(instance.transform.y);
info.rotation = Some(instance.transform.rotation);
info.scale_x = Some(instance.transform.scale_x);
info.scale_y = Some(instance.transform.scale_y);
info.skew_x = Some(instance.transform.skew_x);
info.skew_y = Some(instance.transform.skew_y);
info.opacity = Some(instance.opacity);
// First shape - set initial values
info.x = Some(shape.transform.x);
info.y = Some(shape.transform.y);
info.rotation = Some(shape.transform.rotation);
info.scale_x = Some(shape.transform.scale_x);
info.scale_y = Some(shape.transform.scale_y);
info.skew_x = Some(shape.transform.skew_x);
info.skew_y = Some(shape.transform.skew_y);
info.opacity = Some(shape.opacity);
// Get shape properties
if let Some(shape) = vector_layer.shapes.get(&instance.shape_id) {
info.fill_color = Some(shape.fill_color);
info.stroke_color = Some(shape.stroke_color);
info.stroke_width = shape
.stroke_style
.as_ref()
.map(|s| Some(s.width))
.unwrap_or(Some(1.0));
}
info.fill_color = Some(shape.fill_color);
info.stroke_color = Some(shape.stroke_color);
info.stroke_width = shape
.stroke_style
.as_ref()
.map(|s| Some(s.width))
.unwrap_or(Some(1.0));
first = false;
} else {
// Check if values differ (set to None if mixed)
if info.x != Some(instance.transform.x) {
if info.x != Some(shape.transform.x) {
info.x = None;
}
if info.y != Some(instance.transform.y) {
if info.y != Some(shape.transform.y) {
info.y = None;
}
if info.rotation != Some(instance.transform.rotation) {
if info.rotation != Some(shape.transform.rotation) {
info.rotation = None;
}
if info.scale_x != Some(instance.transform.scale_x) {
if info.scale_x != Some(shape.transform.scale_x) {
info.scale_x = None;
}
if info.scale_y != Some(instance.transform.scale_y) {
if info.scale_y != Some(shape.transform.scale_y) {
info.scale_y = None;
}
if info.skew_x != Some(instance.transform.skew_x) {
if info.skew_x != Some(shape.transform.skew_x) {
info.skew_x = None;
}
if info.skew_y != Some(instance.transform.skew_y) {
if info.skew_y != Some(shape.transform.skew_y) {
info.skew_y = None;
}
if info.opacity != Some(instance.opacity) {
if info.opacity != Some(shape.opacity) {
info.opacity = None;
}
// Check shape properties
if let Some(shape) = vector_layer.shapes.get(&instance.shape_id) {
// Compare fill colors - set to None if mixed
if let Some(current_fill) = &info.fill_color {
if *current_fill != shape.fill_color {
info.fill_color = None;
}
// Compare fill colors - set to None if mixed
if let Some(current_fill) = &info.fill_color {
if *current_fill != shape.fill_color {
info.fill_color = None;
}
// Compare stroke colors - set to None if mixed
if let Some(current_stroke) = &info.stroke_color {
if *current_stroke != shape.stroke_color {
info.stroke_color = None;
}
}
let stroke_w = shape
.stroke_style
.as_ref()
.map(|s| s.width)
.unwrap_or(1.0);
if info.stroke_width != Some(stroke_w) {
info.stroke_width = None;
}
// Compare stroke colors - set to None if mixed
if let Some(current_stroke) = &info.stroke_color {
if *current_stroke != shape.stroke_color {
info.stroke_color = None;
}
}
let stroke_w = shape
.stroke_style
.as_ref()
.map(|s| s.width)
.unwrap_or(1.0);
if info.stroke_width != Some(stroke_w) {
info.stroke_width = None;
}
}
}
}
@@ -488,6 +484,7 @@ impl InfopanelPane {
for instance_id in instance_ids {
let action = SetInstancePropertiesAction::new(
layer_id,
*shared.playback_time,
*instance_id,
make_change(v),
);
@@ -507,6 +504,7 @@ impl InfopanelPane {
for instance_id in instance_ids {
let action = SetInstancePropertiesAction::new(
layer_id,
*shared.playback_time,
*instance_id,
make_change(v),
);
@@ -564,6 +562,7 @@ impl InfopanelPane {
let action = SetShapePropertiesAction::set_fill_color(
layer_id,
*shape_id,
*shared.playback_time,
new_color,
);
shared.pending_actions.push(Box::new(action));
@@ -578,6 +577,7 @@ impl InfopanelPane {
let action = SetShapePropertiesAction::set_fill_color(
layer_id,
*shape_id,
*shared.playback_time,
default_fill,
);
shared.pending_actions.push(Box::new(action));
@@ -612,6 +612,7 @@ impl InfopanelPane {
let action = SetShapePropertiesAction::set_stroke_color(
layer_id,
*shape_id,
*shared.playback_time,
new_color,
);
shared.pending_actions.push(Box::new(action));
@@ -626,6 +627,7 @@ impl InfopanelPane {
let action = SetShapePropertiesAction::set_stroke_color(
layer_id,
*shape_id,
*shared.playback_time,
default_stroke,
);
shared.pending_actions.push(Box::new(action));
@@ -654,6 +656,7 @@ impl InfopanelPane {
let action = SetShapePropertiesAction::set_stroke_width(
layer_id,
*shape_id,
*shared.playback_time,
width,
);
shared.pending_actions.push(Box::new(action));
File diff suppressed because it is too large Load Diff
@@ -18,6 +18,50 @@ const MIN_PIXELS_PER_SECOND: f32 = 1.0; // Allow zooming out to see 10+ minutes
const MAX_PIXELS_PER_SECOND: f32 = 500.0;
const EDGE_DETECTION_PIXELS: f32 = 8.0; // Distance from edge to detect trim handles
const LOOP_CORNER_SIZE: f32 = 12.0; // Size of loop corner hotzone at top-right of clip
const MIN_CLIP_WIDTH_PX: f32 = 8.0; // Minimum visible width for very short clips (e.g. groups)
/// Calculate vertical bounds for a clip instance within a layer row.
/// For vector layers with multiple clip instances, stacks them vertically.
/// Returns (y_min, y_max) relative to the layer top.
fn clip_instance_y_bounds(
layer: &AnyLayer,
clip_index: usize,
clip_count: usize,
) -> (f32, f32) {
if matches!(layer, AnyLayer::Vector(_)) && clip_count > 1 {
let usable_height = LAYER_HEIGHT - 20.0; // 10px padding top/bottom
let row_height = (usable_height / clip_count as f32).min(20.0);
let top = 10.0 + clip_index as f32 * row_height;
(top, top + row_height - 1.0)
} else {
(10.0, LAYER_HEIGHT - 10.0)
}
}
/// Get the effective clip duration for a clip instance on a given layer.
/// For groups on vector layers, the duration spans all consecutive keyframes
/// where the group is present. For regular clips, returns the clip's internal duration.
fn effective_clip_duration(
document: &lightningbeam_core::document::Document,
layer: &AnyLayer,
clip_instance: &ClipInstance,
) -> Option<f64> {
match layer {
AnyLayer::Vector(vl) => {
let vc = document.get_vector_clip(&clip_instance.clip_id)?;
if vc.is_group {
let frame_duration = 1.0 / document.framerate;
let end = vl.group_visibility_end(&clip_instance.id, clip_instance.timeline_start, frame_duration);
Some((end - clip_instance.timeline_start).max(0.0))
} else {
Some(vc.duration)
}
}
AnyLayer::Audio(_) => document.get_audio_clip(&clip_instance.clip_id).map(|c| c.duration),
AnyLayer::Video(_) => document.get_video_clip(&clip_instance.clip_id).map(|c| c.duration),
AnyLayer::Effect(_) => Some(lightningbeam_core::effect::EFFECT_DURATION),
}
}
/// Type of clip drag operation
#[derive(Debug, Clone, Copy, PartialEq)]
@@ -304,7 +348,6 @@ impl TimelinePane {
return None;
}
let hover_time = self.x_to_time(pointer_pos.x - content_rect.min.x);
let relative_y = pointer_pos.y - header_rect.min.y + self.viewport_scroll_y;
let hovered_layer_index = (relative_y / LAYER_HEIGHT) as usize;
@@ -324,34 +367,29 @@ impl TimelinePane {
};
// Check each clip instance
for clip_instance in clip_instances {
let clip_duration = match layer {
lightningbeam_core::layer::AnyLayer::Vector(_) => {
document.get_vector_clip(&clip_instance.clip_id).map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Audio(_) => {
document.get_audio_clip(&clip_instance.clip_id).map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Video(_) => {
document.get_video_clip(&clip_instance.clip_id).map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Effect(_) => {
Some(lightningbeam_core::effect::EFFECT_DURATION)
}
}?;
let clip_count = clip_instances.len();
for (ci_idx, clip_instance) in clip_instances.iter().enumerate() {
let clip_duration = effective_clip_duration(document, layer, clip_instance)?;
let instance_start = clip_instance.effective_start();
let instance_duration = clip_instance.total_duration(clip_duration);
let instance_end = instance_start + instance_duration;
if hover_time >= instance_start && hover_time <= instance_end {
let start_x = self.time_to_x(instance_start);
let end_x = self.time_to_x(instance_end);
let mouse_x = pointer_pos.x - content_rect.min.x;
let start_x = self.time_to_x(instance_start);
let end_x = self.time_to_x(instance_end).max(start_x + MIN_CLIP_WIDTH_PX);
let mouse_x = pointer_pos.x - content_rect.min.x;
if mouse_x >= start_x && mouse_x <= end_x {
// Check vertical bounds for stacked vector layer clips
let layer_top = header_rect.min.y + (hovered_layer_index as f32 * LAYER_HEIGHT) - self.viewport_scroll_y;
let (cy_min, cy_max) = clip_instance_y_bounds(layer, ci_idx, clip_count);
let mouse_rel_y = pointer_pos.y - layer_top;
if mouse_rel_y < cy_min || mouse_rel_y > cy_max {
continue;
}
// Determine drag type based on edge proximity (check both sides of edge)
let is_audio_layer = matches!(layer, lightningbeam_core::layer::AnyLayer::Audio(_));
let layer_top = header_rect.min.y + (hovered_layer_index as f32 * LAYER_HEIGHT) - self.viewport_scroll_y;
let mouse_in_top_corner = pointer_pos.y < layer_top + LOOP_CORNER_SIZE;
let is_looping = clip_instance.timeline_duration.is_some() || clip_instance.loop_before.is_some();
@@ -1047,25 +1085,10 @@ impl TimelinePane {
None
};
for clip_instance in clip_instances {
let clip_instance_count = clip_instances.len();
for (clip_instance_index, clip_instance) in clip_instances.iter().enumerate() {
// Get the clip to determine duration
let clip_duration = match layer {
lightningbeam_core::layer::AnyLayer::Vector(_) => {
document.get_vector_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Audio(_) => {
document.get_audio_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Video(_) => {
document.get_video_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Effect(_) => {
Some(lightningbeam_core::effect::EFFECT_DURATION)
}
};
let clip_duration = effective_clip_duration(document, layer, clip_instance);
if let Some(clip_duration) = clip_duration {
// Calculate effective duration accounting for trimming
@@ -1245,7 +1268,7 @@ impl TimelinePane {
let instance_end = instance_start + instance_duration;
let start_x = self.time_to_x(instance_start);
let end_x = self.time_to_x(instance_end);
let end_x = self.time_to_x(instance_end).max(start_x + MIN_CLIP_WIDTH_PX);
// Only draw if any part is visible in viewport
if end_x >= 0.0 && start_x <= rect.width() {
@@ -1280,9 +1303,11 @@ impl TimelinePane {
),
};
let (cy_min, cy_max) = clip_instance_y_bounds(layer, clip_instance_index, clip_instance_count);
let clip_rect = egui::Rect::from_min_max(
egui::pos2(rect.min.x + visible_start_x, y + 10.0),
egui::pos2(rect.min.x + visible_end_x, y + LAYER_HEIGHT - 10.0),
egui::pos2(rect.min.x + visible_start_x, y + cy_min),
egui::pos2(rect.min.x + visible_end_x, y + cy_max),
);
// Draw the clip instance background(s)
@@ -1639,6 +1664,31 @@ impl TimelinePane {
}
}
// Draw shape keyframe markers for vector layers
if let lightningbeam_core::layer::AnyLayer::Vector(vl) = layer {
for kf in &vl.keyframes {
let x = self.time_to_x(kf.time);
if x >= 0.0 && x <= rect.width() {
let cx = rect.min.x + x;
let cy = y + LAYER_HEIGHT - 8.0;
let size = 5.0;
// Draw diamond shape
let diamond = [
egui::pos2(cx, cy - size),
egui::pos2(cx + size, cy),
egui::pos2(cx, cy + size),
egui::pos2(cx - size, cy),
];
let color = egui::Color32::from_rgb(255, 220, 100);
painter.add(egui::Shape::convex_polygon(
diamond.to_vec(),
color,
egui::Stroke::new(1.0, egui::Color32::from_rgb(180, 150, 50)),
));
}
}
}
// Separator line at bottom
painter.line_segment(
[
@@ -1709,8 +1759,6 @@ impl TimelinePane {
if pos.y >= header_rect.min.y && pos.x >= content_rect.min.x {
let relative_y = pos.y - header_rect.min.y + self.viewport_scroll_y;
let clicked_layer_index = (relative_y / LAYER_HEIGHT) as usize;
let click_time = self.x_to_time(pos.x - content_rect.min.x);
// Get the layer at this index (accounting for reversed display order)
if clicked_layer_index < layer_count {
let layers: Vec<_> = document.root.children.iter().rev().collect();
@@ -1726,33 +1774,25 @@ impl TimelinePane {
};
// Check if click is within any clip instance
for clip_instance in clip_instances {
// Get the clip to determine duration
let clip_duration = match layer {
lightningbeam_core::layer::AnyLayer::Vector(_) => {
document.get_vector_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Audio(_) => {
document.get_audio_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Video(_) => {
document.get_video_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Effect(_) => {
Some(lightningbeam_core::effect::EFFECT_DURATION)
}
};
let click_clip_count = clip_instances.len();
let click_layer_top = pos.y - (relative_y % LAYER_HEIGHT);
for (ci_idx, clip_instance) in clip_instances.iter().enumerate() {
let clip_duration = effective_clip_duration(document, layer, clip_instance);
if let Some(clip_duration) = clip_duration {
let instance_duration = clip_instance.total_duration(clip_duration);
let instance_start = clip_instance.effective_start();
let instance_end = instance_start + instance_duration;
// Check if click is within this clip instance's time range
if click_time >= instance_start && click_time <= instance_end {
// Check if click is within this clip instance's pixel range and vertical bounds
let ci_start_x = self.time_to_x(instance_start);
let ci_end_x = self.time_to_x(instance_end).max(ci_start_x + MIN_CLIP_WIDTH_PX);
let click_x = pos.x - content_rect.min.x;
let (cy_min, cy_max) = clip_instance_y_bounds(layer, ci_idx, click_clip_count);
let click_rel_y = pos.y - click_layer_top;
if click_x >= ci_start_x && click_x <= ci_end_x
&& click_rel_y >= cy_min && click_rel_y <= cy_max
{
// Found a clicked clip instance!
if shift_held {
// Shift+click: add to selection
@@ -1919,27 +1959,7 @@ impl TimelinePane {
// Find selected clip instances in this layer
for clip_instance in clip_instances {
if selection.contains_clip_instance(&clip_instance.id) {
// Get clip duration to validate trim bounds
let clip_duration = match layer {
lightningbeam_core::layer::AnyLayer::Vector(_) => {
document
.get_vector_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Audio(_) => {
document
.get_audio_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Video(_) => {
document
.get_video_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Effect(_) => {
Some(lightningbeam_core::effect::EFFECT_DURATION)
}
};
let clip_duration = effective_clip_duration(document, layer, clip_instance);
if let Some(clip_duration) = clip_duration {
match drag_type {
@@ -2528,24 +2548,7 @@ impl PaneRenderer for TimelinePane {
};
for clip_instance in clip_instances {
// Get clip duration
let clip_duration = match layer {
lightningbeam_core::layer::AnyLayer::Vector(_) => {
document.get_vector_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Audio(_) => {
document.get_audio_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Video(_) => {
document.get_video_clip(&clip_instance.clip_id)
.map(|c| c.duration)
}
lightningbeam_core::layer::AnyLayer::Effect(_) => {
Some(lightningbeam_core::effect::EFFECT_DURATION)
}
};
let clip_duration = effective_clip_duration(document, layer, clip_instance);
if let Some(clip_duration) = clip_duration {
let instance_duration = clip_instance.effective_duration(clip_duration);