Skip to content

Commit 7a7f097

Browse files
KDecayKDecay
KDecay
and
KDecay
committed
Move Size to bevy_ui (#4285)
# Objective - Related #4276. - Part of the splitting process of #3503. ## Solution - Move `Size` to `bevy_ui`. ## Reasons - `Size` is only needed in `bevy_ui` (because it needs to use `Val` instead of `f32`), but it's also used as a worse `Vec2` replacement in other areas. - `Vec2` is more powerful than `Size` so it should be used whenever possible. - Discussion in #3503. ## Changelog ### Changed - The `Size` type got moved from `bevy_math` to `bevy_ui`. ## Migration Guide - The `Size` type got moved from `bevy::math` to `bevy::ui`. To migrate you just have to import `bevy::ui::Size` instead of `bevy::math::Math` or use the `bevy::prelude` instead. Co-authored-by: KDecay <[email protected]>
1 parent 0850975 commit 7a7f097

File tree

16 files changed

+201
-196
lines changed

16 files changed

+201
-196
lines changed

crates/bevy_math/src/geometry.rs

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
11
use bevy_reflect::Reflect;
2-
use glam::Vec2;
3-
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
4-
5-
/// A two dimensional "size" as defined by a width and height
6-
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
7-
#[reflect(PartialEq)]
8-
pub struct Size<T: Reflect + PartialEq = f32> {
9-
pub width: T,
10-
pub height: T,
11-
}
12-
13-
impl<T: Reflect + PartialEq> Size<T> {
14-
pub fn new(width: T, height: T) -> Self {
15-
Size { width, height }
16-
}
17-
}
18-
19-
impl<T: Default + Reflect + PartialEq> Default for Size<T> {
20-
fn default() -> Self {
21-
Self {
22-
width: Default::default(),
23-
height: Default::default(),
24-
}
25-
}
26-
}
272

283
/// A rect, as defined by its "side" locations
294
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
@@ -59,126 +34,3 @@ impl<T: Default + Reflect + PartialEq> Default for Rect<T> {
5934
}
6035
}
6136
}
62-
63-
impl<T: Reflect + PartialEq> Add<Vec2> for Size<T>
64-
where
65-
T: Add<f32, Output = T>,
66-
{
67-
type Output = Size<T>;
68-
69-
fn add(self, rhs: Vec2) -> Self::Output {
70-
Self {
71-
width: self.width + rhs.x,
72-
height: self.height + rhs.y,
73-
}
74-
}
75-
}
76-
77-
impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
78-
where
79-
T: AddAssign<f32>,
80-
{
81-
fn add_assign(&mut self, rhs: Vec2) {
82-
self.width += rhs.x;
83-
self.height += rhs.y;
84-
}
85-
}
86-
87-
impl<T: Reflect + PartialEq> Sub<Vec2> for Size<T>
88-
where
89-
T: Sub<f32, Output = T>,
90-
{
91-
type Output = Size<T>;
92-
93-
fn sub(self, rhs: Vec2) -> Self::Output {
94-
Self {
95-
width: self.width - rhs.x,
96-
height: self.height - rhs.y,
97-
}
98-
}
99-
}
100-
101-
impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
102-
where
103-
T: SubAssign<f32>,
104-
{
105-
fn sub_assign(&mut self, rhs: Vec2) {
106-
self.width -= rhs.x;
107-
self.height -= rhs.y;
108-
}
109-
}
110-
111-
impl<T: Reflect + PartialEq> Mul<f32> for Size<T>
112-
where
113-
T: Mul<f32, Output = T>,
114-
{
115-
type Output = Size<T>;
116-
117-
fn mul(self, rhs: f32) -> Self::Output {
118-
Self::Output {
119-
width: self.width * rhs,
120-
height: self.height * rhs,
121-
}
122-
}
123-
}
124-
125-
impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
126-
where
127-
T: MulAssign<f32>,
128-
{
129-
fn mul_assign(&mut self, rhs: f32) {
130-
self.width *= rhs;
131-
self.height *= rhs;
132-
}
133-
}
134-
135-
impl<T: Reflect + PartialEq> Div<f32> for Size<T>
136-
where
137-
T: Div<f32, Output = T>,
138-
{
139-
type Output = Size<T>;
140-
141-
fn div(self, rhs: f32) -> Self::Output {
142-
Self::Output {
143-
width: self.width / rhs,
144-
height: self.height / rhs,
145-
}
146-
}
147-
}
148-
149-
impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
150-
where
151-
T: DivAssign<f32>,
152-
{
153-
fn div_assign(&mut self, rhs: f32) {
154-
self.width /= rhs;
155-
self.height /= rhs;
156-
}
157-
}
158-
159-
#[cfg(test)]
160-
mod tests {
161-
use super::*;
162-
163-
#[test]
164-
fn size_ops() {
165-
type SizeF = Size<f32>;
166-
167-
assert_eq!(
168-
SizeF::new(10., 10.) + Vec2::new(10., 10.),
169-
SizeF::new(20., 20.)
170-
);
171-
assert_eq!(
172-
SizeF::new(20., 20.) - Vec2::new(10., 10.),
173-
SizeF::new(10., 10.)
174-
);
175-
assert_eq!(SizeF::new(10., 10.) * 2., SizeF::new(20., 20.));
176-
assert_eq!(SizeF::new(20., 20.) / 2., SizeF::new(10., 10.));
177-
178-
let mut size = SizeF::new(10., 10.);
179-
180-
size += Vec2::new(10., 10.);
181-
182-
assert_eq!(size, SizeF::new(20., 20.));
183-
}
184-
}

crates/bevy_math/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use glam::*;
66
pub mod prelude {
77
#[doc(hidden)]
88
pub use crate::{
9-
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, Size, UVec2,
10-
UVec3, UVec4, Vec2, Vec3, Vec4,
9+
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, UVec2, UVec3,
10+
UVec4, Vec2, Vec3, Vec4,
1111
};
1212
}

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bevy_ecs::{
99
prelude::*,
1010
system::{lifetimeless::*, SystemParamItem},
1111
};
12-
use bevy_math::{Mat4, Size};
12+
use bevy_math::{Mat4, Vec2};
1313
use bevy_reflect::TypeUuid;
1414
use bevy_render::{
1515
mesh::{
@@ -458,7 +458,7 @@ impl FromWorld for MeshPipeline {
458458
texture_view,
459459
texture_format: image.texture_descriptor.format,
460460
sampler,
461-
size: Size::new(
461+
size: Vec2::new(
462462
image.texture_descriptor.size.width as f32,
463463
image.texture_descriptor.size.height as f32,
464464
),

crates/bevy_render/src/texture/image.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
};
1515
use bevy_asset::HandleUntyped;
1616
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
17-
use bevy_math::{Size, Vec2};
17+
use bevy_math::Vec2;
1818
use bevy_reflect::TypeUuid;
1919
use thiserror::Error;
2020
use wgpu::{
@@ -533,14 +533,14 @@ impl TextureFormatPixelInfo for TextureFormat {
533533
}
534534

535535
/// The GPU-representation of an [`Image`].
536-
/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's [`Size`].
536+
/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's size.
537537
#[derive(Debug, Clone)]
538538
pub struct GpuImage {
539539
pub texture: Texture,
540540
pub texture_view: TextureView,
541541
pub texture_format: TextureFormat,
542542
pub sampler: Sampler,
543-
pub size: Size,
543+
pub size: Vec2,
544544
}
545545

546546
impl RenderAsset for Image {
@@ -595,7 +595,7 @@ impl RenderAsset for Image {
595595
};
596596

597597
let texture_view = texture.create_view(&TextureViewDescriptor::default());
598-
let size = Size::new(
598+
let size = Vec2::new(
599599
image.texture_descriptor.size.width as f32,
600600
image.texture_descriptor.size.height as f32,
601601
);

crates/bevy_sprite/src/mesh2d/mesh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_ecs::{
44
prelude::*,
55
system::{lifetimeless::*, SystemParamItem},
66
};
7-
use bevy_math::{Mat4, Size};
7+
use bevy_math::{Mat4, Vec2};
88
use bevy_reflect::{Reflect, TypeUuid};
99
use bevy_render::{
1010
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
@@ -195,7 +195,7 @@ impl FromWorld for Mesh2dPipeline {
195195
texture_view,
196196
texture_format: image.texture_descriptor.format,
197197
sampler,
198-
size: Size::new(
198+
size: Vec2::new(
199199
image.texture_descriptor.size.width as f32,
200200
image.texture_descriptor.size.height as f32,
201201
),

crates/bevy_sprite/src/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ pub fn queue_sprites(
431431
gpu_images.get(&Handle::weak(new_batch.image_handle_id))
432432
{
433433
current_batch = new_batch;
434-
current_image_size = Vec2::new(gpu_image.size.width, gpu_image.size.height);
434+
current_image_size = Vec2::new(gpu_image.size.x, gpu_image.size.y);
435435
current_batch_entity = commands.spawn_bundle((current_batch,)).id();
436436

437437
image_bind_groups

crates/bevy_text/src/glyph_brush.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ab_glyph::{Font as _, FontArc, Glyph, ScaleFont as _};
22
use bevy_asset::{Assets, Handle};
3-
use bevy_math::{Size, Vec2};
3+
use bevy_math::Vec2;
44
use bevy_render::texture::Image;
55
use bevy_sprite::TextureAtlas;
66
use glyph_brush_layout::{
@@ -29,11 +29,11 @@ impl GlyphBrush {
2929
pub fn compute_glyphs<S: ToSectionText>(
3030
&self,
3131
sections: &[S],
32-
bounds: Size,
32+
bounds: Vec2,
3333
text_alignment: TextAlignment,
3434
) -> Result<Vec<SectionGlyph>, TextError> {
3535
let geom = SectionGeometry {
36-
bounds: (bounds.width, bounds.height),
36+
bounds: (bounds.x, bounds.y),
3737
..Default::default()
3838
};
3939
let section_glyphs = Layout::default()

crates/bevy_text/src/pipeline.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::hash::Hash;
22

33
use ab_glyph::{PxScale, ScaleFont};
44
use bevy_asset::{Assets, Handle, HandleId};
5-
use bevy_math::Size;
5+
use bevy_math::Vec2;
66
use bevy_render::texture::Image;
77
use bevy_sprite::TextureAtlas;
88
use bevy_utils::HashMap;
@@ -32,7 +32,7 @@ impl<ID> Default for TextPipeline<ID> {
3232

3333
pub struct TextLayoutInfo {
3434
pub glyphs: Vec<PositionedGlyph>,
35-
pub size: Size,
35+
pub size: Vec2,
3636
}
3737

3838
impl<ID: Hash + Eq> TextPipeline<ID> {
@@ -56,7 +56,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
5656
sections: &[TextSection],
5757
scale_factor: f64,
5858
text_alignment: TextAlignment,
59-
bounds: Size,
59+
bounds: Vec2,
6060
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
6161
texture_atlases: &mut Assets<TextureAtlas>,
6262
textures: &mut Assets<Image>,
@@ -92,7 +92,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
9292
id,
9393
TextLayoutInfo {
9494
glyphs: Vec::new(),
95-
size: Size::new(0., 0.),
95+
size: Vec2::new(0., 0.),
9696
},
9797
);
9898
return Ok(());
@@ -112,7 +112,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
112112
max_y = max_y.max(glyph.position.y - scaled_font.descent());
113113
}
114114

115-
let size = Size::new(max_x - min_x, max_y - min_y);
115+
let size = Vec2::new(max_x - min_x, max_y - min_y);
116116

117117
let glyphs = self.brush.process_glyphs(
118118
section_glyphs,

crates/bevy_text/src/text2d.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy_ecs::{
77
reflect::ReflectComponent,
88
system::{Local, ParamSet, Query, Res, ResMut},
99
};
10-
use bevy_math::{Size, Vec3};
10+
use bevy_math::{Vec2, Vec3};
1111
use bevy_reflect::Reflect;
1212
use bevy_render::{texture::Image, view::Visibility, RenderWorld};
1313
use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas};
@@ -22,7 +22,7 @@ use crate::{
2222
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
2323
#[reflect(Component)]
2424
pub struct Text2dSize {
25-
pub size: Size,
25+
pub size: Vec2,
2626
}
2727

2828
/// The maximum width and height of text. The text will wrap according to the specified size.
@@ -35,13 +35,13 @@ pub struct Text2dSize {
3535
#[derive(Component, Copy, Clone, Debug, Reflect)]
3636
#[reflect(Component)]
3737
pub struct Text2dBounds {
38-
pub size: Size,
38+
pub size: Vec2,
3939
}
4040

4141
impl Default for Text2dBounds {
4242
fn default() -> Self {
4343
Self {
44-
size: Size::new(f32::MAX, f32::MAX),
44+
size: Vec2::new(f32::MAX, f32::MAX),
4545
}
4646
}
4747
}
@@ -73,7 +73,7 @@ pub fn extract_text2d_sprite(
7373
if !visibility.is_visible {
7474
continue;
7575
}
76-
let (width, height) = (calculated_size.size.width, calculated_size.size.height);
76+
let (width, height) = (calculated_size.size.x, calculated_size.size.y);
7777

7878
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
7979
let text_glyphs = &text_layout.glyphs;
@@ -161,11 +161,11 @@ pub fn text2d_system(
161161
for entity in queued_text.entities.drain(..) {
162162
if let Ok((text, bounds, mut calculated_size)) = query.get_mut(entity) {
163163
let text_bounds = match bounds {
164-
Some(bounds) => Size {
165-
width: scale_value(bounds.size.width, scale_factor),
166-
height: scale_value(bounds.size.height, scale_factor),
167-
},
168-
None => Size::new(f32::MAX, f32::MAX),
164+
Some(bounds) => Vec2::new(
165+
scale_value(bounds.size.x, scale_factor),
166+
scale_value(bounds.size.y, scale_factor),
167+
),
168+
None => Vec2::new(f32::MAX, f32::MAX),
169169
};
170170
match text_pipeline.queue_text(
171171
entity,
@@ -190,10 +190,10 @@ pub fn text2d_system(
190190
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
191191
"Failed to get glyphs from the pipeline that have just been computed",
192192
);
193-
calculated_size.size = Size {
194-
width: scale_value(text_layout_info.size.width, 1. / scale_factor),
195-
height: scale_value(text_layout_info.size.height, 1. / scale_factor),
196-
};
193+
calculated_size.size = Vec2::new(
194+
scale_value(text_layout_info.size.x, 1. / scale_factor),
195+
scale_value(text_layout_info.size.y, 1. / scale_factor),
196+
);
197197
}
198198
}
199199
}

crates/bevy_ui/src/flex/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
3-
JustifyContent, PositionType, Style, Val,
3+
JustifyContent, PositionType, Size, Style, Val,
44
};
5-
use bevy_math::{Rect, Size};
5+
use bevy_math::Rect;
66

77
pub fn from_rect(
88
scale_factor: f64,

0 commit comments

Comments
 (0)