Skip to content

Commit

Permalink
Spacing *_all helper methods for even spacing on all sides
Browse files Browse the repository at this point in the history
  • Loading branch information
alice-i-cecile committed Aug 1, 2022
1 parent c5a319e commit b4b7b0b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
39 changes: 39 additions & 0 deletions crates/bevy_ui/src/layout_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ impl Spacing {
}
}

/// Sets the margin on all sides to `val`
pub const fn margin_all(val: Val) -> Spacing {
Spacing {
margin: UiRect {
left: val,
right: val,
bottom: val,
top: val,
},
..Self::DEFAULT
}
}

/// Sets only the padding
pub const fn padding(rect: UiRect<Val>) -> Spacing {
Spacing {
Expand All @@ -165,13 +178,39 @@ impl Spacing {
}
}

/// Sets the padding on all sides to `val`
pub const fn padding_all(val: Val) -> Spacing {
Spacing {
padding: UiRect {
left: val,
right: val,
bottom: val,
top: val,
},
..Self::DEFAULT
}
}

/// Sets only the padding
pub const fn border(rect: UiRect<Val>) -> Spacing {
Spacing {
border: rect,
..Self::DEFAULT
}
}

/// Sets the border on all sides to `val`
pub const fn border_all(val: Val) -> Spacing {
Spacing {
border: UiRect {
left: val,
right: val,
bottom: val,
top: val,
},
..Self::DEFAULT
}
}
}

/// Defines the text direction
Expand Down
4 changes: 2 additions & 2 deletions examples/games/game_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod game {
color: TEXT_COLOR,
},
))
.insert(Spacing::margin(UiRect::all(Val::Px(50.0))));
.insert(Spacing::margin_all(Val::Px(50.0)));

parent
.spawn_bundle(TextBundle::from_sections([
Expand Down Expand Up @@ -201,7 +201,7 @@ mod game {
},
),
]))
.insert(Spacing::margin(UiRect::all(Val::Px(50.0))));
.insert(Spacing::margin_all(Val::Px(50.0)));
});
// Spawn a 5 seconds timer to trigger going back to the menu
commands.insert_resource(GameTimer(Timer::from_seconds(5.0, false)));
Expand Down
4 changes: 2 additions & 2 deletions examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Val::Px(200.0),
Val::Percent(100.0),
),
spacing: Spacing::border(UiRect::all(Val::Px(2.0))),
spacing: Spacing::border_all(Val::Px(2.0)),
color: Color::rgb(0.65, 0.65, 0.65).into(),
..default()
})
Expand All @@ -66,7 +66,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
color: Color::WHITE,
},
))
.insert(Spacing::margin(UiRect::all(Val::Px(5.0))));
.insert(Spacing::margin_all(Val::Px(5.0)));
});
});
// right vertical fill
Expand Down
2 changes: 1 addition & 1 deletion examples/window/scale_factor_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent
.spawn_bundle(NodeBundle {
size_constraints: SizeConstraints::suggested(Val::Px(200.), Val::FULL),
spacing: Spacing::border(UiRect::all(Val::Px(2.0))),
spacing: Spacing::border_all(Val::Px(2.0)),
color: Color::rgb(0.65, 0.65, 0.65).into(),
..default()
})
Expand Down

0 comments on commit b4b7b0b

Please sign in to comment.