mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
can specify an anchor for a sprite (#3463)
# Objective - Fixes #1616, fixes #2225 - Let user specify an anchor for a sprite ## Solution - Add an enum for an anchor point for most common values, with a variant for a custom point - Defaults to Center to not change current behaviour Co-authored-by: François <8672791+mockersf@users.noreply.github.com>
This commit is contained in:
parent
dba7790012
commit
8e864fdd18
4 changed files with 53 additions and 3 deletions
|
@ -184,6 +184,7 @@ pub struct ExtractedSprite {
|
|||
pub image_handle_id: HandleId,
|
||||
pub flip_x: bool,
|
||||
pub flip_y: bool,
|
||||
pub anchor: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -248,6 +249,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() {
|
||||
|
@ -266,6 +268,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(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -489,7 +492,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()
|
||||
});
|
||||
|
||||
|
|
|
@ -15,4 +15,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.
|
||||
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),
|
||||
Anchor::Custom(point) => *point,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::Rect;
|
||||
use crate::{Anchor, Rect};
|
||||
use bevy_asset::Handle;
|
||||
use bevy_ecs::component::Component;
|
||||
use bevy_math::Vec2;
|
||||
|
@ -30,6 +30,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 {
|
||||
|
@ -40,6 +41,7 @@ impl Default for TextureAtlasSprite {
|
|||
flip_x: false,
|
||||
flip_y: false,
|
||||
custom_size: None,
|
||||
anchor: Anchor::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ use bevy_ecs::{
|
|||
use bevy_math::{Size, Vec3};
|
||||
use bevy_reflect::Reflect;
|
||||
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::{WindowId, Windows};
|
||||
|
||||
|
@ -116,6 +116,7 @@ pub fn extract_text2d_sprite(
|
|||
image_handle_id: handle.id,
|
||||
flip_x: false,
|
||||
flip_y: false,
|
||||
anchor: Anchor::Center.as_vec(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue