Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split Val into two enums, one with an Auto variant and one without #8096

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
684b32a
changes:
ickshonpe Feb 8, 2023
4b2c93b
improved the `Breadth` doc comment description
ickshonpe Feb 8, 2023
9597f15
impl From<UiRect<Breadth> for <UiRect<Val> and use it to convert the…
ickshonpe Feb 8, 2023
7d02b25
cargo fmt --all
ickshonpe Feb 8, 2023
7ef9ca4
added plugin type registrations for `Breadth` and `UiRect<Breadth>`
ickshonpe Feb 9, 2023
087fc9e
Improved the doc comments for `Breadth`
ickshonpe Feb 9, 2023
d558224
collected and tidied up test imports
ickshonpe Feb 9, 2023
785d320
Merge branch 'breadth' into split-uirect
ickshonpe Mar 15, 2023
379388e
changes:
ickshonpe Mar 15, 2023
942f611
changes:
ickshonpe Mar 15, 2023
366f17e
Changed geometry helper functions to take `impl Into<AutoVal>` parame…
ickshonpe Mar 15, 2023
ab9e662
cleaned up the examples
ickshonpe Mar 15, 2023
cf87ae9
Fix doc tests
ickshonpe Mar 15, 2023
c39d95c
More doc text fixes
ickshonpe Mar 15, 2023
29febb9
Fix doc comment errors
ickshonpe Mar 15, 2023
492de41
Merge branch 'main' into split-uirect-merge
ickshonpe Aug 10, 2023
0488c26
more merge changes
ickshonpe Aug 10, 2023
53461c9
Fixed some examples
ickshonpe Aug 10, 2023
bbae861
Renamed `AutoVal` to `Val` and `Val` to `Num`.
ickshonpe Aug 10, 2023
e742dfc
More merge fixes
ickshonpe Aug 10, 2023
907ead4
More merge fixes
ickshonpe Aug 10, 2023
97b8fa0
fixed more examples
ickshonpe Aug 10, 2023
de76437
fixed game_menu example
ickshonpe Aug 10, 2023
87ba5fa
More merge fixes
ickshonpe Aug 10, 2023
084f5b8
cargo fmt --all
ickshonpe Aug 10, 2023
f334985
Renamed `ValArithmeticError` to `NumArithmeticError` and `AutoValArit…
ickshonpe Aug 10, 2023
6cd390f
`ValConversionError` renamed to `NumConversionError`
ickshonpe Aug 10, 2023
3fb9b65
Removed references to `AutoVal` in comments. Fixed doc comments for `…
ickshonpe Aug 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
683 changes: 418 additions & 265 deletions crates/bevy_ui/src/geometry.rs

Large diffs are not rendered by default.

120 changes: 86 additions & 34 deletions crates/bevy_ui/src/layout/convert.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use taffy::style_helpers;

use crate::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow,
AlignContent, AlignItems, AlignSelf, Border, Display, FlexDirection, FlexWrap, GridAutoFlow,
GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf,
MaxTrackSizingFunction, MinTrackSizingFunction, PositionType, RepeatedGridTrack, Style, UiRect,
Val,
Margin, MaxTrackSizingFunction, MinTrackSizingFunction, Num, Padding, PositionType,
RepeatedGridTrack, Style, Val,
};

use super::LayoutContext;
Expand Down Expand Up @@ -35,6 +35,37 @@ impl Val {
}
}

fn into_dimension(self, context: &LayoutContext) -> taffy::style::Dimension {
self.into_length_percentage_auto(context).into()
}
}

impl Num {
fn into_length_percentage_auto(
self,
context: &LayoutContext,
) -> taffy::style::LengthPercentageAuto {
match self {
//Val::Auto => taffy::style::LengthPercentageAuto::Auto,
Num::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.),
Num::Px(value) => taffy::style::LengthPercentageAuto::Points(
(context.scale_factor * value as f64) as f32,
),
Num::VMin(value) => {
taffy::style::LengthPercentageAuto::Points(context.min_size * value / 100.)
}
Num::VMax(value) => {
taffy::style::LengthPercentageAuto::Points(context.max_size * value / 100.)
}
Num::Vw(value) => {
taffy::style::LengthPercentageAuto::Points(context.physical_size.x * value / 100.)
}
Num::Vh(value) => {
taffy::style::LengthPercentageAuto::Points(context.physical_size.y * value / 100.)
}
}
}

fn into_length_percentage(self, context: &LayoutContext) -> taffy::style::LengthPercentage {
match self.into_length_percentage_auto(context) {
taffy::style::LengthPercentageAuto::Auto => taffy::style::LengthPercentage::Points(0.0),
Expand All @@ -46,14 +77,32 @@ impl Val {
}
}
}
}

