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

Add builder methods to bevy_ui's Style struct #7881

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions crates/bevy_ui/src/node_bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ pub struct NodeBundle {
pub z_index: ZIndex,
}

impl NodeBundle {
pub fn with_style(style: Style) -> Self {
Self {
style,
..Default::default()
}
}
}

impl Default for NodeBundle {
fn default() -> Self {
NodeBundle {
Expand Down
367 changes: 367 additions & 0 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ impl Default for Val {
}
}

impl From<f32> for Val {
fn from(value: f32) -> Self {
Self::Px(value)
}
}

impl From<i32> for Val {
fn from(value: i32) -> Self {
Self::Px(value as f32)
}
}

impl Mul<f32> for Val {
type Output = Val;

Expand Down Expand Up @@ -341,6 +353,361 @@ impl Style {
overflow: Overflow::DEFAULT,
gap: Size::UNDEFINED,
};

/// Construct a style instance with `flex_direction` set to `FlexDirection::Row`
pub fn row() -> Self {
Self {
display: Display::Flex,
flex_direction: FlexDirection::Row,
..Self::DEFAULT
}
}

/// Construct a style instance with `flex_direction` set to `FlexDirection::Column`
pub fn column() -> Self {
Self {
display: Display::Flex,
flex_direction: FlexDirection::Row,
..Self::DEFAULT
}
}

/// Set the `display` property
pub fn display(mut self, val: Display) -> Self {
self.display = val;
self
}

/// Set the `position_type` property
pub fn position_type(mut self, val: PositionType) -> Self {
self.position_type = val;
self
}

/// Set the `direction` property
pub fn direction(mut self, val: Direction) -> Self {
self.direction = val;
self
}
/// Set the `flex_direction` property
pub fn flex_direction(mut self, val: FlexDirection) -> Self {
self.flex_direction = val;
self
}
/// Set the `flex_wrap` property
pub fn flex_wrap(mut self, val: FlexWrap) -> Self {
self.flex_wrap = val;
self
}
/// Set the `align_items` property
pub fn align_items(mut self, val: AlignItems) -> Self {
self.align_items = val;
self
}
/// Set the `align_self` property
pub fn align_self(mut self, val: AlignSelf) -> Self {
self.align_self = val;
self
}
/// Set the `align_content` property
pub fn align_content(mut self, val: AlignContent) -> Self {
self.align_content = val;
self
}
/// Set the `justify_content` property
pub fn justify_content(mut self, val: JustifyContent) -> Self {
self.justify_content = val;
self
}

/// Set the `position.left` property
pub fn left(mut self, val: impl Into<Val>) -> Self {
self.position.left = val.into();
self
}
/// Set the `position.left` property
pub fn right(mut self, val: impl Into<Val>) -> Self {
self.position.right = val.into();
self
}
/// Set the `position.top` property
pub fn top(mut self, val: impl Into<Val>) -> Self {
self.position.top = val.into();
self
}
/// Set the `position.bottom` property
pub fn bottom(mut self, val: impl Into<Val>) -> Self {
self.position.bottom = val.into();
self
}
/// Set the `position.left` and `position.right` properties
pub fn position_horizontal(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.position.right = val;
self.position.left = val;
self
}
/// Set the `position.top` and `position.bottom` properties
pub fn position_vertical(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.position.top = val;
self.position.bottom = val;
self
}
/// Set the `position.top`,`position.bottom`,`position.left`, and `position.right` properties
pub fn position(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.position.top = val;
self.position.bottom = val;
self.position.left = val;
self.position.right = val;
self
}

/// Set the `margin.left` property
pub fn margin_left(mut self, val: impl Into<Val>) -> Self {
self.margin.left = val.into();
self
}
/// Set the `margin.left` property
pub fn margin_right(mut self, val: impl Into<Val>) -> Self {
self.margin.right = val.into();
self
}
/// Set the `margin.top` property
pub fn margin_top(mut self, val: impl Into<Val>) -> Self {
self.margin.top = val.into();
self
}
/// Set the `margin.bottom` property
pub fn margin_bottom(mut self, val: impl Into<Val>) -> Self {
self.margin.bottom = val.into();
self
}
/// Set the `margin.left` and `margin.right` properties
pub fn margin_horizontal(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.margin.right = val;
self.margin.left = val;
self
}
/// Set the `margin.top` and `margin.bottom` properties
pub fn margin_vertical(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.margin.top = val;
self.margin.bottom = val;
self
}
/// Set the `margin.top`,`margin.bottom`,`margin.left`, and `margin.right` properties
pub fn margin(mut self, val: impl Into<Val>) -> Self {
nicoburns marked this conversation as resolved.
Show resolved Hide resolved
let val = val.into();
self.margin.top = val;
self.margin.bottom = val;
self.margin.left = val;
self.margin.right = val;
self
}

/// Set the `padding.left` property
pub fn padding_left(mut self, val: impl Into<Val>) -> Self {
self.padding.left = val.into();
self
}
/// Set the `padding.left` property
pub fn padding_right(mut self, val: impl Into<Val>) -> Self {
self.padding.right = val.into();
self
}
/// Set the `padding.top` property
pub fn padding_top(mut self, val: impl Into<Val>) -> Self {
self.padding.top = val.into();
self
}
/// Set the `padding.bottom` property
pub fn padding_bottom(mut self, val: impl Into<Val>) -> Self {
self.padding.bottom = val.into();
self
}
/// Set the `padding.left` and `padding.right` properties
pub fn padding_horizontal(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.padding.right = val;
self.padding.left = val;
self
}
/// Set the `padding.top` and `padding.bottom` properties
pub fn padding_vertical(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.padding.top = val;
self.padding.bottom = val;
self
}
/// Set the `padding.top`,`padding.bottom`,`padding.left`, and `padding.right` properties
pub fn padding(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.padding.top = val;
self.padding.bottom = val;
self.padding.left = val;
self.padding.right = val;
self
}

/// Set the `border.left` property
pub fn border_left(mut self, val: impl Into<Val>) -> Self {
self.border.left = val.into();
self
}
/// Set the `border.left` property
pub fn border_right(mut self, val: impl Into<Val>) -> Self {
self.border.right = val.into();
self
}
/// Set the `border.top` property
pub fn border_top(mut self, val: impl Into<Val>) -> Self {
self.border.top = val.into();
self
}
/// Set the `border.bottom` property
pub fn border_bottom(mut self, val: impl Into<Val>) -> Self {
self.border.bottom = val.into();
self
}
/// Set the `border.left` and `border.right` properties
pub fn border_horizontal(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.border.right = val;
self.border.left = val;
self
}
/// Set the `border.top` and `border.bottom` properties
pub fn border_vertical(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.border.top = val;
self.border.bottom = val;
self
}
/// Set the `border.top`,`border.bottom`,`border.left`, and `border.right` properties
pub fn border(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.border.top = val;
self.border.bottom = val;
self.border.left = val;
self.border.right = val;
self
}

/// Set the `flex_grow` property
pub fn flex_grow(mut self, val: f32) -> Self {
self.flex_grow = val;
self
}
/// Set the `flex_shrink` property
pub fn flex_shrink(mut self, val: f32) -> Self {
self.flex_shrink = val;
self
}
/// Set the `flex_basis` property
pub fn flex_basis(mut self, val: impl Into<Val>) -> Self {
self.flex_basis = val.into();
self
}
/// Set the `size.width` property
pub fn width(mut self, val: impl Into<Val>) -> Self {
self.size.width = val.into();
self
}
/// Set the `size.height` property
pub fn height(mut self, val: impl Into<Val>) -> Self {
self.size.height = val.into();
self
}
/// Set the `min_size.width` property
pub fn min_width(mut self, val: impl Into<Val>) -> Self {
self.min_size.width = val.into();
self
}
/// Set the `min_size.height` property
pub fn min_height(mut self, val: impl Into<Val>) -> Self {
self.min_size.height = val.into();
self
}
/// Set the `max_size.width` property
pub fn max_width(mut self, val: impl Into<Val>) -> Self {
self.max_size.width = val.into();
self
}
/// Set the `max_size.height` property
pub fn max_height(mut self, val: impl Into<Val>) -> Self {
self.max_size.height = val.into();
self
}
/// Set the `aspect_ratio` property
pub fn aspect_ratio(mut self, val: f32) -> Self {
self.aspect_ratio = Some(val);
self
}
/// Set the `overflow` property
pub fn overflow(mut self, val: Overflow) -> Self {
self.overflow = val;
self
}
/// Set the spacing between rows
pub fn row_gap(mut self, val: impl Into<Val>) -> Self {
self.gap.height = val.into();
self
}
/// Set the spacing between columns
pub fn column_gap(mut self, val: impl Into<Val>) -> Self {
self.gap.width = val.into();
self
}
/// Set the spacing between both rows and columns
pub fn gap(mut self, val: impl Into<Val>) -> Self {
let val = val.into();
self.gap.width = val;
self.gap.height = val;
self
}

/// Center content in the vertical axis using the `align_items` `align_content` and `justify_content` properties
pub fn center_content_vertically(mut self) -> Self {
match (self.display, self.flex_direction) {
(Display::Flex, FlexDirection::Row | FlexDirection::RowReverse) => {
self.align_items = AlignItems::Center;
self.align_content = AlignContent::Center;
}
(Display::Flex, FlexDirection::Column | FlexDirection::ColumnReverse) => {
self.justify_content = JustifyContent::Center;
}
_ => {}
}

self
}

/// Center content in the vertical axis using the `align_items` and `justify_content` properties
pub fn center_content_horizontally(mut self) -> Self {
match (self.display, self.flex_direction) {
(Display::Flex, FlexDirection::Row | FlexDirection::RowReverse) => {
self.justify_content = JustifyContent::Center;
}
(Display::Flex, FlexDirection::Column | FlexDirection::ColumnReverse) => {
self.align_content = AlignContent::Center;
self.align_items = AlignItems::Center;
}
_ => {}
}

self
}

/// Center content in both axes using the `align_items` and `justify_content` properties
pub fn center_content(mut self) -> Self {
self.align_items = AlignItems::Center;
self.justify_content = JustifyContent::Center;
self.align_content = AlignContent::Center;
self
}
}

impl Default for Style {
Expand Down
Loading