mirror of
https://github.com/bevyengine/bevy
synced 2024-12-30 06:53:13 +00:00
015f2c69ca
# Objective Continue improving the user experience of our UI Node API in the direction specified by [Bevy's Next Generation Scene / UI System](https://github.com/bevyengine/bevy/discussions/14437) ## Solution As specified in the document above, merge `Style` fields into `Node`, and move "computed Node fields" into `ComputedNode` (I chose this name over something like `ComputedNodeLayout` because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). `Style` has been removed. This accomplishes a number of goals: ## Ergonomics wins Specifying both `Node` and `Style` is now no longer required for non-default styles Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` ## Conceptual clarity `Style` was never a comprehensive "style sheet". It only defined "core" style properties that all `Nodes` shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties _across_ components (`Node`, `Button`, etc). We have plans to build a true style system (see the doc linked above). By moving the `Style` fields to `Node`, we fully embrace `Node` as the driving concept and remove the "style system" confusion. ## Next Steps * Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15. --- ## Migration Guide Move any fields set on `Style` into `Node` and replace all `Style` component usage with `Node`. Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` For any usage of the "computed node properties" that used to live on `Node`, use `ComputedNode` instead: Before: ```rust fn system(nodes: Query<&Node>) { for node in &nodes { let computed_size = node.size(); } } ``` After: ```rust fn system(computed_nodes: Query<&ComputedNode>) { for computed_node in &computed_nodes { let computed_size = computed_node.size(); } } ```
86 lines
2.5 KiB
Rust
86 lines
2.5 KiB
Rust
//! Shows how to render simple primitive shapes with a single color.
|
|
//!
|
|
//! You can toggle wireframes with the space bar except on wasm. Wasm does not support
|
|
//! `POLYGON_MODE_LINE` on the gpu.
|
|
|
|
use bevy::prelude::*;
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
use bevy::sprite::{Wireframe2dConfig, Wireframe2dPlugin};
|
|
|
|
fn main() {
|
|
let mut app = App::new();
|
|
app.add_plugins((
|
|
DefaultPlugins,
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
Wireframe2dPlugin,
|
|
))
|
|
.add_systems(Startup, setup);
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
app.add_systems(Update, toggle_wireframe);
|
|
app.run();
|
|
}
|
|
|
|
const X_EXTENT: f32 = 900.;
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
) {
|
|
commands.spawn(Camera2d);
|
|
|
|
let shapes = [
|
|
meshes.add(Circle::new(50.0)),
|
|
meshes.add(CircularSector::new(50.0, 1.0)),
|
|
meshes.add(CircularSegment::new(50.0, 1.25)),
|
|
meshes.add(Ellipse::new(25.0, 50.0)),
|
|
meshes.add(Annulus::new(25.0, 50.0)),
|
|
meshes.add(Capsule2d::new(25.0, 50.0)),
|
|
meshes.add(Rhombus::new(75.0, 100.0)),
|
|
meshes.add(Rectangle::new(50.0, 100.0)),
|
|
meshes.add(RegularPolygon::new(50.0, 6)),
|
|
meshes.add(Triangle2d::new(
|
|
Vec2::Y * 50.0,
|
|
Vec2::new(-50.0, -50.0),
|
|
Vec2::new(50.0, -50.0),
|
|
)),
|
|
];
|
|
let num_shapes = shapes.len();
|
|
|
|
for (i, shape) in shapes.into_iter().enumerate() {
|
|
// Distribute colors evenly across the rainbow.
|
|
let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
|
|
|
|
commands.spawn((
|
|
Mesh2d(shape),
|
|
MeshMaterial2d(materials.add(color)),
|
|
Transform::from_xyz(
|
|
// Distribute shapes from -X_EXTENT/2 to +X_EXTENT/2.
|
|
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
|
|
0.0,
|
|
0.0,
|
|
),
|
|
));
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
commands.spawn((
|
|
Text::new("Press space to toggle wireframes"),
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(12.0),
|
|
left: Val::Px(12.0),
|
|
..default()
|
|
},
|
|
));
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
fn toggle_wireframe(
|
|
mut wireframe_config: ResMut<Wireframe2dConfig>,
|
|
keyboard: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
if keyboard.just_pressed(KeyCode::Space) {
|
|
wireframe_config.global = !wireframe_config.global;
|
|
}
|
|
}
|