Skip to content

Instantly share code, notes, and snippets.

@DylanRJohnston
Created June 5, 2025 08:48
Show Gist options
  • Select an option

  • Save DylanRJohnston/02f74b8645b18e4a194ab633a136a4a2 to your computer and use it in GitHub Desktop.

Select an option

Save DylanRJohnston/02f74b8645b18e4a194ab633a136a4a2 to your computer and use it in GitHub Desktop.
Bevy Hover Tooltip Plugin
use bevy::prelude::*;
use crate::observe::Observe;
pub fn tooltip(text: &'static str) -> impl Bundle {
(
Observe::event(tooltip_hover_start(text)),
Observe::event(tooltip_hover),
Observe::event(tooltip_hover_end),
)
}
#[derive(Debug, Clone, Copy, Component)]
struct Tooltip;
fn tooltip_bundle(text: &str) -> impl Bundle {
(
Tooltip,
Node {
padding: UiRect::all(Val::Px(8.0)),
..default()
},
BackgroundColor(Color::BLACK.with_alpha(0.8)),
BorderRadius::all(Val::Px(8.0)),
children![(Text::new(text), Pickable::IGNORE)],
)
}
fn tooltip_hover_start(text: &str) -> impl Fn(Trigger<Pointer<Over>>, Commands) {
move |_, mut commands| {
commands.spawn(tooltip_bundle(text));
}
}
fn tooltip_hover(trigger: Trigger<Pointer<Move>>, mut tooltip: Single<&mut Node, With<Tooltip>>) {
tooltip.top = Val::Px(trigger.pointer_location.position.y);
tooltip.left = Val::Px(trigger.pointer_location.position.x);
}
fn tooltip_hover_end(
_: Trigger<Pointer<Out>>,
tooltips: Query<Entity, With<Tooltip>>,
mut commands: Commands,
) {
tooltips
.iter()
.for_each(|tooltip| commands.entity(tooltip).despawn());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment