extends Control

onready var margin = $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window/canvas/MarginContainer"

func _input(event: InputEvent) -> void:
	if Input.is_action_just_pressed("right_click"):
#		event.pressed=true
		if event is InputEventMouseButton and $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window/canvas/MarginContainer".visible:
			$canvas/PopupMenu.mouse_filter=2
			var x = $".".get_global_mouse_position().x
			var y = $".".get_global_mouse_position().y
			if (margin.margin_top<y) and (y<margin.margin_bottom) and (margin.margin_left<x) and (x<margin.margin_right):
				$canvas/PopupMenu.margin_left=x
				$canvas/PopupMenu.margin_top=y
				$canvas/PopupMenu.visible=true

# сдвиг по всем координатам по целеполаганию полёта к объекту
const translacio = 20

func _on_PopupMenu_index_pressed(index):
	var index_obj = $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window/canvas/MarginContainer/VBoxContainer/scroll/ItemList".get_selected_items()
	if $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['uuid']==$"../ship".uuid:#если свой корабль
		return null
	var koordX =  $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['koordinatoX'] + translacio
	var koordY =  $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['koordinatoY'] + translacio
	var koordZ =  $"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['koordinatoZ'] + translacio

	$"../b_itinero/itinero".add_itinero(
		'',
		$"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['uuid'], #'uuid'
		$"/root/Title/CanvasLayer/UI/Lbar/L_VBox/Objektoj/Window".ItemListContent[index_obj[0]]['nomo'], #'nomo'
		koordX,
		koordY,
		koordZ,
		$"../ship".translation.distance_to(Vector3(koordX,koordY,koordZ))
	)
	$"../b_itinero/itinero".FillItemList()
	if index==0:
#		$"../".set_way_point(Vector3(koordX,koordY,koordZ),null)
		$"../b_itinero/itinero"._on_kom_itinero_pressed()
#	elif index==1:


func _on_PopupMenu_draw():
	$canvas/PopupMenu.mouse_filter=0 #для отключения игнорирования меню и его реакции на выбор пунктов
	pass # Replace with function body.




