From e548a5317f712fa95c7b15f4ed65f93a24787a46 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Tue, 27 Feb 2024 00:13:23 +0100 Subject: [PATCH] Mouse scroll --- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 76aa7a9..803c487 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,7 @@ -use bevy::{input::mouse::MouseMotion, prelude::*}; +use bevy::{ + input::mouse::{MouseMotion, MouseWheel}, + prelude::*, +}; /// A `Plugin` providing the systems and components required to make a ScrollView work. pub struct ScrollViewPlugin; @@ -8,7 +11,10 @@ impl Plugin for ScrollViewPlugin { app.register_type::() .register_type::() .register_type::() - .add_systems(Update, (create_scroll_view, input_mouse_pressed_move)); + .add_systems( + Update, + (create_scroll_view, input_mouse_pressed_move, scroll_events), + ); } } @@ -67,3 +73,36 @@ fn input_mouse_pressed_move( } } } + +fn scroll_events( + mut scroll_evr: EventReader, + mut q: Query<(Entity, &mut Style, &Interaction), With>, +) { + use bevy::input::mouse::MouseScrollUnit; + for ev in scroll_evr.read() { + let y = match ev.unit { + MouseScrollUnit::Line => { + println!( + "Scroll (line units): vertical: {}, horizontal: {}", + ev.y, ev.x + ); + ev.y + } + MouseScrollUnit::Pixel => { + println!( + "Scroll (pixel units): vertical: {}, horizontal: {}", + ev.y, ev.x + ); + ev.y + } + }; + for (_e, mut style, &interaction) in q.iter_mut() { + if interaction == Interaction::Hovered { + style.top = match style.top { + Val::Px(px) => Val::Px(px + y), + _ => Val::Px(0.0), + } + } + } + } +}