Redesign iced_wgpu layering architecture

This commit is contained in:
Héctor Ramón Jiménez 2024-04-03 21:07:54 +02:00
parent 99a904112c
commit b05e61f5c8
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
36 changed files with 2781 additions and 2048 deletions

View file

@ -16,6 +16,22 @@ pub struct Rectangle<T = f32> {
pub height: T,
}
impl<T> Rectangle<T>
where
T: Default,
{
/// Creates a new [`Rectangle`] with its top-left corner at the origin
/// and with the provided [`Size`].
pub fn with_size(size: Size<T>) -> Self {
Self {
x: T::default(),
y: T::default(),
width: size.width,
height: size.height,
}
}
}
impl Rectangle<f32> {
/// Creates a new [`Rectangle`] with its top-left corner in the given
/// [`Point`] and with the provided [`Size`].
@ -28,17 +44,6 @@ impl Rectangle<f32> {
}
}
/// Creates a new [`Rectangle`] with its top-left corner at the origin
/// and with the provided [`Size`].
pub fn with_size(size: Size) -> Self {
Self {
x: 0.0,
y: 0.0,
width: size.width,
height: size.height,
}
}
/// Returns the [`Point`] at the center of the [`Rectangle`].
pub fn center(&self) -> Point {
Point::new(self.center_x(), self.center_y())

View file

@ -9,7 +9,7 @@ use crate::{
/// A component that can be used by widgets to draw themselves on a screen.
pub trait Renderer {
/// Starts recording a new layer.
fn start_layer(&mut self);
fn start_layer(&mut self, bounds: Rectangle);
/// Ends recording a new layer.
///
@ -20,13 +20,13 @@ pub trait Renderer {
///
/// The layer will clip its contents to the provided `bounds`.
fn with_layer(&mut self, bounds: Rectangle, f: impl FnOnce(&mut Self)) {
self.start_layer();
self.start_layer(bounds);
f(self);
self.end_layer(bounds);
}
/// Starts recording with a new [`Transformation`].
fn start_transformation(&mut self);
fn start_transformation(&mut self, transformation: Transformation);
/// Ends recording a new layer.
///
@ -39,7 +39,7 @@ pub trait Renderer {
transformation: Transformation,
f: impl FnOnce(&mut Self),
) {
self.start_transformation();
self.start_transformation(transformation);
f(self);
self.end_transformation(transformation);
}

View file

@ -10,11 +10,11 @@ use crate::{
use std::borrow::Cow;
impl Renderer for () {
fn start_layer(&mut self) {}
fn start_layer(&mut self, _bounds: Rectangle) {}
fn end_layer(&mut self, _bounds: Rectangle) {}
fn start_transformation(&mut self) {}
fn start_transformation(&mut self, _transformation: Transformation) {}
fn end_transformation(&mut self, _transformation: Transformation) {}

View file

@ -99,3 +99,17 @@ where
}
}
}
impl<T> std::ops::Mul<T> for Size<T>
where
T: std::ops::Mul<Output = T> + Copy,
{
type Output = Size<T>;
fn mul(self, rhs: T) -> Self::Output {
Size {
width: self.width * rhs,
height: self.height * rhs,
}
}
}

View file

@ -42,6 +42,12 @@ impl Transformation {
}
}
impl Default for Transformation {
fn default() -> Self {
Transformation::IDENTITY
}
}
impl Mul for Transformation {
type Output = Self;