forked from johanhelsing/bevy_pancam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toggle.rs
51 lines (47 loc) · 1.47 KB
/
toggle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use bevy::prelude::*;
use bevy_pancam::{PanCam, PanCamPlugin};
use rand::prelude::random;
fn main() {
App::new()
.add_plugins((DefaultPlugins, PanCamPlugin::default()))
.add_systems(Startup, setup)
.add_systems(Update, toggle_key)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn((Camera2dBundle::default(), PanCam::default()));
let n = 20;
let spacing = 50.;
let offset = spacing * n as f32 / 2.;
let custom_size = Some(Vec2::new(spacing, spacing));
for x in 0..n {
for y in 0..n {
let x = x as f32 * spacing - offset;
let y = y as f32 * spacing - offset;
let color = Color::hsl(240., random::<f32>() * 0.3, random::<f32>() * 0.3);
commands.spawn(SpriteBundle {
sprite: Sprite {
color,
custom_size,
..default()
},
transform: Transform::from_xyz(x, y, 0.),
..default()
});
}
}
}
fn toggle_key(mut query: Query<&mut PanCam>, keys: Res<ButtonInput<KeyCode>>) {
// Space = Toggle Panning
if keys.just_pressed(KeyCode::Space) {
for mut pancam in &mut query {
pancam.enabled = !pancam.enabled;
}
}
// T = Toggle Zoom to Cursor
if keys.just_pressed(KeyCode::KeyT) {
for mut pancam in &mut query {
pancam.zoom_to_cursor = !pancam.zoom_to_cursor;
}
}
}