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

116 lines
3.3 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-02-06 17:55:05 +00:00
# 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
2025-02-06 17:55:05 +00:00
# determines wheather or not the scene is waiting for a proceed or listening for a skip.
var proceeding: 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
button.text = "continuing ..."
elif is_node_ready():
2025-02-06 17:55:05 +00:00
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)"
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
# Called every frame. 'delta' is the elapsed time since the previous frame.
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()
2025-02-06 17:55:05 +00:00
print("emmitted skip!")
2024-09-15 09:30:31 +00:00
pressed = false
time_pressed = 0
2025-02-06 17:55:05 +00:00
elif is_auto_proceeding and proceeding:
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-02-06 17:55:05 +00:00
if not event is InputEventMouseMotion and unrevealed:
$AnimationPlayer.play("reveal_skip")
unrevealed = false
2024-09-15 09:30:31 +00:00
if event.is_action_pressed("skip"):
if not proceeding:
pressed = true
else:
proceed.emit()
2025-02-06 17:55:05 +00:00
reset()
get_viewport().set_input_as_handled()
2024-09-15 09:30:31 +00:00
elif event.is_action_released("skip"):
if not proceeding:
pressed = false
time_pressed = 0
progress.value = 0
get_viewport().set_input_as_handled()
2025-02-06 17:55:05 +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 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"):
# FIXME this right now would be consumed by the pause menu.
is_auto_proceeding = false
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():
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
proceeding = false