Skip to content

Commit f507b5c

Browse files
committed
fixes #88, cleanup shape abstraction
1 parent e5aadc7 commit f507b5c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1271
-1400
lines changed
Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,22 @@
11
use bevy::prelude::*;
22
use bevy_prototype_lyon::prelude::*;
33

4-
use crate::lyon::lyon_shape::{LyonShape, LyonShapeOp};
5-
use crate::prelude::LayoutData;
4+
use crate::prelude::{LayoutData, OutlineRectangle, ShapeOp};
65

76
use super::theme::BevyUtilsTheme;
87

9-
pub struct LayoutShape<'a> {
10-
pub theme: &'a BevyUtilsTheme,
11-
pub data: LayoutData,
12-
}
13-
14-
impl<'a> LyonShape<shapes::Rectangle> for LayoutShape<'a> {
15-
fn get_name(&self) -> String {
16-
format!("{}", self.data)
17-
}
18-
fn get_shape(&self) -> shapes::Rectangle {
19-
shapes::Rectangle {
20-
width: self.data.size.width,
21-
height: self.data.size.height,
22-
origin: shapes::RectangleOrigin::from(self.data),
23-
}
24-
}
25-
fn get_colors(&self) -> ShapeColors {
26-
ShapeColors::outlined(
27-
self.theme.layout.get_view_color(),
28-
self.theme.layout.border_color,
29-
)
30-
}
31-
fn get_draw_mode(&self) -> DrawMode {
32-
DrawMode::Outlined {
33-
fill_options: FillOptions::default(),
34-
outline_options: StrokeOptions::default()
35-
.with_line_width(self.theme.layout.border_line_width),
8+
impl ShapeOp<BevyUtilsTheme, shapes::Rectangle, OutlineRectangle> for LayoutData {
9+
fn get_shape(&self, theme: &BevyUtilsTheme) -> OutlineRectangle {
10+
let color = theme.layout.get_view_color();
11+
let outline_color = theme.layout.border_color;
12+
OutlineRectangle {
13+
width: self.size.width,
14+
height: self.size.height,
15+
origin: shapes::RectangleOrigin::from(*self),
16+
color,
17+
outline_width: theme.layout.border_line_width,
18+
outline_color,
19+
offset: Vec3::new(self.offset.x, self.offset.y, 2.0),
3620
}
3721
}
38-
fn get_transform(&self) -> Transform {
39-
Transform::from_xyz(self.data.offset.x, self.data.offset.y, 2.0)
40-
}
41-
}
42-
43-
impl<'a> LyonShapeOp<'a, BevyUtilsTheme, LayoutData, shapes::Rectangle, LayoutShape<'a>>
44-
for LayoutShape<'a>
45-
{
46-
fn new_shape(theme: &'a BevyUtilsTheme, data: LayoutData) -> LayoutShape<'a> {
47-
LayoutShape::<'a> { theme, data }
48-
}
4922
}

crates/bevy_utils/src/dev/plugin.rs

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

4-
use super::layout::LayoutShape;
54
use super::theme::BevyUtilsTheme;
65

76
#[derive(Debug, Default)]
@@ -21,7 +20,7 @@ fn on_add_layout_data(
2120
layout_query: Query<(Entity, &LayoutData), Added<LayoutData>>,
2221
) {
2322
for (entity, layout) in layout_query.iter() {
24-
LayoutShape::create(&mut commands, &theme, entity, *layout);
23+
layout.create(&mut commands, &theme, entity);
2524
}
2625
}
2726

@@ -31,6 +30,6 @@ fn on_layout_data_changed(
3130
layout_query: Query<(Entity, &LayoutData), Changed<LayoutData>>,
3231
) {
3332
for (entity, layout) in layout_query.iter() {
34-
LayoutShape::update(&mut commands, &theme, entity, &layout);
33+
layout.update(&mut commands, &theme, entity);
3534
}
3635
}

crates/bevy_utils/src/layout/anchor.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use bevy::prelude::*;
22
use std::fmt::Display;
33

4+
#[cfg(feature = "inspector")]
5+
use bevy_inspector_egui::Inspectable;
6+
47
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8+
#[cfg_attr(feature = "inspector", derive(Inspectable))]
59
pub enum LayoutHAnchor {
610
Left,
711
Center,
@@ -19,6 +23,7 @@ impl Default for LayoutHAnchor {
1923
}
2024

2125
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26+
#[cfg_attr(feature = "inspector", derive(Inspectable))]
2227
pub enum LayoutVAnchor {
2328
Top,
2429
Center,
@@ -36,6 +41,7 @@ impl Default for LayoutVAnchor {
3641
}
3742

3843
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
44+
#[cfg_attr(feature = "inspector", derive(Inspectable))]
3945
pub struct LayoutAnchor {
4046
pub v: LayoutVAnchor,
4147
pub h: LayoutHAnchor,

crates/bevy_utils/src/layout/data.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ use std::ops::{Add, Sub};
44
use bevy::prelude::*;
55
use bevy_prototype_lyon::shapes;
66

7+
#[cfg(feature = "inspector")]
8+
use bevy_inspector_egui::Inspectable;
9+
710
use crate::prelude::{LayoutAnchor, LayoutHAnchor, LayoutVAnchor};
811

912
#[derive(Copy, Clone, Debug, Default)]
13+
#[cfg_attr(feature = "inspector", derive(Inspectable))]
1014
pub struct LayoutSize {
1115
pub width: f32,
1216
pub height: f32,
@@ -46,6 +50,7 @@ impl LayoutConstraint {
4650
}
4751

4852
#[derive(Copy, Clone, Debug, Default)]
53+
#[cfg_attr(feature = "inspector", derive(Inspectable))]
4954
pub struct LayoutData {
5055
pub depth: usize,
5156
pub size: LayoutSize,

crates/bevy_utils/src/layout/view.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ pub trait View<TE: LayoutEnv>: Any + Send + Sync + ToString + 'static {
140140
fn is_root(&self) -> bool {
141141
false
142142
}
143+
fn log_set_layout(&self) -> bool {
144+
false
145+
}
143146
fn log_layout_changed(&self) -> bool {
144147
false
145148
}
@@ -171,19 +174,19 @@ pub trait View<TE: LayoutEnv>: Any + Send + Sync + ToString + 'static {
171174
} else {
172175
data
173176
};
174-
/*
175-
if need_adjust {
176-
println!(
177-
"{}.set_layout_data(\n\t{} {} ->\n\t{}\n)",
178-
self.to_string(),
179-
data.pivot,
180-
data.offset,
181-
adjusted
182-
);
183-
} else {
184-
println!("{}.set_layout_data(\n\t{}\n)", self.to_string(), adjusted);
177+
if self.log_set_layout() {
178+
if need_adjust {
179+
println!(
180+
"{}.set_layout_data(\n\t{} {} ->\n\t{}\n)",
181+
self.to_string(),
182+
data.pivot,
183+
data.offset,
184+
adjusted
185+
);
186+
} else {
187+
println!("{}.set_layout_data(\n\t{}\n)", self.to_string(), adjusted);
188+
}
185189
}
186-
*/
187190
match layout_query.get_mut(entity) {
188191
Ok((mut layout_data, mut transform)) => {
189192
*layout_data = adjusted;

crates/bevy_utils/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod bundle;
22
pub mod layout;
3-
pub mod lyon;
3+
pub mod shape;
44
pub mod util;
55
pub mod view;
66

@@ -28,7 +28,15 @@ pub mod prelude {
2828
View, ViewAddedQuery, ViewEntity, ViewQuery, ViewRootAddedQuery, ViewRootQuery,
2929
};
3030
#[doc(hidden)]
31-
pub use crate::lyon::lyon_shape::{LyonShape, LyonShapeOp, NoThemeLyonShape};
31+
pub use crate::shape::shape::{Shape, ShapeOp};
32+
#[doc(hidden)]
33+
pub use crate::shape::rectangle::{FillRectangle, StrokeRectangle, OutlineRectangle};
34+
#[doc(hidden)]
35+
pub use crate::shape::circle::{FillCircle, OutlineCircle};
36+
#[doc(hidden)]
37+
pub use crate::shape::line::{StrokeLine};
38+
#[doc(hidden)]
39+
pub use crate::shape::path::{StrokePath};
3240
#[doc(hidden)]
3341
pub use crate::util::BevyUtil;
3442
#[doc(hidden)]

crates/bevy_utils/src/lyon/lyon_shape.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

crates/bevy_utils/src/lyon/mod.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use bevy::prelude::*;
2+
use bevy_prototype_lyon::prelude::*;
3+
4+
use crate::prelude::BevyUtil;
5+
6+
use super::shape::Shape;
7+
8+
#[derive(Clone, Debug)]
9+
pub struct FillCircle {
10+
pub radius: f32,
11+
pub color: Color,
12+
pub offset: Vec3,
13+
}
14+
15+
impl Shape<shapes::Circle> for FillCircle {
16+
fn get_shape(&self) -> shapes::Circle {
17+
shapes::Circle {
18+
center: Vec2::ZERO,
19+
radius: self.radius,
20+
}
21+
}
22+
fn get_colors(&self) -> ShapeColors {
23+
ShapeColors::new(self.color)
24+
}
25+
fn get_draw_mode(&self) -> DrawMode {
26+
DrawMode::Fill(FillOptions::default())
27+
}
28+
fn get_transform(&self) -> Transform {
29+
if self.radius <= 0.0 {
30+
return BevyUtil::offscreen_transform();
31+
}
32+
Transform::from_xyz(
33+
self.offset.x,
34+
self.offset.y,
35+
self.offset.z,
36+
)
37+
}
38+
}
39+
40+
#[derive(Clone, Debug)]
41+
pub struct OutlineCircle {
42+
pub radius: f32,
43+
pub color: Color,
44+
pub outline_width: f32,
45+
pub outline_color: Color,
46+
pub offset: Vec3,
47+
}
48+
49+
impl Shape<shapes::Circle> for OutlineCircle {
50+
fn get_shape(&self) -> shapes::Circle {
51+
shapes::Circle {
52+
center: Vec2::ZERO,
53+
radius: self.radius,
54+
}
55+
}
56+
fn get_colors(&self) -> ShapeColors {
57+
if self.outline_width > 0.0 {
58+
ShapeColors::outlined(
59+
self.color,
60+
self.outline_color,
61+
)
62+
} else {
63+
ShapeColors::new(self.color)
64+
}
65+
}
66+
fn get_draw_mode(&self) -> DrawMode {
67+
if self.outline_width > 0.0 {
68+
DrawMode::Outlined {
69+
fill_options: FillOptions::default(),
70+
outline_options: StrokeOptions::default().with_line_width(self.outline_width),
71+
}
72+
} else {
73+
DrawMode::Fill(FillOptions::default())
74+
}
75+
}
76+
fn get_transform(&self) -> Transform {
77+
if self.radius <= 0.0 {
78+
return BevyUtil::offscreen_transform();
79+
}
80+
Transform::from_xyz(
81+
self.offset.x,
82+
self.offset.y,
83+
self.offset.z,
84+
)
85+
}
86+
}
87+

0 commit comments

Comments
 (0)