-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.rs
122 lines (118 loc) · 4.31 KB
/
simple.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use bevy::prelude::*;
use bevy_simple_scroll_view::*;
const CLR_1: Color = Color::rgb(0.168, 0.168, 0.168);
const CLR_2: Color = Color::rgb(0.109, 0.109, 0.109);
const CLR_3: Color = Color::rgb(0.569, 0.592, 0.647);
const CLR_4: Color = Color::rgb(0.902, 0.4, 0.004);
fn main() {
App::new()
.add_plugins((DefaultPlugins, ScrollViewPlugin))
.add_systems(Startup, prepare)
.add_systems(Update, reset_scroll)
.run();
}
fn prepare(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
padding: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: CLR_1.into(),
..default()
})
.with_children(|p| {
p.spawn(ButtonBundle {
style: Style {
margin: UiRect::all(Val::Px(15.0)),
padding: UiRect::all(Val::Px(15.0)),
max_height: Val::Px(100.0),
border: UiRect::all(Val::Px(3.0)),
align_items: AlignItems::Center,
..default()
},
background_color: CLR_2.into(),
border_color: CLR_4.into(),
..default()
})
.with_children(|p| {
p.spawn(TextBundle::from_section(
"Reset scroll",
TextStyle {
font_size: 25.0,
color: CLR_4,
..default()
},
));
});
p.spawn((
NodeBundle {
style: Style {
width: Val::Percent(80.0),
margin: UiRect::all(Val::Px(15.0)),
..default()
},
background_color: CLR_2.into(),
..default()
},
ScrollView::default(),
))
.with_children(|p| {
p.spawn((
NodeBundle {
style: Style {
flex_direction: bevy::ui::FlexDirection::Column,
width: Val::Percent(100.0),
..default()
},
..default()
},
ScrollableContent::default(),
))
.with_children(|scroll_area| {
for i in 0..21 {
scroll_area
.spawn(NodeBundle {
style: Style {
min_width: Val::Px(200.0),
margin: UiRect::all(Val::Px(15.0)),
border: UiRect::all(Val::Px(5.0)),
padding: UiRect::all(Val::Px(30.0)),
..default()
},
border_color: CLR_3.into(),
..default()
})
.with_children(|p| {
p.spawn(
TextBundle::from_section(
format!("Nr {}", i),
TextStyle {
font_size: 25.0,
color: CLR_3,
..default()
},
)
.with_text_justify(JustifyText::Center),
);
});
}
});
});
});
}
fn reset_scroll(
q: Query<(&Button, &Interaction), Changed<Interaction>>,
mut scrolls_q: Query<&mut ScrollableContent>,
) {
for (_, interaction) in q.iter() {
if interaction == &Interaction::Pressed {
for mut scroll in scrolls_q.iter_mut() {
scroll.pos_y = 0.0;
}
}
}
}