Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating an entity's NineSliceTexture doesn't update the material. #5

Closed
rparrett opened this issue Dec 4, 2023 · 4 comments
Closed

Comments

@rparrett
Copy link
Contributor

rparrett commented Dec 4, 2023

Here's a reproducer that can be dropped in the examples directory.

Note that in the example below, I am only changing the texture, but this is also the case when switching between nine slices with the same texture but different bounds.

I am expecting the button to change visually when hovered.

use bevy::prelude::*;
use bevy_nine_slice_ui::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, NineSlicePlugin::default()))
        .add_systems(Startup, setup)
        .add_systems(Update, button_system)
        .run();
}

fn button_system(
    mut commands: Commands,
    mut interaction_query: Query<(Entity, &Interaction), (Changed<Interaction>, With<Button>)>,
    server: Res<AssetServer>,
) {
    for (entity, interaction) in &mut interaction_query {
        match *interaction {
            Interaction::Hovered => {
                info!("Hovered.");
                commands.entity(entity).insert(NineSliceTexture::from_slice(
                    server.load("panel_atlas.png"),
                    Rect::new(0., 0., 32., 32.),
                ));
            }
            _ => {
                info!("Unhovered.");
                commands
                    .entity(entity)
                    .insert(NineSliceTexture::from_image(server.load("panel.png")));
            }
        }
    }
}

fn setup(mut commands: Commands, server: Res<AssetServer>) {
    commands.spawn(Camera2dBundle::default());
    commands
        .spawn(NodeBundle {
            style: Style {
                width: Val::Percent(100.0),
                height: Val::Percent(100.0),
                align_items: AlignItems::Center,
                justify_content: JustifyContent::Center,
                ..default()
            },
            ..default()
        })
        .with_children(|parent| {
            parent.spawn((
                ButtonBundle {
                    style: Style {
                        width: Val::Px(150.0),
                        height: Val::Px(65.0),
                        justify_content: JustifyContent::Center,
                        align_items: AlignItems::Center,
                        ..default()
                    },
                    ..default()
                },
                NineSliceTexture::from_image(server.load("panel.png")),
            ));
        });
}
@rparrett
Copy link
Contributor Author

rparrett commented Dec 4, 2023

Here's a very small diff that fixes the issue for me. Not confident that this is a proper fix, but I'm going to be using this as a workaround and moving on for a bit.

diff --git a/src/lib.rs b/src/lib.rs
index a20f17d..1c3d4b5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -98,7 +98,7 @@ fn sync_nine_slice(
 }
 
 fn spawn_nine_slice(
-    nodes: Query<(Entity, &NineSliceTexture, &Node), Without<Handle<NineSliceMaterial>>>,
+    nodes: Query<(Entity, &NineSliceTexture, &Node), Changed<NineSliceTexture>>,
     images: Res<Assets<Image>>,
     mut cmd: Commands,
     mut materials: ResMut<Assets<NineSliceMaterial>>,

@Lommix
Copy link
Owner

Lommix commented Dec 4, 2023

The texture component was intended to be used as a one-shot component. But this is stupid, I just ran into the same issue in my bevy jam game. I will change it to be a permanent component and sync the materials instead.

Personally I also would love to have some additional shader capabilities, like a blend colors or even a color lookup table for easy to create hover effects, without the need of a new Image each time.

Expect an overhaul very soon.

@rparrett
Copy link
Contributor Author

rparrett commented Dec 4, 2023

Personally I also would love to have some additional shader capabilities, like a blend colors or even a color lookup table for easy to create hover effects, without the need of a new Image each time.

That sounds great

@Lommix
Copy link
Owner

Lommix commented Dec 4, 2023

Everything I mentioned is now in the new version! The component is now synced, checkout the new Readme and examples

@Lommix Lommix closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants