diff options
author | 2025-03-20 11:57:26 +0000 | |
---|---|---|
committer | 2025-03-20 11:57:26 +0000 | |
commit | 27b099c895825ee03555fd7bdaa8cefdb2125ead (patch) | |
tree | 4b77fc19db7785884c4a67e85e452bb140e13688 | |
parent | bae25b74f68078e5ff74cdae717273cf315d4e90 (diff) | |
download | iced-27b099c895825ee03555fd7bdaa8cefdb2125ead.tar.gz iced-27b099c895825ee03555fd7bdaa8cefdb2125ead.tar.bz2 iced-27b099c895825ee03555fd7bdaa8cefdb2125ead.zip |
WIP: background image supportmaster
-rw-r--r-- | core/src/background.rs | 23 | ||||
-rw-r--r-- | core/src/background/background_image.rs | 60 | ||||
-rw-r--r-- | core/src/text.rs | 4 | ||||
-rw-r--r-- | graphics/src/background_image.rs | 1 | ||||
-rw-r--r-- | graphics/src/lib.rs | 1 | ||||
-rw-r--r-- | wgpu/src/lib.rs | 2 | ||||
-rw-r--r-- | wgpu/src/quad.rs | 16 | ||||
-rw-r--r-- | wgpu/src/quad/background_image.rs | 31 |
8 files changed, 133 insertions, 5 deletions
diff --git a/core/src/background.rs b/core/src/background.rs index 1f665ef4..98430020 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -1,14 +1,22 @@ -use crate::Color; +mod background_image; + +use std::sync::Arc; + +use background_image::BackgroundImage; + use crate::gradient::{self, Gradient}; +use crate::image::{FilterMethod, Handle}; +use crate::{Color, Image, Point, Rectangle, Size, image}; /// The background of some element. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum Background { /// A solid color. Color(Color), /// Linearly interpolate between several colors. Gradient(Gradient), - // TODO: Add image variant + /// A background image. + Image(BackgroundImage), } impl Background { @@ -20,6 +28,9 @@ impl Background { Self::Gradient(gradient) => { Self::Gradient(gradient.scale_alpha(factor)) } + Self::Image(background_image) => { + Self::Image(background_image.scale_opacity(factor)) + } } } } @@ -41,3 +52,9 @@ impl From<gradient::Linear> for Background { Background::Gradient(Gradient::Linear(gradient)) } } + +impl From<BackgroundImage> for Background { + fn from(background_image: BackgroundImage) -> Self { + Background::Image(background_image) + } +} diff --git a/core/src/background/background_image.rs b/core/src/background/background_image.rs new file mode 100644 index 00000000..75d63dfc --- /dev/null +++ b/core/src/background/background_image.rs @@ -0,0 +1,60 @@ +use crate::{Image, Point, Size}; + +/// A background image. +#[derive(Debug, Clone, PartialEq)] +pub struct BackgroundImage { + // TODO: svg support + image: Image, + attachment: Attachment, + clip: Sizing, + origin: Sizing, + /// (x, y) + repeat: (Repeat, Repeat), + size: Size, + // filter_method: FilterMethod, + // opacity: f32, + // TODO: blend mode +} + +impl BackgroundImage { + /// Scales the opacity of the [`BackgroundImage`] by the given factor. + pub fn scale_opacity(mut self, factor: f32) -> Self { + let opacity = self.image.opacity * factor; + self.image = self.image.opacity(opacity); + self + } +} + +/// An attachment property. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub enum Attachment { + /// Fixed relative to the viewport. + Fixed, + /// Fixed relative to self. + #[default] + Local, +} + +/// A sizing property. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Sizing { + /// At outside edge of border. + Border, + /// At outside edge of padding. + Padding, + /// At outside edge of content. + Content, +} + +/// A repetition property. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Repeat { + /// Repeat beginning at offset. Will be clipped if necessary. + Repeat(Point), + /// Repeated as much as possible without clipping. Whitespace is evenly distributed between each instance. + Space, + /// Repeated as much as possible without clipping. Stretched until there is room for another to be added. + Round, + /// No repetition. Position defined by point. Will be clipped if necessary. + NoRepeat(Point), +} diff --git a/core/src/text.rs b/core/src/text.rs index a7e1f281..8a6ff140 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -16,7 +16,7 @@ use std::borrow::Cow; use std::hash::{Hash, Hasher}; /// A paragraph. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone)] pub struct Text<Content = String, Font = crate::Font> { /// The content of the paragraph. pub content: Content, @@ -271,7 +271,7 @@ pub struct Span<'a, Link = (), Font = crate::Font> { } /// A text highlight. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct Highlight { /// The [`Background`] of the highlight. pub background: Background, diff --git a/graphics/src/background_image.rs b/graphics/src/background_image.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/graphics/src/background_image.rs @@ -0,0 +1 @@ + diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index b5ef55e7..61c1f4f6 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -12,6 +12,7 @@ mod antialiasing; mod settings; mod viewport; +// pub mod background_image; pub mod cache; pub mod color; pub mod compositor; diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 2ef9992c..875cbcde 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -163,6 +163,8 @@ impl Renderer { device, encoder, &mut engine.staging_belt, + #[cfg(any(feature = "svg", feature = "image"))] + &mut self.image_cache.borrow_mut(), &layer.quads, viewport.projection(), scale_factor, diff --git a/wgpu/src/quad.rs b/wgpu/src/quad.rs index de432d2f..311cd81f 100644 --- a/wgpu/src/quad.rs +++ b/wgpu/src/quad.rs @@ -1,12 +1,15 @@ +mod background_image; mod gradient; mod solid; +use background_image::BackgroundImage; use gradient::Gradient; use solid::Solid; use crate::core::{Background, Rectangle, Transformation}; use crate::graphics; use crate::graphics::color; +use crate::image::Cache; use bytemuck::{Pod, Zeroable}; @@ -47,6 +50,7 @@ pub struct Quad { pub struct Pipeline { solid: solid::Pipeline, gradient: gradient::Pipeline, + background_image: background_image::Pipeline, constant_layout: wgpu::BindGroupLayout, layers: Vec<Layer>, prepare_layer: usize, @@ -85,6 +89,7 @@ impl Pipeline { device: &wgpu::Device, encoder: &mut wgpu::CommandEncoder, belt: &mut wgpu::util::StagingBelt, + #[cfg(any(feature = "svg", feature = "image"))] cache: &mut Cache, quads: &Batch, transformation: Transformation, scale: f32, @@ -139,6 +144,7 @@ impl Pipeline { gradient_offset += count; } + Kind::Image => todo!(), } } } @@ -238,6 +244,9 @@ pub struct Batch { /// The gradient quads of the [`Layer`]. gradients: Vec<Gradient>, + /// The image quads of the [`Layer`]. + images: Vec<BackgroundImage>, + /// The quad order of the [`Layer`]. order: Order, } @@ -273,6 +282,11 @@ impl Batch { Kind::Gradient } + Background::Image(background_image) => { + self.images.push(BackgroundImage { quad }); + + Kind::Image + } }; match self.order.last_mut() { @@ -299,6 +313,8 @@ enum Kind { Solid, /// A gradient quad Gradient, + /// A background image quad + Image, } fn color_target_state( diff --git a/wgpu/src/quad/background_image.rs b/wgpu/src/quad/background_image.rs new file mode 100644 index 00000000..b9217686 --- /dev/null +++ b/wgpu/src/quad/background_image.rs @@ -0,0 +1,31 @@ +use crate::Buffer; +use crate::graphics::image; +use crate::quad::{self, Quad}; + +use bytemuck::{Pod, Zeroable}; + +/// A quad filled with a background image. +#[derive(Clone, Debug)] +#[repr(C)] +pub struct BackgroundImage { + /// The image data of the quad + // TODO: representable image background + pub background_image: image::Image, + + /// The [`Quad`] data of the [`BackgroundImage`]. + pub quad: Quad, +} + +pub struct InnerBackgroundImage { + image: image::Image, + attachment: Attachment, + clip: Sizing, +} + +#[derive(Debug)] +pub struct Pipeline { + pipeline: crate::image::Pipeline, + constant_layout: wgpu::BindGroupLayout, + layers: Vec<Layer>, + prepare_layer: usize, +} |