Skip to content

Commit 733c070

Browse files
committed
Add card tooltips on hover
1 parent 9288d13 commit 733c070

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

src/game/card.rs

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn card(key: impl Into<String>, active: bool) -> impl EntityCommand {
154154
let icon = r!(config.card_icon_map.get(&card.icon_key)).clone();
155155
let name = format!("Card(\"{}\")", card.name);
156156
let height = config.card_height;
157+
let tooltip_text = format!("{}\n\n{}", card.name, card.description);
157158

158159
world
159160
.entity_mut(entity)
@@ -168,6 +169,12 @@ fn card(key: impl Into<String>, active: bool) -> impl EntityCommand {
168169
..default()
169170
},
170171
ThemeColor::CardBorder.target::<BorderColor>(),
172+
Interaction::default(),
173+
Tooltip {
174+
text: tooltip_text,
175+
side: TooltipSide::Top,
176+
offset: Vec2::ZERO,
177+
},
171178
))
172179
.with_children(|children| {
173180
children.spawn_with(background).with_children(|children| {

src/screen/playing/level_up_menu.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::ui::prelude::*;
1010
use crate::util::prelude::*;
1111

1212
// TODO: Deck actions in deck.rs, but disabled by default. Enable them during this menu.
13+
// TODO: Random card selection to add to deck.
14+
// TODO: Helpful message if the player is at deck capacity.
1315
pub(super) fn plugin(app: &mut App) {
1416
app.add_systems(
1517
StateFlush,
@@ -32,7 +34,7 @@ fn open_level_up_menu(mut commands: Commands, ui_root: Res<UiRoot>) {
3234
}
3335

3436
fn level_up_overlay(mut entity: EntityWorldMut) {
35-
entity.add(widget::blocking_overlay).insert((
37+
entity.add(widget::overlay).insert((
3638
Name::new("LevelUpOverlay"),
3739
ZIndex::Global(1),
3840
StateScope::<PlayingMenu>::default(),

src/screen/playing/pause_menu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn open_pause_menu(mut commands: Commands, ui_root: Res<UiRoot>) {
2323
}
2424

2525
fn pause_overlay(mut entity: EntityWorldMut) {
26-
entity.add(widget::blocking_overlay).insert((
26+
entity.add(widget::overlay).insert((
2727
Name::new("PauseOverlay"),
2828
ZIndex::Global(1),
2929
ThemeColor::Overlay.target::<BackgroundColor>(),

src/ui.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub mod prelude {
2020
pub use super::font::THICK_FONT_HANDLE;
2121
pub use super::interaction::InteractionTable;
2222
pub use super::interaction::IsDisabled;
23+
pub use super::tooltip::Tooltip;
24+
pub use super::tooltip::TooltipSide;
2325
pub use super::widget;
2426
pub use super::UiRoot;
2527
pub use crate::core::theme::ThemeColor;

src/ui/tooltip.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl FromWorld for TooltipRoot {
5454
font: FONT_HANDLE,
5555
..default()
5656
},
57-
),
57+
)
58+
.with_text_justify(JustifyText::Center),
5859
// TODO: Adjustable font sizes in ThemeConfig
5960
DynamicFontSize::new(Px(16.0)),
6061
ThemeColorForText(vec![ThemeColor::BodyText]),
@@ -85,7 +86,7 @@ pub struct Tooltip {
8586
impl Configure for Tooltip {
8687
fn configure(app: &mut App) {
8788
app.register_type::<Self>();
88-
app.add_systems(Update, show_tooltip_on_hover.in_set(UpdateSet::SyncLate));
89+
app.add_systems(Update, show_tooltip_on_hover.in_set(UpdateSet::RecordInput));
8990
}
9091
}
9192

@@ -97,35 +98,41 @@ fn show_tooltip_on_hover(
9798
mut text_query: Query<&mut Text>,
9899
interaction_query: Query<(&Interaction, &Tooltip, &GlobalTransform, &Node)>,
99100
) {
100-
let window = r!(window_query.get(window_root.primary));
101101
let (mut visibility, mut style) = r!(container_query.get_mut(tooltip_root.container));
102102
let mut text = r!(text_query.get_mut(tooltip_root.text));
103+
let window = r!(window_query.get(window_root.primary));
104+
let width = window.width();
105+
let height = window.height();
103106

104107
for (interaction, tooltip, gt, node) in &interaction_query {
108+
// Skip nodes that are not hovered.
105109
if matches!(interaction, Interaction::None) {
106110
*visibility = Visibility::Hidden;
107111
continue;
108112
}
109113

110-
let rect = node.logical_rect(gt);
114+
// Set the tooltip text and make it visible.
115+
*visibility = Visibility::Inherited;
116+
text.sections[0].value.clone_from(&tooltip.text);
111117

112-
let width = window.width();
113-
let height = window.height();
118+
// Get the left, right, top, bottom of the target node.
119+
let rect = node.logical_rect(gt);
114120
let (left, right, top, bottom) = (
115121
rect.min.x + tooltip.offset.x,
116122
rect.max.x + tooltip.offset.x,
117123
rect.min.y + tooltip.offset.y,
118124
rect.max.y + tooltip.offset.y,
119125
);
120126

121-
*visibility = Visibility::Inherited;
122-
text.sections[0].value.clone_from(&tooltip.text);
127+
// Set the left, right, top, bottom of the tooltip node.
123128
(style.left, style.right, style.top, style.bottom) = match tooltip.side {
124129
TooltipSide::Left => (Auto, Px(width - left), Auto, Px(height - bottom)),
125130
TooltipSide::Right => (Px(right), Auto, Auto, Px(height - bottom)),
126131
TooltipSide::Top => (Px(left), Auto, Auto, Px(height - top)),
127132
TooltipSide::Bottom => (Px(left), Auto, Px(bottom), Auto),
128133
};
134+
135+
// Exit early (because there's only one tooltip).
129136
return;
130137
}
131138
}

0 commit comments

Comments
 (0)