tool

extends Control

onready var grid = find_node("grid")
onready var key_dialog = get_node("key_dialog")

const INPUT_TYPE_KEY = 0
const INPUT_TYPE_JOYPAD = 1

var last_action:String
var last_event:InputEvent

var new_inputs = {}

func _ready():
	init_mapping()


func init_mapping():
	new_inputs = {}
	for action in InputMap.get_actions():
		if action.begins_with("controls_"):
			var action_label = Label.new()
			action_label.set_name(action + "_label")
			action_label.set_text(action)
			grid.add_child(action_label)
			
			var key_action_button = Button.new()
			key_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
			key_action_button.set_flat(true)
			key_action_button.set_meta("action",action)
			key_action_button.set_button_icon(get_node("icons/key").get_button_icon())
			
			var joypad_action_button = Button.new()
			joypad_action_button.set_h_size_flags(SIZE_SHRINK_CENTER)
			joypad_action_button.set_flat(true)
			joypad_action_button.set_meta("action",action)
			
			for mapping in InputMap.get_action_list(action):
				if mapping is InputEventKey:
					key_action_button.set_text(mapping.as_text())
					key_action_button.set_tooltip(mapping.as_text())
				elif mapping is InputEventJoypadButton:
					joypad_action_button.set_tooltip(str(mapping.button_index))
					if has_node("icons/joypad/" + str(mapping.button_index)):
						joypad_action_button.set_button_icon(get_node("icons/joypad/" + str(mapping.button_index)).get_button_icon())
					else:
						joypad_action_button.set_button_icon(get_node("icons/joypad/other").get_button_icon())
						joypad_action_button.set_text(str(mapping.button_index))
				
			grid.add_child(key_action_button)
			key_action_button.connect("pressed",self,"_on_button_pressed",[action, INPUT_TYPE_KEY])
			
			grid.add_child(joypad_action_button)
			joypad_action_button.connect("pressed",self,"_on_button_pressed",[action, INPUT_TYPE_JOYPAD])

func _unhandled_input(event):
	if key_dialog.is_visible_in_tree():
		if key_dialog.get_meta("type") == INPUT_TYPE_KEY && event is InputEventKey:
			last_event = event
			key_dialog.set_text(tr(last_event.as_text()))
			key_dialog.get_ok().set_disabled(false)
		elif key_dialog.get_meta("type") == INPUT_TYPE_JOYPAD && event is InputEventJoypadButton:
			last_event = event
			key_dialog.set_text(last_event.as_text())
			key_dialog.get_ok().set_disabled(false)


func _on_button_pressed(action, type):
	last_action = action
	last_event = null
	key_dialog.set_text(tr("PRESS_KEY"))
	key_dialog.set_meta("type", type)
	key_dialog.connect("confirmed",self,"_on_dialog_confirmed")
	key_dialog.get_ok().set_disabled(true)
	key_dialog.popup_centered()


func _on_dialog_confirmed(type):
		if not new_inputs.has(last_action):
			new_inputs[last_action] = {}
		match key_dialog.get_meta("type"):
			INPUT_TYPE_KEY:
				if last_event is InputEventKey:
					new_inputs[last_action]["key"] = OS.get_scancode_string(last_event.scancode)
			INPUT_TYPE_JOYPAD:
				if last_event is InputEventJoypadButton:
					new_inputs[last_action]["joypad"] = last_event.button_index