Skip to content

Commit 84b09b9

Browse files
authored
Newtype Anchor (#18439)
# Objective The `Anchor` component doesn't need to be a enum. The variants are just mapped to `Vec2`s so it could be changed to a newtype with associated const values, saving the space needed for the discriminator by the enum. Also there was no benefit I think in hiding the underlying `Vec2` representation of `Anchor`s. Suggested by @atlv24. Fixes #18459 Fixes #18460 ## Solution Change `Anchor` to a struct newtyping a `Vec2`, and its variants into associated constants. ## Migration Guide The anchor component has been changed from an enum to a struct newtyping a `Vec2`. The `Custom` variant has been removed, instead to construct a custom `Anchor` use its tuple constructor: ```rust Sprite { anchor: Anchor(Vec2::new(0.25, 0.4)), ..default() } ``` The other enum variants have been replaced with corresponding constants: * `Anchor::BottomLeft` to `Anchor::BOTTOM_LEFT` * `Anchor::Center` to `Anchor::CENTER` * `Anchor::TopRight` to `Anchor::TOP_RIGHT` * .. and so on for the remaining variants
1 parent 9ae7aa4 commit 84b09b9

File tree

10 files changed

+59
-63
lines changed

10 files changed

+59
-63
lines changed

crates/bevy_sprite/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ mod test {
331331
.world_mut()
332332
.spawn(Sprite {
333333
rect: Some(Rect::new(0., 0., 0.5, 1.)),
334-
anchor: Anchor::TopRight,
334+
anchor: Anchor::TOP_RIGHT,
335335
image: image_handle,
336336
..default()
337337
})

crates/bevy_sprite/src/sprite.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bevy_asset::{Assets, Handle};
22
use bevy_color::Color;
3+
use bevy_derive::{Deref, DerefMut};
34
use bevy_ecs::{component::Component, reflect::ReflectComponent};
45
use bevy_image::{Image, TextureAtlas, TextureAtlasLayout};
56
use bevy_math::{Rect, UVec2, Vec2};
@@ -240,41 +241,37 @@ pub enum ScalingMode {
240241
FitEnd,
241242
}
242243

243-
/// How a sprite is positioned relative to its [`Transform`].
244-
/// It defaults to `Anchor::Center`.
245-
#[derive(Component, Debug, Clone, Copy, PartialEq, Default, Reflect)]
244+
/// Normalized (relative to its size) offset of a 2d renderable entity from its [`Transform`].
245+
#[derive(Component, Debug, Clone, Copy, PartialEq, Deref, DerefMut, Reflect)]
246246
#[reflect(Component, Default, Debug, PartialEq, Clone)]
247247
#[doc(alias = "pivot")]
248-
pub enum Anchor {
249-
#[default]
250-
Center,
251-
BottomLeft,
252-
BottomCenter,
253-
BottomRight,
254-
CenterLeft,
255-
CenterRight,
256-
TopLeft,
257-
TopCenter,
258-
TopRight,
259-
/// Custom anchor point. Top left is `(-0.5, 0.5)`, center is `(0.0, 0.0)`. The value will
260-
/// be scaled with the sprite size.
261-
Custom(Vec2),
262-
}
248+
pub struct Anchor(pub Vec2);
263249

264250
impl Anchor {
251+
pub const BOTTOM_LEFT: Self = Self(Vec2::new(-0.5, -0.5));
252+
pub const BOTTOM_CENTER: Self = Self(Vec2::new(0.0, -0.5));
253+
pub const BOTTOM_RIGHT: Self = Self(Vec2::new(0.5, -0.5));
254+
pub const CENTER_LEFT: Self = Self(Vec2::new(-0.5, 0.0));
255+
pub const CENTER: Self = Self(Vec2::ZERO);
256+
pub const CENTER_RIGHT: Self = Self(Vec2::new(0.5, 0.0));
257+
pub const TOP_LEFT: Self = Self(Vec2::new(-0.5, 0.5));
258+
pub const TOP_CENTER: Self = Self(Vec2::new(0.0, 0.5));
259+
pub const TOP_RIGHT: Self = Self(Vec2::new(0.5, 0.5));
260+
265261
pub fn as_vec(&self) -> Vec2 {
266-
match self {
267-
Anchor::Center => Vec2::ZERO,
268-
Anchor::BottomLeft => Vec2::new(-0.5, -0.5),
269-
Anchor::BottomCenter => Vec2::new(0.0, -0.5),
270-
Anchor::BottomRight => Vec2::new(0.5, -0.5),
271-
Anchor::CenterLeft => Vec2::new(-0.5, 0.0),
272-
Anchor::CenterRight => Vec2::new(0.5, 0.0),
273-
Anchor::TopLeft => Vec2::new(-0.5, 0.5),
274-
Anchor::TopCenter => Vec2::new(0.0, 0.5),
275-
Anchor::TopRight => Vec2::new(0.5, 0.5),
276-
Anchor::Custom(point) => *point,
277-
}
262+
self.0
263+
}
264+
}
265+
266+
impl Default for Anchor {
267+
fn default() -> Self {
268+
Self::CENTER
269+
}
270+
}
271+
272+
impl From<Vec2> for Anchor {
273+
fn from(value: Vec2) -> Self {
274+
Self(value)
278275
}
279276
}
280277

@@ -358,7 +355,7 @@ mod tests {
358355

359356
let sprite = Sprite {
360357
image,
361-
anchor: Anchor::BottomLeft,
358+
anchor: Anchor::BOTTOM_LEFT,
362359
..Default::default()
363360
};
364361

@@ -380,7 +377,7 @@ mod tests {
380377

381378
let sprite = Sprite {
382379
image,
383-
anchor: Anchor::TopRight,
380+
anchor: Anchor::TOP_RIGHT,
384381
..Default::default()
385382
};
386383

@@ -402,7 +399,7 @@ mod tests {
402399

403400
let sprite = Sprite {
404401
image,
405-
anchor: Anchor::BottomLeft,
402+
anchor: Anchor::BOTTOM_LEFT,
406403
flip_x: true,
407404
..Default::default()
408405
};
@@ -425,7 +422,7 @@ mod tests {
425422

426423
let sprite = Sprite {
427424
image,
428-
anchor: Anchor::TopRight,
425+
anchor: Anchor::TOP_RIGHT,
429426
flip_y: true,
430427
..Default::default()
431428
};
@@ -449,7 +446,7 @@ mod tests {
449446
let sprite = Sprite {
450447
image,
451448
rect: Some(Rect::new(1.5, 3.0, 3.0, 9.5)),
452-
anchor: Anchor::BottomLeft,
449+
anchor: Anchor::BOTTOM_LEFT,
453450
..Default::default()
454451
};
455452

@@ -473,7 +470,7 @@ mod tests {
473470

474471
let sprite = Sprite {
475472
image,
476-
anchor: Anchor::BottomLeft,
473+
anchor: Anchor::BOTTOM_LEFT,
477474
texture_atlas: Some(TextureAtlas {
478475
layout: texture_atlas,
479476
index: 0,
@@ -501,7 +498,7 @@ mod tests {
501498

502499
let sprite = Sprite {
503500
image,
504-
anchor: Anchor::BottomLeft,
501+
anchor: Anchor::BOTTOM_LEFT,
505502
texture_atlas: Some(TextureAtlas {
506503
layout: texture_atlas,
507504
index: 0,

crates/bevy_text/src/text2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn extract_text2d_sprite(
213213
image_handle_id: atlas_info.texture.id(),
214214
flip_x: false,
215215
flip_y: false,
216-
anchor: Anchor::Center.as_vec(),
216+
anchor: Anchor::CENTER.as_vec(),
217217
original_entity,
218218
scaling_mode: None,
219219
});

examples/2d/sprite_scale.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn setup_sprites(mut commands: Commands, asset_server: Res<AssetServer>) {
132132
TextLayout::new_with_justify(JustifyText::Center),
133133
TextFont::from_font_size(15.),
134134
Transform::from_xyz(0., -0.5 * rect.size.y - 10., 0.),
135-
bevy::sprite::Anchor::TopCenter,
135+
bevy::sprite::Anchor::TOP_CENTER,
136136
));
137137
});
138138
}
@@ -278,7 +278,7 @@ fn setup_texture_atlas(
278278
TextLayout::new_with_justify(JustifyText::Center),
279279
TextFont::from_font_size(15.),
280280
Transform::from_xyz(0., -0.5 * sprite_sheet.size.y - 10., 0.),
281-
bevy::sprite::Anchor::TopCenter,
281+
bevy::sprite::Anchor::TOP_CENTER,
282282
));
283283
});
284284
}

examples/2d/sprite_slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn spawn_sprites(
9696
text_style,
9797
TextLayout::new_with_justify(JustifyText::Center),
9898
Transform::from_xyz(0., -0.5 * size.y - 10., 0.0),
99-
bevy::sprite::Anchor::TopCenter,
99+
bevy::sprite::Anchor::TOP_CENTER,
100100
)],
101101
));
102102
position.x += 0.5 * size.x + gap;

examples/2d/text2d.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
129129
))
130130
.with_children(|commands| {
131131
for (text_anchor, color) in [
132-
(Anchor::TopLeft, Color::Srgba(LIGHT_SALMON)),
133-
(Anchor::TopRight, Color::Srgba(LIGHT_GREEN)),
134-
(Anchor::BottomRight, Color::Srgba(LIGHT_BLUE)),
135-
(Anchor::BottomLeft, Color::Srgba(LIGHT_YELLOW)),
132+
(Anchor::TOP_LEFT, Color::Srgba(LIGHT_SALMON)),
133+
(Anchor::TOP_RIGHT, Color::Srgba(LIGHT_GREEN)),
134+
(Anchor::BOTTOM_RIGHT, Color::Srgba(LIGHT_BLUE)),
135+
(Anchor::BOTTOM_LEFT, Color::Srgba(LIGHT_YELLOW)),
136136
] {
137137
commands
138138
.spawn((

examples/picking/sprite_picking.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,15 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3838
.spawn((Transform::default(), Visibility::default()))
3939
.with_children(|commands| {
4040
for (anchor_index, anchor) in [
41-
Anchor::TopLeft,
42-
Anchor::TopCenter,
43-
Anchor::TopRight,
44-
Anchor::CenterLeft,
45-
Anchor::Center,
46-
Anchor::CenterRight,
47-
Anchor::BottomLeft,
48-
Anchor::BottomCenter,
49-
Anchor::BottomRight,
50-
Anchor::Custom(Vec2::new(0.5, 0.5)),
41+
Anchor::TOP_LEFT,
42+
Anchor::TOP_CENTER,
43+
Anchor::TOP_RIGHT,
44+
Anchor::CENTER_LEFT,
45+
Anchor::CENTER,
46+
Anchor::CENTER_RIGHT,
47+
Anchor::BOTTOM_LEFT,
48+
Anchor::BOTTOM_CENTER,
49+
Anchor::BOTTOM_RIGHT,
5150
]
5251
.iter()
5352
.enumerate()

examples/stress_tests/many_glyphs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn setup(mut commands: Commands, args: Res<Args>) {
101101
Text2d::new(text_string),
102102
text_font.clone(),
103103
TextColor(RED.into()),
104-
bevy::sprite::Anchor::Center,
104+
bevy::sprite::Anchor::CENTER,
105105
TextBounds::new_horizontal(1000.),
106106
text_block,
107107
));

examples/testbed/2d.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ mod text {
215215
));
216216

217217
for anchor in [
218-
Anchor::TopLeft,
219-
Anchor::TopRight,
220-
Anchor::BottomRight,
221-
Anchor::BottomLeft,
218+
Anchor::TOP_LEFT,
219+
Anchor::TOP_RIGHT,
220+
Anchor::BOTTOM_RIGHT,
221+
Anchor::BOTTOM_LEFT,
222222
] {
223223
let mut text = commands.spawn((
224224
Text2d::new("L R\n"),
@@ -229,7 +229,7 @@ mod text {
229229
));
230230
text.with_children(|parent| {
231231
parent.spawn((
232-
TextSpan::new(format!("{anchor:?}\n")),
232+
TextSpan::new(format!("{}, {}\n", anchor.x, anchor.y)),
233233
TextFont::from_font_size(14.0),
234234
TextColor(palettes::tailwind::BLUE_400.into()),
235235
));

examples/tools/gamepad_viewer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn setup_sticks(
287287
(
288288
Text2d::default(),
289289
Transform::from_xyz(0., STICK_BOUNDS_SIZE + 2., 4.),
290-
Anchor::BottomCenter,
290+
Anchor::BOTTOM_CENTER,
291291
TextWithAxes { x_axis, y_axis },
292292
children![
293293
(TextSpan(format!("{:.3}", 0.)), style.clone()),

0 commit comments

Comments
 (0)