Skip to content

Commit

Permalink
Merge pull request #265 from Nilirad/required-components
Browse files Browse the repository at this point in the history
* Rename `Path` to `Shape`

`Path` will be the "driving concept" component
for Required Components.
The name does not fit well, so I changed it to `Shape`.

* Add required components for `Shape`

* Make examples use Required Components

* Update example in `README.md`

* Make tests use Required Components

* Deprecate ShapeBundle and remove it from prelude

* Update doc comments

* Include fill and stroke data (stub) into `Shape` component

* Add `ShapeBuilder`

* Make examples use ShapeBuilder

* Remove `GeometryBuilder`

* Document missing items

* Update `PathBuilder`

- Added `fill` and `stroke` fields
- Added `fill` and `stroke` setters
- Improved builder pattern by returning `Self` in methods

* Remove `ShapePath`

`ShapePath` is no longer functional
after including fill and stroke data into `Shape`.

Use `ShapeBuilder` or `PathBuilder` instead.

* Remove `Component` impl for `Fill` and `Stroke`

* Update `CHANGELOG.md`

* Update `README.md`

* Remove note from `dynamic_shape` example
  • Loading branch information
Nilirad authored Dec 11, 2024
2 parents f16e09f + defbb12 commit 7dd478c
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 340 deletions.
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
# Changelog

## 0.14.0

This version uses Bevy's **Required Components**,
therefore many items have been changed.
Examples may help migration.

- `Path` component renamed to `Shape`. It is now the central component of a shape entity.
- `Shape` now includes fill and stroke data.
- `Fill` and `Stroke` are no longer `Component`s.
- Deprecated `ShapeBundle` in favor of the `Shape` component.
- `prelude` no longer exports `ShapeBundle`.
- Added `ShapeBuilder`: works similarly to `GeometryBuilder`
- Removed `GeometryBuilder` and `ShapePath`.
- `PathBuilder` now allows chaining methods.

