From 50823c3c397d4d6543e0b8bfa4aeb79bf301f615 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Wed, 28 Feb 2024 21:47:13 +0100 Subject: [PATCH] Rename components, move scroll offset update to separate method --- examples/simple.rs | 4 ++-- src/lib.rs | 48 +++++++++++++++++++++++++++------------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index 232c290..efa8128 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -42,12 +42,12 @@ fn prepare(mut commands: Commands) { background_color: BackgroundColor(Color::YELLOW), ..default() }, - ScrollViewport::default(), + ScrollView::default(), )); }); } -fn add_content(mut commands: Commands, q: Query>) { +fn add_content(mut commands: Commands, q: Query>) { for e in q.iter() { commands.entity(e).with_children(|parent| { for _ in 0..10 { diff --git a/src/lib.rs b/src/lib.rs index 59791e9..10a7e15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,21 +8,27 @@ pub struct ScrollViewPlugin; impl Plugin for ScrollViewPlugin { fn build(&self, app: &mut App) { - app.register_type::() - .register_type::() + app.register_type::() + .register_type::() .add_systems( Update, - (create_scroll_view, input_mouse_pressed_move, scroll_events), + ( + create_scroll_view, + input_mouse_pressed_move, + scroll_events, + scroll_update, + ) + .chain(), ); } } #[derive(Component, Debug, Reflect)] -pub struct ScrollViewport { +pub struct ScrollView { pub scroll_speed: f32, } -impl Default for ScrollViewport { +impl Default for ScrollView { fn default() -> Self { Self { scroll_speed: 500.0, @@ -31,13 +37,13 @@ impl Default for ScrollViewport { } #[derive(Component, Debug, Reflect)] -pub struct ScrollViewContent { +pub struct ScrollableContent { pub pos_y: f32, } pub fn create_scroll_view( mut commands: Commands, - mut q: Query<(Entity, &mut Style), Added>, + mut q: Query<(Entity, &mut Style), Added>, ) { for (e, mut style) in q.iter_mut() { style.overflow = Overflow::clip(); @@ -55,7 +61,7 @@ pub fn create_scroll_view( }, ..default() }, - ScrollViewContent { pos_y: 0.0 }, + ScrollableContent { pos_y: 0.0 }, )); }); } @@ -63,8 +69,8 @@ pub fn create_scroll_view( fn input_mouse_pressed_move( mut motion_evr: EventReader, - mut q: Query<(&Children, &Interaction, &Node), With>, - mut content_q: Query<(&mut Style, &mut ScrollViewContent, &Node)>, + mut q: Query<(&Children, &Interaction, &Node), With>, + mut content_q: Query<(&mut ScrollableContent, &Node)>, ) { for evt in motion_evr.read() { for (children, &interaction, node) in q.iter_mut() { @@ -74,23 +80,27 @@ fn input_mouse_pressed_move( let container_height = node.size().y; for &child in children.iter() { if let Ok(item) = content_q.get_mut(child) { - let mut style = item.0; - let mut scroll = item.1; - let max_scroll = (item.2.size().y - container_height).max(0.0); + let mut scroll = item.0; + let max_scroll = (item.1.size().y - container_height).max(0.0); scroll.pos_y += evt.delta.y; scroll.pos_y = scroll.pos_y.clamp(-max_scroll, 0.); - style.top = Val::Px(scroll.pos_y); } } } } } +fn scroll_update(mut q: Query<(&ScrollableContent, &mut Style), Changed>) { + for (scroll, mut style) in q.iter_mut() { + style.top = Val::Px(scroll.pos_y); + } +} + fn scroll_events( mut scroll_evr: EventReader, - mut q: Query<(&Children, &Interaction, &ScrollViewport, &Node), With>, + mut q: Query<(&Children, &Interaction, &ScrollView, &Node), With>, time: Res