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

Multiple box shadow support #16502

Merged
merged 10 commits into from
Dec 1, 2024
Prev Previous commit
Next Next commit
* Renamed DropShadow to ShadowStyle.
* Added a node with multiple shadows to the `BoxShadow` example
  • Loading branch information
ickshonpe committed Nov 25, 2024
commit 03054803bd9bc551645d3dd638145ed7f73d1c7d
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/render/box_shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ pub fn extract_shadows(
};

// Skip invisible images
if !view_visibility.get() || box_shadow.0.is_empty() || uinode.is_empty() {
if !view_visibility.get() || box_shadow.is_empty() || uinode.is_empty() {
continue;
}

Expand All @@ -278,7 +278,7 @@ pub fn extract_shadows(

let scale_factor = uinode.inverse_scale_factor.recip();

for drop_shadow in box_shadow.0.iter() {
for drop_shadow in box_shadow.iter() {
if drop_shadow.color.is_fully_transparent() {
continue;
}
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2424,30 +2424,30 @@ impl ResolvedBorderRadius {
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub struct BoxShadow(pub Vec<DropShadow>);
pub struct BoxShadow(pub Vec<ShadowStyle>);

impl BoxShadow {
/// A single shadow
pub fn new(shadow: DropShadow) -> Self {
pub fn new(shadow: ShadowStyle) -> Self {
BoxShadow(vec![shadow])
}
}

impl From<DropShadow> for BoxShadow {
fn from(shadow: DropShadow) -> Self {
impl From<ShadowStyle> for BoxShadow {
fn from(shadow: ShadowStyle) -> Self {
Self::new(shadow)
}
}

impl From<Vec<DropShadow>> for BoxShadow {
fn from(shadows: Vec<DropShadow>) -> Self {
impl From<Vec<ShadowStyle>> for BoxShadow {
fn from(shadows: Vec<ShadowStyle>) -> Self {
Self(shadows)
}
}

#[derive(Copy, Clone, Debug, PartialEq, Reflect)]
#[reflect(PartialEq, Default)]
pub struct DropShadow {
pub struct ShadowStyle {
/// The shadow's color
pub color: Color,
/// Horizontal offset
Expand All @@ -2463,7 +2463,7 @@ pub struct DropShadow {
pub blur_radius: Val,
}

impl Default for DropShadow {
impl Default for ShadowStyle {
fn default() -> Self {
Self {
color: Color::BLACK,
Expand Down
49 changes: 48 additions & 1 deletion examples/ui/box_shadow.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! This example shows how to create a node with a shadow

use argh::FromArgs;
use bevy::color::palettes::css::BLUE;
use bevy::color::palettes::css::DEEP_SKY_BLUE;
use bevy::color::palettes::css::GREEN;
use bevy::color::palettes::css::LIGHT_SKY_BLUE;
use bevy::color::palettes::css::RED;
use bevy::color::palettes::css::YELLOW;
use bevy::prelude::*;
use bevy::winit::WinitSettings;

Expand Down Expand Up @@ -189,6 +193,49 @@ fn setup(mut commands: Commands) {
border_radius,
));
}

// Demonstate multiple shadows on one node
commands.spawn((
Node {
width: Val::Px(40.),
height: Val::Px(40.),
border: UiRect::all(Val::Px(4.)),
..default()
},
BorderColor(LIGHT_SKY_BLUE.into()),
BorderRadius::all(Val::Px(20.)),
BackgroundColor(DEEP_SKY_BLUE.into()),
BoxShadow(vec![
ShadowStyle {
color: RED.with_alpha(0.7).into(),
x_offset: Val::Px(-20.),
y_offset: Val::Px(-5.),
spread_radius: Val::Percent(10.),
blur_radius: Val::Px(3.),
},
ShadowStyle {
color: BLUE.with_alpha(0.7).into(),
x_offset: Val::Px(-5.),
y_offset: Val::Px(-20.),
spread_radius: Val::Percent(10.),
blur_radius: Val::Px(3.),
},
ShadowStyle {
color: YELLOW.with_alpha(0.7).into(),
x_offset: Val::Px(20.),
y_offset: Val::Px(5.),
spread_radius: Val::Percent(10.),
blur_radius: Val::Px(3.),
},
ShadowStyle {
color: GREEN.with_alpha(0.7).into(),
x_offset: Val::Px(5.),
y_offset: Val::Px(20.),
spread_radius: Val::Percent(10.),
blur_radius: Val::Px(3.),
},
]),
));
});
}

Expand All @@ -209,7 +256,7 @@ fn box_shadow_node_bundle(
BorderColor(LIGHT_SKY_BLUE.into()),
border_radius,
BackgroundColor(DEEP_SKY_BLUE.into()),
BoxShadow::new(DropShadow {
BoxShadow::new(ShadowStyle {
color: Color::BLACK.with_alpha(0.8),
x_offset: Val::Percent(offset.x),
y_offset: Val::Percent(offset.y),
Expand Down