frame-of-mind/src/logic-scenes/misc/skip_control.gd

116 lines
3.3 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
# 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 wheather or not the scene is waiting for a proceed or listening for a skip.
var proceeding: 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 = "continuing ..."
elif is_node_ready():
button.text = "skip reading (hold)"
proceeding = value
# while this is true, a counter counts up to automatically proceed.
var is_auto_proceeding: bool = true:
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
button.text = "continue (press)"
is_auto_proceeding = value
#resets progress bar on button
time_pressed = 0
# Called every frame. 'delta' is the elapsed time since the previous frame.
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()
print("emmitted skip!")
pressed = false
time_pressed = 0
elif is_auto_proceeding and proceeding:
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 not event is InputEventMouseMotion and unrevealed:
$AnimationPlayer.play("reveal_skip")
unrevealed = false
if event.is_action_pressed("skip"):
if not proceeding:
pressed = true
else:
proceed.emit()
reset()
get_viewport().set_input_as_handled()
elif event.is_action_released("skip"):
if not 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 proceeding:
proceed.emit()
get_viewport().set_input_as_handled()
reset()
elif event.is_action_pressed("ui_cancel"):
# FIXME this right now would be consumed by the pause menu.
is_auto_proceeding = false
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():
proceeding = true
func transition_text():
_transition_text.emit()
func reset():
$AnimationPlayer.play("RESET")
await(get_tree().create_timer(1).timeout)
unrevealed = true
pressed = false
proceeding = false