Skip to content

Commit

Permalink
Move Size to bevy_ui (bevyengine#4285)
Browse files Browse the repository at this point in the history
# Objective

- Related bevyengine#4276.
- Part of the splitting process of bevyengine#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 bevyengine#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 <KDecayMusic@protonmail.com>
  • Loading branch information
2 people authored and exjam committed May 22, 2022
1 parent cd4c862 commit ba64fc9
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 196 deletions.
148 changes: 0 additions & 148 deletions crates/bevy_math/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
use bevy_reflect::Reflect;
use glam::Vec2;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

/// A two dimensional "size" as defined by a width and height
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct Size<T: Reflect + PartialEq = f32> {
pub width: T,
pub height: T,
}

impl<T: Reflect + PartialEq> Size<T> {
pub fn new(width: T, height: T) -> Self {
Size { width, height }
}
}

impl<T: Default + Reflect + PartialEq> Default for Size<T> {
fn default() -> Self {
Self {
width: Default::default(),
height: Default::default(),
}
}
}

/// A rect, as defined by its "side" locations
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
Expand Down Expand Up @@ -59,126 +34,3 @@ impl<T: Default + Reflect + PartialEq> Default for Rect<T> {
}
}
}

impl<T: Reflect + PartialEq> Add<Vec2> for Size<T>
where
T: Add<f32, Output = T>,
{
type Output = Size<T>;

fn add(self, rhs: Vec2) -> Self::Output {
Self {
width: self.width + rhs.x,
height: self.height + rhs.y,
}
}
}

impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
where
T: AddAssign<f32>,
{
fn add_assign(&mut self, rhs: Vec2) {
self.width += rhs.x;
self.height += rhs.y;
}
}

impl<T: Reflect + PartialEq> Sub<Vec2> for Size<T>
where
T: Sub<f32, Output = T>,
{
type Output = Size<T>;

fn sub(self, rhs: Vec2) -> Self::Output {
Self {
width: self.width - rhs.x,
height: self.height - rhs.y,
}
}
}

impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
where
T: SubAssign<f32>,
{
fn sub_assign(&mut self, rhs: Vec2) {
self.width -= rhs.x;
self.height -= rhs.y;
}
}

impl<T: Reflect + PartialEq> Mul<f32> for Size<T>
where
T: Mul<f32, Output = T>,
{
type Output = Size<T>;

fn mul(self, rhs: f32) -> Self::Output {
Self::Output {
width: self.width * rhs,
height: self.height * rhs,
}
}
}

impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
where
T: MulAssign<f32>,
{
fn mul_assign(&mut self, rhs: f32) {
self.width *= rhs;
self.height *= rhs;
}
}

impl<T: Reflect + PartialEq> Div<f32> for Size<T>
where
T: Div<f32, Output = T>,
{
type Output = Size<T>;

fn div(self, rhs: f32) -> Self::Output {
Self::Output {
width: self.width / rhs,
height: self.height / rhs,
}
}
}

impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
where
T: DivAssign<f32>,
{
fn div_assign(&mut self, rhs: f32) {
self.width /= rhs;
self.height /= rhs;
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn size_ops() {
type SizeF = Size<f32>;

assert_eq!(
SizeF::new(10., 10.) + Vec2::new(10., 10.),
SizeF::new(20., 20.)
);
assert_eq!(
SizeF::new(20., 20.) - Vec2::new(10., 10.),
SizeF::new(10., 10.)
);
assert_eq!(SizeF::new(10., 10.) * 2., SizeF::new(20., 20.));
assert_eq!(SizeF::new(20., 20.) / 2., SizeF::new(10., 10.));

let mut size = SizeF::new(10., 10.);

size += Vec2::new(10., 10.);

assert_eq!(size, SizeF::new(20., 20.));
}
}
4 changes: 2 additions & 2 deletions crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use glam::*;
pub mod prelude {
#[doc(hidden)]
pub use crate::{
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, Size, UVec2,
UVec3, UVec4, Vec2, Vec3, Vec4,
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, UVec2, UVec3,
UVec4, Vec2, Vec3, Vec4,
};
}
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy_ecs::{
prelude::*,
system::{lifetimeless::*, SystemParamItem},
};
use bevy_math::{Mat4, Size};
use bevy_math::{Mat4, Vec2};
use bevy_reflect::TypeUuid;
use bevy_render::{
mesh::{
Expand Down Expand Up @@ -458,7 +458,7 @@ impl FromWorld for MeshPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Size::new(
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};
use bevy_asset::HandleUntyped;
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
use bevy_math::{Size, Vec2};
use bevy_math::Vec2;
use bevy_reflect::TypeUuid;
use thiserror::Error;
use wgpu::{
Expand Down Expand Up @@ -533,14 +533,14 @@ impl TextureFormatPixelInfo for TextureFormat {
}

/// The GPU-representation of an [`Image`].
/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's [`Size`].
/// Consists of the [`Texture`], its [`TextureView`] and the corresponding [`Sampler`], and the texture's size.
#[derive(Debug, Clone)]
pub struct GpuImage {
pub texture: Texture,
pub texture_view: TextureView,
pub texture_format: TextureFormat,
pub sampler: Sampler,
pub size: Size,
pub size: Vec2,
}

impl RenderAsset for Image {
Expand Down Expand Up @@ -595,7 +595,7 @@ impl RenderAsset for Image {
};

let texture_view = texture.create_view(&TextureViewDescriptor::default());
let size = Size::new(
let size = Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_sprite/src/mesh2d/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_ecs::{
prelude::*,
system::{lifetimeless::*, SystemParamItem},
};
use bevy_math::{Mat4, Size};
use bevy_math::{Mat4, Vec2};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::{
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
Expand Down Expand Up @@ -195,7 +195,7 @@ impl FromWorld for Mesh2dPipeline {
texture_view,
texture_format: image.texture_descriptor.format,
sampler,
size: Size::new(
size: Vec2::new(
image.texture_descriptor.size.width as f32,
image.texture_descriptor.size.height as f32,
),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ pub fn queue_sprites(
gpu_images.get(&Handle::weak(new_batch.image_handle_id))
{
current_batch = new_batch;
current_image_size = Vec2::new(gpu_image.size.width, gpu_image.size.height);
current_image_size = Vec2::new(gpu_image.size.x, gpu_image.size.y);
current_batch_entity = commands.spawn_bundle((current_batch,)).id();

image_bind_groups
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_text/src/glyph_brush.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ab_glyph::{Font as _, FontArc, Glyph, ScaleFont as _};
use bevy_asset::{Assets, Handle};
use bevy_math::{Size, Vec2};
use bevy_math::Vec2;
use bevy_render::texture::Image;
use bevy_sprite::TextureAtlas;
use glyph_brush_layout::{
Expand Down Expand Up @@ -29,11 +29,11 @@ impl GlyphBrush {
pub fn compute_glyphs<S: ToSectionText>(
&self,
sections: &[S],
bounds: Size,
bounds: Vec2,
text_alignment: TextAlignment,
) -> Result<Vec<SectionGlyph>, TextError> {
let geom = SectionGeometry {
bounds: (bounds.width, bounds.height),
bounds: (bounds.x, bounds.y),
..Default::default()
};
let section_glyphs = Layout::default()
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_text/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::hash::Hash;

use ab_glyph::{PxScale, ScaleFont};
use bevy_asset::{Assets, Handle, HandleId};
use bevy_math::Size;
use bevy_math::Vec2;
use bevy_render::texture::Image;
use bevy_sprite::TextureAtlas;
use bevy_utils::HashMap;
Expand Down Expand Up @@ -32,7 +32,7 @@ impl<ID> Default for TextPipeline<ID> {

pub struct TextLayoutInfo {
pub glyphs: Vec<PositionedGlyph>,
pub size: Size,
pub size: Vec2,
}

impl<ID: Hash + Eq> TextPipeline<ID> {
Expand All @@ -56,7 +56,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
sections: &[TextSection],
scale_factor: f64,
text_alignment: TextAlignment,
bounds: Size,
bounds: Vec2,
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
texture_atlases: &mut Assets<TextureAtlas>,
textures: &mut Assets<Image>,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
id,
TextLayoutInfo {
glyphs: Vec::new(),
size: Size::new(0., 0.),
size: Vec2::new(0., 0.),
},
);
return Ok(());
Expand All @@ -112,7 +112,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
max_y = max_y.max(glyph.position.y - scaled_font.descent());
}

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

let glyphs = self.brush.process_glyphs(
section_glyphs,
Expand Down
28 changes: 14 additions & 14 deletions 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::{
reflect::ReflectComponent,
system::{Local, ParamSet, Query, Res, ResMut},
};
use bevy_math::{Size, Vec3};
use bevy_math::{Vec2, Vec3};
use bevy_reflect::Reflect;
use bevy_render::{texture::Image, view::Visibility, RenderWorld};
use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas};
Expand All @@ -22,7 +22,7 @@ use crate::{
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
#[reflect(Component)]
pub struct Text2dSize {
pub size: Size,
pub size: Vec2,
}

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

impl Default for Text2dBounds {
fn default() -> Self {
Self {
size: Size::new(f32::MAX, f32::MAX),
size: Vec2::new(f32::MAX, f32::MAX),
}
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn extract_text2d_sprite(
if !visibility.is_visible {
continue;
}
let (width, height) = (calculated_size.size.width, calculated_size.size.height);
let (width, height) = (calculated_size.size.x, calculated_size.size.y);

if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
let text_glyphs = &text_layout.glyphs;
Expand Down Expand Up @@ -161,11 +161,11 @@ pub fn text2d_system(
for entity in queued_text.entities.drain(..) {
if let Ok((text, bounds, mut calculated_size)) = query.get_mut(entity) {
let text_bounds = match bounds {
Some(bounds) => Size {
width: scale_value(bounds.size.width, scale_factor),
height: scale_value(bounds.size.height, scale_factor),
},
None => Size::new(f32::MAX, f32::MAX),
Some(bounds) => Vec2::new(
scale_value(bounds.size.x, scale_factor),
scale_value(bounds.size.y, scale_factor),
),
None => Vec2::new(f32::MAX, f32::MAX),
};
match text_pipeline.queue_text(
entity,
Expand All @@ -190,10 +190,10 @@ pub fn text2d_system(
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
"Failed to get glyphs from the pipeline that have just been computed",
);
calculated_size.size = Size {
width: scale_value(text_layout_info.size.width, 1. / scale_factor),
height: scale_value(text_layout_info.size.height, 1. / scale_factor),
};
calculated_size.size = Vec2::new(
scale_value(text_layout_info.size.x, 1. / scale_factor),
scale_value(text_layout_info.size.y, 1. / scale_factor),
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/flex/convert.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
JustifyContent, PositionType, Style, Val,
JustifyContent, PositionType, Size, Style, Val,
};
use bevy_math::{Rect, Size};
use bevy_math::Rect;

pub fn from_rect(
scale_factor: f64,
Expand Down
Loading

0 comments on commit ba64fc9

Please sign in to comment.