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(Update, (draw_example_collection, update_config))
13 .run();
14}
15
16#[derive(Default, Reflect, GizmoConfigGroup)]
18struct MyRoundGizmos {}
19
20fn setup(mut commands: Commands) {
21 commands.spawn(Camera2d);
22 commands.spawn((
24 Text::new(
25 "Hold 'Left' or 'Right' to change the line width of straight gizmos\n\
26 Hold 'Up' or 'Down' to change the line width of round gizmos\n\
27 Press '1' / '2' to toggle the visibility of straight / round gizmos\n\
28 Press 'U' / 'I' to cycle through line styles\n\
29 Press 'J' / 'K' to cycle through line joins\n\
30 Press 'Spacebar' to toggle pause",
31 ),
32 Node {
33 position_type: PositionType::Absolute,
34 top: px(12),
35 left: px(12),
36 ..default()
37 },
38 ));
39}
40
41fn draw_example_collection(
42 mut gizmos: Gizmos,
43 mut my_gizmos: Gizmos<MyRoundGizmos>,
44 time: Res<Time>,
45) {
46 let sin_t_scaled = ops::sin(time.elapsed_secs()) * 50.;
47 gizmos.line_2d(Vec2::Y * -sin_t_scaled, Vec2::splat(-80.), RED);
48 gizmos.ray_2d(Vec2::Y * sin_t_scaled, Vec2::splat(80.), LIME);
49
50 gizmos
51 .grid_2d(
52 Isometry2d::IDENTITY,
53 UVec2::new(16, 9),
54 Vec2::new(80., 80.),
55 LinearRgba::gray(0.05),
57 )
58 .outer_edges();
59
60 gizmos.linestrip_gradient_2d([
62 (Vec2::Y * 300., BLUE),
63 (Vec2::new(-255., -155.), RED),
64 (Vec2::new(255., -155.), LIME),
65 (Vec2::Y * 300., BLUE),
66 ]);
67
68 gizmos.rect_2d(Isometry2d::IDENTITY, Vec2::splat(650.), BLACK);
69
70 gizmos.cross_2d(Vec2::new(-160., 120.), 12., FUCHSIA);
71
72 let domain = Interval::EVERYWHERE;
73 let curve = FunctionCurve::new(domain, |t| Vec2::new(t, ops::sin(t / 25.0) * 100.0));
74 let resolution = ((ops::sin(time.elapsed_secs()) + 1.0) * 50.0) as usize;
75 let times_and_colors = (0..=resolution)
76 .map(|n| n as f32 / resolution as f32)
77 .map(|t| (t - 0.5) * 600.0)
78 .map(|t| (t, TEAL.mix(&HOT_PINK, (t + 300.0) / 600.0)));
79 gizmos.curve_gradient_2d(curve, times_and_colors);
80
81 my_gizmos
82 .rounded_rect_2d(Isometry2d::IDENTITY, Vec2::splat(630.), BLACK)
83 .corner_radius(ops::cos(time.elapsed_secs() / 3.) * 100.);
84
85 my_gizmos
88 .circle_2d(Isometry2d::IDENTITY, 300., NAVY)
89 .resolution(64);
90
91 my_gizmos.ellipse_2d(
92 Rot2::radians(time.elapsed_secs() % TAU),
93 Vec2::new(100., 200.),
94 YELLOW_GREEN,
95 );
96
97 my_gizmos.arc_2d(
100 Rot2::radians(sin_t_scaled / 10.),
101 FRAC_PI_2,
102 310.,
103 ORANGE_RED,
104 );
105 my_gizmos.arc_2d(Isometry2d::IDENTITY, FRAC_PI_2, 80.0, ORANGE_RED);
106 my_gizmos.long_arc_2d_between(Vec2::ZERO, Vec2::X * 20.0, Vec2::Y * 20.0, ORANGE_RED);
107 my_gizmos.short_arc_2d_between(Vec2::ZERO, Vec2::X * 40.0, Vec2::Y * 40.0, ORANGE_RED);
108
109 gizmos.arrow_2d(
110 Vec2::ZERO,
111 Vec2::from_angle(sin_t_scaled / -10. + PI / 2.) * 50.,
112 YELLOW,
113 );
114
115 gizmos
117 .arrow_2d(
118 Vec2::ZERO,
119 Vec2::from_angle(sin_t_scaled / -10.) * 50.,
120 GREEN,
121 )
122 .with_double_end()
123 .with_tip_length(10.);
124}
125
126fn update_config(
127 mut config_store: ResMut<GizmoConfigStore>,
128 keyboard: Res<ButtonInput<KeyCode>>,
129 real_time: Res<Time<Real>>,
130 mut virtual_time: ResMut<Time<Virtual>>,
131) {
132 let (config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
133 if keyboard.pressed(KeyCode::ArrowRight) {
134 config.line.width += 5. * real_time.delta_secs();
135 config.line.width = config.line.width.clamp(0., 50.);
136 }
137 if keyboard.pressed(KeyCode::ArrowLeft) {
138 config.line.width -= 5. * real_time.delta_secs();
139 config.line.width = config.line.width.clamp(0., 50.);
140 }
141 if keyboard.just_pressed(KeyCode::Digit1) {
142 config.enabled ^= true;
143 }
144 if keyboard.just_pressed(KeyCode::KeyU) {
145 config.line.style = match config.line.style {
146 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
147 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
148 gap_scale: 3.0,
149 line_scale: 5.0,
150 },
151 _ => GizmoLineStyle::Solid,
152 };
153 }
154 if keyboard.just_pressed(KeyCode::KeyI) {
155 config.line.style = match config.line.style {
156 GizmoLineStyle::Solid => GizmoLineStyle::Dashed {
157 gap_scale: 3.0,
158 line_scale: 5.0,
159 },
160 GizmoLineStyle::Dotted => GizmoLineStyle::Solid,
161 _ => GizmoLineStyle::Dotted,
162 };
163 }
164 if keyboard.just_pressed(KeyCode::KeyJ) {
165 config.line.joints = match config.line.joints {
166 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
167 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
168 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
169 GizmoLineJoint::None => GizmoLineJoint::Bevel,
170 };
171 }
172
173 if keyboard.just_pressed(KeyCode::KeyK) {
174 config.line.joints = match config.line.joints {
175 GizmoLineJoint::Bevel => GizmoLineJoint::None,
176 GizmoLineJoint::Miter => GizmoLineJoint::Bevel,
177 GizmoLineJoint::Round(_) => GizmoLineJoint::Miter,
178 GizmoLineJoint::None => GizmoLineJoint::Round(4),
179 };
180 }
181
182 let (my_config, _) = config_store.config_mut::<MyRoundGizmos>();
183 if keyboard.pressed(KeyCode::ArrowUp) {
184 my_config.line.width += 5. * real_time.delta_secs();
185 my_config.line.width = my_config.line.width.clamp(0., 50.);
186 }
187 if keyboard.pressed(KeyCode::ArrowDown) {
188 my_config.line.width -= 5. * real_time.delta_secs();
189 my_config.line.width = my_config.line.width.clamp(0., 50.);
190 }
191 if keyboard.just_pressed(KeyCode::Digit2) {
192 my_config.enabled ^= true;
193 }
194 if keyboard.just_pressed(KeyCode::KeyI) {
195 my_config.line.style = match my_config.line.style {
196 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
197 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
198 gap_scale: 3.0,
199 line_scale: 5.0,
200 },
201 _ => GizmoLineStyle::Solid,
202 };
203 }
204 if keyboard.just_pressed(KeyCode::KeyK) {
205 my_config.line.joints = match my_config.line.joints {
206 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
207 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
208 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
209 GizmoLineJoint::None => GizmoLineJoint::Bevel,
210 };
211 }
212 if keyboard.just_pressed(KeyCode::Space) {
213 virtual_time.toggle();
214 }
215}