From 743015639425f22538dc071dfd6ae7afe2584238 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Tue, 27 Feb 2024 00:38:05 +0100 Subject: [PATCH] Update scrollViewContent margin, not ScrollView --- examples/simple.rs | 26 ++++++++++++-------------- src/lib.rs | 18 ++++++++++++------ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index 49d3454..dc840ce 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -1,4 +1,4 @@ -use bevy::{prelude::*}; +use bevy::prelude::*; use bevy_inspector_egui::quick::WorldInspectorPlugin; use bevy_simple_scroll_view::*; @@ -38,21 +38,19 @@ fn add_content(mut commands: Commands, q: Query for e in q.iter() { commands.entity(e).with_children(|parent| { for _ in 0..10 { - parent.spawn( - NodeBundle { - style: Style { - width: Val::Px(200.0), - height: Val::Px(200.0), - margin: UiRect::all(Val::Px(5.0)), - border: UiRect::all(Val::Px(5.0)), - padding: UiRect::all(Val::Px(5.0)), - ..default() - }, - border_color: BORDER_COLOR_ACTIVE.into(), - background_color: BACKGROUND_COLOR.into(), + parent.spawn(NodeBundle { + style: Style { + width: Val::Px(200.0), + height: Val::Px(200.0), + margin: UiRect::all(Val::Px(5.0)), + border: UiRect::all(Val::Px(5.0)), + padding: UiRect::all(Val::Px(5.0)), ..default() }, - ); + border_color: BORDER_COLOR_ACTIVE.into(), + background_color: BACKGROUND_COLOR.into(), + ..default() + }); } }); } diff --git a/src/lib.rs b/src/lib.rs index 803c487..edb42ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,15 +59,21 @@ pub fn create_scroll_view(mut commands: Commands, q: Query, - mut q: Query<(Entity, &mut Style, &Interaction), With>, + mut q: Query<(&Children, &Interaction), With>, + mut content_q: Query<&mut Style, With>, ) { for evt in motion_evr.read() { 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), + for (children, &interaction) in q.iter_mut() { + if interaction != Interaction::Pressed { + continue; + } + for &child in children.iter() { + if let Ok(mut style) = content_q.get_mut(child) { + style.top = match style.top { + Val::Px(px) => Val::Px(px + evt.delta.y), + _ => Val::Px(0.0), + } } } }