Scroll speed

This commit is contained in:
Piotr Siuszko 2024-02-27 10:02:49 +01:00
parent b83a274cdd
commit 9395104dfb
2 changed files with 18 additions and 5 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
/Cargo.lock

View File

@ -21,8 +21,18 @@ impl Plugin for ScrollViewPlugin {
#[derive(Component, Default, Debug, Reflect)] #[derive(Component, Default, Debug, Reflect)]
pub struct ScrollView; pub struct ScrollView;
#[derive(Component, Default, Debug, Reflect)] #[derive(Component, Debug, Reflect)]
pub struct ScrollViewport; pub struct ScrollViewport {
pub scroll_speed: f32,
}
impl Default for ScrollViewport {
fn default() -> Self {
Self {
scroll_speed: 500.0,
}
}
}
#[derive(Component, Debug, Reflect)] #[derive(Component, Debug, Reflect)]
pub struct ScrollViewContent(pub Entity); pub struct ScrollViewContent(pub Entity);
@ -38,7 +48,7 @@ pub fn create_scroll_view(mut commands: Commands, q: Query<Entity, Added<ScrollV
}, },
..Default::default() ..Default::default()
}, },
ScrollViewport, ScrollViewport::default(),
Interaction::None, Interaction::None,
)) ))
.with_children(|v| { .with_children(|v| {
@ -82,7 +92,8 @@ fn input_mouse_pressed_move(
fn scroll_events( fn scroll_events(
mut scroll_evr: EventReader<MouseWheel>, mut scroll_evr: EventReader<MouseWheel>,
mut q: Query<(Entity, &mut Style, &Interaction), With<ScrollViewport>>, mut q: Query<(&mut Style, &Interaction, &ScrollViewport), With<ScrollViewport>>,
time: Res<Time>,
) { ) {
use bevy::input::mouse::MouseScrollUnit; use bevy::input::mouse::MouseScrollUnit;
for ev in scroll_evr.read() { for ev in scroll_evr.read() {
@ -102,8 +113,9 @@ fn scroll_events(
ev.y ev.y
} }
}; };
for (_e, mut style, &interaction) in q.iter_mut() { for (mut style, &interaction, scroll_view) in q.iter_mut() {
if interaction == Interaction::Hovered { if interaction == Interaction::Hovered {
let y = y * time.delta().as_secs_f32() * scroll_view.scroll_speed;
style.top = match style.top { style.top = match style.top {
Val::Px(px) => Val::Px(px + y), Val::Px(px) => Val::Px(px + y),
_ => Val::Px(0.0), _ => Val::Px(0.0),