From 32108959cc1fc41b19d07d089cb079c446aaa066 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Fri, 11 Oct 2024 00:27:06 +0100 Subject: [PATCH] Add BoxSizing to Style --- crates/bevy_ui/src/layout/convert.rs | 15 ++++++++-- crates/bevy_ui/src/ui_node.rs | 42 ++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ui/src/layout/convert.rs b/crates/bevy_ui/src/layout/convert.rs index 433f87785ce969..347547081c803f 100644 --- a/crates/bevy_ui/src/layout/convert.rs +++ b/crates/bevy_ui/src/layout/convert.rs @@ -1,7 +1,7 @@ use taffy::style_helpers; use crate::{ - AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow, + AlignContent, AlignItems, AlignSelf, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow, GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf, MaxTrackSizingFunction, MinTrackSizingFunction, OverflowAxis, PositionType, RepeatedGridTrack, Style, UiRect, Val, @@ -70,7 +70,7 @@ pub fn from_style( ) -> taffy::style::Style { taffy::style::Style { display: style.display.into(), - box_sizing: taffy::BoxSizing::BorderBox, + box_sizing: style.box_sizing.into(), item_is_table: false, text_align: taffy::TextAlign::Auto, overflow: taffy::Point { @@ -255,6 +255,15 @@ impl From for taffy::style::Display { } } +impl From for taffy::style::BoxSizing { + fn from(value: BoxSizing) -> Self { + match value { + BoxSizing::BorderBox => taffy::style::BoxSizing::BorderBox, + BoxSizing::ContentBox => taffy::style::BoxSizing::ContentBox, + } + } +} + impl From for taffy::style::Overflow { fn from(value: OverflowAxis) -> Self { match value { @@ -453,6 +462,7 @@ mod tests { let bevy_style = Style { display: Display::Flex, + box_sizing: BoxSizing::ContentBox, position_type: PositionType::Absolute, left: Val::ZERO, right: Val::Percent(50.), @@ -520,6 +530,7 @@ mod tests { let viewport_values = LayoutContext::new(1.0, bevy_math::Vec2::new(800., 600.)); let taffy_style = from_style(&viewport_values, &bevy_style, false); assert_eq!(taffy_style.display, taffy::style::Display::Flex); + assert_eq!(taffy_style.box_sizing, taffy::style::BoxSizing::ContentBox); assert_eq!(taffy_style.position, taffy::style::Position::Absolute); assert_eq!( taffy_style.inset.left, diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 2d6e763bc0868a..6f74562cef602d 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -271,6 +271,20 @@ pub struct Style { /// pub display: Display, + /// Which layout algorithm to use when laying out this node's contents: + /// - [`Display::Flex`]: Use the Flexbox layout algorithm + /// - [`Display::Grid`]: Use the CSS Grid layout algorithm + /// - [`Display::None`]: Hide this node and perform layout as if it does not exist. + /// + /// Which part of a Node's box length styles like width and height control + /// - [`BoxSizing::BorderBox`]: They refer to the "border box" size (size including padding and border) + /// - [`BoxSizing::ContentBox`]: They refer to the "content box" size (size excluding padding and border) + /// + /// `BoxSizing::BorderBox` is generally considered more intuitive and is the default in Bevy even though it is not on the web. + /// + /// See: + pub box_sizing: BoxSizing, + /// Whether a node should be laid out in-flow with, or independently of its siblings: /// - [`PositionType::Relative`]: Layout this node in-flow with other nodes using the usual (flexbox/grid) layout algorithm. /// - [`PositionType::Absolute`]: Layout this node on top and independently of other nodes. @@ -533,6 +547,7 @@ pub struct Style { impl Style { pub const DEFAULT: Self = Self { display: Display::DEFAULT, + box_sizing: BoxSizing::DEFAULT, position_type: PositionType::DEFAULT, left: Val::Auto, right: Val::Auto, @@ -866,6 +881,33 @@ impl Default for Display { } } +/// Whether to show or hide overflowing items +#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] +#[reflect(Default, PartialEq)] +#[cfg_attr( + feature = "serialize", + derive(serde::Serialize, serde::Deserialize), + reflect(Serialize, Deserialize) +)] + +/// Which part of a Node's box length styles like width and height control +/// +/// See: +pub enum BoxSizing { + /// Length styles like width and height refer to the "border box" size (size including padding and border) + BorderBox, + /// Length styles like width and height refer to the "content box" size (size excluding padding and border) + ContentBox, +} +impl BoxSizing { + pub const DEFAULT: Self = Self::BorderBox; +} +impl Default for BoxSizing { + fn default() -> Self { + Self::DEFAULT + } +} + /// Defines how flexbox items are ordered within a flexbox #[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)] #[reflect(Default, PartialEq)]