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

137 lines
3.8 KiB
GDScript3
Raw Normal View History

2025-02-06 17:55:05 +00:00
class_name SkipControl extends Control
2023-08-01 08:52:08 +00:00
signal skip
signal proceed
2025-02-06 17:55:05 +00:00
signal _transition_text
2023-08-01 08:52:08 +00:00
@export var skip_delay: float = 0.5
2025-02-06 17:55:05 +00:00
@export var auto_continue_delay: float = 2
2023-08-01 08:52:08 +00:00
var time_pressed: float = 0
@onready var button: Button = %SkipButton
@onready var progress: ProgressBar = %ProgressBar
2025-05-26 19:40:13 +00:00
@onready var action_prompt: ActionPrompt = %ActionPrompt
2025-05-26 19:40:13 +00:00
## control is hidden when there is no input.
2025-02-06 17:55:05 +00:00
var unrevealed: bool = true
2025-05-26 19:40:13 +00:00
## as this can be pressed for multiple reasons, this variable is keeping track of all of them.
var pressed: bool = false
2025-05-26 19:40:13 +00:00
## determines if the scene is waiting for a proceed or listening for a skip.
var text_revealed: bool = false:
set(value):
2025-02-06 17:55:05 +00:00
if value and is_node_ready():
if unrevealed:
$AnimationPlayer.play("reveal_skip")
unrevealed = false
else:
$AnimationPlayer.play("replace_text")
await _transition_text
2025-05-26 19:40:13 +00:00
button.text = "skip scene"
elif is_node_ready():
2025-05-26 19:40:13 +00:00
button.text = "reveal full text (hold)"
action_prompt.action = "ui_next"
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
2025-05-26 19:40:13 +00:00
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"
2025-02-06 17:55:05 +00:00
is_auto_proceeding = value
#resets progress bar on button
time_pressed = 0
2023-08-01 08:52:08 +00:00
func _process(delta):
if pressed and is_visible_in_tree():
2024-09-15 09:30:31 +00:00
time_pressed += delta
progress.value = time_pressed / skip_delay
if time_pressed >= skip_delay:
skip.emit()
2024-09-15 09:30:31 +00:00
pressed = false
time_pressed = 0
2025-05-26 19:40:13 +00:00
if not text_revealed:
text_revealed = true
else:
is_auto_proceeding = true
2025-05-26 19:40:13 +00:00
elif is_auto_proceeding and text_revealed:
time_pressed += delta
2025-02-06 17:55:05 +00:00
progress.value = time_pressed / auto_continue_delay
if time_pressed >= auto_continue_delay:
proceed.emit()
2025-02-06 17:55:05 +00:00
reset()
func _input(event: InputEvent) -> void:
if is_visible_in_tree():
2025-05-26 19:40:13 +00:00
if unrevealed:
2025-02-06 17:55:05 +00:00
$AnimationPlayer.play("reveal_skip")
unrevealed = false
2024-09-15 09:30:31 +00:00
if event.is_action_pressed("skip"):
2025-05-26 19:40:13 +00:00
if not (is_auto_proceeding or aborted):
pressed = true
else:
proceed.emit()
2025-05-26 19:40:13 +00:00
is_auto_proceeding = false
$AnimationPlayer.play("vanish_skip")
await $AnimationPlayer.animation_finished
2025-02-06 17:55:05 +00:00
reset()
2025-05-26 19:40:13 +00:00
get_viewport().set_input_as_handled()
2024-09-15 09:30:31 +00:00
elif event.is_action_released("skip"):
2025-05-26 19:40:13 +00:00
if not is_auto_proceeding:
pressed = false
time_pressed = 0
progress.value = 0
get_viewport().set_input_as_handled()
2025-05-26 19:40:13 +00:00
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()
2025-02-06 17:55:05 +00:00
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()
2023-08-01 08:52:08 +00:00
func _on_skip_button_toggled(button_pressed):
2024-09-15 09:30:31 +00:00
if button_pressed:
pressed = true
else:
pressed = false
time_pressed = 0
2025-02-06 17:55:05 +00:00
func start_proceed_countdown():
2025-05-26 19:40:13 +00:00
text_revealed = true
is_auto_proceeding = true
2025-02-06 17:55:05 +00:00
func transition_text():
_transition_text.emit()
func reset():
$AnimationPlayer.play("RESET")
await(get_tree().create_timer(1).timeout)
2025-02-06 17:55:05 +00:00
unrevealed = true
pressed = false
2025-05-26 19:40:13 +00:00
text_revealed = false
aborted = false
is_auto_proceeding = false