fix longstanding annoying in SceneBuilder ctor

SceneBuilder::new() now takes &mut Scene or &mut SceneFragment
This commit is contained in:
Chad Brokaw 2022-11-26 14:38:26 -05:00
parent 016f9de05f
commit 37a9763748
3 changed files with 37 additions and 5 deletions

View file

@ -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<AnyScene<'a>>) -> 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
}

View file

@ -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),

View file

@ -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