Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/ui/drag_to_scroll.rs
6595 views
1
//! This example tests scale factor, dragging and scrolling
2
3
use bevy::color::palettes::css::RED;
4
use bevy::prelude::*;
5
6
#[derive(Component)]
7
struct ScrollableNode;
8
9
#[derive(Component)]
10
struct TileColor(Color);
11
12
fn main() {
13
App::new()
14
.add_plugins(DefaultPlugins)
15
.add_systems(Startup, setup)
16
.run();
17
}
18
19
#[derive(Component)]
20
struct ScrollStart(Vec2);
21
22
fn setup(mut commands: Commands) {
23
let w = 60;
24
let h = 40;
25
26
commands.spawn(Camera2d);
27
commands.insert_resource(UiScale(0.5));
28
29
commands
30
.spawn((
31
Node {
32
width: percent(100),
33
height: percent(100),
34
overflow: Overflow::scroll(),
35
..Default::default()
36
},
37
ScrollPosition(Vec2::ZERO),
38
ScrollableNode,
39
ScrollStart(Vec2::ZERO),
40
))
41
.observe(
42
|
43
drag: On<Pointer<Drag>>,
44
ui_scale: Res<UiScale>,
45
mut scroll_position_query: Query<(
46
&mut ScrollPosition,
47
&ScrollStart),
48
With<ScrollableNode>,
49
>| {
50
if let Ok((mut scroll_position, start)) = scroll_position_query.single_mut() {
51
scroll_position.0 = (start.0 - drag.distance / ui_scale.0).max(Vec2::ZERO);
52
}
53
},
54
)
55
.observe(
56
|
57
on: On<Pointer<DragStart>>,
58
mut scroll_position_query: Query<(
59
&ComputedNode,
60
&mut ScrollStart),
61
With<ScrollableNode>,
62
>| {
63
if on.entity() != on.original_entity() {
64
return;
65
}
66
if let Ok((computed_node, mut start)) = scroll_position_query.single_mut() {
67
start.0 = computed_node.scroll_position * computed_node.inverse_scale_factor;
68
}
69
},
70
)
71
72
.with_children(|commands| {
73
commands
74
.spawn(Node {
75
display: Display::Grid,
76
grid_template_rows: RepeatedGridTrack::px(w as i32, 100.),
77
grid_template_columns: RepeatedGridTrack::px(h as i32, 100.),
78
..Default::default()
79
})
80
.with_children(|commands| {
81
for y in 0..h {
82
for x in 0..w {
83
let tile_color = if (x + y) % 2 == 1 {
84
let hue = ((x as f32 / w as f32) * 270.0) + ((y as f32 / h as f32) * 90.0);
85
Color::hsl(hue, 1., 0.5)
86
} else {
87
Color::BLACK
88
};
89
commands
90
.spawn((
91
Node {
92
grid_row: GridPlacement::start(y + 1),
93
grid_column: GridPlacement::start(x + 1),
94
..Default::default()
95
},
96
Pickable {
97
should_block_lower: false,
98
is_hoverable: true,
99
},
100
TileColor(tile_color),
101
BackgroundColor(tile_color),
102
))
103
.observe(|on_enter: On<Pointer<Over>>, mut query: Query<&mut BackgroundColor>, | {
104
if let Ok(mut background_color) = query.get_mut(on_enter.entity()) {
105
background_color.0 = RED.into();
106
}
107
})
108
.observe(|on_enter: On<Pointer<Out>>, mut query: Query<(&mut BackgroundColor, &TileColor)>,| {
109
if let Ok((mut background_color, tile_color)) = query.get_mut(on_enter.entity()) {
110
background_color.0 = tile_color.0;
111
}
112
});
113
}
114
}
115
});
116
});
117
}
118
119