extends Spatial

#-----------------SCENE--SCRIPT------------------#
#    Close your game faster by clicking 'Esc'    #
#   Change mouse mode by clicking 'Shift + F1'   #
#------------------------------------------------#

export var fast_close := true
var mouse_mode: String = "CAPTURED"

onready var camera = get_node("Player/Head/Camera")

##################################################

func create_water():
	# create water
	var waterscene = load("res://realistic_water_shader/water.tscn")
	for x in range(-1000,1000, 128):
		for y in range(-1000,1000, 128):
			var water_instance = waterscene.instance()
			water_instance.transform.origin.x = x
			water_instance.transform.origin.y = 0
			water_instance.transform.origin.z = y
			add_child(water_instance)

func _ready() -> void:
	if fast_close:
		print("** Fast Close enabled in the 's_main.gd' script **")
		print("** 'Esc' to close 'Shift + F1' to release mouse **")
	var giantsscript = load("res://objectmanager.gd")
	var objmgr = giantsscript.ObjectManager.new()
	# var giants_dir_browser = FileDialog.new()
	# giants_dir_browser.mode = FileDialog.MODE_OPEN_DIR
	# giants_dir_browser.window_title = "Select your Giants directory"
	# giants_dir_browser.access = FileDialog.ACCESS_FILESYSTEM
	# add_child(giants_dir_browser)
	# giants_dir_browser.popup_centered()
	objmgr.build()
	var m = objmgr.load_model("W_story1_rock_2.gbs")
	# print(m.tex)
	# print(m.point_uv)
	print(m.tex)
	var meshinstance = objmgr.load_model_mesh(m)
	meshinstance.transform.origin.y = 50
	add_child(meshinstance)


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_cancel") and fast_close:
		get_tree().quit() # Quits the game
	
	if event.is_action_pressed("mouse_input") and fast_close:
		match mouse_mode: # Switch statement in GDScript
			"CAPTURED":
				Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
				mouse_mode = "VISIBLE"
			"VISIBLE":
				Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
				mouse_mode = "CAPTURED"

func _process(delta):
	var pos = camera.get_global_transform().origin
	get_node("Label").text = "X: "+str(pos.x)+", Y: "+str(pos.y)+", Z: "+str(pos.z)
	var rotation = camera.rotation
	get_node("Label").text += " X: "+str(rotation.x)+", Y: "+str(rotation.y)+", Z: "+str(rotation.z)


func _on_Button_pressed():
	get_tree().quit()


func _on_GiantsDirectory_dir_selected(dir):
	pass # Replace with function body.
