Skip to content

Commit ed518bc

Browse files
committed
fixes #85, add some button to control playback features
1 parent f507b5c commit ed518bc

37 files changed

+507
-90
lines changed

crates/bevy_utils/src/dev/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::prelude::{LayoutData, OutlineRectangle, ShapeOp};
55

66
use super::theme::BevyUtilsTheme;
77

8-
impl ShapeOp<BevyUtilsTheme, shapes::Rectangle, OutlineRectangle> for LayoutData {
8+
impl ShapeOp<BevyUtilsTheme, OutlineRectangle> for LayoutData {
99
fn get_shape(&self, theme: &BevyUtilsTheme) -> OutlineRectangle {
1010
let color = theme.layout.get_view_color();
1111
let outline_color = theme.layout.border_color;

crates/bevy_utils/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ pub mod prelude {
2828
View, ViewAddedQuery, ViewEntity, ViewQuery, ViewRootAddedQuery, ViewRootQuery,
2929
};
3030
#[doc(hidden)]
31-
pub use crate::shape::shape::{Shape, ShapeOp};
31+
pub use crate::shape::shape::{SingleShape, ShapeOp};
3232
#[doc(hidden)]
3333
pub use crate::shape::rectangle::{FillRectangle, StrokeRectangle, OutlineRectangle};
3434
#[doc(hidden)]
35-
pub use crate::shape::circle::{FillCircle, OutlineCircle};
35+
pub use crate::shape::circle::{FillCircle, StrokeCircle, OutlineCircle};
3636
#[doc(hidden)]
3737
pub use crate::shape::line::{StrokeLine};
3838
#[doc(hidden)]
39-
pub use crate::shape::path::{StrokePath};
39+
pub use crate::shape::path::{FillPath, StrokePath, StrokeCirclePath, StrokeRectanglePath};
4040
#[doc(hidden)]
4141
pub use crate::util::BevyUtil;
4242
#[doc(hidden)]

crates/bevy_utils/src/shape/circle.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_prototype_lyon::prelude::*;
33

44
use crate::prelude::BevyUtil;
55

6-
use super::shape::Shape;
6+
use super::shape::{SingleShape, Shape};
77

88
#[derive(Clone, Debug)]
99
pub struct FillCircle {
@@ -12,7 +12,12 @@ pub struct FillCircle {
1212
pub offset: Vec3,
1313
}
1414

15-
impl Shape<shapes::Circle> for FillCircle {
15+
impl Shape for FillCircle {
16+
fn _create(&self, commands: &mut Commands, entity: Entity) {
17+
self._do_create(commands, entity);
18+
}
19+
}
20+
impl SingleShape<shapes::Circle> for FillCircle {
1621
fn get_shape(&self) -> shapes::Circle {
1722
shapes::Circle {
1823
center: Vec2::ZERO,
@@ -37,6 +42,44 @@ impl Shape<shapes::Circle> for FillCircle {
3742
}
3843
}
3944

45+
#[derive(Clone, Debug)]
46+
pub struct StrokeCircle {
47+
pub radius: f32,
48+
pub line_width: f32,
49+
pub color: Color,
50+
pub offset: Vec3,
51+
}
52+
53+
impl Shape for StrokeCircle {
54+
fn _create(&self, commands: &mut Commands, entity: Entity) {
55+
self._do_create(commands, entity);
56+
}
57+
}
58+
impl SingleShape<shapes::Circle> for StrokeCircle {
59+
fn get_shape(&self) -> shapes::Circle {
60+
shapes::Circle {
61+
center: Vec2::ZERO,
62+
radius: self.radius,
63+
}
64+
}
65+
fn get_colors(&self) -> ShapeColors {
66+
ShapeColors::new(self.color)
67+
}
68+
fn get_draw_mode(&self) -> DrawMode {
69+
DrawMode::Stroke(StrokeOptions::default().with_line_width(self.line_width))
70+
}
71+
fn get_transform(&self) -> Transform {
72+
if self.radius <= 0.0 {
73+
return BevyUtil::offscreen_transform();
74+
}
75+
Transform::from_xyz(
76+
self.offset.x,
77+
self.offset.y,
78+
self.offset.z,
79+
)
80+
}
81+
}
82+
4083
#[derive(Clone, Debug)]
4184
pub struct OutlineCircle {
4285
pub radius: f32,
@@ -46,7 +89,12 @@ pub struct OutlineCircle {
4689
pub offset: Vec3,
4790
}
4891

49-
impl Shape<shapes::Circle> for OutlineCircle {
92+
impl Shape for OutlineCircle {
93+
fn _create(&self, commands: &mut Commands, entity: Entity) {
94+
self._do_create(commands, entity);
95+
}
96+
}
97+
impl SingleShape<shapes::Circle> for OutlineCircle {
5098
fn get_shape(&self) -> shapes::Circle {
5199
shapes::Circle {
52100
center: Vec2::ZERO,

crates/bevy_utils/src/shape/line.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use bevy::prelude::*;
22
use bevy_prototype_lyon::prelude::*;
33

4-
use super::shape::Shape;
4+
use super::shape::{Shape, SingleShape};
55

66
#[derive(Clone, Debug)]
77
pub struct StrokeLine {
@@ -12,7 +12,12 @@ pub struct StrokeLine {
1212
pub offset: Vec3,
1313
}
1414

15-
impl Shape<shapes::Line> for StrokeLine {
15+
impl Shape for StrokeLine {
16+
fn _create(&self, commands: &mut Commands, entity: Entity) {
17+
self._do_create(commands, entity);
18+
}
19+
}
20+
impl SingleShape<shapes::Line> for StrokeLine {
1621
fn get_shape(&self) -> shapes::Line {
1722
shapes::Line(self.from, self.to)
1823
}

crates/bevy_utils/src/shape/path.rs

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
use bevy::prelude::*;
22
use bevy_prototype_lyon::prelude::*;
33

4-
use super::shape::Shape;
4+
use super::{shape::{Shape, SingleShape}, shapes::DoubleShape};
5+
6+
#[derive(Clone, Debug)]
7+
pub struct FillPath {
8+
pub size: Vec2,
9+
pub path: String,
10+
pub color: Color,
11+
pub offset: Vec3,
12+
pub scale: f32,
13+
pub angle: f32,
14+
}
15+
16+
impl Shape for FillPath {
17+
fn _create(&self, commands: &mut Commands, entity: Entity) {
18+
self._do_create(commands, entity);
19+
}
20+
}
21+
impl SingleShape<shapes::SvgPathShape> for FillPath {
22+
fn get_shape(&self) -> shapes::SvgPathShape {
23+
shapes::SvgPathShape {
24+
svg_doc_size_in_px: self.size,
25+
svg_path_string: self.path.clone(),
26+
}
27+
}
28+
fn get_colors(&self) -> ShapeColors {
29+
ShapeColors::new(self.color)
30+
}
31+
fn get_draw_mode(&self) -> DrawMode {
32+
DrawMode::Fill(FillOptions::default())
33+
}
34+
fn get_transform(&self) -> Transform {
35+
Transform {
36+
translation: self.offset,
37+
rotation: Quat::from_rotation_z(self.angle),
38+
scale: Vec3::new(self.scale, self.scale, 1.0),
39+
}
40+
}
41+
}
542

643
#[derive(Clone, Debug)]
744
pub struct StrokePath {
@@ -14,7 +51,12 @@ pub struct StrokePath {
1451
pub angle: f32,
1552
}
1653

17-
impl Shape<shapes::SvgPathShape> for StrokePath {
54+
impl Shape for StrokePath {
55+
fn _create(&self, commands: &mut Commands, entity: Entity) {
56+
self._do_create(commands, entity);
57+
}
58+
}
59+
impl SingleShape<shapes::SvgPathShape> for StrokePath {
1860
fn get_shape(&self) -> shapes::SvgPathShape {
1961
shapes::SvgPathShape {
2062
svg_doc_size_in_px: self.size,
@@ -36,4 +78,71 @@ impl Shape<shapes::SvgPathShape> for StrokePath {
3678
scale: Vec3::new(self.scale, self.scale, 1.0),
3779
}
3880
}
81+
}
82+
83+
#[derive(Clone, Debug)]
84+
pub struct StrokeCirclePath {
85+
pub radius: f32,
86+
pub path: StrokePath,
87+
}
88+
89+
impl Shape for StrokeCirclePath {
90+
fn _create(&self, commands: &mut Commands, entity: Entity) {
91+
self._do_create(commands, entity);
92+
}
93+
}
94+
impl DoubleShape<shapes::Circle, shapes::SvgPathShape> for StrokeCirclePath {
95+
fn get_shape1(&self) -> shapes::Circle {
96+
shapes::Circle {
97+
center: Vec2::ZERO,
98+
radius: self.radius,
99+
}
100+
}
101+
fn get_shape2(&self) -> shapes::SvgPathShape {
102+
self.path.get_shape()
103+
}
104+
fn get_colors(&self) -> ShapeColors {
105+
self.path.get_colors()
106+
}
107+
fn get_draw_mode(&self) -> DrawMode {
108+
self.path.get_draw_mode()
109+
}
110+
fn get_transform(&self) -> Transform {
111+
self.path.get_transform()
112+
}
113+
}
114+
115+
#[derive(Clone, Debug)]
116+
pub struct StrokeRectanglePath {
117+
pub width: f32,
118+
pub height: f32,
119+
pub origin: shapes::RectangleOrigin,
120+
pub path: StrokePath,
121+
}
122+
123+
impl Shape for StrokeRectanglePath {
124+
fn _create(&self, commands: &mut Commands, entity: Entity) {
125+
self._do_create(commands, entity);
126+
}
127+
}
128+
impl DoubleShape<shapes::Rectangle, shapes::SvgPathShape> for StrokeRectanglePath {
129+
fn get_shape1(&self) -> shapes::Rectangle {
130+
shapes::Rectangle {
131+
width: self.width,
132+
height: self.height,
133+
origin: self.origin,
134+
}
135+
}
136+
fn get_shape2(&self) -> shapes::SvgPathShape {
137+
self.path.get_shape()
138+
}
139+
fn get_colors(&self) -> ShapeColors {
140+
self.path.get_colors()
141+
}
142+
fn get_draw_mode(&self) -> DrawMode {
143+
self.path.get_draw_mode()
144+
}
145+
fn get_transform(&self) -> Transform {
146+
self.path.get_transform()
147+
}
39148
}

crates/bevy_utils/src/shape/rectangle.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bevy_prototype_lyon::prelude::*;
33

44
use crate::prelude::BevyUtil;
55

6-
use super::shape::Shape;
6+
use super::shape::{Shape, SingleShape};
77

88
#[derive(Clone, Debug)]
99
pub struct FillRectangle {
@@ -14,7 +14,12 @@ pub struct FillRectangle {
1414
pub offset: Vec3,
1515
}
1616

17-
impl Shape<shapes::Rectangle> for FillRectangle {
17+
impl Shape for FillRectangle {
18+
fn _create(&self, commands: &mut Commands, entity: Entity) {
19+
self._do_create(commands, entity);
20+
}
21+
}
22+
impl SingleShape<shapes::Rectangle> for FillRectangle {
1823
fn get_shape(&self) -> shapes::Rectangle {
1924
shapes::Rectangle {
2025
width: self.width,
@@ -50,7 +55,12 @@ pub struct StrokeRectangle {
5055
pub offset: Vec3,
5156
}
5257

53-
impl Shape<shapes::Rectangle> for StrokeRectangle {
58+
impl Shape for StrokeRectangle {
59+
fn _create(&self, commands: &mut Commands, entity: Entity) {
60+
self._do_create(commands, entity);
61+
}
62+
}
63+
impl SingleShape<shapes::Rectangle> for StrokeRectangle {
5464
fn get_shape(&self) -> shapes::Rectangle {
5565
shapes::Rectangle {
5666
width: self.width,
@@ -87,7 +97,12 @@ pub struct OutlineRectangle {
8797
pub offset: Vec3,
8898
}
8999

90-
impl Shape<shapes::Rectangle> for OutlineRectangle {
100+
impl Shape for OutlineRectangle {
101+
fn _create(&self, commands: &mut Commands, entity: Entity) {
102+
self._do_create(commands, entity);
103+
}
104+
}
105+
impl SingleShape<shapes::Rectangle> for OutlineRectangle {
91106
fn get_shape(&self) -> shapes::Rectangle {
92107
shapes::Rectangle {
93108
width: self.width,

crates/bevy_utils/src/shape/shape.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
use bevy::prelude::*;
2-
use bevy::ecs::system::EntityCommands;
32
use bevy_prototype_lyon::prelude::*;
43
use bevy_prototype_lyon::entity::ShapeBundle;
54

6-
pub trait Shape<T: Geometry> {
5+
pub trait Shape {
6+
fn _create(&self, commands: &mut Commands, entity: Entity);
7+
fn create(&self, commands: &mut Commands, parent: Entity) -> Entity {
8+
let entity = commands.spawn().id();
9+
commands.entity(parent).push_children(&[entity]);
10+
self._create(commands, entity);
11+
entity
12+
}
13+
fn update(&self, commands: &mut Commands, entity: Entity) {
14+
commands.entity(entity).remove_bundle::<ShapeBundle>();
15+
self._create(commands, entity);
16+
}
17+
}
18+
19+
pub trait SingleShape<T: Geometry> : Shape {
720
fn get_shape(&self) -> T;
821
fn get_colors(&self) -> ShapeColors;
922
fn get_draw_mode(&self) -> DrawMode;
1023
fn get_transform(&self) -> Transform;
11-
fn _create(&self, entity_commands: &mut EntityCommands) {
24+
fn _do_create(&self, commands: &mut Commands, entity: Entity) {
1225
let shape = self.get_shape();
1326
let colors = self.get_colors();
1427
let draw_mode = self.get_draw_mode();
1528
let transform = self.get_transform();
16-
entity_commands.insert_bundle(GeometryBuilder::build_as(
29+
commands.entity(entity).insert_bundle(GeometryBuilder::build_as(
1730
&shape, colors, draw_mode, transform,
1831
));
1932
}
20-
fn create(&self, commands: &mut Commands, parent: Entity) -> Entity {
21-
let mut entity_commands = commands.spawn();
22-
self._create(&mut entity_commands);
23-
let shape_entity = entity_commands.id();
24-
commands.entity(parent).push_children(&[shape_entity]);
25-
shape_entity
26-
}
27-
fn update(&self, commands: &mut Commands, entity: Entity) {
28-
let mut entity_commands = commands.entity(entity);
29-
entity_commands.remove_bundle::<ShapeBundle>();
30-
self._create(&mut entity_commands);
31-
}
3233
}
3334

34-
pub trait ShapeOp<Theme, T: Geometry, S: Shape<T>> : Clone + Send + Sync + 'static {
35+
pub trait ShapeOp<Theme, S: Shape> : Clone + Send + Sync + 'static {
3536
fn get_shape(&self, theme: &Theme) -> S;
36-
fn create(&self, commands: &mut Commands, theme: &Theme, entity: Entity) -> Entity {
37+
fn create(&self, commands: &mut Commands, theme: &Theme, parent: Entity) -> Entity {
3738
let shape = self.get_shape(theme);
38-
let shape_entity = shape.create(commands, entity);
39+
let shape_entity = shape.create(commands, parent);
3940
commands.entity(shape_entity).insert(self.clone());
4041
shape_entity
4142
}

0 commit comments

Comments
 (0)