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",
30 ),
31 Node {
32 position_type: PositionType::Absolute,
33 top: px(12),
34 left: px(12),
35 ..default()
36 },
37 ));
38}
39
40fn draw_example_collection(
41 mut gizmos: Gizmos,
42 mut my_gizmos: Gizmos<MyRoundGizmos>,
43 time: Res<Time>,
44) {
45 let sin_t_scaled = ops::sin(time.elapsed_secs()) * 50.;
46 gizmos.line_2d(Vec2::Y * -sin_t_scaled, Vec2::splat(-80.), RED);
47 gizmos.ray_2d(Vec2::Y * sin_t_scaled, Vec2::splat(80.), LIME);
48
49 gizmos
50 .grid_2d(
51 Isometry2d::IDENTITY,
52 UVec2::new(16, 9),
53 Vec2::new(80., 80.),
54 LinearRgba::gray(0.05),
56 )
57 .outer_edges();
58
59 gizmos.linestrip_gradient_2d([
61 (Vec2::Y * 300., BLUE),
62 (Vec2::new(-255., -155.), RED),
63 (Vec2::new(255., -155.), LIME),
64 (Vec2::Y * 300., BLUE),
65 ]);
66
67 gizmos.rect_2d(Isometry2d::IDENTITY, Vec2::splat(650.), BLACK);
68
69 gizmos.cross_2d(Vec2::new(-160., 120.), 12., FUCHSIA);
70
71 let domain = Interval::EVERYWHERE;
72 let curve = FunctionCurve::new(domain, |t| Vec2::new(t, ops::sin(t / 25.0) * 100.0));
73 let resolution = ((ops::sin(time.elapsed_secs()) + 1.0) * 50.0) as usize;
74 let times_and_colors = (0..=resolution)
75 .map(|n| n as f32 / resolution as f32)
76 .map(|t| (t - 0.5) * 600.0)
77 .map(|t| (t, TEAL.mix(&HOT_PINK, (t + 300.0) / 600.0)));
78 gizmos.curve_gradient_2d(curve, times_and_colors);
79
80 my_gizmos
81 .rounded_rect_2d(Isometry2d::IDENTITY, Vec2::splat(630.), BLACK)
82 .corner_radius(ops::cos(time.elapsed_secs() / 3.) * 100.);
83
84 my_gizmos
87 .circle_2d(Isometry2d::IDENTITY, 300., NAVY)
88 .resolution(64);
89
90 my_gizmos.ellipse_2d(
91 Rot2::radians(time.elapsed_secs() % TAU),
92 Vec2::new(100., 200.),
93 YELLOW_GREEN,
94 );
95
96 my_gizmos.arc_2d(
99 Rot2::radians(sin_t_scaled / 10.),
100 FRAC_PI_2,
101 310.,
102 ORANGE_RED,
103 );
104 my_gizmos.arc_2d(Isometry2d::IDENTITY, FRAC_PI_2, 80.0, ORANGE_RED);
105 my_gizmos.long_arc_2d_between(Vec2::ZERO, Vec2::X * 20.0, Vec2::Y * 20.0, ORANGE_RED);
106 my_gizmos.short_arc_2d_between(Vec2::ZERO, Vec2::X * 40.0, Vec2::Y * 40.0, ORANGE_RED);
107
108 gizmos.arrow_2d(
109 Vec2::ZERO,
110 Vec2::from_angle(sin_t_scaled / -10. + PI / 2.) * 50.,
111 YELLOW,
112 );
113
114 gizmos
116 .arrow_2d(
117 Vec2::ZERO,
118 Vec2::from_angle(sin_t_scaled / -10.) * 50.,
119 GREEN,
120 )
121 .with_double_end()
122 .with_tip_length(10.);
123}
124
125fn update_config(
126 mut config_store: ResMut<GizmoConfigStore>,
127 keyboard: Res<ButtonInput<KeyCode>>,
128 time: Res<Time>,
129) {
130 let (config, _) = config_store.config_mut::<DefaultGizmoConfigGroup>();
131 if keyboard.pressed(KeyCode::ArrowRight) {
132 config.line.width += 5. * time.delta_secs();
133 config.line.width = config.line.width.clamp(0., 50.);
134 }
135 if keyboard.pressed(KeyCode::ArrowLeft) {
136 config.line.width -= 5. * time.delta_secs();
137 config.line.width = config.line.width.clamp(0., 50.);
138 }
139 if keyboard.just_pressed(KeyCode::Digit1) {
140 config.enabled ^= true;
141 }
142 if keyboard.just_pressed(KeyCode::KeyU) {
143 config.line.style = match config.line.style {
144 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
145 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
146 gap_scale: 3.0,
147 line_scale: 5.0,
148 },
149 _ => GizmoLineStyle::Solid,
150 };
151 }
152 if keyboard.just_pressed(KeyCode::KeyJ) {
153 config.line.joints = match config.line.joints {
154 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
155 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
156 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
157 GizmoLineJoint::None => GizmoLineJoint::Bevel,
158 };
159 }
160
161 let (my_config, _) = config_store.config_mut::<MyRoundGizmos>();
162 if keyboard.pressed(KeyCode::ArrowUp) {
163 my_config.line.width += 5. * time.delta_secs();
164 my_config.line.width = my_config.line.width.clamp(0., 50.);
165 }
166 if keyboard.pressed(KeyCode::ArrowDown) {
167 my_config.line.width -= 5. * time.delta_secs();
168 my_config.line.width = my_config.line.width.clamp(0., 50.);
169 }
170 if keyboard.just_pressed(KeyCode::Digit2) {
171 my_config.enabled ^= true;
172 }
173 if keyboard.just_pressed(KeyCode::KeyI) {
174 my_config.line.style = match my_config.line.style {
175 GizmoLineStyle::Solid => GizmoLineStyle::Dotted,
176 GizmoLineStyle::Dotted => GizmoLineStyle::Dashed {
177 gap_scale: 3.0,
178 line_scale: 5.0,
179 },
180 _ => GizmoLineStyle::Solid,
181 };
182 }
183 if keyboard.just_pressed(KeyCode::KeyK) {
184 my_config.line.joints = match my_config.line.joints {
185 GizmoLineJoint::Bevel => GizmoLineJoint::Miter,
186 GizmoLineJoint::Miter => GizmoLineJoint::Round(4),
187 GizmoLineJoint::Round(_) => GizmoLineJoint::None,
188 GizmoLineJoint::None => GizmoLineJoint::Bevel,
189 };
190 }
191}