Implement composable, type-safe renderer fallback
This commit is contained in:
parent
7e4ae8450e
commit
3645d34d6a
35 changed files with 1474 additions and 1210 deletions
|
|
@ -8,8 +8,9 @@ use crate::core::text::Text;
|
|||
use crate::core::{
|
||||
Background, Color, Font, Pixels, Point, Rectangle, Size, Transformation,
|
||||
};
|
||||
use crate::mesh;
|
||||
use crate::text;
|
||||
use crate::Primitive;
|
||||
use crate::{Mesh, Primitive};
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
|
@ -20,6 +21,7 @@ pub struct Renderer<B: Backend> {
|
|||
default_font: Font,
|
||||
default_text_size: Pixels,
|
||||
primitives: Vec<Primitive<B::Primitive>>,
|
||||
stack: Vec<Vec<Primitive<B::Primitive>>>,
|
||||
}
|
||||
|
||||
impl<B: Backend> Renderer<B> {
|
||||
|
|
@ -34,6 +36,7 @@ impl<B: Backend> Renderer<B> {
|
|||
default_font,
|
||||
default_text_size,
|
||||
primitives: Vec::new(),
|
||||
stack: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -56,59 +59,45 @@ impl<B: Backend> Renderer<B> {
|
|||
f(&mut self.backend, &self.primitives)
|
||||
}
|
||||
|
||||
/// Starts recording a new layer.
|
||||
pub fn start_layer(&mut self) -> Vec<Primitive<B::Primitive>> {
|
||||
std::mem::take(&mut self.primitives)
|
||||
}
|
||||
|
||||
/// Ends the recording of a layer.
|
||||
pub fn end_layer(
|
||||
#[cfg(feature = "geometry")]
|
||||
pub fn draw_geometry<Geometry>(
|
||||
&mut self,
|
||||
primitives: Vec<Primitive<B::Primitive>>,
|
||||
bounds: Rectangle,
|
||||
) {
|
||||
let layer = std::mem::replace(&mut self.primitives, primitives);
|
||||
|
||||
self.primitives.push(Primitive::group(layer).clip(bounds));
|
||||
}
|
||||
|
||||
/// Starts recording a translation.
|
||||
pub fn start_transformation(&mut self) -> Vec<Primitive<B::Primitive>> {
|
||||
std::mem::take(&mut self.primitives)
|
||||
}
|
||||
|
||||
/// Ends the recording of a translation.
|
||||
pub fn end_transformation(
|
||||
&mut self,
|
||||
primitives: Vec<Primitive<B::Primitive>>,
|
||||
transformation: Transformation,
|
||||
) {
|
||||
let layer = std::mem::replace(&mut self.primitives, primitives);
|
||||
|
||||
self.primitives
|
||||
.push(Primitive::group(layer).transform(transformation));
|
||||
layers: impl IntoIterator<Item = Geometry>,
|
||||
) where
|
||||
Geometry: Into<Primitive<B::Primitive>>,
|
||||
{
|
||||
for layer in layers {
|
||||
self.draw_primitive(layer.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> iced_core::Renderer for Renderer<B> {
|
||||
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) {
|
||||
let current = self.start_layer();
|
||||
|
||||
f(self);
|
||||
|
||||
self.end_layer(current, bounds);
|
||||
fn start_layer(&mut self) {
|
||||
self.stack.push(std::mem::take(&mut self.primitives));
|
||||
}
|
||||
|
||||
fn with_transformation(
|
||||
&mut self,
|
||||
transformation: Transformation,
|
||||
f: impl FnOnce(&mut Self),
|
||||
) {
|
||||
let current = self.start_transformation();
|
||||
fn end_layer(&mut self, bounds: Rectangle) {
|
||||
let layer = std::mem::replace(
|
||||
&mut self.primitives,
|
||||
self.stack.pop().expect("a layer should be recording"),
|
||||
);
|
||||
|
||||
f(self);
|
||||
self.primitives.push(Primitive::group(layer).clip(bounds));
|
||||
}
|
||||
|
||||
self.end_transformation(current, transformation);
|
||||
fn start_transformation(&mut self) {
|
||||
self.stack.push(std::mem::take(&mut self.primitives));
|
||||
}
|
||||
|
||||
fn end_transformation(&mut self, transformation: Transformation) {
|
||||
let layer = std::mem::replace(
|
||||
&mut self.primitives,
|
||||
self.stack.pop().expect("a layer should be recording"),
|
||||
);
|
||||
|
||||
self.primitives
|
||||
.push(Primitive::group(layer).transform(transformation));
|
||||
}
|
||||
|
||||
fn fill_quad(
|
||||
|
|
@ -250,3 +239,34 @@ where
|
|||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Backend> mesh::Renderer for Renderer<B> {
|
||||
fn draw_mesh(&mut self, mesh: Mesh) {
|
||||
match B::Primitive::try_from(mesh) {
|
||||
Ok(primitive) => {
|
||||
self.draw_primitive(Primitive::Custom(primitive));
|
||||
}
|
||||
Err(error) => {
|
||||
log::warn!("mesh primitive could not be drawn: {error:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "geometry")]
|
||||
impl<B> crate::geometry::Renderer for Renderer<B>
|
||||
where
|
||||
B: Backend + crate::geometry::Backend,
|
||||
B::Frame: crate::geometry::Frame<Geometry = Primitive<B::Primitive>>,
|
||||
{
|
||||
type Frame = B::Frame;
|
||||
type Geometry = Primitive<B::Primitive>;
|
||||
|
||||
fn new_frame(&self, size: Size) -> Self::Frame {
|
||||
self.backend.new_frame(size)
|
||||
}
|
||||
|
||||
fn draw_geometry(&mut self, geometry: Self::Geometry) {
|
||||
self.draw_primitive(geometry);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue