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_simple_scroll_view::*;
@ -39,7 +39,7 @@ fn add_content(mut commands: Commands, q: Query<Entity, Added<ScrollViewContent>
commands.entity(e).with_children(|parent| {
for _ in 0..10 {
parent.spawn(
(NodeBundle {
NodeBundle {
style: Style {
width: 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(),
background_color: BACKGROUND_COLOR.into(),
..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>()
.register_type::<ScrollViewport>()
.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()
},
ScrollViewport,
Interaction::None,
))
.with_children(|v| {
v.spawn((
@ -43,20 +44,21 @@ pub fn create_scroll_view(mut commands: Commands, q: Query<Entity, Added<ScrollV
},
..default()
},
ScrollViewContent(e.clone()),
ScrollViewContent(e),
));
});
});
}
}
fn input_mouse_scroll(
fn input_mouse_pressed_move(
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 {
info!("{:?}", evt);
for (_e, mut style, &interaction) in q.iter_mut() {
if interaction == Interaction::Pressed {
style.top = match style.top {
Val::Px(px) => Val::Px(px + evt.delta.y),
_ => Val::Px(0.0),