fn into_dimension(self, context: &LayoutContext) -> taffy::style::Dimension {
self.into_length_percentage_auto(context).into()
impl Margin {
fn map_to_taffy_rect<T>(self, map_fn: impl Fn(Val) -> T) -> taffy::geometry::Rect<T> {
taffy::geometry::Rect {
left: map_fn(self.left),
right: map_fn(self.right),
top: map_fn(self.top),
bottom: map_fn(self.bottom),
}
}
}

impl UiRect {
fn map_to_taffy_rect<T>(self, map_fn: impl Fn(Val) -> T) -> taffy::geometry::Rect<T> {
impl Border {
fn map_to_taffy_rect<T>(self, map_fn: impl Fn(Num) -> T) -> taffy::geometry::Rect<T> {
taffy::geometry::Rect {
left: map_fn(self.left),
right: map_fn(self.right),
top: map_fn(self.top),
bottom: map_fn(self.bottom),
}
}
}

impl Padding {
fn map_to_taffy_rect<T>(self, map_fn: impl Fn(Num) -> T) -> taffy::geometry::Rect<T> {
taffy::geometry::Rect {
left: map_fn(self.left),
right: map_fn(self.right),
Expand Down Expand Up @@ -301,10 +350,10 @@ impl MinTrackSizingFunction {
fn into_taffy(self, context: &LayoutContext) -> taffy::style::MinTrackSizingFunction {
match self {
MinTrackSizingFunction::Px(val) => taffy::style::MinTrackSizingFunction::Fixed(
Val::Px(val).into_length_percentage(context),
Num::Px(val).into_length_percentage(context),
),
MinTrackSizingFunction::Percent(val) => taffy::style::MinTrackSizingFunction::Fixed(
Val::Percent(val).into_length_percentage(context),
Num::Percent(val).into_length_percentage(context),
),
MinTrackSizingFunction::Auto => taffy::style::MinTrackSizingFunction::Auto,
MinTrackSizingFunction::MinContent => taffy::style::MinTrackSizingFunction::MinContent,
Expand All @@ -317,22 +366,22 @@ impl MaxTrackSizingFunction {
fn into_taffy(self, context: &LayoutContext) -> taffy::style::MaxTrackSizingFunction {
match self {
MaxTrackSizingFunction::Px(val) => taffy::style::MaxTrackSizingFunction::Fixed(
Val::Px(val).into_length_percentage(context),
Num::Px(val).into_length_percentage(context),
),
MaxTrackSizingFunction::Percent(val) => taffy::style::MaxTrackSizingFunction::Fixed(
Val::Percent(val).into_length_percentage(context),
Num::Percent(val).into_length_percentage(context),
),
MaxTrackSizingFunction::Auto => taffy::style::MaxTrackSizingFunction::Auto,
MaxTrackSizingFunction::MinContent => taffy::style::MaxTrackSizingFunction::MinContent,
MaxTrackSizingFunction::MaxContent => taffy::style::MaxTrackSizingFunction::MaxContent,
MaxTrackSizingFunction::FitContentPx(val) => {
taffy::style::MaxTrackSizingFunction::FitContent(
Val::Px(val).into_length_percentage(context),
Num::Px(val).into_length_percentage(context),
)
}
MaxTrackSizingFunction::FitContentPercent(val) => {
taffy::style::MaxTrackSizingFunction::FitContent(
Val::Percent(val).into_length_percentage(context),
Num::Percent(val).into_length_percentage(context),
)
}
MaxTrackSizingFunction::Fraction(fraction) => {
Expand Down Expand Up @@ -393,12 +442,16 @@ impl RepeatedGridTrack {

#[cfg(test)]
mod tests {
use crate::Border;
use crate::Margin;
use crate::Padding;
use crate::Val;

use super::*;

#[test]
fn test_convert_from() {
use taffy::style_helpers as sh;

let bevy_style = crate::Style {
display: Display::Flex,
position_type: PositionType::Absolute,
Expand All @@ -415,23 +468,23 @@ mod tests {
justify_items: JustifyItems::Default,
justify_self: JustifySelf::Center,
justify_content: JustifyContent::SpaceEvenly,
margin: UiRect {
margin: Margin {
left: Val::Percent(0.),
right: Val::Px(0.),
top: Val::Auto,
bottom: Val::Auto,
},
padding: UiRect {
left: Val::Percent(0.),
right: Val::Px(0.),
top: Val::Percent(0.),
bottom: Val::Percent(0.),
padding: Padding {
left: Num::Percent(0.),
right: Num::Px(0.),
top: Num::Percent(0.),
bottom: Num::Percent(0.),
},
border: UiRect {
left: Val::Px(0.),
right: Val::Px(0.),
top: Val::Auto,
bottom: Val::Px(0.),
border: Border {
left: Num::Px(0.),
right: Num::Px(0.),
top: Num::Percent(0.),
bottom: Num::Px(0.),
},
flex_grow: 1.,
flex_shrink: 0.,
Expand All @@ -444,8 +497,8 @@ mod tests {
max_height: Val::Px(0.),
aspect_ratio: None,
overflow: crate::Overflow::clip(),
column_gap: Val::Px(0.),
row_gap: Val::Percent(0.),
column_gap: Num::Px(0.),
row_gap: Num::Percent(0.),
grid_auto_flow: GridAutoFlow::ColumnDense,
grid_template_rows: vec![
GridTrack::px(10.0),
Expand Down Expand Up @@ -635,13 +688,12 @@ mod tests {
use taffy::style::LengthPercentage;
let context = LayoutContext::new(2.0, bevy_math::Vec2::new(800., 600.));
let cases = [
(Val::Auto, LengthPercentage::Points(0.)),
(Val::Percent(1.), LengthPercentage::Percent(0.01)),
(Val::Px(1.), LengthPercentage::Points(2.)),
(Val::Vw(1.), LengthPercentage::Points(8.)),
(Val::Vh(1.), LengthPercentage::Points(6.)),
(Val::VMin(2.), LengthPercentage::Points(12.)),
(Val::VMax(2.), LengthPercentage::Points(16.)),
(Num::Percent(1.), LengthPercentage::Percent(0.01)),
(Num::Px(1.), LengthPercentage::Points(2.)),
(Num::Vw(1.), LengthPercentage::Points(8.)),
(Num::Vh(1.), LengthPercentage::Points(6.)),
(Num::VMin(2.), LengthPercentage::Points(12.)),
(Num::VMax(2.), LengthPercentage::Points(16.)),
];
for (val, length) in cases {
assert!(match (val.into_length_percentage(&context), length) {
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ impl Plugin for UiPlugin {
.register_type::<Overflow>()
.register_type::<OverflowAxis>()
.register_type::<PositionType>()
.register_type::<Size>()
.register_type::<Margin>()
.register_type::<Padding>()
.register_type::<Border>()
.register_type::<RelativeCursorPosition>()
.register_type::<RepeatedGridTrack>()
.register_type::<Style>()
.register_type::<UiImage>()
.register_type::<UiImageSize>()
.register_type::<UiRect>()
.register_type::<Val>()
.register_type::<UiImageSize>()
.register_type::<Num>()
.register_type::<BorderColor>()
.register_type::<widget::Button>()
.register_type::<widget::Label>()
Expand Down
19 changes: 9 additions & 10 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub use pipeline::*;
pub use render_pass::*;

use crate::{
prelude::UiCameraConfig, BackgroundColor, BorderColor, CalculatedClip, ContentSize, Node,
Style, UiImage, UiScale, UiStack, UiTextureAtlasImage, Val,
prelude::UiCameraConfig, BackgroundColor, BorderColor, CalculatedClip, ContentSize, Node, Num,
Style, UiImage, UiScale, UiStack, UiTextureAtlasImage,
};

use bevy_app::prelude::*;
Expand Down Expand Up @@ -245,15 +245,14 @@ pub fn extract_atlas_uinodes(
}
}

fn resolve_border_thickness(value: Val, parent_width: f32, viewport_size: Vec2) -> f32 {
fn resolve_border_thickness(value: Num, parent_width: f32, viewport_size: Vec2) -> f32 {
match value {
Val::Auto => 0.,
Val::Px(px) => px.max(0.),
Val::Percent(percent) => (parent_width * percent / 100.).max(0.),
Val::Vw(percent) => (viewport_size.x * percent / 100.).max(0.),
Val::Vh(percent) => (viewport_size.y * percent / 100.).max(0.),
Val::VMin(percent) => (viewport_size.min_element() * percent / 100.).max(0.),
Val::VMax(percent) => (viewport_size.max_element() * percent / 100.).max(0.),
Num::Px(px) => px.max(0.),
Num::Percent(percent) => (parent_width * percent / 100.).max(0.),
Num::Vw(percent) => (viewport_size.x * percent / 100.).max(0.),
Num::Vh(percent) => (viewport_size.y * percent / 100.).max(0.),
Num::VMin(percent) => (viewport_size.min_element() * percent / 100.).max(0.),
Num::VMax(percent) => (viewport_size.max_element() * percent / 100.).max(0.),
}
}

Expand Down
Loading
Loading