Skip to content

Commit

Permalink
refactor: naming to be consistent (#65)
Browse files Browse the repository at this point in the history
Renames `Alignment`s to `Anchor`s. This is consistent with how bevy
names the equivalent component, `Anchor` within `Text2dBundle`.
(https://docs.rs/bevy/latest/bevy/text/struct.Text2dBundle.html).

Also, this corrects and consistently names fields in the internals, not
in the public API.
  • Loading branch information
simbleau committed Jul 20, 2024
1 parent 5d900d7 commit 864cbc9
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 117 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

## Unreleased

### Changed

- The field `VelloAssetBundle.vector` was renamed to `VelloAssetBundle.asset`.
- Renamed `VelloAssetAlignment` to `VelloAssetAnchor`. Fields were renamed `alignment` were renamed to `asset_anchor`.
- Renamed `VelloTextAlignment` to `VelloTextAnchor`. Fields were renamed `alignment` were renamed to `text_anchor`.

### Removed

- Removed `ZFunction`s from the render pipeline. Now ordering is based solely on the `Transform`'s z component. If you dependeded on this behavior, you'll need to adjust the transform Z in a system prior to render.
Expand Down
3 changes: 2 additions & 1 deletion examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn setup_vector_graphics(mut commands: Commands, asset_server: ResMut<AssetServe
commands.spawn((Camera2dBundle::default(), bevy_pancam::PanCam::default()));
commands
.spawn(VelloAssetBundle {
vector: asset_server.load::<VelloAsset>("embedded://demo/assets/calendar.json"),
asset: asset_server.load::<VelloAsset>("embedded://demo/assets/calendar.json"),
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0))
.with_scale(Vec3::splat(20.0)),
debug_visualizations: DebugVisualizations::Visible,
Expand Down Expand Up @@ -78,6 +78,7 @@ fn print_metadata(
for ev in asset_ev.read() {
if let AssetEvent::LoadedWithDependencies { id } = ev {
let asset = assets.get(*id).unwrap();
#[allow(irrefutable_let_patterns)]
if let VectorFile::Lottie(composition) = &asset.file {
info!(
"Animated asset loaded. Layers:\n{:#?}",
Expand Down
4 changes: 3 additions & 1 deletion examples/demo/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ pub fn controls_ui(
};

let asset = assets.get(handle.id()).unwrap();
let VectorFile::Lottie(composition) = &asset.file else {
#[allow(irrefutable_let_patterns)]
let VectorFile::Lottie(composition) = &asset.file
else {
return;
};

Expand Down
2 changes: 1 addition & 1 deletion examples/drag_n_drop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy_vello = { path = "../../" }
bevy_vello = { path = "../../", features = ["svg", "lottie"] }
bevy = { workspace = true }
6 changes: 3 additions & 3 deletions examples/drag_n_drop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
fn setup_vector_graphics(mut commands: Commands, asset_server: ResMut<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(VelloAssetBundle {
vector: asset_server.load::<VelloAsset>("embedded://drag_n_drop/assets/fountain.svg"),
asset: asset_server.load::<VelloAsset>("embedded://drag_n_drop/assets/fountain.svg"),
debug_visualizations: DebugVisualizations::Visible,
transform: Transform::from_scale(Vec3::splat(5.0)),
..default()
Expand All @@ -34,14 +34,14 @@ fn drag_and_drop(
asset_server: ResMut<AssetServer>,
mut dnd_evr: EventReader<FileDragAndDrop>,
) {
let Ok(mut vector) = query.get_single_mut() else {
let Ok(mut asset) = query.get_single_mut() else {
return;
};
for ev in dnd_evr.read() {
let FileDragAndDrop::DroppedFile { path_buf, .. } = ev else {
continue;
};
let new_handle = asset_server.load(path_buf.clone());
*vector = new_handle;
*asset = new_handle;
}
}
2 changes: 1 addition & 1 deletion examples/lottie/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn load_lottie(mut commands: Commands, asset_server: ResMut<AssetServer>) {

// Yes, it's this simple.
commands.spawn(VelloAssetBundle {
vector: asset_server.load("embedded://lottie/assets/Tiger.json"),
asset: asset_server.load("embedded://lottie/assets/Tiger.json"),
debug_visualizations: DebugVisualizations::Visible,
transform: Transform::from_scale(Vec3::splat(0.5)),
..default()
Expand Down
2 changes: 1 addition & 1 deletion examples/svg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn load_svg(mut commands: Commands, asset_server: ResMut<AssetServer>) {

// Yes, it's this simple.
commands.spawn(VelloAssetBundle {
vector: asset_server.load("embedded://svg/assets/fountain.svg"),
asset: asset_server.load("embedded://svg/assets/fountain.svg"),
debug_visualizations: DebugVisualizations::Visible,
transform: Transform::from_scale(Vec3::splat(5.0)),
..default()
Expand Down
6 changes: 3 additions & 3 deletions examples/text/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::{
asset::{embedded_asset, AssetMetaCheck},
prelude::*,
};
use bevy_vello::{prelude::*, text::VelloTextAlignment, vello::peniko, VelloPlugin};
use bevy_vello::{prelude::*, text::VelloTextAnchor, vello::peniko, VelloPlugin};

fn main() {
let mut app = App::new();
Expand Down Expand Up @@ -32,7 +32,7 @@ fn setup_worldspace_text(mut commands: Commands, asset_server: ResMut<AssetServe
size: 50.0,
brush: None,
},
alignment: VelloTextAlignment::Center,
text_anchor: VelloTextAnchor::Center,
transform: Transform::from_xyz(100.0, 100.0, 0.0),
debug_visualizations: DebugVisualizations::Visible,
..default()
Expand Down Expand Up @@ -60,7 +60,7 @@ fn setup_screenspace_text(mut commands: Commands, asset_server: ResMut<AssetServ
size: 15.0,
brush: Some(peniko::Brush::Solid(peniko::Color::RED)),
},
alignment: bevy_vello::text::VelloTextAlignment::TopLeft,
text_anchor: bevy_vello::text::VelloTextAnchor::TopLeft,
transform: Transform::from_xyz(100.0, 85.0, 0.0),
coordinate_space: CoordinateSpace::ScreenSpace,
debug_visualizations: DebugVisualizations::Visible,
Expand Down
63 changes: 31 additions & 32 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Logic for rendering debug visualizations
use crate::{
text::VelloTextAlignment, CoordinateSpace, VelloAsset, VelloAssetAlignment, VelloFont,
VelloText,
text::VelloTextAnchor, CoordinateSpace, VelloAsset, VelloAssetAnchor, VelloFont, VelloText,
};
use bevy::{color::palettes::css, math::Vec3Swizzles, prelude::*};

Expand All @@ -28,14 +27,14 @@ fn render_asset_debug(
query_vectors: Query<
(
&Handle<VelloAsset>,
&VelloAssetAlignment,
&VelloAssetAnchor,
&GlobalTransform,
&CoordinateSpace,
&DebugVisualizations,
),
Without<Node>,
>,
vectors: Res<Assets<VelloAsset>>,
assets: Res<Assets<VelloAsset>>,
query_cam: Query<(&Camera, &GlobalTransform, &OrthographicProjection), With<Camera2d>>,
mut gizmos: Gizmos,
) {
Expand All @@ -44,20 +43,20 @@ fn render_asset_debug(
};

// Show vectors
for (vector, alignment, gtransform, space, _) in query_vectors
for (asset, asset_anchor, gtransform, space, _) in query_vectors
.iter()
.filter(|(_, _, _, _, d)| **d == DebugVisualizations::Visible)
{
if let Some(vector) = vectors.get(vector) {
if let Some(asset) = assets.get(asset) {
match space {
CoordinateSpace::WorldSpace => {
// Origin
let origin = gtransform.translation().xy();
draw_origin(&mut gizmos, projection, origin);
// Bounding box
let gtransform = &alignment.compute(vector, gtransform);
let gtransform = &asset_anchor.compute(asset, gtransform);
let rect_center = gtransform.translation().xy();
let rect = vector.bb_in_world_space(gtransform);
let rect = asset.bb_in_world_space(gtransform);
draw_bounding_box(&mut gizmos, rect_center, rect.size());
}
CoordinateSpace::ScreenSpace => {
Expand All @@ -68,12 +67,12 @@ fn render_asset_debug(
};
draw_origin(&mut gizmos, projection, origin);
// Bounding box
let gtransform = &alignment.compute(vector, gtransform);
let gtransform = &asset_anchor.compute(asset, gtransform);
let rect_center = gtransform.translation().xy();
let Some(rect_center) = camera.viewport_to_world_2d(view, rect_center) else {
continue;
};
let Some(rect) = vector.bb_in_screen_space(gtransform, camera, view) else {
let Some(rect) = asset.bb_in_screen_space(gtransform, camera, view) else {
continue;
};
draw_bounding_box(&mut gizmos, rect_center, rect.size());
Expand All @@ -89,7 +88,7 @@ fn render_text_debug(
(
&Handle<VelloFont>,
&VelloText,
&VelloTextAlignment,
&VelloTextAnchor,
&GlobalTransform,
&CoordinateSpace,
&DebugVisualizations,
Expand All @@ -105,7 +104,7 @@ fn render_text_debug(
};

// Show world-space vectors
for (font, text, alignment, gtransform, space, _) in query_world
for (font, text, text_anchor, gtransform, space, _) in query_world
.iter()
.filter(|(_, _, _, _, _, d)| **d == DebugVisualizations::Visible)
{
Expand All @@ -117,33 +116,33 @@ fn render_text_debug(
draw_origin(&mut gizmos, projection, origin);
let size = rect.size();
let (width, height) = size.into();
match alignment {
VelloTextAlignment::BottomLeft => {}
VelloTextAlignment::Bottom => {
match text_anchor {
VelloTextAnchor::BottomLeft => {}
VelloTextAnchor::Bottom => {
origin.x += -width / 2.0;
}
VelloTextAlignment::BottomRight => {
VelloTextAnchor::BottomRight => {
origin.x += -width;
}
VelloTextAlignment::TopLeft => {
VelloTextAnchor::TopLeft => {
origin.y += -height;
}
VelloTextAlignment::Left => {
VelloTextAnchor::Left => {
origin.y += -height / 2.0;
}
VelloTextAlignment::Top => {
VelloTextAnchor::Top => {
origin.x += -width / 2.0;
origin.y += -height;
}
VelloTextAlignment::Center => {
VelloTextAnchor::Center => {
origin.x += -width / 2.0;
origin.y += -height / 2.0;
}
VelloTextAlignment::TopRight => {
VelloTextAnchor::TopRight => {
origin.x += -width;
origin.y += -height;
}
VelloTextAlignment::Right => {
VelloTextAnchor::Right => {
origin.x += -width;
origin.y += -height / 2.0;
}
Expand All @@ -163,33 +162,33 @@ fn render_text_debug(
draw_origin(&mut gizmos, projection, origin);
let size = rect.size();
let (width, height) = size.into();
match alignment {
VelloTextAlignment::BottomLeft => {}
VelloTextAlignment::Bottom => {
match text_anchor {
VelloTextAnchor::BottomLeft => {}
VelloTextAnchor::Bottom => {
origin.x += -width / 2.0;
}
VelloTextAlignment::BottomRight => {
VelloTextAnchor::BottomRight => {
origin.x += -width;
}
VelloTextAlignment::TopLeft => {
VelloTextAnchor::TopLeft => {
origin.y += height;
}
VelloTextAlignment::Left => {
VelloTextAnchor::Left => {
origin.y += height / 2.0;
}
VelloTextAlignment::Top => {
VelloTextAnchor::Top => {
origin.x += -width / 2.0;
origin.y += height;
}
VelloTextAlignment::Center => {
VelloTextAnchor::Center => {
origin.x += -width / 2.0;
origin.y += height / 2.0;
}
VelloTextAlignment::TopRight => {
VelloTextAnchor::TopRight => {
origin.x += -width;
origin.y += height;
}
VelloTextAlignment::Right => {
VelloTextAnchor::Right => {
origin.x += -width;
origin.y += height / 2.0;
}
Expand Down
26 changes: 13 additions & 13 deletions src/integrations/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ impl VelloAsset {
}
}

/// Describes how to position the asset from the origin
/// Describes how the asset is positioned relative to its [`Transform`]. It defaults to [`VelloAssetAnchor::Center`].
#[derive(Component, Default, Clone, Copy, PartialEq, Eq)]
pub enum VelloAssetAlignment {
pub enum VelloAssetAnchor {
/// Bounds start from the render position and advance up and to the right.
BottomLeft,
/// Bounds start from the render position and advance up.
Expand All @@ -67,24 +67,24 @@ pub enum VelloAssetAlignment {
TopRight,
}

impl VelloAssetAlignment {
impl VelloAssetAnchor {
pub(crate) fn compute(
&self,
asset: &VelloAsset,
transform: &GlobalTransform,
) -> GlobalTransform {
let (width, height) = (asset.width, asset.height);
// Apply alignment
// Apply positioning
let adjustment = match self {
VelloAssetAlignment::TopLeft => Vec3::new(width / 2.0, -height / 2.0, 0.0),
VelloAssetAlignment::Left => Vec3::new(width / 2.0, 0.0, 0.0),
VelloAssetAlignment::BottomLeft => Vec3::new(width / 2.0, height / 2.0, 0.0),
VelloAssetAlignment::Top => Vec3::new(0.0, -height / 2.0, 0.0),
VelloAssetAlignment::Center => Vec3::new(0.0, 0.0, 0.0),
VelloAssetAlignment::Bottom => Vec3::new(0.0, height / 2.0, 0.0),
VelloAssetAlignment::TopRight => Vec3::new(-width / 2.0, -height / 2.0, 0.0),
VelloAssetAlignment::Right => Vec3::new(-width / 2.0, 0.0, 0.0),
VelloAssetAlignment::BottomRight => Vec3::new(-width / 2.0, height / 2.0, 0.0),
VelloAssetAnchor::TopLeft => Vec3::new(width / 2.0, -height / 2.0, 0.0),
VelloAssetAnchor::Left => Vec3::new(width / 2.0, 0.0, 0.0),
VelloAssetAnchor::BottomLeft => Vec3::new(width / 2.0, height / 2.0, 0.0),
VelloAssetAnchor::Top => Vec3::new(0.0, -height / 2.0, 0.0),
VelloAssetAnchor::Center => Vec3::new(0.0, 0.0, 0.0),
VelloAssetAnchor::Bottom => Vec3::new(0.0, height / 2.0, 0.0),
VelloAssetAnchor::TopRight => Vec3::new(-width / 2.0, -height / 2.0, 0.0),
VelloAssetAnchor::Right => Vec3::new(-width / 2.0, 0.0, 0.0),
VelloAssetAnchor::BottomRight => Vec3::new(-width / 2.0, height / 2.0, 0.0),
};
let new_translation: Vec3 = (transform.compute_matrix() * adjustment.extend(1.0)).xyz();
GlobalTransform::from(
Expand Down
6 changes: 3 additions & 3 deletions src/integrations/lottie/asset_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl AssetLoader for VelloLottieLoader {
debug!("parsing {}...", load_context.path().display());
match ext {
"json" => {
let vello_vector = load_lottie_from_bytes(&bytes)?;
let asset = load_lottie_from_bytes(&bytes)?;
info!(
path = format!("{}", load_context.path().display()),
size = format!("{:?}", (vello_vector.width, vello_vector.height)),
size = format!("{:?}", (asset.width, asset.height)),
"finished parsing lottie json asset"
);
Ok(vello_vector)
Ok(asset)
}
ext => Err(VectorLoaderError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
Expand Down
4 changes: 2 additions & 2 deletions src/integrations/lottie/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn load_lottie_from_bytes(bytes: &[u8]) -> Result<VelloAsset, VectorLoaderEr
let width = composition.width as f32;
let height = composition.height as f32;

let vello_vector = VelloAsset {
let asset = VelloAsset {
file: VectorFile::Lottie(Arc::new(composition)),
local_transform_center: {
let mut transform = Transform::default();
Expand All @@ -23,7 +23,7 @@ pub fn load_lottie_from_bytes(bytes: &[u8]) -> Result<VelloAsset, VectorLoaderEr
alpha: 1.0,
};

Ok(vello_vector)
Ok(asset)
}

/// Deserialize a Lottie file from a string slice.
Expand Down
2 changes: 1 addition & 1 deletion src/integrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod error;
pub use error::VectorLoaderError;

mod asset;
pub use asset::{VelloAsset, VelloAssetAlignment};
pub use asset::{VelloAsset, VelloAssetAnchor};

#[derive(Clone)]
pub enum VectorFile {
Expand Down
6 changes: 3 additions & 3 deletions src/integrations/svg/asset_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl AssetLoader for VelloSvgLoader {
debug!("parsing {}...", load_context.path().display());
match ext {
"svg" => {
let vello_vector = load_svg_from_bytes(&bytes)?;
let asset = load_svg_from_bytes(&bytes)?;
info!(
path = format!("{}", load_context.path().display()),
size = format!("{:?}", (vello_vector.width, vello_vector.height)),
size = format!("{:?}", (asset.width, asset.height)),
"finished parsing svg asset"
);
Ok(vello_vector)
Ok(asset)
}
ext => Err(VectorLoaderError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidData,
Expand Down
Loading

0 comments on commit 864cbc9

Please sign in to comment.