generated from krampus/template-godot4
48 lines
1.3 KiB
GDScript3
48 lines
1.3 KiB
GDScript3
|
class_name Fader extends Control
|
||
|
## A UI container which fades between child elements
|
||
|
## I don't want to write logic to check for non CanvasItems here so don't try it.
|
||
|
|
||
|
## Time in seconds for which an element will be shown before fading out.
|
||
|
@export var show_time := 2.0
|
||
|
|
||
|
## Time in seconds for an element to fade in or out.
|
||
|
@export var fade_time := 0.4
|
||
|
|
||
|
## Time in seconds for which an element will be hidden before fading in.
|
||
|
@export var hidden_time := 0.0
|
||
|
|
||
|
## Modulate color when fully visible
|
||
|
@export var visible_modulate := Color.WHITE
|
||
|
|
||
|
## Modulate color when fully hidden
|
||
|
@export var hidden_modulate := Color.TRANSPARENT
|
||
|
|
||
|
var _index := -1
|
||
|
|
||
|
|
||
|
func _ready() -> void:
|
||
|
if get_child_count() == 0:
|
||
|
return
|
||
|
|
||
|
# Hide all children initially
|
||
|
for child: Node in get_children():
|
||
|
var canvas_item: CanvasItem = child
|
||
|
canvas_item.hide()
|
||
|
canvas_item.modulate = hidden_modulate
|
||
|
|
||
|
# Start animation cycle
|
||
|
_fade_next_child()
|
||
|
|
||
|
|
||
|
func _fade_next_child() -> void:
|
||
|
_index = wrapi(_index + 1, 0, get_child_count())
|
||
|
var child: CanvasItem = get_children()[_index]
|
||
|
child.show()
|
||
|
var tween := create_tween()
|
||
|
tween.tween_property(child, "modulate", visible_modulate, fade_time)
|
||
|
tween.tween_interval(show_time)
|
||
|
tween.tween_property(child, "modulate", hidden_modulate, fade_time)
|
||
|
tween.tween_interval(hidden_time)
|
||
|
tween.tween_callback(child.hide)
|
||
|
tween.tween_callback(_fade_next_child)
|