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

ButtonBundle starts up with Hovered Interaction. #1036

Closed
visualcode-t opened this issue Dec 9, 2020 · 2 comments · Fixed by #1070
Closed

ButtonBundle starts up with Hovered Interaction. #1036

visualcode-t opened this issue Dec 9, 2020 · 2 comments · Fixed by #1070
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior

Comments

@visualcode-t
Copy link

Bevy version

bevy = { git = "https://github.com/bevyengine/bevy", version = "0.3.0" }

Operating system & version

Windows 10

What you did

use bevy::prelude::*;

struct Output {
    msg: String,
    msg_prev: String,
}

impl Default for Output {
    fn default() -> Self {
        Output {
            msg: String::new(),
            msg_prev: String::new(),
        }
    }
}

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .add_system(interaction)
        .init_resource::<Output>()
        .add_system(debug)
        .run();
}
fn setup(cmds: &mut Commands) {
    cmds.spawn(CameraUiBundle::default());
    cmds.spawn(ButtonBundle {
        style: Style {
            size: Size::new(Val::Px(120.0), Val::Px(120.0)),
            ..Default::default()
        },
        ..Default::default()
    });
}
fn interaction(mut output: ResMut<Output>, i_query: Query<&Interaction>) {
    for a in i_query.iter() {
        match *a {
            Interaction::Clicked => output.msg = "Clicked".to_string(),
            Interaction::Hovered => output.msg = "Hover".to_string(),
            Interaction::None => output.msg = "Normal".to_string(),
        }
    }
}
fn debug(mut mut_res: ResMut<Output>) {
    if mut_res.msg != mut_res.msg_prev {
        println!("{}", mut_res.msg);
        mut_res.msg_prev = mut_res.msg.clone();
    }
}

What you expected to happen

I expected to start my application with Interaction::None.

What actually happened

Normal
Dec 08 21:09:46.756 WARN wgpu_core::device: Failed to parse shader SPIR-V code: UnsupportedBuiltIn(4)
Dec 08 21:09:46.756 WARN wgpu_core::device: Shader module will not be validated
Hover

Additional information

  • I am not sure if the WARN messages are relevant, though I found it odd that they show up between the change in state.
  • If setup is changed to add a second button, Interaction::Hovered never happens.
  • If the mouse is within the bounds of the window, Interaction::Hovered never happens.
  • If the mouse is within the bounds of the button, Interaction::Hovered occurs as expected.
@Moxinilian Moxinilian added C-Bug An unexpected or incorrect behavior A-UI Graphical user interfaces, styles, layouts, and widgets labels Dec 9, 2020
@mockersf
Copy link
Member

mockersf commented Dec 9, 2020

the issue is that before you move your mouse, it uses the default value for a position which is (0, 0) so over your button in the corner... it's the default value of this field. This should be fixed, maybe by using an option to ignore mouse interaction before the mouse has been moved for the first time.

for your issue with two buttons, as you are using a single resource to keep the state of all your buttons, you can only see the state of one button (the last added)
if you change your system like this:

fn interaction(mut output: ResMut<Output>, i_query: Query<&Interaction, Changed<Interaction>>) {
    for a in i_query.iter() {
        eprintln!("{:?}", a);
        match *a {
            Interaction::Clicked => output.msg = "Clicked".to_string(),
            Interaction::Hovered => output.msg = "Hover".to_string(),
            Interaction::None => output.msg = "Normal".to_string(),
        }
    }
}

you should see all interactions. I added a filter Changed<Interaction> to the query to not be flooded every frame

@visualcode-t
Copy link
Author

Thank you for the input. I'm still trying to figure out some of the Query stuff.

As for the position, it would be better if the position could be determined prior to events. Otherwise, you could end up with the reverse situation of Hovered not triggering when it should. While this is such a minor issue, it would prevent odd behavior that makes UI feel a little clunky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants