Skip to main content

ferroui_base/media/immutable/
immutable_image_brush.rs

1use crate::media::imaging::Bitmap;
2use crate::media::immutable::immutable_tile_brush::immutable_tile_brush_interfaces;
3use crate::media::immutable::{ImmutableTileBrush, ImmutableTransform};
4use crate::media::{AlignmentX, AlignmentY, IImageBrush, IImageBrushSource, Stretch, TileMode};
5use crate::{RelativePoint, RelativeRect};
6use std::rc::Rc;
7
8/// Paints an area with an image.
9#[derive(Clone)]
10pub struct ImmutableImageBrush {
11    base: ImmutableTileBrush,
12    source: Option<Rc<dyn IImageBrushSource>>,
13}
14
15immutable_tile_brush_interfaces!(ImmutableImageBrush, {
16    fn as_image_brush(&self) -> Option<&dyn crate::media::IImageBrush> {
17        Some(self)
18    }
19});
20
21impl ImmutableImageBrush {
22    /// Creates a brush. `destination_rect` and `source_rect` default to the
23    /// whole area.
24    #[allow(clippy::too_many_arguments)]
25    pub fn new(
26        source: Option<Rc<Bitmap>>,
27        alignment_x: AlignmentX,
28        alignment_y: AlignmentY,
29        destination_rect: Option<RelativeRect>,
30        opacity: f64,
31        transform: Option<Rc<ImmutableTransform>>,
32        transform_origin: RelativePoint,
33        source_rect: Option<RelativeRect>,
34        stretch: Stretch,
35        tile_mode: TileMode,
36        relative_transform: Option<Rc<ImmutableTransform>>,
37    ) -> Self {
38        Self {
39            base: ImmutableTileBrush::new(
40                alignment_x,
41                alignment_y,
42                destination_rect.unwrap_or(RelativeRect::FILL),
43                opacity,
44                transform,
45                transform_origin,
46                source_rect.unwrap_or(RelativeRect::FILL),
47                stretch,
48                tile_mode,
49                relative_transform,
50            ),
51            source: source.map(|source| source as Rc<dyn IImageBrushSource>),
52        }
53    }
54
55    /// Creates a brush drawing `source` with default values for everything
56    /// else: centered, uniformly stretched, not tiled and fully opaque.
57    pub fn from_bitmap(source: Option<Rc<Bitmap>>) -> Self {
58        Self::new(
59            source,
60            AlignmentX::Center,
61            AlignmentY::Center,
62            None,
63            1.0,
64            None,
65            RelativePoint::default(),
66            None,
67            Stretch::Uniform,
68            TileMode::None,
69            None,
70        )
71    }
72
73    /// Creates an immutable copy of `source`.
74    pub fn from_brush(source: &dyn IImageBrush) -> Self {
75        Self { base: ImmutableTileBrush::from_brush(source), source: source.source() }
76    }
77
78    /// The image to draw.
79    pub fn source(&self) -> Option<Rc<dyn IImageBrushSource>> {
80        self.source.clone()
81    }
82}
83
84impl IImageBrush for ImmutableImageBrush {
85    fn source(&self) -> Option<Rc<dyn IImageBrushSource>> {
86        self.source.clone()
87    }
88}