-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6b0bc6
commit 9af23d4
Showing
4 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use bevy::input::mouse::MouseScrollUnit; | ||
use bevy::input::mouse::MouseWheel; | ||
use bevy::prelude::*; | ||
|
||
use crate::AppSet; | ||
|
||
pub struct ScrollPlugin; | ||
|
||
impl Plugin for ScrollPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.register_type::<ScrollContent>() | ||
.add_systems(Update, mouse_scroll.in_set(AppSet::Input)); | ||
} | ||
} | ||
|
||
#[derive(Component, Reflect, Default)] | ||
pub struct ScrollContent { | ||
position: f32, | ||
sensitivity: f32, | ||
} | ||
|
||
impl ScrollContent { | ||
pub fn with_sensitivity(sensitivity: f32) -> Self { | ||
Self { | ||
sensitivity, | ||
..default() | ||
} | ||
} | ||
} | ||
|
||
fn mouse_scroll( | ||
mut events: EventReader<MouseWheel>, | ||
mut scroll_query: Query<(&mut ScrollContent, &mut Style, &Parent, &Node)>, | ||
node_query: Query<&Node>, | ||
) { | ||
let pixels = events | ||
.read() | ||
.map(|event| { | ||
event.y | ||
* match event.unit { | ||
MouseScrollUnit::Line => 20.0, | ||
MouseScrollUnit::Pixel => 1.0, | ||
} | ||
}) | ||
.sum::<f32>(); | ||
|
||
for (mut scroll, mut style, parent, node) in &mut scroll_query { | ||
scroll.position += pixels * scroll.sensitivity; | ||
|
||
let height = node.size().y; | ||
let parent_height = node_query.get(parent.get()).unwrap().size().y; | ||
let max_scroll = (height - parent_height).max(0.0); | ||
scroll.position = scroll.position.clamp(-max_scroll, 0.0); | ||
|
||
style.top = Val::Px(scroll.position); | ||
} | ||
} |