This commit is contained in:
Piotr Siuszko 2024-02-26 20:37:42 +01:00
parent 7daadae659
commit aacd25da3d
2 changed files with 10 additions and 24 deletions

View File

@ -1,4 +1,4 @@
use bevy::{input::mouse::MouseMotion, prelude::*, ui::Interaction}; use bevy::{prelude::*};
use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_simple_scroll_view::*; use bevy_simple_scroll_view::*;
@ -39,7 +39,7 @@ fn add_content(mut commands: Commands, q: Query<Entity, Added<ScrollViewContent>
commands.entity(e).with_children(|parent| { commands.entity(e).with_children(|parent| {
for _ in 0..10 { for _ in 0..10 {
parent.spawn( parent.spawn(
(NodeBundle { NodeBundle {
style: Style { style: Style {
width: Val::Px(200.0), width: Val::Px(200.0),
height: Val::Px(200.0), height: Val::Px(200.0),
@ -51,25 +51,9 @@ fn add_content(mut commands: Commands, q: Query<Entity, Added<ScrollViewContent>
border_color: BORDER_COLOR_ACTIVE.into(), border_color: BORDER_COLOR_ACTIVE.into(),
background_color: BACKGROUND_COLOR.into(), background_color: BACKGROUND_COLOR.into(),
..default() ..default()
}), },
); );
} }
}); });
} }
} }
fn input_mouse_scroll(
mut motion_evr: EventReader<MouseMotion>,
mut q: Query<(Entity, &mut Style, &Interaction), With<ScrollViewport>>,
) {
for evt in motion_evr.read() {
for (e, mut style, &interaction) in q.iter_mut() {
if interaction == Interaction::Hovered {
style.top = match style.top {
Val::Px(px) => Val::Px(px + evt.delta.y),
_ => Val::Px(0.0),
}
}
}
}
}

View File

@ -8,7 +8,7 @@ impl Plugin for ScrollViewPlugin {
app.register_type::<ScrollView>() app.register_type::<ScrollView>()
.register_type::<ScrollViewport>() .register_type::<ScrollViewport>()
.register_type::<ScrollViewContent>() .register_type::<ScrollViewContent>()
.add_systems(Update, (create_scroll_view, input_mouse_scroll)); .add_systems(Update, (create_scroll_view, input_mouse_pressed_move));
} }
} }
@ -33,6 +33,7 @@ pub fn create_scroll_view(mut commands: Commands, q: Query<Entity, Added<ScrollV
..Default::default() ..Default::default()
}, },
ScrollViewport, ScrollViewport,
Interaction::None,
)) ))
.with_children(|v| { .with_children(|v| {
v.spawn(( v.spawn((
@ -43,20 +44,21 @@ pub fn create_scroll_view(mut commands: Commands, q: Query<Entity, Added<ScrollV
}, },
..default() ..default()
}, },
ScrollViewContent(e.clone()), ScrollViewContent(e),
)); ));
}); });
}); });
} }
} }
fn input_mouse_scroll( fn input_mouse_pressed_move(
mut motion_evr: EventReader<MouseMotion>, mut motion_evr: EventReader<MouseMotion>,
mut q: Query<(Entity, &mut Style, &Interaction), With<ScrollViewport>>, mut q: Query<(Entity, &mut Style, &Interaction), With<ScrollViewport>>,
) { ) {
for evt in motion_evr.read() { for evt in motion_evr.read() {
for (e, mut style, &interaction) in q.iter_mut() { info!("{:?}", evt);
if interaction == Interaction::Hovered { for (_e, mut style, &interaction) in q.iter_mut() {
if interaction == Interaction::Pressed {
style.top = match style.top { style.top = match style.top {
Val::Px(px) => Val::Px(px + evt.delta.y), Val::Px(px) => Val::Px(px + evt.delta.y),
_ => Val::Px(0.0), _ => Val::Px(0.0),