extends CanvasLayer


var pages = 1
var currentpage = 0
var boxtext = []


func _ready():
	get_tree().paused = true


func set_text(text):
	#Split the text into pages and detect how many pages there are
	boxtext = text.split("@")
	print(boxtext)
	pages = boxtext.size()
	print(str(pages)+" pages")
	

func _process(_delta):
	#Show the current page
	if currentpage >= pages:
		return 0
	
	$MarginContainer/Panel/Label.bbcode_text = boxtext[currentpage]
	
	#Show an arrow if there's a page after current, otherwise show a stop
	if currentpage+1 == pages:
		$MarginContainer/Panel/TextureRect.texture = load("res://sprites/ui/PageStop.png")
	else:
		$MarginContainer/Panel/TextureRect.texture = load("res://sprites/ui/PageNext.png")


func _input(_event):
	#Check the current page, and close the textbox if it's already the last page
	if Input.is_action_just_pressed("ui_accept"):
		currentpage += 1
		if currentpage >= pages:
			get_tree().paused = false
			queue_free()
