Skip to content

Instantly share code, notes, and snippets.

@oceantume
Created March 5, 2022 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oceantume/84964b3daf1111e0e98f954942e74b5d to your computer and use it in GitHub Desktop.
Save oceantume/84964b3daf1111e0e98f954942e74b5d to your computer and use it in GitHub Desktop.
Bevy Widgets Experiments - Tooltip
use bevy::prelude::*;
use bevy_ui_widgets::tooltip::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(TooltipPlugin)
.add_startup_system(setup)
.add_system(setup_text)
.run();
}
struct MyTooltip(Entity);
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
let tooltip = commands
.spawn_bundle(TooltipBundle {
align: TooltipAlign::Left,
position: TooltipPosition::FollowCursor,
color: Color::rgb(0.15, 0.15, 0.15).into(),
// NOTE:
// it would be simpler to have Text here, but it involves more copying
// and kind of prevents someone from manually setting Text in the child.
// text: Text::with_section(...),
..Default::default()
})
.id();
commands.insert_resource(MyTooltip(tooltip))
}
fn setup_text(
mut tooltip_q: Query<(&TooltipTextUiNode, &mut Text), Added<TooltipTextUiNode>>,
my_tooltip: Res<MyTooltip>,
asset_server: Res<AssetServer>,
) {
// NOTE:
// This is a little weird and not very friendly, especially if your tooltip's text doesn't need to change.
// An alternative would be to have a Text component be a part of tooltip that gets copied to children.
if let Some((_, mut text)) = tooltip_q.iter_mut().find(|(node, _)| node.0 == my_tooltip.0) {
*text = Text::with_section(
"Hello, tooltip!",
TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 25.0,
color: Color::rgb(0.9, 0.9, 0.9),
},
Default::default(),
);
}
}
@oceantume
Copy link
Author

oceantume commented Mar 5, 2022

WIP preview. This is very "barebones" and doesn't yet have any advanced feature like flipping when reaching screen edges.

tooltip_FPCAXwi73O.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment