Skip to content

Commit

Permalink
Add Text::installation().
Browse files Browse the repository at this point in the history
Builds a transaction to place all of a text object at once.
  • Loading branch information
kpreid committed Nov 22, 2023
1 parent 82d32f9 commit 2b9fcb2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
11 changes: 2 additions & 9 deletions all-is-cubes-content/src/exhibits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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();
}

Expand Down
34 changes: 33 additions & 1 deletion all-is-cubes/src/block/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2b9fcb2

Please sign in to comment.