Skip to content

Draft: Discussion about builder pattern #10833

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use bevy_render::{
view::{ColorGrading, VisibleEntities},
};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_utils::Setters;
use serde::{Deserialize, Serialize};

/// Configuration for the "main 3d render graph".
Expand Down Expand Up @@ -135,7 +136,7 @@ pub enum ScreenSpaceTransmissionQuality {
Ultra,
}

#[derive(Bundle)]
#[derive(Bundle, Setters)]
pub struct Camera3dBundle {
pub camera: Camera,
pub camera_render_graph: CameraRenderGraph,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use bevy_render::{
};
use bevy_transform::components::{GlobalTransform, Transform};
use bevy_utils::HashMap;
use bevy_utils::Setters;

/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;

/// A component bundle for entities with a [`Mesh`] and a [`Material`].
#[derive(Bundle, Clone)]
#[derive(Bundle, Clone, Setters)]
pub struct MaterialMeshBundle<M: Material> {
pub mesh: Handle<Mesh>,
pub material: Handle<M>,
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ impl From<TextAlignment> for glyph_brush_layout::HorizontalAlign {
}
}

#[derive(Clone, Debug, Reflect)]
use bevy_utils::Setters;
#[derive(Clone, Debug, Reflect, Setters)]
pub struct TextStyle {
/// If this is not specified, then
/// * if `default_font` feature is enabled (enabled by default in `bevy` crate),
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_ui/src/node_bundles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ use bevy_sprite::TextureAtlas;
#[cfg(feature = "bevy_text")]
use bevy_text::{BreakLineOn, Text, TextAlignment, TextLayoutInfo, TextSection, TextStyle};
use bevy_transform::prelude::{GlobalTransform, Transform};
use bevy_utils::Setters;

/// The basic UI node
///
/// Useful as a container for a variety of child nodes.
#[derive(Bundle, Clone, Debug)]
#[derive(Bundle, Clone, Debug, Setters)]
pub struct NodeBundle {
/// Describes the logical size of the node
pub node: Node,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bevy_math::{Rect, Vec2};
use bevy_reflect::prelude::*;
use bevy_render::{color::Color, texture::Image};
use bevy_transform::prelude::GlobalTransform;
use bevy_utils::Setters;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use std::num::{NonZeroI16, NonZeroU16};
Expand Down Expand Up @@ -132,8 +133,7 @@ impl Default for Node {
/// - [MDN: Basic Concepts of Grid Layout](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout)
/// - [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work.
/// - [CSS Grid Garden](https://cssgridgarden.com/). An interactive tutorial/game that teaches the essential parts of CSS Grid in a fun engaging way.

#[derive(Component, Clone, PartialEq, Debug, Deserialize, Serialize, Reflect)]
#[derive(Component, Clone, PartialEq, Debug, Deserialize, Serialize, Reflect, Setters)]
#[reflect(Component, Default, PartialEq, Deserialize, Serialize)]
pub struct Style {
/// Which layout algorithm to use when laying out this node's contents:
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bevy_utils_proc_macros = { version = "0.12.0", path = "macros" }
petgraph = "0.6"
thiserror = "1.0"
nonmax = "0.5"
derive_setters = "0.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.0", features = ["js"] }
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use ahash::{AHasher, RandomState};
pub use bevy_utils_proc_macros::*;
pub use cow_arc::*;
pub use default::default;
pub use derive_setters::Setters;
pub use float_ord::*;
pub use hashbrown;
pub use instant::{Duration, Instant};
Expand Down
18 changes: 9 additions & 9 deletions examples/3d/3d_gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(
Camera3dBundle::default()
.transform(Transform::from_xyz(0., 1.5, 6.).looking_at(Vec3::ZERO, Vec3::Y))
);
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane::from_size(5.0))),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
commands.spawn(
PbrBundle::default()
.mesh(meshes.add(Mesh::from(shape::Plane::from_size(5.0))))
.material(materials.add(Color::rgb(0.3, 0.5, 0.3).into()))
);
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
Expand Down
206 changes: 95 additions & 111 deletions examples/ui/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,45 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Camera
commands.spawn(Camera2dBundle::default());

// root node
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::SpaceBetween,
..default()
},
..default()
})
.spawn(
NodeBundle::default().style(
Style::default()
.width(Val::Percent(100.0))
.height(Val::Percent(100.0))
.justify_content(JustifyContent::SpaceBetween),
),
)
.with_children(|parent| {
// left vertical fill (border)
parent
.spawn(NodeBundle {
style: Style {
width: Val::Px(200.),
border: UiRect::all(Val::Px(2.)),
..default()
},
background_color: Color::rgb(0.65, 0.65, 0.65).into(),
..default()
})
.spawn(
NodeBundle::default()
.style(
Style::default()
.width(Val::Px(200.))
.border(UiRect::all(Val::Px(2.))),
)
.background_color(Color::rgb(0.65, 0.65, 0.65).into()),
)
.with_children(|parent| {
// left vertical fill (content)
parent
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
..default()
})
.spawn(
NodeBundle::default()
.style(Style::default().width(Val::Percent(100.)))
.background_color(Color::rgb(0.15, 0.15, 0.15).into()),
)
.with_children(|parent| {
// text
parent.spawn((
TextBundle::from_section(
"Text Example",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 30.0,
..default()
},
TextStyle::default()
.font(asset_server.load("fonts/FiraSans-Bold.ttf"))
.font_size(30.),
)
.with_style(Style {
margin: UiRect::all(Val::Px(5.)),
..default()
}),
.with_style(Style::default().margin(UiRect::all(Val::Px(5.)))),
// Because this is a distinct label widget and
// not button/list item text, this is necessary
// for accessibility to treat the text accordingly.
Expand All @@ -82,55 +72,50 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
// right vertical fill
parent
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
width: Val::Px(200.),
..default()
},
background_color: Color::rgb(0.15, 0.15, 0.15).into(),
..default()
})
.spawn(
NodeBundle::default()
.style(
Style::default()
.flex_direction(FlexDirection::Column)
.justify_content(JustifyContent::Center)
.align_items(AlignItems::Center)
.width(Val::Px(200.)),
)
.background_color(Color::rgb(0.15, 0.15, 0.15).into()),
)
.with_children(|parent| {
// Title
parent.spawn((
TextBundle::from_section(
"Scrolling list",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 25.,
..default()
},
TextStyle::default()
.font(asset_server.load("fonts/FiraSans-Bold.ttf"))
.font_size(25.),
),
Label,
));
// List with hidden overflow
parent
.spawn(NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
align_self: AlignSelf::Stretch,
height: Val::Percent(50.),
overflow: Overflow::clip_y(),
..default()
},
background_color: Color::rgb(0.10, 0.10, 0.10).into(),
..default()
})
.spawn(
NodeBundle::default()
.style(
Style::default()
.flex_direction(FlexDirection::Column)
.align_self(AlignSelf::Stretch)
.height(Val::Percent(50.))
.overflow(Overflow::clip_y()),
)
.background_color(Color::rgb(0.10, 0.10, 0.10).into()),
)
.with_children(|parent| {
// Moving panel
parent
.spawn((
NodeBundle {
style: Style {
flex_direction: FlexDirection::Column,
align_items: AlignItems::Center,
..default()
},
..default()
},
NodeBundle::default().style(
Style::default()
.flex_direction(FlexDirection::Column)
.align_items(AlignItems::Center),
),
ScrollingList::default(),
AccessibilityNode(NodeBuilder::new(Role::List)),
))
Expand All @@ -140,12 +125,12 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent.spawn((
TextBundle::from_section(
format!("Item {i}"),
TextStyle {
font: asset_server
.load("fonts/FiraSans-Bold.ttf"),
font_size: 20.,
..default()
},
TextStyle::default()
.font(
asset_server
.load("fonts/FiraSans-Bold.ttf"),
)
.font_size(20.),
),
Label,
AccessibilityNode(NodeBuilder::new(Role::ListItem)),
Expand All @@ -155,44 +140,43 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
});
});
parent
.spawn(NodeBundle {
style: Style {
width: Val::Px(200.0),
height: Val::Px(200.0),
position_type: PositionType::Absolute,
left: Val::Px(210.),
bottom: Val::Px(10.),
border: UiRect::all(Val::Px(20.)),
..default()
},
border_color: Color::GREEN.into(),
background_color: Color::rgb(0.4, 0.4, 1.).into(),
..default()
})
.spawn(
NodeBundle::default()
.style(
Style::default()
.width(Val::Px(200.0))
.height(Val::Px(200.0))
.position_type(PositionType::Absolute)
.left(Val::Px(210.))
.bottom(Val::Px(10.))
.border(UiRect::all(Val::Px(20.))),
)
.border_color(Color::GREEN.into())
.background_color(Color::rgb(0.4, 0.4, 1.).into()),
)
.with_children(|parent| {
parent.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
background_color: Color::rgb(0.8, 0.8, 1.).into(),
..default()
});
parent.spawn(
NodeBundle::default()
.style(
Style::default()
.width(Val::Percent(100.0))
.height(Val::Percent(100.0)),
)
.background_color(Color::rgb(0.8, 0.8, 1.).into()),
);
});
// render order test: reddest in the back, whitest in the front (flex center)
parent
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
position_type: PositionType::Absolute,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
..default()
})
.spawn(
NodeBundle::default().style(
Style::default()
.width(Val::Percent(100.0))
.height(Val::Percent(100.0))
.position_type(PositionType::Absolute)
.align_items(AlignItems::Center)
.justify_content(JustifyContent::Center),
),
)
.with_children(|parent| {
parent
.spawn(NodeBundle {
Expand Down