1#[cfg(not(target_arch = "wasm32"))]
17use bevy::{
18 input::common_conditions::input_just_pressed,
19 sprite_render::{Wireframe2dConfig, Wireframe2dPlugin},
20};
21use bevy::{input::common_conditions::input_toggle_active, prelude::*};
22
23fn main() {
24 let mut app = App::new();
25 app.add_plugins((
26 DefaultPlugins,
27 #[cfg(not(target_arch = "wasm32"))]
28 Wireframe2dPlugin::default(),
29 ))
30 .add_systems(Startup, setup);
31 #[cfg(not(target_arch = "wasm32"))]
32 app.add_systems(
33 Update,
34 toggle_wireframe.run_if(input_just_pressed(KeyCode::Space)),
35 );
36 app.add_systems(
37 Update,
38 rotate.run_if(input_toggle_active(false, KeyCode::KeyR)),
39 );
40 app.run();
41}
42
43const X_EXTENT: f32 = 1000.;
44const Y_EXTENT: f32 = 150.;
45const THICKNESS: f32 = 5.0;
46
47fn setup(
48 mut commands: Commands,
49 mut meshes: ResMut<Assets<Mesh>>,
50 mut materials: ResMut<Assets<ColorMaterial>>,
51) {
52 commands.spawn(Camera2d);
53
54 let shapes = [
55 meshes.add(Circle::new(50.0)),
56 meshes.add(CircularSector::new(50.0, 1.0)),
57 meshes.add(CircularSegment::new(50.0, 1.25)),
58 meshes.add(Ellipse::new(25.0, 50.0)),
59 meshes.add(Annulus::new(25.0, 50.0)),
60 meshes.add(Capsule2d::new(25.0, 50.0)),
61 meshes.add(Rhombus::new(75.0, 100.0)),
62 meshes.add(Rectangle::new(50.0, 100.0)),
63 meshes.add(RegularPolygon::new(50.0, 6)),
64 meshes.add(Triangle2d::new(
65 Vec2::Y * 50.0,
66 Vec2::new(-50.0, -50.0),
67 Vec2::new(50.0, -50.0),
68 )),
69 meshes.add(Segment2d::new(
70 Vec2::new(-50.0, 50.0),
71 Vec2::new(50.0, -50.0),
72 )),
73 meshes.add(Polyline2d::new(vec![
74 Vec2::new(-50.0, 50.0),
75 Vec2::new(0.0, -50.0),
76 Vec2::new(50.0, 50.0),
77 ])),
78 ];
79 let num_shapes = shapes.len();
80
81 for (i, shape) in shapes.into_iter().enumerate() {
82 let color = Color::hsl(360. * i as f32 / num_shapes as f32, 0.95, 0.7);
84
85 commands.spawn((
86 Mesh2d(shape),
87 MeshMaterial2d(materials.add(color)),
88 Transform::from_xyz(
89 -X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
91 Y_EXTENT / 2.,
92 0.0,
93 ),
94 ));
95 }
96
97 let rings = [
98 meshes.add(Circle::new(50.0).to_ring(THICKNESS)),
99 meshes.add(Ring::new(
101 CircularSector::new(50.0, 1.0),
102 CircularSector::new(45.0, 1.0),
103 )),
104 meshes.add(CircularSegment::new(50.0, 1.25).to_ring(THICKNESS)),
105 meshes.add({
106 let outer = Ellipse::new(25.0, 50.0);
108 let mut inner = outer;
109 inner.half_size -= Vec2::splat(THICKNESS);
110 Ring::new(outer, inner)
111 }),
112 meshes.add(Ring::new(Circle::new(50.0), Circle::new(25.0))),
114 meshes.add(Capsule2d::new(25.0, 50.0).to_ring(THICKNESS)),
115 meshes.add(Rhombus::new(75.0, 100.0).to_ring(THICKNESS)),
116 meshes.add(Rectangle::new(50.0, 100.0).to_ring(THICKNESS)),
117 meshes.add(RegularPolygon::new(50.0, 6).to_ring(THICKNESS)),
118 meshes.add(
119 Triangle2d::new(
120 Vec2::Y * 50.0,
121 Vec2::new(-50.0, -50.0),
122 Vec2::new(50.0, -50.0),
123 )
124 .to_ring(THICKNESS),
125 ),
126 ];
127 let num_rings = rings.len() + 2;
129
130 for (i, shape) in rings.into_iter().enumerate() {
131 let color = Color::hsl(360. * i as f32 / num_rings as f32, 0.95, 0.7);
133
134 commands.spawn((
135 Mesh2d(shape),
136 MeshMaterial2d(materials.add(color)),
137 Transform::from_xyz(
138 -X_EXTENT / 2. + i as f32 / (num_rings - 1) as f32 * X_EXTENT,
140 -Y_EXTENT / 2.,
141 0.0,
142 ),
143 ));
144 }
145
146 let mut text = "Press 'R' to pause/resume rotation".to_string();
147 #[cfg(not(target_arch = "wasm32"))]
148 text.push_str("\nPress 'Space' to toggle wireframes");
149
150 commands.spawn((
151 Text::new(text),
152 Node {
153 position_type: PositionType::Absolute,
154 top: px(12),
155 left: px(12),
156 ..default()
157 },
158 ));
159}
160
161#[cfg(not(target_arch = "wasm32"))]
162fn toggle_wireframe(mut wireframe_config: ResMut<Wireframe2dConfig>) {
163 wireframe_config.global = !wireframe_config.global;
164}
165
166fn rotate(mut query: Query<&mut Transform, With<Mesh2d>>, time: Res<Time>) {
167 for mut transform in &mut query {
168 transform.rotate_z(time.delta_secs() / 2.0);
169 }
170}