## 0.13.0
- Support for Bevy 0.15.0-rc.3.
- Support for Bevy 0.15.0.
- `Rectangle` now supports border radii (see `rectangle.rs` example).
- Removed deprecated `SpatialBundle` from `ShapeBundle`: `Transform` and `Visibility` are now added separately.

Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ fn setup_system(mut commands: Commands) {
};

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
..default()
},
Fill::color(DARK_CYAN),
Stroke::new(BLACK, 10.0),
));
commands.spawn(
ShapeBuilder::with(&shape)
.fill(DARK_CYAN)
.stroke((BLACK, 10.0))
.build(),
);
}
```

Expand Down
43 changes: 17 additions & 26 deletions examples/dynamic_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ fn main() {
App::new()
.add_plugins((DefaultPlugins, ShapePlugin))
.add_systems(Startup, setup_system)
.add_systems(Update, change_draw_mode_system)
.add_systems(Update, change_number_of_sides)
.add_systems(Update, redraw_shape)
.add_systems(Update, rotate_shape_system)
.run();
}
Expand All @@ -24,28 +23,22 @@ fn rotate_shape_system(mut query: Query<&mut Transform, With<ExampleShape>>, tim
}
}

fn change_draw_mode_system(mut query: Query<(&mut Fill, &mut Stroke)>, time: Res<Time>) {
fn redraw_shape(mut query: Query<&mut Shape, With<ExampleShape>>, time: Res<Time>) {
let hue = (time.elapsed_secs_f64() * 50.0) % 360.0;
let color = Color::hsl(hue as f32, 1.0, 0.5);
let outline_width = 2.0 + time.elapsed_secs_f64().sin().abs() * 10.0;

for (mut fill_mode, mut stroke_mode) in query.iter_mut() {
fill_mode.color = Color::hsl(hue as f32, 1.0, 0.5);
stroke_mode.options.line_width = outline_width as f32;
}
}

fn change_number_of_sides(mut query: Query<&mut Path>, time: Res<Time>) {
let sides = ((time.elapsed_secs_f64() - PI * 2.5).sin() * 2.5 + 5.5).round() as usize;
let polygon = shapes::RegularPolygon {
sides,
feature: shapes::RegularPolygonFeature::Radius(200.0),
..shapes::RegularPolygon::default()
};

for mut path in query.iter_mut() {
let polygon = shapes::RegularPolygon {
sides,
feature: shapes::RegularPolygonFeature::Radius(200.0),
..shapes::RegularPolygon::default()
};

*path = ShapePath::build_as(&polygon);
}
let mut shape = query.single_mut();
*shape = ShapeBuilder::with(&polygon)
.fill(color)
.stroke((BLACK, outline_width as f32))
.build();
}

fn setup_system(mut commands: Commands) {
Expand All @@ -57,12 +50,10 @@ fn setup_system(mut commands: Commands) {

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
..default()
},
Fill::color(DARK_CYAN),
Stroke::new(BLACK, 10.0),
ShapeBuilder::with(&shape)
.fill(DARK_CYAN)
.stroke((BLACK, 10.0))
.build(),
ExampleShape,
));
}
41 changes: 17 additions & 24 deletions examples/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,23 @@ fn main() {
}

fn setup_system(mut commands: Commands) {
let mut path_builder = PathBuilder::new();
path_builder.move_to(Vec2::new(0., 0.));
path_builder.cubic_bezier_to(
Vec2::new(70., 70.),
Vec2::new(175., -35.),
Vec2::new(0., -140.),
);
path_builder.cubic_bezier_to(
Vec2::new(-175., -35.),
Vec2::new(-70., 70.),
Vec2::new(0., 0.),
);
path_builder.close();
let path = path_builder.build();
let shape = PathBuilder::new()
.fill(RED)
.stroke((BLACK, 10.0))
.move_to(Vec2::new(0., 0.))
.cubic_bezier_to(
Vec2::new(70., 70.),
Vec2::new(175., -35.),
Vec2::new(0., -140.),
)
.cubic_bezier_to(
Vec2::new(-175., -35.),
Vec2::new(-70., 70.),
Vec2::new(0., 0.),
)
.close()
.build();

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path,
transform: Transform::from_xyz(0., 75., 0.),
visibility: default(),
..default()
},
Stroke::new(BLACK, 10.0),
Fill::color(RED),
));
commands.spawn((shape, Transform::from_xyz(0., 75., 0.)));
}
14 changes: 6 additions & 8 deletions examples/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ fn setup_system(mut commands: Commands) {
};

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
..default()
},
Fill::color(DARK_CYAN),
Stroke::new(BLACK, 10.0),
));
commands.spawn(
ShapeBuilder::with(&shape)
.fill(DARK_CYAN)
.stroke((BLACK, 10.0))
.build(),
);
}
14 changes: 6 additions & 8 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ fn setup_system(mut commands: Commands) {
};

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&rect),
..default()
},
Stroke::new(BLACK, 10.0),
Fill::color(RED),
));
commands.spawn(
ShapeBuilder::with(&rect)
.stroke((BLACK, 10.0))
.fill(RED)
.build(),
);
}
8 changes: 1 addition & 7 deletions examples/rounded_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,5 @@ fn setup_system(mut commands: Commands) {
};

commands.spawn((Camera2d, Msaa::Sample4));
commands.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shape),
..default()
},
Fill::color(DARK_CYAN),
));
commands.spawn(ShapeBuilder::with(&shape).fill(DARK_CYAN).build());
}
71 changes: 31 additions & 40 deletions examples/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,18 @@ fn setup_system(mut commands: Commands) {
//the art style was approximated from https://www.kenney.nl/assets/cartography-pack
.with_children(|parent| {
let svg_doc_size = Vec2::new(512., 512.);
parent.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shapes::SvgPathShape {
svg_path_string: BLACKSMITH_OUTLINE.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
}),
..default()
},
Stroke::new(Color::BLACK, 4.0),
));
parent.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shapes::SvgPathShape {
svg_path_string: BLACKSMITH_DETAIL.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
}),
..default()
},
Stroke::new(Color::BLACK, 2.5),
));

let svg = shapes::SvgPathShape {
svg_path_string: BLACKSMITH_OUTLINE.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
};
parent.spawn(ShapeBuilder::with(&svg).stroke((Color::BLACK, 4.0)).build());

let svg = shapes::SvgPathShape {
svg_path_string: BLACKSMITH_DETAIL.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
};
parent.spawn(ShapeBuilder::with(&svg).stroke((Color::BLACK, 2.5)).build());
});

commands
Expand All @@ -69,28 +61,27 @@ fn setup_system(mut commands: Commands) {
//the art style was approximated from https://www.kenney.nl/assets/cartography-pack
.with_children(|parent| {
let svg_doc_size = Vec2::new(1000., 1000.);
parent.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shapes::SvgPathShape {
svg_path_string: SHACK.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
}),
..default()
},
Stroke::new(Color::BLACK, 20.0),
));

let svg = shapes::SvgPathShape {
svg_path_string: SHACK.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
};
parent.spawn(
ShapeBuilder::with(&svg)
.stroke((Color::BLACK, 20.0))
.build(),
);

// shack walls
parent.spawn((
ShapeBundle {
path: GeometryBuilder::build_as(&shapes::SvgPathShape {
svg_path_string: SHACK_WALLS.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
}),
..default()
},
Stroke::new(Color::BLACK, 17.5),
));
let svg = shapes::SvgPathShape {
svg_path_string: SHACK_WALLS.to_owned(),
svg_doc_size_in_px: svg_doc_size.to_owned(),
};
parent.spawn(
ShapeBuilder::with(&svg)
.stroke((Color::BLACK, 17.5))
.build(),
);
});
}

Expand Down
Loading

0 comments on commit 7dd478c

Please sign in to comment.