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 <KDecayMusic@protonmail.com>
This commit is contained in:
parent
0850975192
commit
7a7f097485
@ -1,29 +1,4 @@
|
|||||||
use bevy_reflect::Reflect;
|
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
|
/// A rect, as defined by its "side" locations
|
||||||
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
|
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
|
||||||
@ -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.));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ pub use glam::*;
|
|||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, Size, UVec2,
|
BVec2, BVec3, BVec4, EulerRot, IVec2, IVec3, IVec4, Mat3, Mat4, Quat, Rect, UVec2, UVec3,
|
||||||
UVec3, UVec4, Vec2, Vec3, Vec4,
|
UVec4, Vec2, Vec3, Vec4,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ use bevy_ecs::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
system::{lifetimeless::*, SystemParamItem},
|
system::{lifetimeless::*, SystemParamItem},
|
||||||
};
|
};
|
||||||
use bevy_math::{Mat4, Size};
|
use bevy_math::{Mat4, Vec2};
|
||||||
use bevy_reflect::TypeUuid;
|
use bevy_reflect::TypeUuid;
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
mesh::{
|
mesh::{
|
||||||
@ -458,7 +458,7 @@ impl FromWorld for MeshPipeline {
|
|||||||
texture_view,
|
texture_view,
|
||||||
texture_format: image.texture_descriptor.format,
|
texture_format: image.texture_descriptor.format,
|
||||||
sampler,
|
sampler,
|
||||||
size: Size::new(
|
size: Vec2::new(
|
||||||
image.texture_descriptor.size.width as f32,
|
image.texture_descriptor.size.width as f32,
|
||||||
image.texture_descriptor.size.height as f32,
|
image.texture_descriptor.size.height as f32,
|
||||||
),
|
),
|
||||||
|
|||||||
@ -14,7 +14,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use bevy_asset::HandleUntyped;
|
use bevy_asset::HandleUntyped;
|
||||||
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
|
use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem};
|
||||||
use bevy_math::{Size, Vec2};
|
use bevy_math::Vec2;
|
||||||
use bevy_reflect::TypeUuid;
|
use bevy_reflect::TypeUuid;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use wgpu::{
|
use wgpu::{
|
||||||
@ -533,14 +533,14 @@ impl TextureFormatPixelInfo for TextureFormat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// The GPU-representation of an [`Image`].
|
/// 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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct GpuImage {
|
pub struct GpuImage {
|
||||||
pub texture: Texture,
|
pub texture: Texture,
|
||||||
pub texture_view: TextureView,
|
pub texture_view: TextureView,
|
||||||
pub texture_format: TextureFormat,
|
pub texture_format: TextureFormat,
|
||||||
pub sampler: Sampler,
|
pub sampler: Sampler,
|
||||||
pub size: Size,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RenderAsset for Image {
|
impl RenderAsset for Image {
|
||||||
@ -595,7 +595,7 @@ impl RenderAsset for Image {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let texture_view = texture.create_view(&TextureViewDescriptor::default());
|
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.width as f32,
|
||||||
image.texture_descriptor.size.height as f32,
|
image.texture_descriptor.size.height as f32,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -4,7 +4,7 @@ use bevy_ecs::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
system::{lifetimeless::*, SystemParamItem},
|
system::{lifetimeless::*, SystemParamItem},
|
||||||
};
|
};
|
||||||
use bevy_math::{Mat4, Size};
|
use bevy_math::{Mat4, Vec2};
|
||||||
use bevy_reflect::{Reflect, TypeUuid};
|
use bevy_reflect::{Reflect, TypeUuid};
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
|
mesh::{GpuBufferInfo, Mesh, MeshVertexBufferLayout},
|
||||||
@ -195,7 +195,7 @@ impl FromWorld for Mesh2dPipeline {
|
|||||||
texture_view,
|
texture_view,
|
||||||
texture_format: image.texture_descriptor.format,
|
texture_format: image.texture_descriptor.format,
|
||||||
sampler,
|
sampler,
|
||||||
size: Size::new(
|
size: Vec2::new(
|
||||||
image.texture_descriptor.size.width as f32,
|
image.texture_descriptor.size.width as f32,
|
||||||
image.texture_descriptor.size.height as f32,
|
image.texture_descriptor.size.height as f32,
|
||||||
),
|
),
|
||||||
|
|||||||
@ -431,7 +431,7 @@ pub fn queue_sprites(
|
|||||||
gpu_images.get(&Handle::weak(new_batch.image_handle_id))
|
gpu_images.get(&Handle::weak(new_batch.image_handle_id))
|
||||||
{
|
{
|
||||||
current_batch = new_batch;
|
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();
|
current_batch_entity = commands.spawn_bundle((current_batch,)).id();
|
||||||
|
|
||||||
image_bind_groups
|
image_bind_groups
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
use ab_glyph::{Font as _, FontArc, Glyph, ScaleFont as _};
|
use ab_glyph::{Font as _, FontArc, Glyph, ScaleFont as _};
|
||||||
use bevy_asset::{Assets, Handle};
|
use bevy_asset::{Assets, Handle};
|
||||||
use bevy_math::{Size, Vec2};
|
use bevy_math::Vec2;
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
use bevy_sprite::TextureAtlas;
|
use bevy_sprite::TextureAtlas;
|
||||||
use glyph_brush_layout::{
|
use glyph_brush_layout::{
|
||||||
@ -29,11 +29,11 @@ impl GlyphBrush {
|
|||||||
pub fn compute_glyphs<S: ToSectionText>(
|
pub fn compute_glyphs<S: ToSectionText>(
|
||||||
&self,
|
&self,
|
||||||
sections: &[S],
|
sections: &[S],
|
||||||
bounds: Size,
|
bounds: Vec2,
|
||||||
text_alignment: TextAlignment,
|
text_alignment: TextAlignment,
|
||||||
) -> Result<Vec<SectionGlyph>, TextError> {
|
) -> Result<Vec<SectionGlyph>, TextError> {
|
||||||
let geom = SectionGeometry {
|
let geom = SectionGeometry {
|
||||||
bounds: (bounds.width, bounds.height),
|
bounds: (bounds.x, bounds.y),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let section_glyphs = Layout::default()
|
let section_glyphs = Layout::default()
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use std::hash::Hash;
|
|||||||
|
|
||||||
use ab_glyph::{PxScale, ScaleFont};
|
use ab_glyph::{PxScale, ScaleFont};
|
||||||
use bevy_asset::{Assets, Handle, HandleId};
|
use bevy_asset::{Assets, Handle, HandleId};
|
||||||
use bevy_math::Size;
|
use bevy_math::Vec2;
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
use bevy_sprite::TextureAtlas;
|
use bevy_sprite::TextureAtlas;
|
||||||
use bevy_utils::HashMap;
|
use bevy_utils::HashMap;
|
||||||
@ -32,7 +32,7 @@ impl<ID> Default for TextPipeline<ID> {
|
|||||||
|
|
||||||
pub struct TextLayoutInfo {
|
pub struct TextLayoutInfo {
|
||||||
pub glyphs: Vec<PositionedGlyph>,
|
pub glyphs: Vec<PositionedGlyph>,
|
||||||
pub size: Size,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ID: Hash + Eq> TextPipeline<ID> {
|
impl<ID: Hash + Eq> TextPipeline<ID> {
|
||||||
@ -56,7 +56,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
|||||||
sections: &[TextSection],
|
sections: &[TextSection],
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
text_alignment: TextAlignment,
|
text_alignment: TextAlignment,
|
||||||
bounds: Size,
|
bounds: Vec2,
|
||||||
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
|
font_atlas_set_storage: &mut Assets<FontAtlasSet>,
|
||||||
texture_atlases: &mut Assets<TextureAtlas>,
|
texture_atlases: &mut Assets<TextureAtlas>,
|
||||||
textures: &mut Assets<Image>,
|
textures: &mut Assets<Image>,
|
||||||
@ -92,7 +92,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
|||||||
id,
|
id,
|
||||||
TextLayoutInfo {
|
TextLayoutInfo {
|
||||||
glyphs: Vec::new(),
|
glyphs: Vec::new(),
|
||||||
size: Size::new(0., 0.),
|
size: Vec2::new(0., 0.),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@ -112,7 +112,7 @@ impl<ID: Hash + Eq> TextPipeline<ID> {
|
|||||||
max_y = max_y.max(glyph.position.y - scaled_font.descent());
|
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(
|
let glyphs = self.brush.process_glyphs(
|
||||||
section_glyphs,
|
section_glyphs,
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use bevy_ecs::{
|
|||||||
reflect::ReflectComponent,
|
reflect::ReflectComponent,
|
||||||
system::{Local, ParamSet, Query, Res, ResMut},
|
system::{Local, ParamSet, Query, Res, ResMut},
|
||||||
};
|
};
|
||||||
use bevy_math::{Size, Vec3};
|
use bevy_math::{Vec2, Vec3};
|
||||||
use bevy_reflect::Reflect;
|
use bevy_reflect::Reflect;
|
||||||
use bevy_render::{texture::Image, view::Visibility, RenderWorld};
|
use bevy_render::{texture::Image, view::Visibility, RenderWorld};
|
||||||
use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas};
|
use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, TextureAtlas};
|
||||||
@ -22,7 +22,7 @@ use crate::{
|
|||||||
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
|
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Text2dSize {
|
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.
|
/// The maximum width and height of text. The text will wrap according to the specified size.
|
||||||
@ -35,13 +35,13 @@ pub struct Text2dSize {
|
|||||||
#[derive(Component, Copy, Clone, Debug, Reflect)]
|
#[derive(Component, Copy, Clone, Debug, Reflect)]
|
||||||
#[reflect(Component)]
|
#[reflect(Component)]
|
||||||
pub struct Text2dBounds {
|
pub struct Text2dBounds {
|
||||||
pub size: Size,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Text2dBounds {
|
impl Default for Text2dBounds {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
size: Size::new(f32::MAX, f32::MAX),
|
size: Vec2::new(f32::MAX, f32::MAX),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ pub fn extract_text2d_sprite(
|
|||||||
if !visibility.is_visible {
|
if !visibility.is_visible {
|
||||||
continue;
|
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) {
|
if let Some(text_layout) = text_pipeline.get_glyphs(&entity) {
|
||||||
let text_glyphs = &text_layout.glyphs;
|
let text_glyphs = &text_layout.glyphs;
|
||||||
@ -161,11 +161,11 @@ pub fn text2d_system(
|
|||||||
for entity in queued_text.entities.drain(..) {
|
for entity in queued_text.entities.drain(..) {
|
||||||
if let Ok((text, bounds, mut calculated_size)) = query.get_mut(entity) {
|
if let Ok((text, bounds, mut calculated_size)) = query.get_mut(entity) {
|
||||||
let text_bounds = match bounds {
|
let text_bounds = match bounds {
|
||||||
Some(bounds) => Size {
|
Some(bounds) => Vec2::new(
|
||||||
width: scale_value(bounds.size.width, scale_factor),
|
scale_value(bounds.size.x, scale_factor),
|
||||||
height: scale_value(bounds.size.height, scale_factor),
|
scale_value(bounds.size.y, scale_factor),
|
||||||
},
|
),
|
||||||
None => Size::new(f32::MAX, f32::MAX),
|
None => Vec2::new(f32::MAX, f32::MAX),
|
||||||
};
|
};
|
||||||
match text_pipeline.queue_text(
|
match text_pipeline.queue_text(
|
||||||
entity,
|
entity,
|
||||||
@ -190,10 +190,10 @@ pub fn text2d_system(
|
|||||||
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
|
let text_layout_info = text_pipeline.get_glyphs(&entity).expect(
|
||||||
"Failed to get glyphs from the pipeline that have just been computed",
|
"Failed to get glyphs from the pipeline that have just been computed",
|
||||||
);
|
);
|
||||||
calculated_size.size = Size {
|
calculated_size.size = Vec2::new(
|
||||||
width: scale_value(text_layout_info.size.width, 1. / scale_factor),
|
scale_value(text_layout_info.size.x, 1. / scale_factor),
|
||||||
height: scale_value(text_layout_info.size.height, 1. / scale_factor),
|
scale_value(text_layout_info.size.y, 1. / scale_factor),
|
||||||
};
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
|
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(
|
pub fn from_rect(
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
|
|||||||
149
crates/bevy_ui/src/geometry.rs
Normal file
149
crates/bevy_ui/src/geometry.rs
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
use bevy_math::Vec2;
|
||||||
|
use bevy_reflect::Reflect;
|
||||||
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,6 +4,7 @@
|
|||||||
//! This UI is laid out with the Flexbox paradigm (see <https://cssreference.io/flexbox/> ) except the vertical axis is inverted
|
//! This UI is laid out with the Flexbox paradigm (see <https://cssreference.io/flexbox/> ) except the vertical axis is inverted
|
||||||
mod flex;
|
mod flex;
|
||||||
mod focus;
|
mod focus;
|
||||||
|
mod geometry;
|
||||||
mod render;
|
mod render;
|
||||||
mod ui_node;
|
mod ui_node;
|
||||||
|
|
||||||
@ -14,19 +15,21 @@ pub mod widget;
|
|||||||
use bevy_render::camera::CameraTypePlugin;
|
use bevy_render::camera::CameraTypePlugin;
|
||||||
pub use flex::*;
|
pub use flex::*;
|
||||||
pub use focus::*;
|
pub use focus::*;
|
||||||
|
pub use geometry::*;
|
||||||
pub use render::*;
|
pub use render::*;
|
||||||
pub use ui_node::*;
|
pub use ui_node::*;
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub use crate::{entity::*, ui_node::*, widget::Button, Interaction};
|
pub use crate::{entity::*, geometry::*, ui_node::*, widget::Button, Interaction};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use crate::Size;
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
|
use bevy_ecs::schedule::{ParallelSystemDescriptorCoercion, SystemLabel};
|
||||||
use bevy_input::InputSystem;
|
use bevy_input::InputSystem;
|
||||||
use bevy_math::{Rect, Size};
|
use bevy_math::Rect;
|
||||||
use bevy_transform::TransformSystem;
|
use bevy_transform::TransformSystem;
|
||||||
use update::{ui_z_system, update_clipping_system};
|
use update::{ui_z_system, update_clipping_system};
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
use crate::Size;
|
||||||
use bevy_asset::Handle;
|
use bevy_asset::Handle;
|
||||||
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
|
||||||
use bevy_math::{Rect, Size, Vec2};
|
use bevy_math::{Rect, Vec2};
|
||||||
use bevy_reflect::{Reflect, ReflectDeserialize};
|
use bevy_reflect::{Reflect, ReflectDeserialize};
|
||||||
use bevy_render::{
|
use bevy_render::{
|
||||||
color::Color,
|
color::Color,
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use crate::Size;
|
||||||
use crate::{CalculatedSize, UiImage};
|
use crate::{CalculatedSize, UiImage};
|
||||||
use bevy_asset::Assets;
|
use bevy_asset::Assets;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
@ -6,7 +7,6 @@ use bevy_ecs::{
|
|||||||
reflect::ReflectComponent,
|
reflect::ReflectComponent,
|
||||||
system::{Query, Res},
|
system::{Query, Res},
|
||||||
};
|
};
|
||||||
use bevy_math::Size;
|
|
||||||
use bevy_reflect::{Reflect, ReflectDeserialize};
|
use bevy_reflect::{Reflect, ReflectDeserialize};
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
use crate::{CalculatedSize, Style, Val};
|
use crate::{CalculatedSize, Size, Style, Val};
|
||||||
use bevy_asset::Assets;
|
use bevy_asset::Assets;
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
query::{Changed, Or, With},
|
query::{Changed, Or, With},
|
||||||
system::{Local, ParamSet, Query, Res, ResMut},
|
system::{Local, ParamSet, Query, Res, ResMut},
|
||||||
};
|
};
|
||||||
use bevy_math::Size;
|
use bevy_math::Vec2;
|
||||||
use bevy_render::texture::Image;
|
use bevy_render::texture::Image;
|
||||||
use bevy_sprite::TextureAtlas;
|
use bevy_sprite::TextureAtlas;
|
||||||
use bevy_text::{DefaultTextPipeline, Font, FontAtlasSet, Text, TextError};
|
use bevy_text::{DefaultTextPipeline, Font, FontAtlasSet, Text, TextError};
|
||||||
@ -78,7 +78,7 @@ pub fn text_system(
|
|||||||
let mut query = text_queries.p2();
|
let mut query = text_queries.p2();
|
||||||
for entity in queued_text.entities.drain(..) {
|
for entity in queued_text.entities.drain(..) {
|
||||||
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
|
if let Ok((text, style, mut calculated_size)) = query.get_mut(entity) {
|
||||||
let node_size = Size::new(
|
let node_size = Vec2::new(
|
||||||
text_constraint(
|
text_constraint(
|
||||||
style.min_size.width,
|
style.min_size.width,
|
||||||
style.size.width,
|
style.size.width,
|
||||||
@ -117,8 +117,8 @@ pub fn text_system(
|
|||||||
"Failed to get glyphs from the pipeline that have just been computed",
|
"Failed to get glyphs from the pipeline that have just been computed",
|
||||||
);
|
);
|
||||||
calculated_size.size = Size {
|
calculated_size.size = Size {
|
||||||
width: scale_value(text_layout_info.size.width, inv_scale_factor),
|
width: scale_value(text_layout_info.size.x, inv_scale_factor),
|
||||||
height: scale_value(text_layout_info.size.height, inv_scale_factor),
|
height: scale_value(text_layout_info.size.y, inv_scale_factor),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -52,12 +52,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
})
|
})
|
||||||
.insert(AnimateScale);
|
.insert(AnimateScale);
|
||||||
// Demonstrate text wrapping
|
// Demonstrate text wrapping
|
||||||
let box_size = Size::new(300.0, 200.0);
|
let box_size = Vec2::new(300.0, 200.0);
|
||||||
let box_position = Vec2::new(0.0, -250.0);
|
let box_position = Vec2::new(0.0, -250.0);
|
||||||
commands.spawn_bundle(SpriteBundle {
|
commands.spawn_bundle(SpriteBundle {
|
||||||
sprite: Sprite {
|
sprite: Sprite {
|
||||||
color: Color::rgb(0.25, 0.25, 0.75),
|
color: Color::rgb(0.25, 0.25, 0.75),
|
||||||
custom_size: Some(Vec2::new(box_size.width, box_size.height)),
|
custom_size: Some(Vec2::new(box_size.x, box_size.y)),
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
transform: Transform::from_translation(box_position.extend(0.0)),
|
transform: Transform::from_translation(box_position.extend(0.0)),
|
||||||
@ -81,8 +81,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|||||||
// box is centered at box_position, so it is necessary to move by half of the box size to
|
// box is centered at box_position, so it is necessary to move by half of the box size to
|
||||||
// keep the text in the box.
|
// keep the text in the box.
|
||||||
transform: Transform::from_xyz(
|
transform: Transform::from_xyz(
|
||||||
box_position.x - box_size.width / 2.0,
|
box_position.x - box_size.x / 2.0,
|
||||||
box_position.y + box_size.height / 2.0,
|
box_position.y + box_size.y / 2.0,
|
||||||
1.0,
|
1.0,
|
||||||
),
|
),
|
||||||
..default()
|
..default()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user