bevy_ui_pointer_capture_detector

Crates.iobevy_ui_pointer_capture_detector
lib.rsbevy_ui_pointer_capture_detector
version0.3.1
sourcesrc
created_at2022-06-14 00:04:49.372741
updated_at2022-11-25 13:24:17.774074
descriptiona Bevy plugin that detects when the mouse pointer is over a UI node
homepage
repositoryhttps://github.com/ickshonpe/bevy_ui_pointer_capture_detector
max_upload_size
id605445
size534,440
(ickshonpe)

documentation

README

bevy_ui_pointer_capture_detector

A plugin that detects if the mouse pointer is above a Bevy Ui Node.

  • supports Bevy 0.9

Usage

In your Cargo.toml [dependencies] section, add

bevy_ui_pointer_capture_detector = "0.3"

This example draws a square red UI node. Each update it sets the color of the background depending on if the mouse pointer is over the red node:

use bevy::prelude::*;
use bevy_ui_pointer_capture_detector::*;

fn setup(
    mut commands: Commands
) {
    commands
        .spawn(Camera2dBundle::default())
        .commands()
        .spawn(NodeBundle {
            style: Style {
                margin: UiRect { 
                    left: Val::Px(100.0), 
                    bottom: Val::Px(100.0), 
                    right: Val::Auto, 
                    top: Val::Auto 
                },
                size: Size { 
                    width: Val::Px(100.0), 
                    height: Val::Px(100.0) 
                },
                ..Default::default()
            },
            background_color: BackgroundColor(Color::RED),
            ..Default::default()
        });
}

fn is_pointer_captured(
    capture: Res<UiPointerCaptureStatus>,
    mut clear_color: ResMut<ClearColor>,
) {
    clear_color.0 = if capture.is_captured() {
            Color::DARK_GRAY
        } else {
            Color::WHITE
        };
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(BevyUiPointerCaptureDetectorPlugin)
        .add_startup_system(setup)
        .add_system(is_pointer_captured)
        .run();
}

Examples

cargo run --example example
cargo run --example minimal
Commit count: 10

cargo fmt