Repository navigation
Expand file tree
/
Copy pathinfinite_grid_2d.rs
More file actions
71 lines (64 loc) · 2.3 KB
/
Copy pathinfinite_grid_2d.rs
File metadata and controls
71 lines (64 loc) · 2.3 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Shows how to use an infinite grid in a 2D scene.
use bevy::{
dev_tools::infinite_grid::{InfiniteGrid, InfiniteGridPlugin, InfiniteGridSettings},
input::mouse::{AccumulatedMouseScroll, MouseScrollPixelsPerLine},
prelude::*,
};
fn main() {
App::new()
.add_plugins((DefaultPlugins, InfiniteGridPlugin))
.add_systems(Startup, setup)
.add_systems(Update, zoom)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn((
InfiniteGrid,
InfiniteGridSettings {
// The grid was originally made for the XZ plane so we need to rotate it and
// recolor the Z axis
// This color is the Y_AXIS in bevy_feathers
z_axis_color: Color::oklcha(0.5866, 0.1543, 129.84, 1.0),
// Distance between minor lines is 1/scale so this results in 1 minor line every 10
// world unit. In 2d a world unit is 1 pixel
scale: 0.1,
major_line_interval: 10,
// Disable fadeout because it doesn't make sense in 2d
dot_fadeout_strength: 0.0,
..default()
},
// Draw the grid behind the sprites
Transform::from_xyz(0.0, 0.0, -1.0)
// The grid defaults to be on the XZ plane which doesn't work in 2d so we need to rotate it
// so it's on the XY plane
.with_rotation(Quat::from_rotation_x(std::f32::consts::FRAC_PI_2)),
));
commands.spawn(Camera2d);
commands.spawn((
Text::new("Use scroll to zoom"),
Node {
position_type: PositionType::Absolute,
top: px(12),
left: px(12),
..default()
},
));
commands.spawn(Sprite::from_image(
asset_server.load("branding/bevy_bird_dark.png"),
));
}
fn zoom(
mut projection: Single<&mut Projection, With<Camera2d>>,
mouse_scroll: Res<AccumulatedMouseScroll>,
scroll_conversion: Res<MouseScrollPixelsPerLine>,
) {
let scroll = mouse_scroll.to_lines(&scroll_conversion).delta.y;
if scroll == 0.0 {
return;
}
let Projection::Orthographic(orthographic) = &mut **projection else {
return;
};
let scroll_speed = 0.1;
orthographic.scale = (orthographic.scale * (1.0 - scroll * scroll_speed)).clamp(0.1, 5.0);
}