From 3071efb8bd27c19086a33bb1e592f03b15c30e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristoffer=20=C3=96dmark?= Date: Fri, 1 Dec 2023 20:25:51 +0100 Subject: [PATCH] test with Setters --- .../src/core_3d/camera_3d.rs | 3 +- crates/bevy_pbr/src/bundle.rs | 3 +- crates/bevy_text/src/text.rs | 3 +- crates/bevy_ui/src/node_bundles.rs | 3 +- crates/bevy_ui/src/ui_node.rs | 4 +- crates/bevy_utils/Cargo.toml | 1 + crates/bevy_utils/src/lib.rs | 1 + examples/3d/3d_gizmos.rs | 18 +- examples/ui/ui.rs | 206 ++++++++---------- 9 files changed, 116 insertions(+), 126 deletions(-) diff --git a/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs b/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs index 33579994c9aca..50ec8c36fa209 100644 --- a/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs +++ b/crates/bevy_core_pipeline/src/core_3d/camera_3d.rs @@ -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". @@ -135,7 +136,7 @@ pub enum ScreenSpaceTransmissionQuality { Ultra, } -#[derive(Bundle)] +#[derive(Bundle, Setters)] pub struct Camera3dBundle { pub camera: Camera, pub camera_render_graph: CameraRenderGraph, diff --git a/crates/bevy_pbr/src/bundle.rs b/crates/bevy_pbr/src/bundle.rs index 851f87bcd7466..027bdb6e8e6b6 100644 --- a/crates/bevy_pbr/src/bundle.rs +++ b/crates/bevy_pbr/src/bundle.rs @@ -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; /// A component bundle for entities with a [`Mesh`] and a [`Material`]. -#[derive(Bundle, Clone)] +#[derive(Bundle, Clone, Setters)] pub struct MaterialMeshBundle { pub mesh: Handle, pub material: Handle, diff --git a/crates/bevy_text/src/text.rs b/crates/bevy_text/src/text.rs index 7c0c272fe11a1..d662693ba090a 100644 --- a/crates/bevy_text/src/text.rs +++ b/crates/bevy_text/src/text.rs @@ -185,7 +185,8 @@ impl From 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), diff --git a/crates/bevy_ui/src/node_bundles.rs b/crates/bevy_ui/src/node_bundles.rs index 35a1330a75bd8..8786a907d379b 100644 --- a/crates/bevy_ui/src/node_bundles.rs +++ b/crates/bevy_ui/src/node_bundles.rs @@ -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, diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 2425d7fdb0c74..bf7be5e9f8464 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -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}; @@ -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: diff --git a/crates/bevy_utils/Cargo.toml b/crates/bevy_utils/Cargo.toml index bf1eea4b5f069..0da7e64ccebc1 100644 --- a/crates/bevy_utils/Cargo.toml +++ b/crates/bevy_utils/Cargo.toml @@ -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"] } diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index fadbf1c1a0606..ca07acaf3f24a 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -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}; diff --git a/examples/3d/3d_gizmos.rs b/examples/3d/3d_gizmos.rs index 14f4b3946450f..629a8fa24b26a 100644 --- a/examples/3d/3d_gizmos.rs +++ b/examples/3d/3d_gizmos.rs @@ -17,16 +17,16 @@ fn setup( mut meshes: ResMut>, mut materials: ResMut>, ) { - 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 })), diff --git a/examples/ui/ui.rs b/examples/ui/ui.rs index 21817b62e7f76..843346dc6bbb7 100644 --- a/examples/ui/ui.rs +++ b/examples/ui/ui.rs @@ -24,55 +24,45 @@ fn setup(mut commands: Commands, asset_server: Res) { // 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. @@ -82,55 +72,50 @@ fn setup(mut commands: Commands, asset_server: Res) { }); // 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)), )) @@ -140,12 +125,12 @@ fn setup(mut commands: Commands, asset_server: Res) { 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)), @@ -155,44 +140,43 @@ fn setup(mut commands: Commands, asset_server: Res) { }); }); 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 {