From 947c64ee7f97259fc4fff962ec9fc6edc657ecab Mon Sep 17 00:00:00 2001 From: Ben Frankel Date: Mon, 19 Aug 2024 19:32:44 -0700 Subject: [PATCH] Improve ergonomics a bit for common cases --- src/context.rs | 2 +- src/lib.rs | 38 ++++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/context.rs b/src/context.rs index 2aaf490..d7f59cd 100644 --- a/src/context.rs +++ b/src/context.rs @@ -65,7 +65,7 @@ impl Default for TooltipContext { target: Entity::PLACEHOLDER, timer: 0, cursor_pos: Vec2::ZERO, - tooltip: Tooltip::new(Entity::PLACEHOLDER), + tooltip: Tooltip::cursor(Entity::PLACEHOLDER), } } } diff --git a/src/lib.rs b/src/lib.rs index fdaee68..75807a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,7 @@ use bevy_ecs::{ }; use bevy_hierarchy::BuildWorldChildren as _; use bevy_render::view::Visibility; +use bevy_sprite::Anchor; use bevy_text::{JustifyText, Text, TextSection, TextStyle}; use bevy_transform::TransformSystem; use bevy_ui::{ @@ -200,8 +201,18 @@ pub struct Tooltip { } impl Tooltip { - /// Construct a new `Tooltip` with default behavior. - pub fn new(content: impl Into) -> Self { + /// Create a new fixed `Tooltip`. + pub fn fixed(placement: Anchor, content: impl Into) -> Self { + Self { + activation: TooltipActivation::IMMEDIATE, + transfer: TooltipTransfer::SHORT, + placement: placement.into(), + content: content.into(), + } + } + + /// Create a new cursor `Tooltip`. + pub fn cursor(content: impl Into) -> Self { Self { activation: TooltipActivation::IDLE, transfer: TooltipTransfer::NONE, @@ -210,25 +221,10 @@ impl Tooltip { } } - /// Construct a new `Tooltip` from a single [`TextSection`] and default behavior. - pub fn from_section(value: impl Into, style: TextStyle) -> Self { - Self::new(TooltipContent::Primary(Text::from_section(value, style))) - } - - /// Construct a new `Tooltip` from a list of [`TextSection`]s and default behavior. - pub fn from_sections(sections: impl IntoIterator) -> Self { - Self::new(TooltipContent::Primary(Text::from_sections(sections))) - } - - /// Construct a new `Tooltip` from a given [`Text`] and default behavior. - pub fn from_text(text: impl Into) -> Self { - Self::new(TooltipContent::Primary(text.into())) - } - /// Set [`JustifyText`]. /// /// NOTE: This does nothing for custom tooltips. - pub fn with_justify_text(mut self, justify_text: JustifyText) -> Self { + pub fn with_justify(mut self, justify_text: JustifyText) -> Self { // TODO: Warn otherwise? if let TooltipContent::Primary(text) = &mut self.content { text.justify = justify_text; @@ -418,6 +414,12 @@ impl From for TooltipContent { } } +impl From> for TooltipContent { + fn from(value: Vec) -> Self { + Self::Primary(Text::from_sections(value)) + } +} + impl From for TooltipContent { fn from(value: Text) -> Self { Self::Primary(value)