#Clickable Text2d

1 messages · Page 1 of 1 (latest)

mossy flint
#

Howdy, I'm trying to make a clickable text2d but haven't had success in getting the observer to trigger. Unfortunately Google hasn't been too helpful and that AI overview has been even worse. I've tried with and without Pickable as well.

fn on_click_options(_: On<Pointer<Click>>, mut next_menu_state: ResMut<NextState<MainMenuState>>) {
    warn!("Click called");
    next_menu_state.set(MainMenuState::Options) ;
}

fn options_button(commands: &mut Commands, window: &Single<&Window>) {
    let (x, y) = options_button_position(window);
    commands.spawn((
        Name::new("Options button"),
        MainMenuButton::Options,
        Transform::from_translation(Vec3::new(x, y, 0.0)),
        Text2d::new("Options"),
        TextFont {
            font_size: 42.0,
            ..Default::default()
        },
        Pickable::default(),
    )).observe(on_click_options);
}
quick chasm
glass sigil
#

Do you have the picking backend features active?

lapis cipher
#

I just checked Bevy's code and, unless I am missing something, Text2d does not have picking support.

Text2d is part of the bevy_sprite crate, and that crate only has one "picking" backend which only queries Sprites:

impl Plugin for SpritePickingPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<SpritePickingSettings>()
            .add_systems(PreUpdate, sprite_picking.in_set(PickingSystems::Backend));
    }
}

fn sprite_picking(
    sprite_query: Query<(
        Entity,
        &Sprite, // <---- Only works for Sprites
        &GlobalTransform,
        &Anchor,
        &Pickable,
        &ViewVisibility,
        Option<&RenderLayers>,
    )>,
    /* ... */
) { /* ... */ }
#

So your options are to either make your own backend implementation, or the easiest solution would be to have a Sprite as a parent until Text2d gets proper picking implementation

mossy flint
mossy flint
mossy flint
glass sigil
#

there are some features that you need to enable on Cargo.toml to enable picking

lapis cipher
#

Which is weird, I'll see if there is an open issue about this

glass sigil
#

it is probably on the sprite backend

lapis cipher
mossy flint
#

(and maybe I can take a look at that PR and update it later lol)