From eacff6d6e7e55cc8f9edf921d854e70b0ec31ce9 Mon Sep 17 00:00:00 2001 From: Ben Frankel Date: Thu, 21 Nov 2024 14:08:33 -0800 Subject: [PATCH] Add simple tooltip placement example --- Cargo.toml | 3 ++ examples/placement.rs | 71 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 examples/placement.rs diff --git a/Cargo.toml b/Cargo.toml index af2602c..f422a9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,9 @@ bevy_ui = { version = "0.15.0-rc.3", default-features = false, features = [ bevy_window = { version = "0.15.0-rc.3", default-features = false } tiny_bail = "0.4" +[dev-dependencies] +bevy = "0.15.0-rc.3" + [lints.rust] missing_docs = "deny" diff --git a/examples/placement.rs b/examples/placement.rs new file mode 100644 index 0000000..3ababc8 --- /dev/null +++ b/examples/placement.rs @@ -0,0 +1,71 @@ +//! A demonstration of some tooltip placement options. + +use bevy::prelude::*; +use bevy::sprite::Anchor; +use bevy::ui::Val::*; +use pyri_tooltip::prelude::*; + +fn main() { + let mut app = App::new(); + app.add_plugins((DefaultPlugins, TooltipPlugin::default())); + app.add_systems(Startup, spawn_scene); + app.add_systems(Update, highlight_hovered_tile); + app.run(); +} + +fn spawn_scene(mut commands: Commands) { + commands.spawn(Camera2d::default()); + commands + .spawn(Node { + display: Display::Grid, + align_self: AlignSelf::Center, + justify_self: JustifySelf::Center, + row_gap: Px(8.0), + column_gap: Px(8.0), + grid_template_columns: RepeatedGridTrack::auto(3), + ..default() + }) + .with_children(|parent| { + let tile = ( + Node { + width: Px(64.0), + height: Px(64.0), + border: UiRect::all(Px(4.0)), + ..default() + }, + BackgroundColor(Color::WHITE), + BorderColor(Color::BLACK), + BorderRadius::all(Px(8.0)), + ); + + // Demonstrate fixed placement. + for anchor in [ + Anchor::TopLeft, + Anchor::TopCenter, + Anchor::TopRight, + Anchor::CenterLeft, + Anchor::Center, + Anchor::CenterRight, + Anchor::BottomLeft, + Anchor::BottomCenter, + Anchor::BottomRight, + ] { + parent.spawn(( + tile.clone(), + Tooltip::fixed(anchor, format!("Tooltip::fixed({:?}, text)", anchor)), + )); + } + + // Demonstrate cursor placement. + parent.spawn((tile.clone(), Tooltip::cursor("Tooltip::cursor(text)"))); + }); +} + +fn highlight_hovered_tile(mut tile_query: Query<(&Interaction, &mut BackgroundColor)>) { + for (interaction, mut background_color) in &mut tile_query { + background_color.0 = match interaction { + Interaction::None => Color::NONE, + _ => Color::WHITE, + } + } +}