frame-of-mind/src/ui/skip_control/skip_control.gd

137 lines
3.8 KiB
GDScript

class_name SkipControl extends Control
signal skip
signal proceed
signal _transition_text
@export var skip_delay: float = 0.5
@export var auto_continue_delay: float = 2
var time_pressed: float = 0
@onready var button: Button = %SkipButton
@onready var progress: ProgressBar = %ProgressBar
@onready var action_prompt: ActionPrompt = %ActionPrompt
## control is hidden when there is no input.
var unrevealed: bool = true
## as this can be pressed for multiple reasons, this variable is keeping track of all of them.
var pressed: bool = false
## determines if the scene is waiting for a proceed or listening for a skip.
var text_revealed: bool = false:
set(value):
if value and is_node_ready():
if unrevealed:
$AnimationPlayer.play("reveal_skip")
unrevealed = false
else:
$AnimationPlayer.play("replace_text")
await _transition_text
button.text = "skip scene"
elif is_node_ready():
button.text = "reveal full text (hold)"
action_prompt.action = "skip"
text_revealed = value
## while this is true, a counter counts up to automatically proceed.
var aborted
var is_auto_proceeding: bool = false:
set(value):
if is_auto_proceeding and not value:
var tween = get_tree().create_tween()
tween.set_ease(Tween.EASE_IN)
tween.set_trans(Tween.TRANS_QUAD)
tween.tween_property(progress, "value", 0.0, 0.3)
time_pressed = 0
$AnimationPlayer.play("replace_text")
# use this to disable the updates during progress.
is_auto_proceeding = false
await _transition_text
action_prompt.action = "skip"
button.text = "continue"
elif not is_auto_proceeding and value:
$AnimationPlayer.play("replace_text")
# use this to disable the updates during progress.
await _transition_text
is_auto_proceeding = true
action_prompt.action = "ui_cancel"
button.text = "keep reading"
is_auto_proceeding = value
#resets progress bar on button
time_pressed = 0
func _process(delta):
if pressed and is_visible_in_tree():
time_pressed += delta
progress.value = time_pressed / skip_delay
if time_pressed >= skip_delay:
skip.emit()
pressed = false
time_pressed = 0
if not text_revealed:
text_revealed = true
else:
is_auto_proceeding = true
elif is_auto_proceeding and text_revealed:
time_pressed += delta
progress.value = time_pressed / auto_continue_delay
if time_pressed >= auto_continue_delay:
proceed.emit()
reset()
func _input(event: InputEvent) -> void:
if is_visible_in_tree():
if unrevealed:
$AnimationPlayer.play("reveal_skip")
unrevealed = false
if event.is_action_pressed("skip"):
if not (is_auto_proceeding or aborted):
pressed = true
else:
proceed.emit()
is_auto_proceeding = false
$AnimationPlayer.play("vanish_skip")
await $AnimationPlayer.animation_finished
reset()
get_viewport().set_input_as_handled()
elif event.is_action_released("skip"):
if not is_auto_proceeding:
pressed = false
time_pressed = 0
progress.value = 0
get_viewport().set_input_as_handled()
elif Input.is_action_just_pressed("ui_accept") or Input.is_action_just_pressed("ui_focus_next") or Input.is_action_just_pressed("skip") and is_auto_proceeding:
proceed.emit()
get_viewport().set_input_as_handled()
reset()
elif event.is_action_pressed("ui_cancel"):
if not aborted and is_auto_proceeding:
is_auto_proceeding = false
aborted = true
get_viewport().set_input_as_handled()
func _on_skip_button_toggled(button_pressed):
if button_pressed:
pressed = true
else:
pressed = false
time_pressed = 0
func start_proceed_countdown():
text_revealed = true
is_auto_proceeding = true
func transition_text():
_transition_text.emit()
func reset():
$AnimationPlayer.play("RESET")
await(get_tree().create_timer(1).timeout)
unrevealed = true
pressed = false
text_revealed = false
aborted = false
is_auto_proceeding = false