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

Support for Text2dBundle #358

Open
ChrisBeeson opened this issue Aug 3, 2024 · 0 comments
Open

Support for Text2dBundle #358

ChrisBeeson opened this issue Aug 3, 2024 · 0 comments

Comments

@ChrisBeeson
Copy link

Any ideas why this isn't working?

use bevy::prelude::*;
use bevy::text::Text2dBounds;
use bevy_mod_picking::prelude::*;

pub fn run () {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(DefaultPickingPlugins)
        .add_systems(Startup, setup)
        .run();
}

#[derive(Component)]
struct ClickableText;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    // Camera
    commands.spawn(Camera2dBundle::default());

    let font = asset_server.load("fonts/OpenSans-Regular.ttf");
    let text_style = TextStyle {
        font,
        font_size: 30.0,
        color: Color::WHITE,
    };

    commands.spawn((
        Text2dBundle {
            text: Text::from_section(
                "Click me!",
                text_style,
            ),
            text_2d_bounds: Text2dBounds {
                size: Vec2::new(300.0, 100.0),
            },
            transform: Transform::from_xyz(0.0, 0.0, 0.0),
            ..default()
        },
        PickableBundle::default(),
        On::<Pointer<Click>>::run(on_click),
        ClickableText,
    ));
}

fn on_click(
    event: Listener<Pointer<Click>>,
    query: Query<&Text, With<ClickableText>>
) {
     println!("Not firing");

    if let Ok(text) = query.get(event.target) {
        println!("Clicked text: {}", text.sections[0].value);
    }
}
@ChrisBeeson ChrisBeeson changed the title Cannot get events on Text2dBundle Support for Text2dBundle Aug 3, 2024
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

1 participant