Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - can specify an anchor for a sprite #3463

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ pub struct ExtractedSprite {
pub image_handle_id: HandleId,
pub flip_x: bool,
pub flip_y: bool,
pub anchor: Vec2,
}

#[derive(Default)]
Expand Down Expand Up @@ -257,6 +258,7 @@ pub fn extract_sprites(
flip_x: sprite.flip_x,
flip_y: sprite.flip_y,
image_handle_id: handle.id,
anchor: sprite.anchor.as_vec(),
});
}
for (visibility, atlas_sprite, transform, texture_atlas_handle) in atlas_query.iter() {
Expand All @@ -275,6 +277,7 @@ pub fn extract_sprites(
flip_x: atlas_sprite.flip_x,
flip_y: atlas_sprite.flip_y,
image_handle_id: texture_atlas.texture.id,
anchor: atlas_sprite.anchor.as_vec(),
});
}
}
Expand Down Expand Up @@ -498,7 +501,7 @@ pub fn queue_sprites(
let positions = QUAD_VERTEX_POSITIONS.map(|quad_pos| {
extracted_sprite
.transform
.mul_vec3((quad_pos * quad_size).extend(0.))
.mul_vec3(((quad_pos - extracted_sprite.anchor) * quad_size).extend(0.))
.into()
});

Expand Down
44 changes: 44 additions & 0 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,48 @@ pub struct Sprite {
/// An optional custom size for the sprite that will be used when rendering, instead of the size
/// of the sprite's image
pub custom_size: Option<Vec2>,
/// [`Anchor`] point of the sprite in the world
pub anchor: Anchor,
}

/// How a sprite is positioned relative to its [`Transform`](bevy_transform::components::Transform).
/// It defaults to `Anchor::Center`.
#[derive(Debug, Clone, Reflect)]
#[doc(alias = "pivot")]
pub enum Anchor {
Center,
BottomLeft,
BottomCenter,
BottomRight,
CenterLeft,
CenterRight,
TopLeft,
TopCenter,
TopRight,
/// Custom anchor point. Top left is `(-0.5, 0.5)`, center is `(0.0, 0.0)`. The value will
/// be scaled with the sprite size.
Comment on lines +37 to +38
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought, but should this be (0, 0) bottom left to (1, 1) top right? I think that might be clearer for those who want to select an anchor as an absolute position using Custom rather than as an offset from the center point (which could also maybe play into UI stuff as well).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that (0, 0) is the default behavior personally.

It doesn't matter much as long as it's clearly documented though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default being centered is good in my opinion as well.

Custom(Vec2),
}

impl Default for Anchor {
fn default() -> Self {
Anchor::Center
}
}

impl Anchor {
pub fn as_vec(&self) -> Vec2 {
match self {
Anchor::Center => Vec2::ZERO,
Anchor::BottomLeft => Vec2::new(-0.5, -0.5),
Anchor::BottomCenter => Vec2::new(0.0, -0.5),
Anchor::BottomRight => Vec2::new(0.5, -0.5),
Anchor::CenterLeft => Vec2::new(-0.5, 0.0),
Anchor::CenterRight => Vec2::new(0.5, 0.0),
Anchor::TopLeft => Vec2::new(-0.5, 0.5),
Anchor::TopCenter => Vec2::new(0.0, 0.5),
Anchor::TopRight => Vec2::new(0.5, 0.5),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like that you provided these anchors for convenience.

Anchor::Custom(point) => *point,
}
}
}
4 changes: 3 additions & 1 deletion crates/bevy_sprite/src/texture_atlas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Rect;
use crate::{Anchor, Rect};
use bevy_asset::Handle;
use bevy_ecs::component::Component;
use bevy_math::Vec2;
Expand Down Expand Up @@ -31,6 +31,7 @@ pub struct TextureAtlasSprite {
/// An optional custom size for the sprite that will be used when rendering, instead of the size
/// of the sprite's image in the atlas
pub custom_size: Option<Vec2>,
pub anchor: Anchor,
}

impl Default for TextureAtlasSprite {
Expand All @@ -41,6 +42,7 @@ impl Default for TextureAtlasSprite {
flip_x: false,
flip_y: false,
custom_size: None,
anchor: Anchor::default(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bevy_ecs::{
};
use bevy_math::{Size, Vec3};
use bevy_render::{texture::Image, view::Visibility, RenderWorld};
use bevy_sprite::{ExtractedSprite, ExtractedSprites, TextureAtlas};
use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_window::Windows;

Expand Down Expand Up @@ -103,6 +103,7 @@ pub fn extract_text2d_sprite(
image_handle_id: handle.id,
flip_x: false,
flip_y: false,
anchor: Anchor::Center.as_vec(),
});
}
}
Expand Down