From 2b9fcb297ad423e9e932f8b20b77272ee76800d2 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sun, 19 Nov 2023 08:16:32 -0800 Subject: [PATCH] Add `Text::installation()`. Builds a transaction to place all of a text object at once. --- all-is-cubes-content/src/exhibits.rs | 11 ++------- all-is-cubes/src/block/text.rs | 34 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/all-is-cubes-content/src/exhibits.rs b/all-is-cubes-content/src/exhibits.rs index c0f8933c9..b4b0355da 100644 --- a/all-is-cubes-content/src/exhibits.rs +++ b/all-is-cubes-content/src/exhibits.rs @@ -11,7 +11,6 @@ use exhaust::Exhaust as _; use rand::SeedableRng as _; use all_is_cubes::arcstr::literal; -use all_is_cubes::block; use all_is_cubes::content::load_image::{default_srgb, space_from_image}; use all_is_cubes::drawing::VoxelBrush; use all_is_cubes::drawing::{ @@ -328,14 +327,8 @@ async fn TEXT(_: &Exhibit, _universe: &mut Universe) { let mut space = Space::builder(bounds_for_text).build(); for text in texts { - space - .fill(text.bounding_blocks(), |cube| { - // TODO: should be able to make the translation happen smoothly - Some(Block::from_primitive(block::Primitive::Text { - text: text.clone(), - offset: cube.lower_bounds().to_vector(), - })) - }) + text.installation(Gridgid::IDENTITY, core::convert::identity) + .execute(&mut space, &mut transaction::no_outputs) .unwrap(); } diff --git a/all-is-cubes/src/block/text.rs b/all-is-cubes/src/block/text.rs index be3e339ee..272cde020 100644 --- a/all-is-cubes/src/block/text.rs +++ b/all-is-cubes/src/block/text.rs @@ -7,10 +7,11 @@ use embedded_graphics as eg; use embedded_graphics::{prelude::Dimensions as _, Drawable as _}; use euclid::vec3; -use crate::block::{self, BlockAttributes, EvalBlockError, Evoxel, MinEval, Resolution}; +use crate::block::{self, Block, BlockAttributes, EvalBlockError, Evoxel, MinEval, Resolution}; use crate::content::palette; use crate::drawing::{rectangle_to_aab, DrawingPlane}; use crate::math::{GridAab, GridCoordinate, GridPoint, GridVector, Gridgid, Rgb, Vol}; +use crate::space::{self, SpaceTransaction}; use crate::universe; #[cfg(doc)] @@ -87,6 +88,37 @@ impl Text { ) } + /// Returns a transaction which places [`Primitive::Text`] blocks containing this text. + /// + /// The text lies within the volume [`Self::bounding_blocks()`] transformed by `transform`. + /// + /// Each individual block is given to `block_fn` to allow alterations. + /// + /// The transaction has no preconditions. + pub fn installation( + &self, + transform: Gridgid, + block_fn: impl Fn(Block) -> Block, + ) -> SpaceTransaction { + let dst_to_src_transform = transform.inverse(); + let block_rotation = transform.rotation; + SpaceTransaction::filling(self.bounding_blocks(), |cube| { + space::CubeTransaction::replacing( + None, + Some(block_fn( + Block::from_primitive(block::Primitive::Text { + text: self.clone(), + offset: dst_to_src_transform + .transform_cube(cube) + .lower_bounds() + .to_vector(), + }) + .rotate(block_rotation), + )), + ) + }) + } + pub(crate) fn evaluate( &self, block_offset: GridVector,