class_name Slide
extends Node2D
# Display slide content


signal finished

@export var always_visible: bool = false

@export_group("Processing management", "processing_")
@export var processing_start_when_enabled := true ## Override processing when focused
@export var processing_keep_when_disabled := false ## Enable to process at startup and keep processing after transitioning to another slide

var action_finish := "ui_accept"
var center_offset: Vector2


func _ready() -> void:
	center_offset = _compute_center_offset()
	disable()


func disable() -> void:
	set_process_unhandled_key_input(false)

	if processing_keep_when_disabled or get_parent() is Window:
		return
	get_children().all(disable_processing)


func enable() -> void:
	set_visible(true)
	set_process_unhandled_key_input(true)

	if processing_start_when_enabled:
		get_children().all(enable_processing)


func get_center() -> Vector2:
	return get_position() + center_offset


func gently_hide() -> void:
	if always_visible:
		return

	set_visible(false)


func set_action_finish(action_name: String) -> void:
	action_finish = action_name


func _unhandled_key_input(event: InputEvent) -> void:
	if event.is_action(action_finish) and event.is_pressed():
		finished.emit()
		get_viewport().set_input_as_handled()


func _compute_center_offset() -> Vector2:
	var w: float = ProjectSettings.get("display/window/size/viewport_width")
	var h: float = ProjectSettings.get("display/window/size/viewport_height")
	return Vector2(w * scale.x / 2, h * scale.y / 2)


static func enable_processing(node: Node) -> bool:
	node.set_process_mode(Node.PROCESS_MODE_PAUSABLE)
	return true # to make it work with Array.all()


static func disable_processing(node: Node) -> bool:
	node.set_process_mode(Node.PROCESS_MODE_DISABLED)
	return true # to make it work with Array.all()
