1use std::f32::consts::{FRAC_PI_2, PI, TAU};
4
5use bevy::{color::palettes::css::*, math::Isometry2d, prelude::*};
6
7fn main() {
8 App::new()
9 .add_plugins(DefaultPlugins)
10 .init_gizmo_group::<MyRoundGizmos>()
11 .add_systems(Startup, setup)
12 .add_systems(
13 Update,
14 (
15 draw_example_collection,
16 update_config,
17 drive_gizmos_animation,
18 ),
19 )
20 .run();
21}
22
23#[derive(Default, Reflect, GizmoConfigGroup)]
25struct MyRoundGizmos {}
26
27fn setup(mut commands: Commands) {
28 commands.spawn(Camera2d);
29 commands.spawn((
31 Text::new(
32 "Hold 'Left' or 'Right' to change the line width of straight gizmos\n\
33 Hold 'Up' or 'Down' to change the line width of round gizmos\n\
34 Press '1' / '2' to toggle the visibility of straight / round gizmos\n\
35 Press 'U' / 'I' to cycle through line styles\n\
36 Press 'J' / 'K' to cycle through line joins\n\
37 Press 'L' to cycle through gizmos animations (only for dotted/dashed round gizmos)\n\
38 Press 'Spacebar' to toggle pause",
39 ),
40 Node {
41 position_type: PositionType::Absolute,
42 top: px(12),
43 left: px(12),
44 ..default()
45 },
46 ));
47}
48
49fn draw_example_collection(
50 mut gizmos: Gizmos,
51 mut my_gizmos: Gizmos<MyRoundGizmos>,
52 time: Res<Time>,
53) {
54 let sin_t_scaled = ops::sin(time.elapsed_secs()) * 50.;
55 gizmos.line_2d(Vec2::Y * -sin_t_scaled, Vec2::splat(-80.), RED);
56 gizmos.ray_2d(Vec2::Y * sin_t_scaled, Vec2::splat(80.), LIME);
57
58 gizmos
59 .grid_2d(
60 Isometry2d::IDENTITY,
61 UVec2::new(16, 9),
62 Vec2::new(80., 80.),
63 LinearRgba::gray(0.05),
65 )
66 .outer_edges();
67
68 gizmos.linestrip_gradient_2d([
70 (Vec2::Y * 300., BLUE),
71 (Vec2::new(-255., -155.), RED),
72 (Vec2::new(255., -155.), LIME),
73 (Vec2::Y * 300., BLUE),
74 ]);
75
76 gizmos.rect_2d(Isometry2d::IDENTITY, Vec2::splat(650.), BLACK);
77
78 gizmos.cross_2d(Vec2::new(-160., 120.), 12., FUCHSIA);
79
80 let domain = Interval::EVERYWHERE;
81 let curve = FunctionCurve::new(domain, |t| Vec2::new(t, ops::sin(t / 25.0) * 100.0));
82 let resolution = ((ops::sin(time.elapsed_secs()) + 1.0) * 50.0) as usize;
83 let times_and_colors = (0..=resolution)
84 .map(|n| n as f32 / resolution as f32)
85 .map(|t| (t - 0.5) * 600.0)
86 .map(|t| (t, TEAL.mix(&HOT_PINK, (t + 300.0) / 600.0)));
87 gizmos.curve_gradient_2d(curve, times_and_colors);
88
89 my_gizmos
90 .rounded_rect_2d(Isometry2d::IDENTITY, Vec2::splat(630.), BLACK)
91 .corner_radius(ops::cos(time.elapsed_secs() / 3.) * 100.);
92
93 my_gizmos
96 .circle_2d(Isometry2d::IDENTITY, 300., NAVY)
97 .resolution(64);
98
99 my_gizmos.ellipse_2d(
100 Rot2::radians(time.elapsed_secs() % TAU),
101 Vec2::new(100., 200.),
102 YELLOW_GREEN,
103 );
104
105 my_gizmos.arc_2d(
108 Rot2::radians(sin_t_scaled / 10.),
109 FRAC_PI_2,
110 310.,
111 ORANGE_RED,
112 );
113 my_gizmos.arc_2d(Isometry2d::IDENTITY, FRAC_PI_2, 80.0, ORANGE_RED);
114 my_gizmos.long_arc_2d_between(Vec2::ZERO, Vec2::X * 20.0, Vec2::Y * 20.0, ORANGE_RED);
115 my_gizmos.short_arc_2d_between(Vec2::ZERO, Vec2::X * 40.0, Vec2::Y * 40.0, ORANGE_RED);
116
117 gizmos.arrow_2d(
118 Vec2::ZERO,
119 Vec2::from_angle(sin_t_scaled / -10. + PI / 2.) * 50.,
120 YELLOW,
121 );
122
123 gizmos
125 .arrow_2d(
126 Vec2::ZERO,
127 Vec2::from_angle(sin_t_scaled / -10.) * 50.,
128 GREEN,
129 )
130 .with_double_end()
131 .with_tip_length(10.);
132
133 my_gizmos.arc_2d(Isometry2d::default(), FRAC_PI_2, 210., OLD_LACE);
134}
135
136fn update_config(
137 mut config_store: ResMut<GizmoConfigStore>,
138 keyboard: Res<ButtonInput<KeyCode>>,
139 real_time: Res<Time<Real>>,
140 mut virtual_time: ResMut<Time<Virtual>>,
141) {
142 let (config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
143 if keyboard.pressed(KeyCode::ArrowRight) {
144 config.line.width += 5. * real_time.delta_secs();
145 config.line.width = config.line.width.clamp(0., 50.);
146 }
147 if keyboard.pressed(KeyCode::ArrowLeft) {
148 config.line.width -= 5. * real_time.delta_secs();
149 config.line.width = config.line.width.clamp(0., 50.);
150 }
151 if keyboard.just_pressed(KeyCode::Digit1) {
152 config.enabled ^= true;
153 }
154 if keyboard.just_pressed(KeyCode::KeyU) {
155 config.line.style = match config.line.style {
156 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
157 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
158 gap_scale: 3.0,
159 line_scale: 5.0,
160 },
161 _ => GizmoLineStyle::Solid,
162 };
163 }
164 if keyboard.just_pressed(KeyCode::KeyI) {
165 config.line.style = match config.line.style {
166 GizmoLineStyle::Solid => GizmoLineStyle::Dashed {
167 gap_scale: 3.0,
168 line_scale: 5.0,
169 },
170 GizmoLineStyle::Dotted => GizmoLineStyle::Solid,
171 _ => GizmoLineStyle::Dotted,
172 };
173 }
174 if keyboard.just_pressed(KeyCode::KeyJ) {
175 config.line.joints = match config.line.joints {
176 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
177 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
178 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
179 GizmoLineJoint::None => GizmoLineJoint::Bevel,
180 };
181 }
182
183 if keyboard.just_pressed(KeyCode::KeyK) {
184 config.line.joints = match config.line.joints {
185 GizmoLineJoint::Bevel => GizmoLineJoint::None,
186 GizmoLineJoint::Miter => GizmoLineJoint::Bevel,
187 GizmoLineJoint::Round(_) => GizmoLineJoint::Miter,
188 GizmoLineJoint::None => GizmoLineJoint::Round(4),
189 };
190 }
191
192 let (my_config, _) = config_store.config_mut::<MyRoundGizmos>();
193 if keyboard.pressed(KeyCode::ArrowUp) {
194 my_config.line.width += 5. * real_time.delta_secs();
195 my_config.line.width = my_config.line.width.clamp(0., 50.);
196 }
197 if keyboard.pressed(KeyCode::ArrowDown) {
198 my_config.line.width -= 5. * real_time.delta_secs();
199 my_config.line.width = my_config.line.width.clamp(0., 50.);
200 }
201 if keyboard.just_pressed(KeyCode::Digit2) {
202 my_config.enabled ^= true;
203 }
204 if keyboard.just_pressed(KeyCode::KeyI) {
205 my_config.line.style = match my_config.line.style {
206 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
207 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
208 gap_scale: 3.0,
209 line_scale: 5.0,
210 },
211 _ => GizmoLineStyle::Solid,
212 };
213 }
214 if keyboard.just_pressed(KeyCode::KeyK) {
215 my_config.line.joints = match my_config.line.joints {
216 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
217 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
218 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
219 GizmoLineJoint::None => GizmoLineJoint::Bevel,
220 };
221 }
222
223 if keyboard.just_pressed(KeyCode::Space) {
224 virtual_time.toggle();
225 }
226}
227
228enum GizmosAnimationType {
229 Linear,
230 BackAndForth,
231 Stutter,
232}
233
234fn drive_gizmos_animation(
235 mut config_store: ResMut<GizmoConfigStore>,
236 virtual_time: ResMut<Time<Virtual>>,
237 keyboard: Res<ButtonInput<KeyCode>>,
238 mut animation_type: Local<Option<GizmosAnimationType>>,
239) {
240 if keyboard.just_pressed(KeyCode::KeyL) {
241 *animation_type = match *animation_type {
242 None => Some(GizmosAnimationType::Linear),
243 Some(GizmosAnimationType::Linear) => Some(GizmosAnimationType::BackAndForth),
244 Some(GizmosAnimationType::BackAndForth) => Some(GizmosAnimationType::Stutter),
245 Some(GizmosAnimationType::Stutter) => None,
246 };
247 }
248
249 if let Some(animation_type) = animation_type.as_ref() {
250 let (my_config, _) = config_store.config_mut::<MyRoundGizmos>();
251 match animation_type {
252 GizmosAnimationType::Linear => {
253 my_config.line.animation_offset = virtual_time.elapsed_secs() * 10.0;
254 }
255 GizmosAnimationType::BackAndForth => {
256 my_config.line.animation_offset = ops::sin(virtual_time.elapsed_secs()) * 10.0;
257 }
258 GizmosAnimationType::Stutter => {
259 my_config.line.animation_offset = (virtual_time.elapsed_secs() * 4.0).round();
260 }
261 }
262 }
263}