From 37a9763748a8406e12c7357f57aece000efb4b6d Mon Sep 17 00:00:00 2001 From: Chad Brokaw Date: Sat, 26 Nov 2022 14:38:26 -0500 Subject: [PATCH] fix longstanding annoying in SceneBuilder ctor SceneBuilder::new() now takes &mut Scene or &mut SceneFragment --- piet-scene/src/scene/builder.rs | 38 ++++++++++++++++++++-- piet-wgsl/examples/winit/src/main.rs | 2 +- piet-wgsl/examples/winit/src/test_scene.rs | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/piet-scene/src/scene/builder.rs b/piet-scene/src/scene/builder.rs index 17a236a..a3c6cbd 100644 --- a/piet-scene/src/scene/builder.rs +++ b/piet-scene/src/scene/builder.rs @@ -30,18 +30,30 @@ pub struct SceneBuilder<'a> { impl<'a> SceneBuilder<'a> { /// Creates a new builder for filling a scene. Any current content in the scene /// will be cleared. + #[deprecated = "use SceneBuilder::new() instead"] pub fn for_scene(scene: &'a mut Scene) -> Self { - Self::new(&mut scene.data, false) + Self::new_inner(&mut scene.data, false) } /// Creates a new builder for filling a scene fragment. Any current content in /// the fragment will be cleared. + #[deprecated = "use SceneBuilder::new() instead"] pub fn for_fragment(fragment: &'a mut SceneFragment) -> Self { - Self::new(&mut fragment.data, true) + Self::new_inner(&mut fragment.data, true) + } + + /// Creates a new scene builder for a scene or scene fragment. Any content will be + /// cleared. The types accepted for the scene parameter are `&mut Scene` or + /// `&mut SceneFragment`. + pub fn new(scene: impl Into>) -> Self { + match scene.into() { + AnyScene::Scene(scene) => Self::new_inner(&mut scene.data, false), + AnyScene::Fragment(fragment) => Self::new_inner(&mut fragment.data, true), + } } /// Creates a new builder for constructing a scene. - fn new(scene: &'a mut SceneData, is_fragment: bool) -> Self { + fn new_inner(scene: &'a mut SceneData, is_fragment: bool) -> Self { scene.reset(is_fragment); Self { scene, @@ -276,6 +288,26 @@ impl<'a> SceneBuilder<'a> { } } +/// Helper for constructing a scene builder. +#[doc(hidden)] +pub enum AnyScene<'a> { + Scene(&'a mut Scene), + Fragment(&'a mut SceneFragment), +} + +impl<'a> From<&'a mut Scene> for AnyScene<'a> { + fn from(scene: &'a mut Scene) -> Self { + Self::Scene(scene) + } +} + +impl<'a> From<&'a mut SceneFragment> for AnyScene<'a> { + fn from(scene: &'a mut SceneFragment) -> Self { + Self::Fragment(scene) + } +} + + fn encode_blend_mode(mode: BlendMode) -> u32 { (mode.mix as u32) << 8 | mode.compose as u32 } diff --git a/piet-wgsl/examples/winit/src/main.rs b/piet-wgsl/examples/winit/src/main.rs index ba6226c..3537dce 100644 --- a/piet-wgsl/examples/winit/src/main.rs +++ b/piet-wgsl/examples/winit/src/main.rs @@ -70,7 +70,7 @@ async fn run() -> Result<()> { current_frame += 1; let width = surface.config.width; let height = surface.config.height; - let mut builder = SceneBuilder::for_scene(&mut scene); + let mut builder = SceneBuilder::new(&mut scene); const N_SCENES: usize = 6; match scene_ix % N_SCENES { 0 => test_scene::render_anim_frame(&mut builder, &mut simple_text, current_frame), diff --git a/piet-wgsl/examples/winit/src/test_scene.rs b/piet-wgsl/examples/winit/src/test_scene.rs index 4c90829..e57dce8 100644 --- a/piet-wgsl/examples/winit/src/test_scene.rs +++ b/piet-wgsl/examples/winit/src/test_scene.rs @@ -265,7 +265,7 @@ fn render_blend_square(sb: &mut SceneBuilder, blend: BlendMode, transform: Affin #[allow(unused)] fn blend_square(blend: BlendMode) -> SceneFragment { let mut fragment = SceneFragment::default(); - let mut sb = SceneBuilder::for_fragment(&mut fragment); + let mut sb = SceneBuilder::new(&mut fragment); render_blend_square(&mut sb, blend, Affine::IDENTITY); sb.finish(); fragment