Skip to content

Commit

Permalink
Last fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Leinnan committed Dec 17, 2024
1 parent 2f23d23 commit 6d1fb83
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ license = "MIT OR Apache-2.0"
description = "Simple to use plugin implementing ScrollView into Bevy engine."

[dependencies.bevy]
version = "0.15.0-rc.3"
version = "0.15"
default-features = false
features = ["bevy_ui", "bevy_asset", "bevy_text"]

[dev-dependencies.bevy]
version = "0.15.0-rc.3"
version = "0.15"
default-features = true

[features]
Expand Down
7 changes: 4 additions & 3 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
}

fn prepare(mut commands: Commands) {
commands.spawn(Camera2d::default());
commands.spawn(Camera2d);
commands
.spawn((
BackgroundColor(CLR_1),
Expand Down Expand Up @@ -47,7 +47,7 @@ fn prepare(mut commands: Commands) {
BackgroundColor(CLR_2),
BorderColor(CLR_4),
Button,
btn_action.clone(),
btn_action,
))
.with_children(|p| {
p.spawn((
Expand Down Expand Up @@ -110,13 +110,14 @@ fn prepare(mut commands: Commands) {
}

#[derive(Component, PartialEq, Debug, Clone, Copy)]
#[require(Button)]
enum ScrollButton {
MoveToTop,
MoveToBottom,
}

fn reset_scroll(
q: Query<(&Interaction, &ScrollButton), (Changed<Interaction>, With<Button>)>,
q: Query<(&Interaction, &ScrollButton), Changed<Interaction>>,
mut scrolls_q: Query<&mut ScrollableContent>,
) {
let Ok(mut scroll) = scrolls_q.get_single_mut() else {
Expand Down
30 changes: 24 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
/// A `Plugin` providing the systems and components required to make a ScrollView work.
///
/// # Example
/// ```
/// ```no_run
/// use bevy::prelude::*;
/// use bevy_simple_scroll_view::*;
///
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Plugin for ScrollViewPlugin {

/// Root component of scroll, it should have clipped style.
#[derive(Component, Debug, Reflect)]
#[require(Interaction)]
#[require(Interaction, Node)]
pub struct ScrollView {
/// Field which control speed of the scrolling.
/// Could be negative number to implement invert scroll
Expand All @@ -57,12 +57,23 @@ impl Default for ScrollView {
/// Component containing offset value of the scroll container to the parent.
/// It is possible to update the field `pos_y` manually to move scrollview to desired location.
#[derive(Component, Debug, Reflect, Default)]
#[require(Node(scroll_view_node))]
pub struct ScrollableContent {
/// Scroll container offset to the `ScrollView`.
pub pos_y: f32,
pub max_scroll: f32,
}

pub fn scroll_view_node() -> Node {
Node {
overflow: Overflow::clip(),
align_items: AlignItems::Start,
align_self: AlignSelf::Stretch,
flex_direction: FlexDirection::Row,
..default()
}
}

impl ScrollableContent {
pub fn scroll_to_top(&mut self) {
self.pos_y = 0.0;
Expand All @@ -77,11 +88,18 @@ impl ScrollableContent {
}

pub fn create_scroll_view(mut q: Query<&mut Node, Added<ScrollView>>) {
let Node {
overflow,
align_items,
align_self,
flex_direction,
..
} = scroll_view_node();
for mut style in q.iter_mut() {
style.overflow = Overflow::clip();
style.align_items = AlignItems::Start;
style.align_self = AlignSelf::Stretch;
style.flex_direction = FlexDirection::Row;
style.overflow = overflow;
style.align_items = align_items;
style.align_self = align_self;
style.flex_direction = flex_direction;
}
}

Expand Down

0 comments on commit 6d1fb83

Please sign in to comment.