extends Node2D


# The radius of the circle that joystick_ring can stay within.
export (float) var radius = 30;
# The results/axes-value for the joystick. Works exactly like a controller's joystick.
var joystick_vector = Vector2(0.0, 0.0);
# The touch ID for the finger/mouse being used for this joystick. (null ID = mouse)
var joystick_touch_id = null;
# A boolean for tracking whether this joystick is active or not (currently being used or not)
var joystick_active = false;
# If hidden, input shouldn't work
var can_input = true


# Called when the node enters the scene tree for the first time.
func _ready():
	return
# warning-ignore:return_value_discarded
	#$Joystick.connect("Joystick_Start", self, "update_label")
# warning-ignore:return_value_discarded
	#$Joystick.connect("Joystick_Updated", self, "update_label")
# warning-ignore:return_value_discarded
	#$Joystick.connect("Joystick_End", self, "update_label")
	pass # Replace with function body.


func update_label():
	$Joystick/ColorRect/Label.text = String($Joystick.joystick_vector)
	joystick_vector = $Joystick.joystick_vector


# warning-ignore:unused_argument
func _process(delta):
	can_input = is_visible()
	#print($Joystick.rect_global_position)


# --------------------- D-PAD JOYSTICK --------------------------

func get_center_of_joystick():
	# Return the center position of the joystick texture.
	return ($Joystick.get_rect().position + $Joystick.get_rect().size/2) - $Joystick.rect_global_position + global_position;




func _input(event):
	
	if !can_input: return
	
	# If the event is a press/touch...
	if event is InputEventScreenTouch or event is InputEventMouseButton:
		
		var event_is_press = true;
		if event is InputEventScreenTouch:
			event_is_press = event.pressed;
		elif event is InputEventMouseButton:
			event_is_press = event.pressed;
		
		#print('teste')
		# If the event is a press...
		if (event_is_press == true):
			if (joystick_active == false):
				var event_position = Vector2();
				var event_ID = null;
					
					# Get the event position and ID
				if event is InputEventScreenTouch:
					event_position = event.position;
					event_ID = event.index;
				elif event is InputEventMouseButton:
					event_position = get_global_mouse_position();
					event_ID = null;
				
					# If the event is within the radius of the joystick.
				if ( ((get_center_of_joystick() + $Joystick.rect_global_position) - event_position).length() <= radius):
					
					# Set the joystick event ID.
					joystick_touch_id = event_ID;
					
					# Make the joystick active.
					joystick_active = true;
					
					# Calculate the new joystick vector value, using some math to make a vector pointing from the
					# center of the joystick to the event's position, and then divide it by the joystick radius to get
					# a normalized vector.
					joystick_vector = ((get_center_of_joystick() + $Joystick.rect_global_position) - event_position) / radius;
					
					# Set the joystick's position
					$Joystick/JoystickRing.rect_global_position = event_position - ($Joystick/JoystickRing.rect_size/2);
					
					# Now the joystick has just been activated, emit Joystick_Start.
					emit_signal("Joystick_Start");
				
		
		# If the event is a release...
		else:
			# If the joystick is active...
			if (joystick_active == true):
				
				# We need to figure out if the event has the Index we are bound to
				var event_ID = null;
				
				# Get the event ID
				if event is InputEventScreenTouch:
					event_ID = event.index;
				elif event is InputEventMouseButton:
					event_ID = null;
				
				# Figure out if it is this joystick's event
				if (joystick_touch_id == event_ID):
					
					# Reset everything, and if we are using a portion of the screen, then become
					# invisible
					$Joystick/JoystickRing.rect_global_position = get_center_of_joystick() + $Joystick.rect_global_position - ($Joystick/JoystickRing.rect_size/2);
					joystick_vector = Vector2(0, 0);
					
					joystick_touch_id = null;
					joystick_active = false;
					
					# Emit the Joystick_End signal because the joystick is now inactive.
					emit_signal("Joystick_End");
		

	# If the event is a motion event... (mouse/finger is moving on/across the screen)
	if event is InputEventScreenDrag or event is InputEventMouseMotion:
		
		# Only bother to update if we are active/in-use
		if (joystick_active == true):
			
			# We need to figure out if the event has the Index we are bound to
			# and we need to get the position of the event
			var event_ID = null;
			var event_position = Vector2();
			
			# Get the event ID and position
			if event is InputEventScreenDrag:
				event_ID = event.index;
				event_position = event.position;
			elif event is InputEventMouseMotion:
				event_ID = null;
				event_position = get_global_mouse_position();
			
			# If this event is this joystick's event.
			if (event_ID == joystick_touch_id):
				
				# Check to see if the event position is within the joystick's radius.
				# If it is, update the position of the inner ring, update joystick_vector with the new values,
				# and emit the Joystick_Updated signal.
				if ( ((get_center_of_joystick() + $Joystick.rect_global_position) - event_position).length() <= radius):
					$Joystick/JoystickRing.rect_global_position = event_position - ($Joystick/JoystickRing.rect_size/2);
					
					joystick_vector = ((get_center_of_joystick() + $Joystick.rect_global_position) - event_position) / radius;
					
					emit_signal("Joystick_Updated");
				
				# If the event position is NOT within the joystick radius, we need to calculate the values
				# differently, but within the radius of the joystick (even though the event is outside it)
				#
				# We calculate the joystick vector using a normalized vector,
				# We use a normalized direction from the center to the event position and multiply it by the joystick's
				# radius so that it is on the edge of the joystick,
				# and emit the Joystick_Updated signal.
				else:
					joystick_vector = ((get_center_of_joystick() + $Joystick.rect_global_position) - event_position).normalized()
					
					$Joystick/JoystickRing.rect_global_position = get_center_of_joystick() + $Joystick.rect_global_position - ($Joystick/JoystickRing.rect_size/2);
					$Joystick/JoystickRing.rect_global_position -= joystick_vector * radius;
					
					emit_signal("Joystick_Updated");





