Skip to content

Commit

Permalink
Upgrade to Taffy 0.3.3 (bevyengine#7859)
Browse files Browse the repository at this point in the history
# Objective

Upgrade to Taffy 0.3.3

Fixes: bevyengine#7712

## Solution

Upgrade to Taffy 0.3.3 with the `grid` feature disabled.

---

## Changelog
* Changed Taffy version to 0.3.3 and disabled its `grid` feature. 
* Added the `Start` and `End` variants to `AlignItems`, `AlignSelf`, `AlignContent` and `JustifyContent`.
* Added the `SpaceEvenly` variant to `AlignContent`.
* Updated `from_style` for Taffy 0.3.3.
  • Loading branch information
ickshonpe authored and Shfty committed Mar 19, 2023
1 parent 73f3df7 commit e95f275
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 98 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" }
bevy_utils = { path = "../bevy_utils", version = "0.9.0" }

# other
taffy = "0.2.2"
taffy = { version = "0.3.3", default-features = false, features = ["std"] }
serde = { version = "1", features = ["derive"] }
smallvec = { version = "1.6", features = ["union", "const_generics"] }
bytemuck = { version = "1.5", features = ["derive"] }
Expand Down
188 changes: 132 additions & 56 deletions crates/bevy_ui/src/flex/convert.rs
Original file line number Diff line number Diff line change
@@ -1,67 +1,136 @@
use taffy::style::LengthPercentageAuto;

use crate::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
PositionType, Size, Style, UiRect, Val,
};

