Skip to main content

ferroui_base/media/
image_drawing.rs

1use crate::media::{Drawing, DrawingContext, DrawingImpl, IImage};
2use crate::{
3    ferro_class, ferro_impl_classes, ferro_property, instantiate, FerroObjectImpl, FerroProperty, Rect, Ref,
4    StyledProperty,
5};
6use std::rc::Rc;
7
8/// Draws an image within a region defined by a [`Rect`].
9#[repr(C)]
10pub struct ImageDrawing {
11    base: Drawing,
12}
13
14ferro_class!(ImageDrawing: Drawing);
15crate::ferro_class_info!(ImageDrawing { new: ImageDrawing::new });
16ferro_impl_classes!(ImageDrawing: FerroObjectImpl);
17
18impl DrawingImpl for ImageDrawing {
19    fn draw_core(this: &Self, context: &mut DrawingContext) {
20        let image_source = this.image_source();
21        let rect = this.rect();
22
23        if let Some(image_source) = image_source {
24            if rect.width != 0.0 || rect.height != 0.0 {
25                context.draw_image(&*image_source, rect);
26            }
27        }
28    }
29
30    fn get_bounds(this: &Self) -> Rect {
31        this.rect()
32    }
33}
34
35crate::ferro_properties! { impl ImageDrawing {
36    ferro_property!(
37        /// Defines the `ImageSource` property.
38        pub fn image_source_property() -> StyledProperty<Option<Rc<dyn IImage>>> {
39            FerroProperty::register::<ImageDrawing, _>("ImageSource", None)
40        }
41    );
42
43    ferro_property!(
44        /// Defines the `Rect` property.
45        pub fn rect_property() -> StyledProperty<Rect> {
46            FerroProperty::register::<ImageDrawing, _>("Rect", Rect::default())
47        }
48    );
49} }
50
51impl ImageDrawing {
52    /// Creates the class data.
53    pub fn construct() -> Self {
54        Self { base: Drawing::construct() }
55    }
56
57    pub fn new() -> Ref<Self> {
58        instantiate(Self::construct())
59    }
60
61    /// The source of the image.
62    pub fn image_source(&self) -> Option<Rc<dyn IImage>> {
63        self.get_value(Self::image_source_property())
64    }
65
66    pub fn set_image_source(&self, value: Option<Rc<dyn IImage>>) {
67        self.set_value(Self::image_source_property(), value)
68    }
69
70    /// The region in which the image is drawn.
71    pub fn rect(&self) -> Rect {
72        self.get_value(Self::rect_property())
73    }
74
75    pub fn set_rect(&self, value: Rect) {
76        self.set_value(Self::rect_property(), value)
77    }
78}