iced_wgpu/
primitive.rs

1//! Draw custom primitives.
2use crate::core::{self, Rectangle};
3use crate::graphics::Viewport;
4
5use rustc_hash::FxHashMap;
6use std::any::{Any, TypeId};
7use std::fmt::Debug;
8
9/// A batch of primitives.
10pub type Batch = Vec<Instance>;
11
12/// A set of methods which allows a [`Primitive`] to be rendered.
13pub trait Primitive: Debug + Send + Sync + 'static {
14    /// Processes the [`Primitive`], allowing for GPU buffer allocation.
15    fn prepare(
16        &self,
17        device: &wgpu::Device,
18        queue: &wgpu::Queue,
19        format: wgpu::TextureFormat,
20        storage: &mut Storage,
21        bounds: &Rectangle,
22        viewport: &Viewport,
23    );
24
25    /// Renders the [`Primitive`].
26    fn render(
27        &self,
28        encoder: &mut wgpu::CommandEncoder,
29        storage: &Storage,
30        target: &wgpu::TextureView,
31        clip_bounds: &Rectangle<u32>,
32    );
33}
34
35#[derive(Debug)]
36/// An instance of a specific [`Primitive`].
37pub struct Instance {
38    /// The bounds of the [`Instance`].
39    pub bounds: Rectangle,
40
41    /// The [`Primitive`] to render.
42    pub primitive: Box<dyn Primitive>,
43}
44
45impl Instance {
46    /// Creates a new [`Instance`] with the given [`Primitive`].
47    pub fn new(bounds: Rectangle, primitive: impl Primitive) -> Self {
48        Instance {
49            bounds,
50            primitive: Box::new(primitive),
51        }
52    }
53}
54
55/// A renderer than can draw custom primitives.
56pub trait Renderer: core::Renderer {
57    /// Draws a custom primitive.
58    fn draw_primitive(&mut self, bounds: Rectangle, primitive: impl Primitive);
59}
60
61/// Stores custom, user-provided types.
62#[derive(Default, Debug)]
63pub struct Storage {
64    pipelines: FxHashMap<TypeId, Box<dyn Any + Send>>,
65}
66
67impl Storage {
68    /// Returns `true` if `Storage` contains a type `T`.
69    pub fn has<T: 'static>(&self) -> bool {
70        self.pipelines.contains_key(&TypeId::of::<T>())
71    }
72
73    /// Inserts the data `T` in to [`Storage`].
74    pub fn store<T: 'static + Send>(&mut self, data: T) {
75        let _ = self.pipelines.insert(TypeId::of::<T>(), Box::new(data));
76    }
77
78    /// Returns a reference to the data with type `T` if it exists in [`Storage`].
79    pub fn get<T: 'static>(&self) -> Option<&T> {
80        self.pipelines.get(&TypeId::of::<T>()).map(|pipeline| {
81            pipeline
82                .downcast_ref::<T>()
83                .expect("Value with this type does not exist in Storage.")
84        })
85    }
86
87    /// Returns a mutable reference to the data with type `T` if it exists in [`Storage`].
88    pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
89        self.pipelines.get_mut(&TypeId::of::<T>()).map(|pipeline| {
90            pipeline
91                .downcast_mut::<T>()
92                .expect("Value with this type does not exist in Storage.")
93        })
94    }
95}