pub fn from_rect(
scale_factor: f64,
rect: UiRect,
) -> taffy::geometry::Rect<taffy::style::Dimension> {
taffy::geometry::Rect {
left: from_val(scale_factor, rect.left),
right: from_val(scale_factor, rect.right),
top: from_val(scale_factor, rect.top),
bottom: from_val(scale_factor, rect.bottom),
impl Val {
fn scaled(self, scale_factor: f64) -> Self {
match self {
Val::Auto => Val::Auto,
Val::Percent(value) => Val::Percent(value),
Val::Px(value) => Val::Px((scale_factor * value as f64) as f32),
Val::Undefined => Val::Undefined,
}
}

fn to_inset(self) -> LengthPercentageAuto {
match self {
Val::Auto | Val::Undefined => taffy::style::LengthPercentageAuto::Auto,
Val::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentageAuto::Points(value),
}
}
}

impl UiRect {
fn scaled(self, scale_factor: f64) -> Self {
Self {
left: self.left.scaled(scale_factor),
right: self.right.scaled(scale_factor),
top: self.top.scaled(scale_factor),
bottom: self.bottom.scaled(scale_factor),
}
}
}

impl Size {
fn scaled(self, scale_factor: f64) -> Self {
Self {
width: self.width.scaled(scale_factor),
height: self.height.scaled(scale_factor),
}
}
}

impl<T: From<Val>> From<UiRect> for taffy::prelude::Rect<T> {
fn from(value: UiRect) -> Self {
Self {
left: value.left.into(),
right: value.right.into(),
top: value.top.into(),
bottom: value.bottom.into(),
}
}
}

pub fn from_val_size(
scale_factor: f64,
size: Size,
) -> taffy::geometry::Size<taffy::style::Dimension> {
taffy::geometry::Size {
width: from_val(scale_factor, size.width),
height: from_val(scale_factor, size.height),
impl<T: From<Val>> From<Size> for taffy::prelude::Size<T> {
fn from(value: Size) -> Self {
Self {
width: value.width.into(),
height: value.height.into(),
}
}
}

impl From<Val> for taffy::style::Dimension {
fn from(value: Val) -> Self {
match value {
Val::Auto | Val::Undefined => taffy::style::Dimension::Auto,
Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0),
Val::Px(value) => taffy::style::Dimension::Points(value),
}
}
}

impl From<Val> for taffy::style::LengthPercentage {
fn from(value: Val) -> Self {
match value {
Val::Auto | Val::Undefined => taffy::style::LengthPercentage::Points(0.0),
Val::Percent(value) => taffy::style::LengthPercentage::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentage::Points(value),
}
}
}

impl From<Val> for taffy::style::LengthPercentageAuto {
fn from(value: Val) -> Self {
match value {
Val::Auto => taffy::style::LengthPercentageAuto::Auto,
Val::Percent(value) => taffy::style::LengthPercentageAuto::Percent(value / 100.0),
Val::Px(value) => taffy::style::LengthPercentageAuto::Points(value),
Val::Undefined => taffy::style::LengthPercentageAuto::Points(0.),
}
}
}

pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
pub fn from_style(scale_factor: f64, style: &Style) -> taffy::style::Style {
taffy::style::Style {
display: value.display.into(),
position_type: value.position_type.into(),
flex_direction: value.flex_direction.into(),
flex_wrap: value.flex_wrap.into(),
align_items: value.align_items.into(),
align_self: value.align_self.into(),
align_content: value.align_content.into(),
justify_content: value.justify_content.into(),
position: from_rect(scale_factor, value.position),
margin: from_rect(scale_factor, value.margin),
padding: from_rect(scale_factor, value.padding),
border: from_rect(scale_factor, value.border),
flex_grow: value.flex_grow,
flex_shrink: value.flex_shrink,
flex_basis: from_val(scale_factor, value.flex_basis),
size: from_val_size(scale_factor, value.size),
min_size: from_val_size(scale_factor, value.min_size),
max_size: from_val_size(scale_factor, value.max_size),
aspect_ratio: value.aspect_ratio,
gap: from_val_size(scale_factor, value.gap),
}
}

pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension {
match val {
Val::Auto => taffy::style::Dimension::Auto,
Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0),
Val::Px(value) => taffy::style::Dimension::Points((scale_factor * value as f64) as f32),
Val::Undefined => taffy::style::Dimension::Undefined,
display: style.display.into(),
position: style.position_type.into(),
flex_direction: style.flex_direction.into(),
flex_wrap: style.flex_wrap.into(),
align_items: Some(style.align_items.into()),
align_self: style.align_self.into(),
align_content: Some(style.align_content.into()),
justify_content: Some(style.justify_content.into()),
inset: taffy::prelude::Rect {
left: style.position.left.scaled(scale_factor).to_inset(),
right: style.position.right.scaled(scale_factor).to_inset(),
top: style.position.top.scaled(scale_factor).to_inset(),
bottom: style.position.bottom.scaled(scale_factor).to_inset(),
},
margin: style.margin.scaled(scale_factor).into(),
padding: style.padding.scaled(scale_factor).into(),
border: style.border.scaled(scale_factor).into(),
flex_grow: style.flex_grow,
flex_shrink: style.flex_shrink,
flex_basis: style.flex_basis.scaled(scale_factor).into(),
size: style.size.scaled(scale_factor).into(),
min_size: style.size.scaled(scale_factor).into(),
max_size: style.size.scaled(scale_factor).into(),
aspect_ratio: style.aspect_ratio,
gap: style.gap.scaled(scale_factor).into(),
justify_self: None,
}
}

impl From<AlignItems> for taffy::style::AlignItems {
fn from(value: AlignItems) -> Self {
match value {
AlignItems::Start => taffy::style::AlignItems::Start,
AlignItems::End => taffy::style::AlignItems::End,
AlignItems::FlexStart => taffy::style::AlignItems::FlexStart,
AlignItems::FlexEnd => taffy::style::AlignItems::FlexEnd,
AlignItems::Center => taffy::style::AlignItems::Center,
Expand All @@ -71,28 +140,33 @@ impl From<AlignItems> for taffy::style::AlignItems {
}
}

impl From<AlignSelf> for taffy::style::AlignSelf {
impl From<AlignSelf> for Option<taffy::style::AlignSelf> {
fn from(value: AlignSelf) -> Self {
match value {
AlignSelf::Auto => taffy::style::AlignSelf::Auto,
AlignSelf::FlexStart => taffy::style::AlignSelf::FlexStart,
AlignSelf::FlexEnd => taffy::style::AlignSelf::FlexEnd,
AlignSelf::Center => taffy::style::AlignSelf::Center,
AlignSelf::Baseline => taffy::style::AlignSelf::Baseline,
AlignSelf::Stretch => taffy::style::AlignSelf::Stretch,
AlignSelf::Auto => None,
AlignSelf::Start => taffy::style::AlignSelf::Start.into(),
AlignSelf::End => taffy::style::AlignSelf::End.into(),
AlignSelf::FlexStart => taffy::style::AlignSelf::FlexStart.into(),
AlignSelf::FlexEnd => taffy::style::AlignSelf::FlexEnd.into(),
AlignSelf::Center => taffy::style::AlignSelf::Center.into(),
AlignSelf::Baseline => taffy::style::AlignSelf::Baseline.into(),
AlignSelf::Stretch => taffy::style::AlignSelf::Stretch.into(),
}
}
}

impl From<AlignContent> for taffy::style::AlignContent {
fn from(value: AlignContent) -> Self {
match value {
AlignContent::Start => taffy::style::AlignContent::Start,
AlignContent::End => taffy::style::AlignContent::End,
AlignContent::FlexStart => taffy::style::AlignContent::FlexStart,
AlignContent::FlexEnd => taffy::style::AlignContent::FlexEnd,
AlignContent::Center => taffy::style::AlignContent::Center,
AlignContent::Stretch => taffy::style::AlignContent::Stretch,
AlignContent::SpaceBetween => taffy::style::AlignContent::SpaceBetween,
AlignContent::SpaceAround => taffy::style::AlignContent::SpaceAround,
AlignContent::SpaceEvenly => taffy::style::AlignContent::SpaceEvenly,
}
}
}
Expand Down Expand Up @@ -120,6 +194,8 @@ impl From<FlexDirection> for taffy::style::FlexDirection {
impl From<JustifyContent> for taffy::style::JustifyContent {
fn from(value: JustifyContent) -> Self {
match value {
JustifyContent::Start => taffy::style::JustifyContent::Start,
JustifyContent::End => taffy::style::JustifyContent::End,
JustifyContent::FlexStart => taffy::style::JustifyContent::FlexStart,
JustifyContent::FlexEnd => taffy::style::JustifyContent::FlexEnd,
JustifyContent::Center => taffy::style::JustifyContent::Center,
Expand All @@ -130,11 +206,11 @@ impl From<JustifyContent> for taffy::style::JustifyContent {
}
}

impl From<PositionType> for taffy::style::PositionType {
impl From<PositionType> for taffy::style::Position {
fn from(value: PositionType) -> Self {
match value {
PositionType::Relative => taffy::style::PositionType::Relative,
PositionType::Absolute => taffy::style::PositionType::Absolute,
PositionType::Relative => taffy::style::Position::Relative,
PositionType::Absolute => taffy::style::Position::Absolute,
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use bevy_window::{PrimaryWindow, Window, WindowResolution, WindowScaleFactorChan
use std::fmt;
use taffy::{
prelude::{AvailableSpace, Size},
style_helpers::TaffyMaxContent,
Taffy,
};

Expand Down Expand Up @@ -61,14 +62,17 @@ impl FlexSurface {
pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) {
let mut added = false;
let taffy = &mut self.taffy;
let taffy_style = convert::from_style(scale_factor, style);
let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| {
added = true;
taffy.new_leaf(taffy_style).unwrap()
taffy
.new_leaf(convert::from_style(scale_factor, style))
.unwrap()
});

if !added {
self.taffy.set_style(*taffy_node, taffy_style).unwrap();
self.taffy
.set_style(*taffy_node, convert::from_style(scale_factor, style))
.unwrap();
}
}

Expand Down
Loading

0 comments on commit e95f275

Please sign in to comment.