Created
March 14, 2025 13:36
-
-
Save viniciusemferreira/e7af7f3241cb31fff072a577e5a564b9 to your computer and use it in GitHub Desktop.
This Tooltip script for Godot 4 is a reusable component that displays a tooltip when hovering over an assigned node. It supports automatic positioning, optional screen clamping, and customizable offsets. The tooltip fades in and out smoothly and can be toggled using the ALT key if enabled. It works with both Control and Node2D nodes and ensures …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Control | |
class_name Tooltip | |
@export_category("Tooltip Settings") | |
## The node that owns this tooltip and triggers its visibility. | |
@export var owner_node: Node | |
## If true, allows the tooltip to be toggled on and off with a key press. | |
## This is kind of specific to some projects, so you might want to remove it. | |
## I just wanted to show what I did in one of my projects to toggle tooltips by pressing a key. | |
## @experimental | |
@export var can_toggle: bool = false | |
## If true, prevents the tooltip from going outside the screen bounds. | |
@export var clamp_to_screen: bool = false | |
## X-axis offset from the owner node. | |
@export var offset_x: float | |
## Y-axis offset from the owner node. | |
@export var offset_y: float | |
## The offset vector calculated from the X and Y offsets. | |
@onready var offset: Vector2 = Vector2(offset_x, offset_y) | |
## Whether the tooltip is currently toggled. | |
var toggled: bool = false | |
## Called when the node is added to the scene. | |
func _ready() -> void: | |
# Connects the mouse events of the owner node to show or hide the tooltip. | |
if owner_node is Control or owner_node is Node2D: | |
owner_node.mouse_entered.connect(_on_mouse_entered) | |
owner_node.mouse_exited.connect(_on_mouse_exited) | |
# Initially hide the tooltip and set transparency to 0. | |
modulate = Color(1, 1, 1, 0) | |
hide() | |
## Updates the tooltip's position if it is visible. | |
func _process(_delta: float) -> void: | |
if visible: | |
global_position = _get_position() | |
## Handles the ALT key press to toggle the tooltip if allowed. | |
## This is kind of specific to some projects, so you might want to remove it. | |
## I just wanted to show what I did in one of my projects to toggle tooltips by pressing a key. | |
## @experimental | |
func _unhandled_input(event) -> void: | |
if event is InputEventKey: | |
if event.pressed and event.keycode == KEY_ALT and !event.is_echo(): | |
if can_toggle: | |
toggled = !toggled | |
## Calculates the tooltip's position based on the owner node. | |
## Displays at the top by default. | |
func _get_position() -> Vector2: | |
var pos = Vector2() | |
if owner_node is Node2D: | |
pos = owner_node.get_global_transform_with_canvas().origin | |
elif owner_node is Control: | |
pos = owner_node.global_position - Vector2(0, get_global_rect().size.y) + offset | |
return pos | |
## Shows or hides the tooltip based on the given boolean value. | |
func _manage_tooltip_visibility(should_show: bool) -> void: | |
position = _get_position() | |
if should_show: | |
if !visible: | |
show_tooltip() | |
else: | |
if visible: | |
hide_tooltip() | |
## Displays the tooltip with a fade-in effect. | |
func show_tooltip() -> void: | |
show() | |
var tween = create_tween() | |
tween.tween_property(self, "modulate", Color(1, 1, 1, 1), 0.15) | |
## Hides the tooltip with a fade-out effect. | |
## Calls an optional function after completion (default is hide). | |
func hide_tooltip(on_end: Callable = hide) -> void: | |
if !is_queued_for_deletion(): | |
var tween = create_tween() | |
tween.tween_property(self, "modulate", Color(1, 1, 1, 0), 0.15) | |
tween.tween_callback(on_end) | |
## Callback for when the mouse enters the owner node, showing the tooltip. | |
func _on_mouse_entered() -> void: | |
show_tooltip() | |
## Callback for when the mouse exits the owner node, hiding the tooltip. | |
func _on_mouse_exited() -> void: | |
hide_tooltip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment