Last fixes

This commit is contained in:
Piotr Siuszko 2024-12-17 13:31:02 +01:00
parent 2f23d23b02
commit 6d1fb83636
3 changed files with 30 additions and 11 deletions

View File

@ -11,12 +11,12 @@ license = "MIT OR Apache-2.0"
description = "Simple to use plugin implementing ScrollView into Bevy engine." description = "Simple to use plugin implementing ScrollView into Bevy engine."
[dependencies.bevy] [dependencies.bevy]
version = "0.15.0-rc.3" version = "0.15"
default-features = false default-features = false
features = ["bevy_ui", "bevy_asset", "bevy_text"] features = ["bevy_ui", "bevy_asset", "bevy_text"]
[dev-dependencies.bevy] [dev-dependencies.bevy]
version = "0.15.0-rc.3" version = "0.15"
default-features = true default-features = true
[features] [features]

View File

@ -15,7 +15,7 @@ fn main() {
} }
fn prepare(mut commands: Commands) { fn prepare(mut commands: Commands) {
commands.spawn(Camera2d::default()); commands.spawn(Camera2d);
commands commands
.spawn(( .spawn((
BackgroundColor(CLR_1), BackgroundColor(CLR_1),
@ -47,7 +47,7 @@ fn prepare(mut commands: Commands) {
BackgroundColor(CLR_2), BackgroundColor(CLR_2),
BorderColor(CLR_4), BorderColor(CLR_4),
Button, Button,
btn_action.clone(), btn_action,
)) ))
.with_children(|p| { .with_children(|p| {
p.spawn(( p.spawn((
@ -110,13 +110,14 @@ fn prepare(mut commands: Commands) {
} }
#[derive(Component, PartialEq, Debug, Clone, Copy)] #[derive(Component, PartialEq, Debug, Clone, Copy)]
#[require(Button)]
enum ScrollButton { enum ScrollButton {
MoveToTop, MoveToTop,
MoveToBottom, MoveToBottom,
} }
fn reset_scroll( fn reset_scroll(
q: Query<(&Interaction, &ScrollButton), (Changed<Interaction>, With<Button>)>, q: Query<(&Interaction, &ScrollButton), Changed<Interaction>>,
mut scrolls_q: Query<&mut ScrollableContent>, mut scrolls_q: Query<&mut ScrollableContent>,
) { ) {
let Ok(mut scroll) = scrolls_q.get_single_mut() else { let Ok(mut scroll) = scrolls_q.get_single_mut() else {

View File

@ -8,7 +8,7 @@ use bevy::{
/// A `Plugin` providing the systems and components required to make a ScrollView work. /// A `Plugin` providing the systems and components required to make a ScrollView work.
/// ///
/// # Example /// # Example
/// ``` /// ```no_run
/// use bevy::prelude::*; /// use bevy::prelude::*;
/// use bevy_simple_scroll_view::*; /// use bevy_simple_scroll_view::*;
/// ///
@ -39,7 +39,7 @@ impl Plugin for ScrollViewPlugin {
/// Root component of scroll, it should have clipped style. /// Root component of scroll, it should have clipped style.
#[derive(Component, Debug, Reflect)] #[derive(Component, Debug, Reflect)]
#[require(Interaction)] #[require(Interaction, Node)]
pub struct ScrollView { pub struct ScrollView {
/// Field which control speed of the scrolling. /// Field which control speed of the scrolling.
/// Could be negative number to implement invert scroll /// Could be negative number to implement invert scroll
@ -57,12 +57,23 @@ impl Default for ScrollView {
/// Component containing offset value of the scroll container to the parent. /// 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. /// It is possible to update the field `pos_y` manually to move scrollview to desired location.
#[derive(Component, Debug, Reflect, Default)] #[derive(Component, Debug, Reflect, Default)]
#[require(Node(scroll_view_node))]
pub struct ScrollableContent { pub struct ScrollableContent {
/// Scroll container offset to the `ScrollView`. /// Scroll container offset to the `ScrollView`.
pub pos_y: f32, pub pos_y: f32,
pub max_scroll: 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 { impl ScrollableContent {
pub fn scroll_to_top(&mut self) { pub fn scroll_to_top(&mut self) {
self.pos_y = 0.0; self.pos_y = 0.0;
@ -77,11 +88,18 @@ impl ScrollableContent {
} }
pub fn create_scroll_view(mut q: Query<&mut Node, Added<ScrollView>>) { 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() { for mut style in q.iter_mut() {
style.overflow = Overflow::clip(); style.overflow = overflow;
style.align_items = AlignItems::Start; style.align_items = align_items;
style.align_self = AlignSelf::Stretch; style.align_self = align_self;
style.flex_direction = FlexDirection::Row; style.flex_direction = flex_direction;
} }
} }