extends Node2D

var start_location = Vector2(0, 0)

func _unhandled_input(event):
	if event is InputEventScreenTouch:
		if event.pressed:
			start_location = event.position
			Input.action_press("ui_accept")
		else:
			Input.action_release("ui_accept")
			release_all()
	
	if event is InputEventScreenDrag:
		var input_vector = event.position - start_location
		input_vector = input_vector/get_viewport().size.x
		
		release_all()
		
		if input_vector.length() > 0.1:
			if input_vector.x > abs(input_vector.y/2):
				Input.action_press("ui_right")
			if -input_vector.x > abs(input_vector.y/2):
				Input.action_press("ui_left")
			if input_vector.y > abs(input_vector.x/2):
				Input.action_press("ui_down")
			if -input_vector.y > abs(input_vector.x/2):
				Input.action_press("ui_up")


func release_all():
	if Input.is_action_pressed("ui_right"):
		Input.action_release("ui_right")
		
	if Input.is_action_pressed("ui_left"):
		Input.action_release("ui_left")
		
	if Input.is_action_pressed("ui_up"):
		Input.action_release("ui_up")
		
	if Input.is_action_pressed("ui_down"):
		Input.action_release("ui_down")
