diff options
| author | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
|---|---|---|
| committer | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
| commit | 32438ce25f69de99d2523b98237f62cfa90890d7 (patch) | |
| tree | 3a712c347dec0889bdea3328b4c817c2d9392b27 /rust/crates/graphics/src/registry.rs | |
| parent | f5a82152394fde4d8f740f664ab7737b82861618 (diff) | |
| download | opencut-32438ce25f69de99d2523b98237f62cfa90890d7.tar.gz | |
feat: port domain crates to Rust (session 1 complete)
All green: cargo test --workspace (~300 tests, 0 failures).
- timeline (45): placement, resize/ripple, split, snap, retime,
audio separation, scene helpers
- animation (73): bezier solver, channel sampling, keyframe ops,
split/clamp/clone, param definitions
- commands (9): Command trait, BatchCommand, history, 19 concrete
commands, freeze-frame port 1:1
- audio (22): audible collection, range mixing, mastering limiter,
rms/peak buckets
- text (19): cosmic-text layout/measure/raster, backgrounds,
decorations
- graphics (52): full CSS gradient parser, tiny-skia raster,
4 shape definitions
- effect-defs (16): blur definition, registry, pass resolution
- mask-defs (19): all 9 mask renderers via tiny-skia
- renderer (6): node model + scene builder port
- desktop: GPUI projects screen wired to ProjectStore
- scene: factory (web defaults: v32, Main Track, 1920x1080, 30fps)
Diffstat (limited to 'rust/crates/graphics/src/registry.rs')
| -rw-r--r-- | rust/crates/graphics/src/registry.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/rust/crates/graphics/src/registry.rs b/rust/crates/graphics/src/registry.rs new file mode 100644 index 00000000..199d3162 --- /dev/null +++ b/rust/crates/graphics/src/registry.rs @@ -0,0 +1,107 @@ +use std::collections::BTreeMap; +use std::sync::LazyLock; + +use scene::params::ParamValues; + +use crate::definitions::default_graphic_definitions; +use crate::error::GraphicsError; +use crate::params::build_default_param_values; +use crate::types::{GraphicDefinition, GraphicInstance}; + +#[derive(Default)] +pub struct GraphicsRegistry { + definitions: BTreeMap<String, &'static GraphicDefinition>, +} + +impl GraphicsRegistry { + pub fn new() -> Self { + Self::default() + } + + pub fn with_defaults() -> Self { + let mut registry = Self::new(); + registry.register_defaults(); + registry + } + + pub fn register_defaults(&mut self) { + for definition in default_graphic_definitions() { + if self.has(definition.id) { + continue; + } + self.register(definition.id, definition); + } + } + + pub fn register(&mut self, key: &str, definition: &'static GraphicDefinition) { + self.definitions.insert(key.to_string(), definition); + } + + pub fn has(&self, key: &str) -> bool { + self.definitions.contains_key(key) + } + + pub fn get(&self, key: &str) -> Result<&'static GraphicDefinition, GraphicsError> { + self.definitions + .get(key) + .copied() + .ok_or_else(|| GraphicsError::UnknownGraphic(key.to_string())) + } + + pub fn get_all(&self) -> Vec<&'static GraphicDefinition> { + self.definitions.values().copied().collect() + } +} + +static REGISTRY: LazyLock<GraphicsRegistry> = LazyLock::new(GraphicsRegistry::with_defaults); + +pub fn graphics_registry() -> &'static GraphicsRegistry { + ®ISTRY +} + +pub fn get_graphic_definition( + definition_id: &str, +) -> Result<&'static GraphicDefinition, GraphicsError> { + graphics_registry().get(definition_id) +} + +pub fn build_default_graphic_instance( + definition_id: &str, +) -> Result<GraphicInstance, GraphicsError> { + let definition = get_graphic_definition(definition_id)?; + Ok(GraphicInstance { + definition_id: definition_id.to_string(), + params: build_default_param_values(definition.params), + }) +} + +pub fn resolve_graphic_params( + definition: &GraphicDefinition, + params: Option<&ParamValues>, +) -> ParamValues { + let mut resolved = build_default_param_values(definition.params); + if let Some(params) = params { + resolved.extend(params.clone()); + } + resolved +} + +pub fn render_graphic( + definition_id: &str, + params: Option<&ParamValues>, + width: u32, + height: u32, +) -> Result<tiny_skia::Pixmap, GraphicsError> { + let definition = get_graphic_definition(definition_id)?; + let resolved = resolve_graphic_params(definition, params); + definition.render_to_pixmap(&resolved, width, height) +} + +pub fn render_graphic_rgba8( + definition_id: &str, + params: Option<&ParamValues>, + width: u32, + height: u32, +) -> Result<Vec<u8>, GraphicsError> { + Ok(render_graphic(definition_id, params, width, height)?.take()) +} |
