Add asset pane
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
//! Add clip instance action
|
||||
//!
|
||||
//! Handles adding a clip instance to a layer.
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that adds a clip instance to a layer
|
||||
pub struct AddClipInstanceAction {
|
||||
/// The target layer ID
|
||||
layer_id: Uuid,
|
||||
|
||||
/// The clip instance to add
|
||||
clip_instance: ClipInstance,
|
||||
|
||||
/// Whether the action has been executed (for rollback)
|
||||
executed: bool,
|
||||
}
|
||||
|
||||
impl AddClipInstanceAction {
|
||||
/// Create a new add clip instance action
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `layer_id` - The ID of the layer to add the clip instance to
|
||||
/// * `clip_instance` - The clip instance to add
|
||||
pub fn new(layer_id: Uuid, clip_instance: ClipInstance) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
clip_instance,
|
||||
executed: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the ID of the clip instance that will be/was added
|
||||
pub fn clip_instance_id(&self) -> Uuid {
|
||||
self.clip_instance.id
|
||||
}
|
||||
|
||||
/// Get the layer ID this action targets
|
||||
pub fn layer_id(&self) -> Uuid {
|
||||
self.layer_id
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for AddClipInstanceAction {
|
||||
fn execute(&mut self, document: &mut Document) {
|
||||
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
|
||||
match layer {
|
||||
AnyLayer::Vector(vector_layer) => {
|
||||
vector_layer.clip_instances.push(self.clip_instance.clone());
|
||||
}
|
||||
AnyLayer::Audio(audio_layer) => {
|
||||
audio_layer.clip_instances.push(self.clip_instance.clone());
|
||||
}
|
||||
AnyLayer::Video(video_layer) => {
|
||||
video_layer.clip_instances.push(self.clip_instance.clone());
|
||||
}
|
||||
}
|
||||
self.executed = true;
|
||||
}
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) {
|
||||
if !self.executed {
|
||||
return;
|
||||
}
|
||||
|
||||
let instance_id = self.clip_instance.id;
|
||||
|
||||
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
|
||||
match layer {
|
||||
AnyLayer::Vector(vector_layer) => {
|
||||
vector_layer
|
||||
.clip_instances
|
||||
.retain(|ci| ci.id != instance_id);
|
||||
}
|
||||
AnyLayer::Audio(audio_layer) => {
|
||||
audio_layer
|
||||
.clip_instances
|
||||
.retain(|ci| ci.id != instance_id);
|
||||
}
|
||||
AnyLayer::Video(video_layer) => {
|
||||
video_layer
|
||||
.clip_instances
|
||||
.retain(|ci| ci.id != instance_id);
|
||||
}
|
||||
}
|
||||
self.executed = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
"Add clip instance".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::layer::VectorLayer;
|
||||
|
||||
#[test]
|
||||
fn test_add_clip_instance_to_vector_layer() {
|
||||
let mut document = Document::new("Test");
|
||||
|
||||
// Add a layer
|
||||
let layer = VectorLayer::new("Test Layer");
|
||||
let layer_id = layer.layer.id;
|
||||
document.root_mut().add_child(AnyLayer::Vector(layer));
|
||||
|
||||
// Create a clip instance (using a fake clip_id since we're just testing the action)
|
||||
let clip_id = Uuid::new_v4();
|
||||
let clip_instance = ClipInstance::new(clip_id);
|
||||
let instance_id = clip_instance.id;
|
||||
|
||||
// Execute action
|
||||
let mut action = AddClipInstanceAction::new(layer_id, clip_instance);
|
||||
action.execute(&mut document);
|
||||
|
||||
// Verify clip instance was added
|
||||
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer(&layer_id) {
|
||||
assert_eq!(vector_layer.clip_instances.len(), 1);
|
||||
assert_eq!(vector_layer.clip_instances[0].id, instance_id);
|
||||
} else {
|
||||
panic!("Layer not found");
|
||||
}
|
||||
|
||||
// Rollback
|
||||
action.rollback(&mut document);
|
||||
|
||||
// Verify clip instance was removed
|
||||
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer(&layer_id) {
|
||||
assert_eq!(vector_layer.clip_instances.len(), 0);
|
||||
} else {
|
||||
panic!("Layer not found");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_clip_instance_description() {
|
||||
let action = AddClipInstanceAction::new(Uuid::new_v4(), ClipInstance::new(Uuid::new_v4()));
|
||||
assert_eq!(action.description(), "Add clip instance");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
//! This module contains all the concrete action types that can be executed
|
||||
//! through the action system.
|
||||
|
||||
pub mod add_clip_instance;
|
||||
pub mod add_layer;
|
||||
pub mod add_shape;
|
||||
pub mod move_clip_instances;
|
||||
@@ -13,6 +14,7 @@ pub mod transform_clip_instances;
|
||||
pub mod transform_objects;
|
||||
pub mod trim_clip_instances;
|
||||
|
||||
pub use add_clip_instance::AddClipInstanceAction;
|
||||
pub use add_layer::AddLayerAction;
|
||||
pub use add_shape::AddShapeAction;
|
||||
pub use move_clip_instances::MoveClipInstancesAction;
|
||||
|
||||
@@ -15,6 +15,7 @@ use crate::layer::AnyLayer;
|
||||
use crate::layer_tree::LayerTree;
|
||||
use crate::object::Transform;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
use vello::kurbo::{Rect, Affine, Shape as KurboShape};
|
||||
|
||||
@@ -158,6 +159,71 @@ impl VectorClip {
|
||||
}
|
||||
}
|
||||
|
||||
/// Image asset for static images
|
||||
///
|
||||
/// Images can be used as fill textures for shapes or (in the future)
|
||||
/// added to video tracks as still frames. Unlike clips, images don't
|
||||
/// have a duration or timeline properties.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct ImageAsset {
|
||||
/// Unique identifier
|
||||
pub id: Uuid,
|
||||
|
||||
/// Asset name (usually derived from filename)
|
||||
pub name: String,
|
||||
|
||||
/// Original file path
|
||||
pub path: PathBuf,
|
||||
|
||||
/// Image width in pixels
|
||||
pub width: u32,
|
||||
|
||||
/// Image height in pixels
|
||||
pub height: u32,
|
||||
|
||||
/// Embedded image data (for project portability)
|
||||
/// If None, the image will be loaded from path when needed
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub data: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl ImageAsset {
|
||||
/// Create a new image asset
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
path: impl Into<PathBuf>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
name: name.into(),
|
||||
path: path.into(),
|
||||
width,
|
||||
height,
|
||||
data: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create with embedded data
|
||||
pub fn with_data(
|
||||
name: impl Into<String>,
|
||||
path: impl Into<PathBuf>,
|
||||
width: u32,
|
||||
height: u32,
|
||||
data: Vec<u8>,
|
||||
) -> Self {
|
||||
Self {
|
||||
id: Uuid::new_v4(),
|
||||
name: name.into(),
|
||||
path: path.into(),
|
||||
width,
|
||||
height,
|
||||
data: Some(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Video clip referencing an external video file
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct VideoClip {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
//! The Document represents a complete animation project with settings
|
||||
//! and a root graphics object containing the scene graph.
|
||||
|
||||
use crate::clip::{AudioClip, VideoClip, VectorClip};
|
||||
use crate::clip::{AudioClip, ImageAsset, VideoClip, VectorClip};
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::shape::ShapeColor;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -103,6 +103,9 @@ pub struct Document {
|
||||
/// Audio clip library - sampled audio and MIDI clips
|
||||
pub audio_clips: HashMap<Uuid, AudioClip>,
|
||||
|
||||
/// Image asset library - static images for fill textures
|
||||
pub image_assets: HashMap<Uuid, ImageAsset>,
|
||||
|
||||
/// Current playback time in seconds
|
||||
#[serde(skip)]
|
||||
pub current_time: f64,
|
||||
@@ -122,6 +125,7 @@ impl Default for Document {
|
||||
vector_clips: HashMap::new(),
|
||||
video_clips: HashMap::new(),
|
||||
audio_clips: HashMap::new(),
|
||||
image_assets: HashMap::new(),
|
||||
current_time: 0.0,
|
||||
}
|
||||
}
|
||||
@@ -272,6 +276,30 @@ impl Document {
|
||||
pub fn remove_audio_clip(&mut self, id: &Uuid) -> Option<AudioClip> {
|
||||
self.audio_clips.remove(id)
|
||||
}
|
||||
|
||||
// === IMAGE ASSET METHODS ===
|
||||
|
||||
/// Add an image asset to the library
|
||||
pub fn add_image_asset(&mut self, asset: ImageAsset) -> Uuid {
|
||||
let id = asset.id;
|
||||
self.image_assets.insert(id, asset);
|
||||
id
|
||||
}
|
||||
|
||||
/// Get an image asset by ID
|
||||
pub fn get_image_asset(&self, id: &Uuid) -> Option<&ImageAsset> {
|
||||
self.image_assets.get(id)
|
||||
}
|
||||
|
||||
/// Get a mutable image asset by ID
|
||||
pub fn get_image_asset_mut(&mut self, id: &Uuid) -> Option<&mut ImageAsset> {
|
||||
self.image_assets.get_mut(id)
|
||||
}
|
||||
|
||||
/// Remove an image asset from the library
|
||||
pub fn remove_image_asset(&mut self, id: &Uuid) -> Option<ImageAsset> {
|
||||
self.image_assets.remove(id)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
//! File type detection and supported extension constants
|
||||
//!
|
||||
//! This module provides shared file extension constants that can be used
|
||||
//! across the codebase for file dialogs, import detection, etc.
|
||||
|
||||
/// Supported image file extensions
|
||||
pub const IMAGE_EXTENSIONS: &[&str] = &["png", "gif", "avif", "jpg", "jpeg"];
|
||||
|
||||
/// Supported audio file extensions
|
||||
pub const AUDIO_EXTENSIONS: &[&str] = &["mp3", "wav", "aiff", "ogg", "flac"];
|
||||
|
||||
/// Supported video file extensions
|
||||
pub const VIDEO_EXTENSIONS: &[&str] = &["mp4", "mov", "avi", "mkv", "webm", "m4v"];
|
||||
|
||||
/// Supported MIDI file extensions
|
||||
pub const MIDI_EXTENSIONS: &[&str] = &["mid", "midi"];
|
||||
|
||||
// Note: SVG import deferred to future task
|
||||
// Note: .beam project files handled separately in file save/load feature
|
||||
|
||||
/// File type categories for import routing
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FileType {
|
||||
Image,
|
||||
Audio,
|
||||
Video,
|
||||
Midi,
|
||||
}
|
||||
|
||||
/// Detect file type from extension string
|
||||
///
|
||||
/// Returns `None` if the extension is not recognized.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use lightningbeam_core::file_types::get_file_type;
|
||||
///
|
||||
/// assert_eq!(get_file_type("png"), Some(lightningbeam_core::file_types::FileType::Image));
|
||||
/// assert_eq!(get_file_type("MP3"), Some(lightningbeam_core::file_types::FileType::Audio));
|
||||
/// assert_eq!(get_file_type("unknown"), None);
|
||||
/// ```
|
||||
pub fn get_file_type(extension: &str) -> Option<FileType> {
|
||||
let ext = extension.to_lowercase();
|
||||
if IMAGE_EXTENSIONS.contains(&ext.as_str()) {
|
||||
return Some(FileType::Image);
|
||||
}
|
||||
if AUDIO_EXTENSIONS.contains(&ext.as_str()) {
|
||||
return Some(FileType::Audio);
|
||||
}
|
||||
if VIDEO_EXTENSIONS.contains(&ext.as_str()) {
|
||||
return Some(FileType::Video);
|
||||
}
|
||||
if MIDI_EXTENSIONS.contains(&ext.as_str()) {
|
||||
return Some(FileType::Midi);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Get all supported extensions as a single flat list
|
||||
///
|
||||
/// Useful for "All Supported Files" filter in file dialogs.
|
||||
pub fn all_supported_extensions() -> Vec<&'static str> {
|
||||
let mut all = Vec::new();
|
||||
all.extend_from_slice(IMAGE_EXTENSIONS);
|
||||
all.extend_from_slice(AUDIO_EXTENSIONS);
|
||||
all.extend_from_slice(VIDEO_EXTENSIONS);
|
||||
all.extend_from_slice(MIDI_EXTENSIONS);
|
||||
all
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_file_type() {
|
||||
assert_eq!(get_file_type("png"), Some(FileType::Image));
|
||||
assert_eq!(get_file_type("PNG"), Some(FileType::Image));
|
||||
assert_eq!(get_file_type("jpg"), Some(FileType::Image));
|
||||
assert_eq!(get_file_type("jpeg"), Some(FileType::Image));
|
||||
|
||||
assert_eq!(get_file_type("mp3"), Some(FileType::Audio));
|
||||
assert_eq!(get_file_type("wav"), Some(FileType::Audio));
|
||||
assert_eq!(get_file_type("flac"), Some(FileType::Audio));
|
||||
|
||||
assert_eq!(get_file_type("mp4"), Some(FileType::Video));
|
||||
assert_eq!(get_file_type("webm"), Some(FileType::Video));
|
||||
|
||||
assert_eq!(get_file_type("mid"), Some(FileType::Midi));
|
||||
assert_eq!(get_file_type("midi"), Some(FileType::Midi));
|
||||
|
||||
assert_eq!(get_file_type("unknown"), None);
|
||||
assert_eq!(get_file_type("svg"), None); // SVG deferred
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_supported_extensions() {
|
||||
let all = all_supported_extensions();
|
||||
assert!(all.contains(&"png"));
|
||||
assert!(all.contains(&"mp3"));
|
||||
assert!(all.contains(&"mp4"));
|
||||
assert!(all.contains(&"mid"));
|
||||
}
|
||||
}
|
||||
@@ -324,6 +324,21 @@ impl VectorLayer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio layer subtype - distinguishes sampled audio from MIDI
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum AudioLayerType {
|
||||
/// Sampled audio (WAV, MP3, etc.)
|
||||
Sampled,
|
||||
/// MIDI sequence
|
||||
Midi,
|
||||
}
|
||||
|
||||
impl Default for AudioLayerType {
|
||||
fn default() -> Self {
|
||||
AudioLayerType::Sampled
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio layer containing audio clips
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct AudioLayer {
|
||||
@@ -333,6 +348,10 @@ pub struct AudioLayer {
|
||||
/// Clip instances (references to audio clips)
|
||||
/// AudioLayer can contain instances of AudioClips (sampled or MIDI)
|
||||
pub clip_instances: Vec<ClipInstance>,
|
||||
|
||||
/// Audio layer subtype (sampled vs MIDI)
|
||||
#[serde(default)]
|
||||
pub audio_layer_type: AudioLayerType,
|
||||
}
|
||||
|
||||
impl LayerTrait for AudioLayer {
|
||||
@@ -406,11 +425,26 @@ impl LayerTrait for AudioLayer {
|
||||
}
|
||||
|
||||
impl AudioLayer {
|
||||
/// Create a new audio layer
|
||||
/// Create a new sampled audio layer
|
||||
pub fn new(name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
layer: Layer::new(LayerType::Audio, name),
|
||||
clip_instances: Vec::new(),
|
||||
audio_layer_type: AudioLayerType::Sampled,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new sampled audio layer (explicit)
|
||||
pub fn new_sampled(name: impl Into<String>) -> Self {
|
||||
Self::new(name)
|
||||
}
|
||||
|
||||
/// Create a new MIDI layer
|
||||
pub fn new_midi(name: impl Into<String>) -> Self {
|
||||
Self {
|
||||
layer: Layer::new(LayerType::Audio, name),
|
||||
clip_instances: Vec::new(),
|
||||
audio_layer_type: AudioLayerType::Midi,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,3 +28,4 @@ pub mod gap_handling;
|
||||
pub mod intersection_graph;
|
||||
pub mod segment_builder;
|
||||
pub mod planar_graph;
|
||||
pub mod file_types;
|
||||
|
||||
@@ -29,6 +29,8 @@ pub enum PaneType {
|
||||
NodeEditor,
|
||||
/// Preset/asset browser
|
||||
PresetBrowser,
|
||||
/// Asset library for browsing clips
|
||||
AssetLibrary,
|
||||
}
|
||||
|
||||
impl PaneType {
|
||||
@@ -43,6 +45,7 @@ impl PaneType {
|
||||
PaneType::PianoRoll => "Piano Roll",
|
||||
PaneType::NodeEditor => "Node Editor",
|
||||
PaneType::PresetBrowser => "Preset Browser",
|
||||
PaneType::AssetLibrary => "Asset Library",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +62,7 @@ impl PaneType {
|
||||
PaneType::PianoRoll => "piano-roll.svg",
|
||||
PaneType::NodeEditor => "node-editor.svg",
|
||||
PaneType::PresetBrowser => "stage.svg", // TODO: needs own icon
|
||||
PaneType::AssetLibrary => "stage.svg", // TODO: needs own icon
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +78,7 @@ impl PaneType {
|
||||
"pianoroll" => Some(PaneType::PianoRoll),
|
||||
"nodeeditor" => Some(PaneType::NodeEditor),
|
||||
"presetbrowser" => Some(PaneType::PresetBrowser),
|
||||
"assetlibrary" => Some(PaneType::AssetLibrary),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -89,6 +94,7 @@ impl PaneType {
|
||||
PaneType::NodeEditor,
|
||||
PaneType::PianoRoll,
|
||||
PaneType::PresetBrowser,
|
||||
PaneType::AssetLibrary,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -103,6 +109,7 @@ impl PaneType {
|
||||
PaneType::PianoRoll => "pianoRoll",
|
||||
PaneType::NodeEditor => "nodeEditor",
|
||||
PaneType::PresetBrowser => "presetBrowser",
|
||||
PaneType::AssetLibrary => "assetLibrary",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,28 +3,93 @@
|
||||
//! Renders documents to Vello scenes for GPU-accelerated display.
|
||||
|
||||
use crate::animation::TransformProperty;
|
||||
use crate::clip::ImageAsset;
|
||||
use crate::document::Document;
|
||||
use crate::layer::{AnyLayer, LayerTrait, VectorLayer};
|
||||
use crate::object::ShapeInstance;
|
||||
use kurbo::{Affine, Shape};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
use vello::kurbo::Rect;
|
||||
use vello::peniko::Fill;
|
||||
use vello::peniko::{Blob, Fill, Image, ImageFormat};
|
||||
use vello::Scene;
|
||||
|
||||
/// Cache for decoded image data to avoid re-decoding every frame
|
||||
pub struct ImageCache {
|
||||
cache: HashMap<Uuid, Arc<Image>>,
|
||||
}
|
||||
|
||||
impl ImageCache {
|
||||
/// Create a new empty image cache
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
cache: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get or decode an image, caching the result
|
||||
pub fn get_or_decode(&mut self, asset: &ImageAsset) -> Option<Arc<Image>> {
|
||||
if let Some(cached) = self.cache.get(&asset.id) {
|
||||
return Some(Arc::clone(cached));
|
||||
}
|
||||
|
||||
// Decode and cache
|
||||
let image = decode_image_asset(asset)?;
|
||||
let arc_image = Arc::new(image);
|
||||
self.cache.insert(asset.id, Arc::clone(&arc_image));
|
||||
Some(arc_image)
|
||||
}
|
||||
|
||||
/// Clear cache entry when an image asset is deleted or modified
|
||||
pub fn invalidate(&mut self, id: &Uuid) {
|
||||
self.cache.remove(id);
|
||||
}
|
||||
|
||||
/// Clear all cached images
|
||||
pub fn clear(&mut self) {
|
||||
self.cache.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ImageCache {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// Decode an image asset to peniko Image
|
||||
fn decode_image_asset(asset: &ImageAsset) -> Option<Image> {
|
||||
// Get the raw file data
|
||||
let data = asset.data.as_ref()?;
|
||||
|
||||
// Decode using the image crate
|
||||
let img = image::load_from_memory(data).ok()?;
|
||||
let rgba = img.to_rgba8();
|
||||
|
||||
// Create peniko Image
|
||||
Some(Image::new(
|
||||
Blob::from(rgba.into_raw()),
|
||||
ImageFormat::Rgba8,
|
||||
asset.width,
|
||||
asset.height,
|
||||
))
|
||||
}
|
||||
|
||||
/// Render a document to a Vello scene
|
||||
pub fn render_document(document: &Document, scene: &mut Scene) {
|
||||
render_document_with_transform(document, scene, Affine::IDENTITY);
|
||||
pub fn render_document(document: &Document, scene: &mut Scene, image_cache: &mut ImageCache) {
|
||||
render_document_with_transform(document, scene, Affine::IDENTITY, image_cache);
|
||||
}
|
||||
|
||||
/// Render a document to a Vello scene with a base transform
|
||||
/// The base transform is composed with all object transforms (useful for camera zoom/pan)
|
||||
pub fn render_document_with_transform(document: &Document, scene: &mut Scene, base_transform: Affine) {
|
||||
pub fn render_document_with_transform(document: &Document, scene: &mut Scene, base_transform: Affine, image_cache: &mut ImageCache) {
|
||||
// 1. Draw background
|
||||
render_background(document, scene, base_transform);
|
||||
|
||||
// 2. Recursively render the root graphics object at current time
|
||||
let time = document.current_time;
|
||||
render_graphics_object(document, time, scene, base_transform);
|
||||
render_graphics_object(document, time, scene, base_transform, image_cache);
|
||||
}
|
||||
|
||||
/// Draw the document background
|
||||
@@ -44,7 +109,7 @@ fn render_background(document: &Document, scene: &mut Scene, base_transform: Aff
|
||||
}
|
||||
|
||||
/// Recursively render the root graphics object and its children
|
||||
fn render_graphics_object(document: &Document, time: f64, scene: &mut Scene, base_transform: Affine) {
|
||||
fn render_graphics_object(document: &Document, time: f64, scene: &mut Scene, base_transform: Affine, image_cache: &mut ImageCache) {
|
||||
// Check if any layers are soloed
|
||||
let any_soloed = document.visible_layers().any(|layer| layer.soloed());
|
||||
|
||||
@@ -56,19 +121,19 @@ fn render_graphics_object(document: &Document, time: f64, scene: &mut Scene, bas
|
||||
if any_soloed {
|
||||
// Only render soloed layers when solo is active
|
||||
if layer.soloed() {
|
||||
render_layer(document, time, layer, scene, base_transform, 1.0);
|
||||
render_layer(document, time, layer, scene, base_transform, 1.0, image_cache);
|
||||
}
|
||||
} else {
|
||||
// Render all visible layers when no solo is active
|
||||
render_layer(document, time, layer, scene, base_transform, 1.0);
|
||||
render_layer(document, time, layer, scene, base_transform, 1.0, image_cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a single layer
|
||||
fn render_layer(document: &Document, time: f64, layer: &AnyLayer, scene: &mut Scene, base_transform: Affine, parent_opacity: f64) {
|
||||
fn render_layer(document: &Document, time: f64, layer: &AnyLayer, scene: &mut Scene, base_transform: Affine, parent_opacity: f64, image_cache: &mut ImageCache) {
|
||||
match layer {
|
||||
AnyLayer::Vector(vector_layer) => render_vector_layer(document, time, vector_layer, scene, base_transform, parent_opacity),
|
||||
AnyLayer::Vector(vector_layer) => render_vector_layer(document, time, vector_layer, scene, base_transform, parent_opacity, image_cache),
|
||||
AnyLayer::Audio(_) => {
|
||||
// Audio layers don't render visually
|
||||
}
|
||||
@@ -87,6 +152,7 @@ fn render_clip_instance(
|
||||
scene: &mut Scene,
|
||||
base_transform: Affine,
|
||||
animation_data: &crate::animation::AnimationData,
|
||||
image_cache: &mut ImageCache,
|
||||
) {
|
||||
// Try to find the clip in the document's clip libraries
|
||||
// For now, only handle VectorClips (VideoClip and AudioClip rendering not yet implemented)
|
||||
@@ -214,19 +280,19 @@ fn render_clip_instance(
|
||||
if !layer_node.data.visible() {
|
||||
continue;
|
||||
}
|
||||
render_layer(document, clip_time, &layer_node.data, scene, instance_transform, clip_opacity);
|
||||
render_layer(document, clip_time, &layer_node.data, scene, instance_transform, clip_opacity, image_cache);
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a vector layer with all its clip instances and shape instances
|
||||
fn render_vector_layer(document: &Document, time: f64, layer: &VectorLayer, scene: &mut Scene, base_transform: Affine, parent_opacity: f64) {
|
||||
fn render_vector_layer(document: &Document, time: f64, layer: &VectorLayer, scene: &mut Scene, base_transform: Affine, parent_opacity: f64, image_cache: &mut ImageCache) {
|
||||
|
||||
// Cascade opacity: parent_opacity × layer.opacity
|
||||
let layer_opacity = parent_opacity * layer.layer.opacity;
|
||||
|
||||
// Render clip instances first (they appear under shape instances)
|
||||
for clip_instance in &layer.clip_instances {
|
||||
render_clip_instance(document, time, clip_instance, layer_opacity, scene, base_transform, &layer.layer.animation_data);
|
||||
render_clip_instance(document, time, clip_instance, layer_opacity, scene, base_transform, &layer.layer.animation_data, image_cache);
|
||||
}
|
||||
|
||||
// Render each shape instance in the layer
|
||||
@@ -384,29 +450,51 @@ fn render_vector_layer(document: &Document, time: f64, layer: &VectorLayer, scen
|
||||
// layer_opacity already includes parent_opacity from render_vector_layer
|
||||
let final_opacity = (layer_opacity * opacity) as f32;
|
||||
|
||||
// Render fill if present
|
||||
if let Some(fill_color) = &shape.fill_color {
|
||||
// Apply opacity to color
|
||||
let alpha = ((fill_color.a as f32 / 255.0) * final_opacity * 255.0) as u8;
|
||||
let adjusted_color = crate::shape::ShapeColor::rgba(
|
||||
fill_color.r,
|
||||
fill_color.g,
|
||||
fill_color.b,
|
||||
alpha,
|
||||
);
|
||||
// Determine fill rule
|
||||
let fill_rule = match shape.fill_rule {
|
||||
crate::shape::FillRule::NonZero => Fill::NonZero,
|
||||
crate::shape::FillRule::EvenOdd => Fill::EvenOdd,
|
||||
};
|
||||
|
||||
let fill_rule = match shape.fill_rule {
|
||||
crate::shape::FillRule::NonZero => Fill::NonZero,
|
||||
crate::shape::FillRule::EvenOdd => Fill::EvenOdd,
|
||||
};
|
||||
// Render fill - prefer image fill over color fill
|
||||
let mut filled = false;
|
||||
|
||||
scene.fill(
|
||||
fill_rule,
|
||||
affine,
|
||||
adjusted_color.to_peniko(),
|
||||
None,
|
||||
&path,
|
||||
);
|
||||
// Check for image fill first
|
||||
if let Some(image_asset_id) = shape.image_fill {
|
||||
if let Some(image_asset) = document.get_image_asset(&image_asset_id) {
|
||||
if let Some(image) = image_cache.get_or_decode(image_asset) {
|
||||
// Apply opacity to image (clone is cheap - Image uses Arc<Blob> internally)
|
||||
let image_with_alpha = (*image).clone().with_alpha(final_opacity);
|
||||
|
||||
// The image is rendered as a fill for the shape path
|
||||
// Since the shape path is a rectangle matching the image dimensions,
|
||||
// the image should fill the shape perfectly
|
||||
scene.fill(fill_rule, affine, &image_with_alpha, None, &path);
|
||||
filled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to color fill if no image fill (or image failed to load)
|
||||
if !filled {
|
||||
if let Some(fill_color) = &shape.fill_color {
|
||||
// Apply opacity to color
|
||||
let alpha = ((fill_color.a as f32 / 255.0) * final_opacity * 255.0) as u8;
|
||||
let adjusted_color = crate::shape::ShapeColor::rgba(
|
||||
fill_color.r,
|
||||
fill_color.g,
|
||||
fill_color.b,
|
||||
alpha,
|
||||
);
|
||||
|
||||
scene.fill(
|
||||
fill_rule,
|
||||
affine,
|
||||
adjusted_color.to_peniko(),
|
||||
None,
|
||||
&path,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Render stroke if present
|
||||
@@ -445,8 +533,9 @@ mod tests {
|
||||
fn test_render_empty_document() {
|
||||
let doc = Document::new("Test");
|
||||
let mut scene = Scene::new();
|
||||
let mut image_cache = ImageCache::new();
|
||||
|
||||
render_document(&doc, &mut scene);
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
// Should render background without errors
|
||||
}
|
||||
|
||||
@@ -472,7 +561,8 @@ mod tests {
|
||||
|
||||
// Render
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
// Should render without errors
|
||||
}
|
||||
|
||||
@@ -514,7 +604,8 @@ mod tests {
|
||||
|
||||
// Render should work without errors
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -544,7 +635,8 @@ mod tests {
|
||||
|
||||
// Render should work
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -570,14 +662,15 @@ mod tests {
|
||||
|
||||
// Render
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hidden_layer_not_rendered() {
|
||||
let mut doc = Document::new("Test");
|
||||
|
||||
let mut layer1 = VectorLayer::new("Layer 1");
|
||||
let layer1 = VectorLayer::new("Layer 1");
|
||||
let mut layer2 = VectorLayer::new("Layer 2");
|
||||
|
||||
// Hide layer 2
|
||||
@@ -591,7 +684,8 @@ mod tests {
|
||||
|
||||
// Render
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -621,7 +715,8 @@ mod tests {
|
||||
|
||||
// Render
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -659,7 +754,8 @@ mod tests {
|
||||
|
||||
// Render
|
||||
let mut scene = Scene::new();
|
||||
render_document(&doc, &mut scene);
|
||||
let mut image_cache = ImageCache::new();
|
||||
render_document(&doc, &mut scene, &mut image_cache);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -170,7 +170,7 @@ impl ShapeColor {
|
||||
|
||||
/// Convert to peniko Color
|
||||
pub fn to_peniko(&self) -> Color {
|
||||
Color::rgba8(self.r, self.g, self.b, self.a)
|
||||
Color::from_rgba8(self.r, self.g, self.b, self.a)
|
||||
}
|
||||
|
||||
/// Convert to peniko Brush
|
||||
@@ -197,11 +197,13 @@ impl Default for ShapeColor {
|
||||
|
||||
impl From<Color> for ShapeColor {
|
||||
fn from(color: Color) -> Self {
|
||||
// peniko 0.4 uses components array [r, g, b, a] as floats 0.0-1.0
|
||||
let components = color.components;
|
||||
Self {
|
||||
r: color.r,
|
||||
g: color.g,
|
||||
b: color.b,
|
||||
a: color.a,
|
||||
r: (components[0] * 255.0) as u8,
|
||||
g: (components[1] * 255.0) as u8,
|
||||
b: (components[2] * 255.0) as u8,
|
||||
a: (components[3] * 255.0) as u8,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,9 +218,14 @@ pub struct Shape {
|
||||
/// The shape animates between these by varying the shapeIndex property
|
||||
pub versions: Vec<ShapeVersion>,
|
||||
|
||||
/// Fill color
|
||||
/// Fill color (used when image_fill is None)
|
||||
pub fill_color: Option<ShapeColor>,
|
||||
|
||||
/// Image fill - references an ImageAsset by UUID
|
||||
/// When set, the image is rendered as the fill instead of fill_color
|
||||
#[serde(default)]
|
||||
pub image_fill: Option<Uuid>,
|
||||
|
||||
/// Fill rule
|
||||
#[serde(default)]
|
||||
pub fill_rule: FillRule,
|
||||
@@ -237,6 +244,7 @@ impl Shape {
|
||||
id: Uuid::new_v4(),
|
||||
versions: vec![ShapeVersion::new(path, 0)],
|
||||
fill_color: Some(ShapeColor::rgb(0, 0, 0)),
|
||||
image_fill: None,
|
||||
fill_rule: FillRule::NonZero,
|
||||
stroke_color: None,
|
||||
stroke_style: None,
|
||||
@@ -249,12 +257,20 @@ impl Shape {
|
||||
id,
|
||||
versions: vec![ShapeVersion::new(path, 0)],
|
||||
fill_color: Some(ShapeColor::rgb(0, 0, 0)),
|
||||
image_fill: None,
|
||||
fill_rule: FillRule::NonZero,
|
||||
stroke_color: None,
|
||||
stroke_style: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set image fill (references an ImageAsset by UUID)
|
||||
pub fn with_image_fill(mut self, image_asset_id: Uuid) -> Self {
|
||||
self.image_fill = Some(image_asset_id);
|
||||
self.fill_color = None; // Image fill takes precedence
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a new version for morphing
|
||||
pub fn add_version(&mut self, path: BezPath) -> usize {
|
||||
let index = self.versions.len();
|
||||
|
||||
Reference in New Issue
Block a user