Skip to main content

ferroui_base/media/
i_scene_brush.rs

1use crate::media::immutable::immutable_tile_brush::immutable_tile_brush_interfaces;
2use crate::media::immutable::ImmutableTileBrush;
3use crate::media::ref_adapter::RefAdapter;
4use crate::media::{Brush, DrawingBrush, IImmutableBrush, ITileBrush, VisualBrush};
5use crate::platform::IDrawingContextImpl;
6use crate::{Matrix, ObjectType, Rect, Upcast};
7use std::rc::Rc;
8
9/// A tile brush whose content is a recorded scene.
10pub trait ISceneBrush: ITileBrush {
11    /// Records the current content of the brush.
12    fn create_content(&self) -> Option<Rc<dyn ISceneBrushContent>>;
13}
14
15/// The scene brush classes ([`VisualBrush`], [`DrawingBrush`]) behind a
16/// class handle.
17impl<T: ObjectType + Upcast<Brush>> ISceneBrush for RefAdapter<T> {
18    fn create_content(&self) -> Option<Rc<dyn ISceneBrushContent>> {
19        match self.object().downcast_ref::<VisualBrush>() {
20            Some(visual_brush) => visual_brush.create_content(),
21            None => self.class::<DrawingBrush>().create_content(),
22        }
23    }
24}
25
26/// The recorded content of an [`ISceneBrush`].
27pub trait ISceneBrushContent: IImmutableBrush {
28    /// The tile brush parameters the content is drawn with.
29    fn brush(&self) -> Rc<dyn ITileBrush>;
30
31    /// The bounds of the content.
32    fn rect(&self) -> Rect;
33
34    /// Replays the content into a drawing context.
35    fn render(&self, context: &mut dyn IDrawingContextImpl, transform: Option<Matrix>);
36
37    /// Whether the content should be rasterized at the resolution it is
38    /// displayed at.
39    fn use_scalable_rasterization(&self) -> bool;
40
41    /// Releases the content.
42    fn dispose(&self);
43}
44
45/// The immutable tile brush parameters of a scene brush.
46#[derive(Clone)]
47pub struct ImmutableSceneBrush {
48    base: ImmutableTileBrush,
49}
50
51immutable_tile_brush_interfaces!(ImmutableSceneBrush);
52
53impl ImmutableSceneBrush {
54    /// Creates an immutable copy of the tile brush parameters of `source`.
55    pub fn new(source: &dyn ITileBrush) -> Self {
56        Self { base: ImmutableTileBrush::from_brush(source) }
57    }
58}