require 'scripts/core/core.lua'
require 'scripts/VC_methods.lua'


-- Popups ids
POPUP_ID_NONE = 0
POPUP_ID_CONFIRM_NO_MENU_KEY = 1

local g_configurableControllers = {
	drc = {
			controller_world_node = { 'controller_drc' },
			keys = {
				{ id = 'LSTICK', 	default = {2, 1, 1},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_LSTICK'},
				{ id = 'RSTICK', 	default = {1, 2, 2},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_RSTICK'},
				{ id = 'A', 		default = {3, 3, 3},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_A' },
				{ id = 'B',   		default = {4, 4, 4},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_B' },
				{ id = 'X',		    default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_X' },
				{ id = 'Y', 		default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_Y' },
				{ id = 'START',		default = {12, 12, 12},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_PLUS' },
				{ id = 'SELECT',	default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_MINUS' },
				{ id = 'L', 		default = {9, 9, 9},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_L' },
				{ id = 'R',			default = {10, 10, 10},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_R' },
				{ id = 'ZL',		default = {11, 11, 11},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_ZL' },
				{ id = 'ZR',		default = {13, 13, 13},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_ZR' }
			}
	},
	cla = {
			controller_world_node = { 'controller_cla' },
			keys = {
				{ id = 'LSTICK', 	default = {2, 1, 1},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_CLASSIC_LSTICK'},
				{ id = 'RSTICK', 	default = {1, 2, 2},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_CLASSIC_RSTICK'},
				{ id = 'A', 		default = {3, 3, 3},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_A' },
				{ id = 'B',   		default = {4, 4, 4},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_B' },
				{ id = 'X',		    default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_X' },
				{ id = 'Y', 		default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_Y' },
				{ id = 'START',		default = {12, 12, 12},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_PLUS' },
				{ id = 'SELECT',	default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_MINUS' },
				{ id = 'L', 		default = {9, 9, 9},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_L' },
				{ id = 'R',			default = {10, 10, 10},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_R' },
				{ id = 'ZL',		default = {11, 11, 11},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_ZL' },
				{ id = 'ZR',		default = {13, 13, 13},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_CLASSIC_ZR' }
			}
	},
	urcc = {
			controller_world_node = { 'controller_urcc' },
			keys = {
				{ id = 'LSTICK', 	default = {2, 1, 1},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_LSTICK'},
				{ id = 'RSTICK', 	default = {1, 2, 2},	bindable = {1, 2},										text = Localization_getText'SYS_BUTTON_RSTICK'},
				{ id = 'A', 		default = {3, 3, 3},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_A' },
				{ id = 'B',   		default = {4, 4, 4},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_B' },
				{ id = 'X',		    default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_X' },
				{ id = 'Y', 		default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_Y' },
				{ id = 'START',		default = {12, 12, 12},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_PLUS' },
				{ id = 'SELECT',	default = {14, 14, 14},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_MINUS' },
				{ id = 'L', 		default = {9, 9, 9},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_L' },
				{ id = 'R',			default = {10, 10, 10},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_R' },
				{ id = 'ZL',		default = {11, 11, 11},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_ZL' },
				{ id = 'ZR',		default = {13, 13, 13},	bindable = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},	text = Localization_getText'SYS_BUTTON_ZR' }
			}
	}
}

local g_keyConfigurations = {}

local g_controllerN64 = {
	id = 'n64',
	controller_world_node = { 'controller_n64' },
	keys = {
		{ id = '3DSTICK',    	text = Localization_getText'SYS_BUTTON_NUS_3DSTICK' },
		{ id = 'C_UNIT',    	text = Localization_getText'SYS_BUTTON_NUS_C_UNIT' },
		{ id = 'A',     		text = Localization_getText'SYS_BUTTON_NUS_A' },
		{ id = 'B',     		text = Localization_getText'SYS_BUTTON_NUS_B' },		
		{ id = 'C_UP',     		text = Localization_getText'SYS_BUTTON_NUS_C_UP' },
		{ id = 'C_LEFT',     	text = Localization_getText'SYS_BUTTON_NUS_C_LEFT' },
		{ id = 'C_DOWN',     	text = Localization_getText'SYS_BUTTON_NUS_C_DOWN' },
		{ id = 'C_RIGHT',     	text = Localization_getText'SYS_BUTTON_NUS_C_RIGHT' },
		{ id = 'L',  			text = Localization_getText'SYS_BUTTON_NUS_L' },
		{ id = 'R',  			text = Localization_getText'SYS_BUTTON_NUS_R' },
		{ id = 'Z',     		text = Localization_getText'SYS_BUTTON_NUS_Z' },
		{ id = 'START',			text = Localization_getText'SYS_BUTTON_START' },
		{ id = 'VCM', 			text = Localization_getText'SYS_BUTTON_VCM' },
		{ id = 'NONE', 			text = Localization_getText'SYS_BUTTON_NONE' }
	}
}

--------------------------------------------------------------------------------
-- KeyConfig Object script
--------------------------------------------------------------------------------

local function setListElements(list, controllerKeys, currentPos)
	local elements = {}
	for _, key in ipairs(controllerKeys) do
		elements[#elements + 1] = key.text
	end
	list:setElements(elements, currentPos)
end

KeyConfig = class()

-- Callback when object is added to the world
function KeyConfig:start()
	print('KeyConfig:start()')
	local realKeyListNode = Scene_instantiate(self.scrolling_list)
	WorldNodeManager_addHierarchyToNode(self:get{'select_real_key', 'mask_left'}, realKeyListNode)
	WorldNode_setLocalPosition(realKeyListNode, -7, 0)
	self.real_key_list = ScriptComponent_getScriptTable(WorldNode_getComponentByTypeName(realKeyListNode, 'script'))
	self.real_key_list.camera = self.camera
	
	local virtualKeyListNode = Scene_instantiate(self.scrolling_list)
	WorldNodeManager_addHierarchyToNode(self:get{'select_virtual_key', 'mask_right'}, virtualKeyListNode)
	WorldNode_setLocalPosition(virtualKeyListNode, 7, 0)
	self.virtual_key_list = ScriptComponent_getScriptTable(WorldNode_getComponentByTypeName(virtualKeyListNode, 'script'))
	self.virtual_key_list.camera = self.camera
	
	self.animator = WorldNode_getComponentByTypeName(self.worldNode, 'animator')
	self.clicked = {}
	self.thread = coroutine.create(self.threadFunc)

	self.button_select_controller_close = self:getScriptTable({'select_controller', 'button_close'}, 'GUIButtonNoPressedEnter')
	self.button_select_real_key_back = self:getScriptTable({'select_real_key', 'button_back'}, 'GUIButtonNoPressedEnter')
	self.button_select_real_key_reset = self:getScriptTable({'select_real_key', 'button_reset'}, 'GUIButtonNoPressedEnter')
	self.button_select_real_key_change = self:getScriptTable({'select_real_key', 'button_change'}, 'GUIButtonNoPressedEnter')
	self.button_select_virtual_key_cancel = self:getScriptTable({'select_virtual_key', 'button_cancel'}, 'GUIButtonNoPressedEnter')
	self.button_select_virtual_key_confirm  = self:getScriptTable({'select_virtual_key', 'button_confirm'}, 'GUIButtonNoPressedEnter')
    self.button_reset_cancel = self:getScriptTable({'confirm_reset', 'button_cancel'}, 'GUIButtonNoPressedEnter')
    self.button_reset_left = self:getScriptTable({'confirm_reset', 'button_l'}, 'GUIButton')
    self.button_reset_center = self:getScriptTable({'confirm_reset', 'button_c'}, 'GUIButton')
    self.button_reset_right = self:getScriptTable({'confirm_reset', 'button_r'}, 'GUIButton')
    self.reset_buttons = {self.button_reset_left, self.button_reset_center, self.button_reset_right}


 	self.configurableControllers = g_configurableControllers
 	for key, controller in pairs(g_configurableControllers) do 
 		self.configurableControllers[key].controller_node = self:get(controller.controller_world_node)
 		self.configurableControllers[key].controller_script = self:get(controller.controller_world_node, 'script')
 		self.configurableControllers[key].controller_script.camera = self.camera
 	end

	self.controllerN64 = {
		node = self:get(g_controllerN64.controller_world_node),
		script = self:get(g_controllerN64.controller_world_node, 'script')
	}
	self.controllerN64.script.camera = self.camera
	
	self.linkLine = self:getScriptTable({'select_virtual_key', 'link_line'}, 'LinkLine')

	self.main_page = self:get{'..', 'main_page'}
	self.select_controller = self:get{'select_controller'}
	self.select_real_key = self:get{'select_real_key'}
	self.select_virtual_key = self:get{'select_virtual_key'}
	self.confirm_reset = self:get{'confirm_reset'}

	self.drc_controller_node = self:get{'select_controller', 'drc_controller'}
	self.drc_controller_scale_node = self:get{'select_controller', 'drc_controller', 'scale'}
	self.drc_controller_label = self:get({'select_controller', 'drc_controller', 'scale','Label'}, 'label')
	self.drc_controller_button = self:getScriptTable({'select_controller', 'drc_controller', 'scale', 'drc_button' }, 'GUIButton')

	self.p1_controller_node = self:get{'select_controller', 'p1_controller'}
	self.p1_controller_scale_node = self:get{'select_controller', 'p1_controller', 'scale'}
	self.p1_cla_controller_node = self:get{'select_controller', 'p1_controller', 'scale', 'cla_button'}
	self.p1_urcc_controller_node = self:get{'select_controller', 'p1_controller', 'scale', 'urcc_button'}
	self.p1_cla_controller_button = self:getScriptTable({'select_controller', 'p1_controller', 'scale', 'cla_button' }, 'GUIButton')
	self.p1_urcc_controller_button = self:getScriptTable({'select_controller', 'p1_controller', 'scale', 'urcc_button' }, 'GUIButton')

	self.p2_controller_node = self:get{'select_controller', 'p2_controller'}
	self.p2_controller_scale_node = self:get{'select_controller', 'p2_controller', 'scale'}
	self.p2_cla_controller_node = self:get{'select_controller', 'p2_controller', 'scale', 'cla_button'}
	self.p2_urcc_controller_node = self:get{'select_controller', 'p2_controller', 'scale', 'urcc_button'}
	self.p2_cla_controller_button = self:getScriptTable({'select_controller', 'p2_controller', 'scale', 'cla_button' }, 'GUIButton')
	self.p2_urcc_controller_button = self:getScriptTable({'select_controller', 'p2_controller', 'scale', 'urcc_button' }, 'GUIButton')

	self.p3_controller_node = self:get{'select_controller', 'p3_controller'}
	self.p3_controller_scale_node = self:get{'select_controller', 'p3_controller', 'scale'}
	self.p3_cla_controller_node = self:get{'select_controller', 'p3_controller', 'scale', 'cla_button'}
	self.p3_urcc_controller_node = self:get{'select_controller', 'p3_controller', 'scale', 'urcc_button'}
	self.p3_cla_controller_button = self:getScriptTable({'select_controller', 'p3_controller', 'scale', 'cla_button' }, 'GUIButton')
	self.p3_urcc_controller_button = self:getScriptTable({'select_controller', 'p3_controller', 'scale', 'urcc_button' }, 'GUIButton')

	self.p4_controller_node = self:get{'select_controller', 'p4_controller'}
	self.p4_controller_scale_node = self:get{'select_controller', 'p4_controller', 'scale'}
	self.p4_cla_controller_node = self:get{'select_controller', 'p4_controller', 'scale', 'cla_button'}
	self.p4_urcc_controller_node = self:get{'select_controller', 'p4_controller', 'scale', 'urcc_button'}
	self.p4_cla_controller_button = self:getScriptTable({'select_controller', 'p4_controller', 'scale', 'cla_button' }, 'GUIButton')
	self.p4_urcc_controller_button = self:getScriptTable({'select_controller', 'p4_controller', 'scale', 'urcc_button' }, 'GUIButton')

	self.select_controller_nodes = {self.drc_controller_node, self.p1_controller_node, self.p2_controller_node, self.p3_controller_node, self.p4_controller_node}
	self.select_controller_scale_nodes = {self.drc_controller_scale_node, self.p1_controller_scale_node, self.p2_controller_scale_node, self.p3_controller_scale_node, self.p4_controller_scale_node }
	self.select_controller_drc_label_texts = {"SYS_PLAYER_SELECT_1P", "SYS_PLAYER_SELECT_2P", "SYS_PLAYER_SELECT_3P", "SYS_PLAYER_SELECT_4P"}
	self.select_controller_buttons = {	
										{self.drc_controller_button}, 
										{self.p1_cla_controller_button, self.p1_urcc_controller_button},
										{self.p2_cla_controller_button, self.p2_urcc_controller_button},
										{self.p3_cla_controller_button, self.p3_urcc_controller_button},
										{self.p4_cla_controller_button, self.p4_urcc_controller_button}
									}
	self.select_controller_avilable = {}
	self.anims_show_controller = {self.anim_show_drc_controller, self.anim_show_p1_controller, self.anim_show_p2_controller, self.anim_show_p3_controller, self.anim_show_p4_controller }
	self.anims_hide_controller = {self.anim_hide_drc_controller, self.anim_hide_p1_controller, self.anim_hide_p2_controller, self.anim_hide_p3_controller, self.anim_hide_p4_controller }

	WorldNode_setEnabled(self.worldNode, false)
	WorldNode_setEnabled(self.select_controller, false)
	WorldNode_setEnabled(self.select_real_key, false)
	WorldNode_setEnabled(self.select_virtual_key, false)
	WorldNode_setEnabled(self.confirm_reset, false)
	WorldNode_setEnabled(self.controllerN64.node, false)
	for _, controller in pairs(self.configurableControllers) do
		WorldNode_setEnabled(controller.controller_node, false)
	end
	self.current_dialog = nil
end

-- Callback when object is removed from the world
function KeyConfig:stop()
end

function KeyConfig:transition(from, to, anim, speed)
	WorldNode_setEnabled(to, true)
	if anim then
		self:playAnim(anim, speed)
	end
	WorldNode_setEnabled(from, false)
end

function KeyConfig:get(worldNodePath, componentType)
	return get(self.worldNode, worldNodePath, componentType)
end

function KeyConfig:getScriptTable(worldNodePath, scriptType)
	return getScript(self.worldNode, worldNodePath, scriptType)
end

function KeyConfig:setDefaultConfig(controllerType, playerId, resetPoseIndex)
	local bindings = {}
	for _, key in ipairs(g_configurableControllers[controllerType].keys) do
		bindings[#bindings + 1] = key.default[resetPoseIndex]
	end
	if controllerType == "drc" then
		g_keyConfigurations[2*playerId - 1] = bindings
		VC_setKeyConfigMap(2*playerId - 1, bindings)
	else
		g_keyConfigurations[2*playerId] = bindings
		VC_setKeyConfigMap(2*playerId, bindings)
	end
	VC_saveKeyConfigFile()
end

function KeyConfig:playAnim(anim, speed)
	speed = speed or 1
	Component_enable(self.animator)
	AnimatorComponent_setSceneAnimation(self.animator, anim, false)
	AnimatorComponent_setSpeed(self.animator, speed)
	AnimatorComponent_stop(self.animator)
	AnimatorComponent_play(self.animator)
	while not AnimatorComponent_isStopped(self.animator) do
		Input_freeze()
		coroutine.yield()
	end
	Component_disable(self.animator)
end

function KeyConfig:confirmReset()

	local resetPoseIndex = 2
	GUI:focusElement(self.reset_buttons[resetPoseIndex])
	self.lost_focus_index = nil
	GUI.unfocusedElementByTouched = nil
	while true do
		if (GUI:isCancelPressed(g_menu.invokerDevice) or self.clicked[self.button_reset_cancel]) and  GUI.pressedCommandTarget == nil then
			self.lost_focus_index = nil
			GUI:focusElement(nil)
			if not self.clicked[self.button_reset_cancel] then
				VC_playSoundId(SOUND_KEY_CANCEL)
			else
				VC_playSoundId(SOUND_CANCEL)
			end

			self.button_reset_cancel:playClickedAnimation()
			while not AnimatorComponent_isStopped(self.button_reset_cancel.clickedAnimation) do
				Input_freeze()
				coroutine.yield()
			end
			return false
		end

		local resetKey = false

		if GUI:isLeftPressed(g_menu.invokerDevice) and GUI.pressedCommandTarget == nil then
			if self.lost_focus_index then
				resetPoseIndex = self.lost_focus_index
			else	
				resetPoseIndex = ((resetPoseIndex - 1) - 1) % 3 + 1
			end
			self.lost_focus_index = nil
			GUI.unfocusedElementByTouched = nil
			GUI:focusElement(self.reset_buttons[resetPoseIndex])
			VC_playSoundId(SOUND_SELECT)
		end
		
		if GUI:isRightPressed(g_menu.invokerDevice) and GUI.pressedCommandTarget == nil then
			if self.lost_focus_index then 	
				resetPoseIndex = self.lost_focus_index
			else
				resetPoseIndex = ((resetPoseIndex + 1) - 1) % 3 + 1
			end
			self.lost_focus_index = nil
			GUI.unfocusedElementByTouched = nil
			GUI:focusElement(self.reset_buttons[resetPoseIndex])
			VC_playSoundId(SOUND_SELECT)
		end
		
		if GUI.unfocusedElementByTouched ~= nil then
			self.lost_focus_index = resetPoseIndex
			GUI:focusElement(nil)
		end

		for index = 1, 3 do
			if self.clicked[self.reset_buttons[index]] then
				resetPoseIndex = index
				resetKey = true
			end
		end
		
		if (GUI:isValidatePressed(g_menu.invokerDevice) or resetKey) and GUI.pressedCommandTarget == nil then
			self.lost_focus_index = nil
			GUI:focusElement(nil)
			if not resetKey then
				self.reset_buttons[resetPoseIndex]:playClickedAnimation()
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
				VC_playSoundId(SOUND_DECIDE_SUB)
			end
			return resetPoseIndex
		end

		coroutine.yield()
	end
end

function KeyConfig:selectVirtualKey(bindableKeys)
	while true do

		if self.virtual_key_list.changed then
			self.controllerN64.script.target = bindableKeys[clamp(math.floor(self.virtual_key_list.currentPos + 0.5), 1, #bindableKeys)]
			self.controllerN64.script.changed = true
			self.virtual_key_list.changed = false
		end
		
		if (GUI:isCancelPressed(g_menu.invokerDevice) or self.clicked[self.button_select_virtual_key_cancel]) and GUI.pressedCommandTarget == nil and not self.virtual_key_list.drag then
			if not self.clicked[self.button_select_virtual_key_cancel] then
				VC_playSoundId(SOUND_KEY_CANCEL)
			else
				VC_playSoundId(SOUND_CANCEL)
			end

			self.virtual_key_list.disabled = true
			self.button_select_virtual_key_cancel:playClickedAnimation()
			while not AnimatorComponent_isStopped(self.button_select_virtual_key_cancel.clickedAnimation) do
				Input_freeze()
				coroutine.yield()
			end
			return nil
		end
		
		if (GUI:isValidatePressed(g_menu.invokerDevice) or self.clicked[self.button_select_virtual_key_confirm] or self.virtual_key_list.isDecided) and GUI.pressedCommandTarget == nil and not self.virtual_key_list.drag then
			self.virtual_key_list.stiffness = 30
			
			if not self.clicked[self.button_select_virtual_key_confirm] and not self.virtual_key_list.isDecided then
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
				VC_playSoundId(SOUND_DECIDE_SUB)
			end
			if not self.virtual_key_list.isDecided then
				self.button_select_virtual_key_confirm:playClickedAnimation()
			end
			self.virtual_key_list.disabled = true

			if self.controllerN64.script.target ~= 14 then
				self.linkLine.startPosX, self.linkLine.startPosY = WorldNode_getWorldPosition(self.controllerN64.script.buttons[self.controllerN64.script.target])
				self.linkLine.endPosX, self.linkLine.endPosY = WorldNode_getWorldPosition(self.configingController.controller_script.buttons[self.configingController.controller_script.target])
				self.linkLine.disabled = false
				while not self.linkLine.disabled do
					self.controllerN64.script.timer = 0
					Input_freeze()
					coroutine.yield()
				end
			else
				if not self.virtual_key_list.isDecided then
					while not AnimatorComponent_isStopped(self.button_select_virtual_key_confirm.clickedAnimation) do
						Input_freeze()
						coroutine.yield()
					end
				end
			end
			
			self.virtual_key_list.isDecided = false
			
			return self.controllerN64.script.target
		end

		coroutine.yield()
	end
end

function KeyConfig:selectRealKey(controllerType, playerId)
	self.configingController = self.configurableControllers[controllerType]
	self.configingController.controller_script.editingDisabled = false
	self.configingController.controller_script.circlesDisabled = false
	self.configingController.controller_script.target = 1
	self.configingController.controller_script.current = 1
	self.controllerN64.script.circlesDisabled = false
	local currentKeyBinding , configurationIndex
	local virtualKeyList = {}
	local bindableKeys ={}
	local indexOfCurrentKeyBindingInBindableKeys
	while true do
		if self.real_key_list.changed then
			self.configingController.controller_script.target = clamp(math.floor(self.real_key_list.currentPos + 0.5), 1, #self.configingController.controller_script.buttons)
			self.configingController.controller_script.changed = true
			self.real_key_list.changed = false
		end
		
		if (GUI:isCancelPressed(g_menu.invokerDevice) or self.clicked[self.button_select_real_key_back]) and GUI.pressedCommandTarget == nil and not self.real_key_list.drag then
			if not self.clicked[self.button_select_real_key_back] then
				VC_playSoundId(SOUND_KEY_CANCEL)
			else
				VC_playSoundId(SOUND_CANCEL)
			end

			self.real_key_list.disabled = true
			self.button_select_real_key_back:playClickedAnimation()
			if self.current_dialog == nil and controllerType ~= "drc" and not self:isMenuKeyAssigned(playerId) then
				self.current_dialog = createPopupDialog(self, self.popup_config_confirm, "VCM_DIALOG_KEYVCM_NOASSIGN_GUIDE", "VCM_DIALOG_KEYVCM_NOASSIGN_NOTICE")
				self.current_dialog.popup_id = POPUP_ID_CONFIRM_NO_MENU_KEY
			else
				self.configingController.controller_script.editingDisabled = true
				self.configingController.controller_script.circlesDisabled = true
				self.controllerN64.script.editingDisabled = true
				self.controllerN64.script.circlesDisabled = true
				while not AnimatorComponent_isStopped(self.button_select_real_key_back.clickedAnimation) do
					Input_freeze()
					coroutine.yield()
				end
				self.configingController.controller_script.target = 1
				return
			end
			while self.current_dialog do
				if self.no_menu_key_confirm == "yes" then
					self.configingController.controller_script.editingDisabled = true
					self.configingController.controller_script.circlesDisabled = true
					self.controllerN64.script.editingDisabled = true
					self.controllerN64.script.circlesDisabled = true
					self.configingController.controller_script.target = 1
					self.configingController.controller_script.current = 1
					self.no_menu_key_confirm = nil
					self.current_dialog = nil
					return
				elseif self.no_menu_key_confirm == "no" then
					self.no_menu_key_confirm = nil
					self.current_dialog = nil
					self.real_key_list.disabled = false
					break
				end	
				coroutine.yield()
			end
		end

		bindableKeys = self.configurableControllers[controllerType].keys[self.configingController.controller_script.target].bindable
		virtualKeyList = {}
		for i = 1, #self.configurableControllers[controllerType].keys[self.configingController.controller_script.target].bindable do 
			virtualKeyList[i] = g_controllerN64.keys[bindableKeys[i]]
		end

		if controllerType == "drc" then
			configurationIndex = 2*playerId - 1
			currentKeyBinding = g_keyConfigurations[configurationIndex][self.configingController.controller_script.target]

		else
			configurationIndex = 2*playerId
			currentKeyBinding = g_keyConfigurations[configurationIndex][self.configingController.controller_script.target]
		end

		self.controllerN64.script.target = currentKeyBinding
		self.controllerN64.script.current = currentKeyBinding
 
		if (GUI:isValidatePressed(g_menu.invokerDevice) or self.clicked[self.button_select_real_key_change]  or self.real_key_list.isDecided) and GUI.pressedCommandTarget == nil and not self.real_key_list.drag then
		
			self.real_key_list.stiffness = 30

			for i = 1, #bindableKeys do
				if currentKeyBinding == bindableKeys[i] then indexOfCurrentKeyBindingInBindableKeys = i end
			end

			setListElements(self.virtual_key_list, virtualKeyList, indexOfCurrentKeyBindingInBindableKeys)
			
			if not self.clicked[self.button_select_real_key_change] and not self.real_key_list.isDecided then
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
				VC_playSoundId(SOUND_DECIDE_SUB)
			end

			if not self.real_key_list.isDecided then
				self.button_select_real_key_change:playClickedAnimation()
			end
		
			self.real_key_list.disabled = true
			self.virtual_key_list.disabled = true

			if not self.real_key_list.isDecided then
				while not AnimatorComponent_isStopped(self.button_select_real_key_change.clickedAnimation) do
					Input_freeze()
					coroutine.yield()
				end
			end

			self.configingController.controller_script.editingDisabled = true
			self.configingController.controller_script.circlesDisabled = true
			self.controllerN64.script.circlesDisabled = true
			
			self.real_key_list.isDecided = false
			
			self:transition(self.select_real_key, self.select_virtual_key, self.transition_selectrealkey_selectvirtualkey)
			
			self.virtual_key_list.disabled = false

			self.configingController.controller_script.circlesDisabled = false
			self.controllerN64.script.editingDisabled = false
			self.controllerN64.script.circlesDisabled = false

			local newVirtualKey = self:selectVirtualKey(bindableKeys)
			
			self.configingController.controller_script.editingDisabled = false
			self.configingController.controller_script.circlesDisabled = true
			self.controllerN64.script.editingDisabled = true
			self.controllerN64.script.circlesDisabled = true

			if newVirtualKey then
				g_keyConfigurations[configurationIndex][self.configingController.controller_script.target] = newVirtualKey
				VC_setKeyConfig(configurationIndex, self.configingController.controller_script.target, newVirtualKey)
				VC_saveKeyConfigFile()
			end
			
			self:transition(self.select_virtual_key, self.select_real_key, self.transition_selectrealkey_selectvirtualkey, -1)
			
			self.real_key_list.disabled = false
			self.configingController.controller_script.circlesDisabled = false			
			self.controllerN64.script.circlesDisabled = false

		end
	
		if (GUI:isSelectPressed(g_menu.invokerDevice) or self.clicked[self.button_select_real_key_reset]) and GUI.pressedCommandTarget == nil and not self.real_key_list.drag then
			self.real_key_list.stiffness = 30
			self.configingController.controller_script.editingDisabled = true
			self.configingController.controller_script.circlesDisabled = true
			self.controllerN64.script.circlesDisabled = true
			if not self.clicked[self.button_select_real_key_reset] then
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
				VC_playSoundId(SOUND_DECIDE_SUB)
			end
			-- VC_playSound(g_menu.sound_select, 1.0)
			self.real_key_list.disabled = true
			self.button_select_real_key_reset:playClickedAnimation()
			while not AnimatorComponent_isStopped(self.button_select_real_key_reset.clickedAnimation) do
				Input_freeze()
				coroutine.yield()
			end
			self:transition(self.select_real_key, self.confirm_reset, self.transition_selectrealkey_confirmreset)
			WorldNode_setEnabled(self.controllerN64.node, false)
			WorldNode_setEnabled(self.configingController.controller_node, false)
			local resetPoseIndex = self:confirmReset()
			if resetPoseIndex then
				self:setDefaultConfig(controllerType, playerId, resetPoseIndex)
				setListElements(self.real_key_list, self.configurableControllers[controllerType].keys)
				self.configingController.controller_script.target = 1
				self.configingController.controller_script.current = 1
			end
			WorldNode_setEnabled(self.configingController.controller_node, true)
			WorldNode_setEnabled(self.controllerN64.node, true)
			self:transition(self.confirm_reset, self.select_real_key, self.transition_selectrealkey_confirmreset, -1)
			self.real_key_list.disabled = false
			self.configingController.controller_script.editingDisabled = false
			self.configingController.controller_script.circlesDisabled = false
			self.controllerN64.script.circlesDisabled = false
		end
		
		coroutine.yield()
	end
end

function KeyConfig:selectController()
	local selectedIndex = g_menu.invokerIndex
	GUI.unfocusedElementByTouched = nil
	GUI:focusElement(self.select_controller_avilable[selectedIndex])
	local target_playerId_texts = { "SYS_PLAYER_TARGET_1P", "SYS_PLAYER_TARGET_2P", "SYS_PLAYER_TARGET_3P", "SYS_PLAYER_TARGET_4P"}
	while true do
		if (GUI:isCancelPressed(g_menu.invokerDevice) or self.clicked[self.button_select_controller_close]) and GUI.pressedCommandTarget == nil then
			self.lost_focus_index = nil
			GUI:focusElement(nil)
			if not self.clicked[self.button_select_controller_close] then
				VC_playSoundId(SOUND_KEY_CANCEL)
			else
				VC_playSoundId(SOUND_CANCEL)
			end
			-- VC_playSound(g_menu.sound_cancel, 1.0)
			self.button_select_controller_close:playClickedAnimation()
			while not AnimatorComponent_isStopped(self.button_select_controller_close.clickedAnimation) do
				self:controllersLayoutUpdate(self.dt)
				Input_freeze()
				coroutine.yield()
			end
			self:controllersLayoutUpdate(self.dt)
			return
		end
		self.controllersChanged  = self:controllersStatusChanged()
		self.controllers_targetPos, self.controllers_targetScale = self:layoutControllers()

		for i = 1, 5 do
			if self.controllersChanged[i] == -1 then
				self:hideController(i)
				self.controller_hide_delay = 0.2
			elseif self.controllersChanged[i] == 1 then
				self:showController(i)
				self.controllers_currentPos[i] = self.controllers_targetPos[i] 
				self.delay = 0
			elseif self.controllersChanged[i] == 2 then
				self:switchController(i)
			else
			end
		end
	
		if self.controller_hide_delay > 0 then
			self.controller_hide_delay = self.controller_hide_delay - self.dt
		else
			self:controllersLayoutUpdate(self.dt)
		end
		
		if GUI:isLeftPressed(g_menu.invokerDevice) and GUI.pressedCommandTarget == nil then
			if self.lost_focus_index and self.select_controller_avilable[self.lost_focus_index] ~= nil then
				selectedIndex = self.lost_focus_index
			else
				while self.select_controller_avilable[((selectedIndex - 1) - 1) % 5 + 1] == nil do
					selectedIndex = selectedIndex - 1
				end
				selectedIndex = ((selectedIndex - 1) - 1) % 5 + 1
			end
			self.lost_focus_index = nil

			if GUI.focusedElement ~= self.select_controller_avilable[selectedIndex] then
				GUI.unfocusedElementByTouched = nil
				GUI:focusElement(self.select_controller_avilable[selectedIndex])
				VC_playSoundId(SOUND_SELECT)
			end
		end
		
		if GUI:isRightPressed(g_menu.invokerDevice) and GUI.pressedCommandTarget == nil then
			if self.lost_focus_index and self.select_controller_avilable[self.lost_focus_index] ~= nil then
				selectedIndex = self.lost_focus_index
			else
				while self.select_controller_avilable[((selectedIndex + 1) - 1) % 5 + 1] == nil do
					selectedIndex = selectedIndex + 1
				end
				selectedIndex = ((selectedIndex + 1) - 1) % 5 + 1
			end
			self.lost_focus_index = nil
			if GUI.focusedElement ~= self.select_controller_avilable[selectedIndex] then
				GUI.unfocusedElementByTouched = nil
				GUI:focusElement(self.select_controller_avilable[selectedIndex])
				VC_playSoundId(SOUND_SELECT)
			end
		end
		
		if self.select_controller_avilable[selectedIndex] == nil or GUI.unfocusedElementByTouched ~= nil then
			self.lost_focus_index = selectedIndex
			if GUI.focusedElement ~= nil then
				GUI:focusElement(nil)
			end
		end
		
		local enterSelectRealKey = false
		for index = 1, 5 do
			if self.clicked[self.select_controller_avilable[index]] then
				selectedIndex = index
				enterSelectRealKey = true
			end
		end

		if (GUI:isValidatePressed(g_menu.invokerDevice) and GUI.focusedElement ~= nil ) or enterSelectRealKey then
			g_keyConfigurations = VC_getKeyConfigurations()
			self.lost_focus_index = nil
			if not enterSelectRealKey then
				VC_playSoundId(SOUND_KEY_DECIDE_SUB)
			else
				VC_playSoundId(SOUND_DECIDE_SUB)
			end
			-- VC_playSound(g_menu.sound_select, 1.0)
			self.select_controller_avilable[selectedIndex]:playClickedAnimation()
			while not AnimatorComponent_isStopped(self.select_controller_avilable[selectedIndex].clickedAnimation) do
				Input_freeze()
				coroutine.yield()
			end
			WorldNode_setEnabled(self.controllerN64.node, true)
			self.controllerN64.script.editingDisabled = true
			setListElements(self.real_key_list, self.configurableControllers[self.controllers[selectedIndex].controller_type].keys)
			local controller_node = self.configurableControllers[self.controllers[selectedIndex].controller_type].controller_node
			WorldNode_setEnabled(controller_node, true)
			
			local playerId = self.controllers[selectedIndex].playerId
			if not playerId then
				playerId = selectedIndex - 1
			end
			local selectRealKeyPlayerIdLabel =  self:get({'select_real_key', 'title', 'player_id_circle'}, 'label')
			local selectVirtualPlayerIdLabel = 	self:get({'select_virtual_key', 'title', 'player_id_circle'}, 'label')
			local resetPlayerIdLabel = 	self:get({'confirm_reset', 'title', 'player_id_circle'}, 'label')
			LabelComponent_setText(selectRealKeyPlayerIdLabel, Localization_getText(target_playerId_texts[playerId]))
			LabelComponent_setText(selectVirtualPlayerIdLabel, Localization_getText(target_playerId_texts[playerId]))
			LabelComponent_setText(resetPlayerIdLabel, Localization_getText(target_playerId_texts[playerId]))
			self.real_key_list.disabled = true

			self:transition(self.select_controller, self.select_real_key, self.transition_selectcontroller_selectrealkey)

			GUI:focusElement(nil)
			self.real_key_list.disabled = false

			self:selectRealKey(self.controllers[selectedIndex].controller_type, playerId)
			
			self:initControllers()
			
			self:transition(self.select_real_key, self.select_controller, self.transition_selectcontroller_selectrealkey, -1)

			WorldNode_setEnabled(controller_node, false)
			WorldNode_setEnabled(self.controllerN64.node, false)
			
			GUI.unfocusedElementByTouched = nil
			
			if self.select_controller_avilable[selectedIndex] then
				GUI:focusElement(self.select_controller_avilable[selectedIndex])
			else
				self.lost_focus_index = g_menu.invokerIndex
			end

		end
		coroutine.yield()
	end
end

function KeyConfig:threadFunc()
	while true do
		self:initControllers()
		self:transition(self.main_page, self.select_controller, self.transition_mainpage_selectcontroller)
		self:selectController()
		g_menu.main_page_script.is_active = true
		self:transition(self.select_controller, self.main_page, self.transition_mainpage_selectcontroller, -1)
		WorldNode_setEnabled(self.worldNode, false)
		g_menu.main_page_script:enableClickables(true)
		coroutine.yield()
	end
end

-- Callback called every frames
function KeyConfig:update(dt)
	self.dt = dt
	if self.thread and coroutine.status(self.thread) ~= 'dead' then
		local success, errorMsg = coroutine.resume(self.thread, self)
		if not success then
			error(errorMsg)
		end
	end
	
	while next(self.clicked) do
		self.clicked[next(self.clicked)] = nil
	end
end

function KeyConfig:initControllers()
	self.controllers = copyTab(connectedControllers)
	self.controllers_currentPos, self.controllers_currentScale = self:layoutControllers()
	self.controllers_targetPos = copyTab(self.controllers_currentPos)
	self.controllers_currentPosVel = {0, 0, 0, 0, 0}
	self.controllers_targetScale = self.controllers_currentScale
	self.controllers_currentScaleVel = 0
	self.controllersChanged = {}
	self.controller_hide_delay = 0
	for i = 1, 5 do
		WorldNode_setLocalPosition(self.select_controller_nodes[i], self.controllers_currentPos[i], -235)
		WorldNode_setLocalScale(self.select_controller_scale_nodes[i], self.controllers_currentScale, self.controllers_currentScale) 
	end
	self:showControllers()
end

function KeyConfig:showControllers()
	local cla_node
	local urcc_node
	self.select_controller_avilable = {}
	for i = 1, 5 do
		AnimatorComponent_stop(self.anims_show_controller[i])
		AnimatorComponent_stop(self.anims_hide_controller[i])
		if (next(self.controllers[i])) then
			if i == 1 then
				WorldNode_setEnabled(self.drc_controller_node, true)
				self.select_controller_avilable[1] = self.select_controller_buttons[1][1]
				LabelComponent_setText(self.drc_controller_label, Localization_getText(self.select_controller_drc_label_texts[self.controllers[1].playerId]))
			else
				WorldNode_setEnabled(self.select_controller_nodes[i], true)
				cla_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "cla_button")
				urcc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "urcc_button")
				if self.controllers[i].controller_type == "cla" then
					WorldNode_setEnabled(cla_node, true)
					WorldNode_setEnabled(urcc_node, false)
					self.select_controller_avilable[i] = self.select_controller_buttons[i][1]
				elseif self.controllers[i].controller_type == "urcc" then
					WorldNode_setEnabled(cla_node, false)
					WorldNode_setEnabled(urcc_node, true)
					self.select_controller_avilable[i] = self.select_controller_buttons[i][2]
				end
			end
			AnimatorComponent_setCurrentTime(self.anims_show_controller[i], 0.4)
		else
			WorldNode_setEnabled(self.select_controller_nodes[i], false)
		end
	end
end

function KeyConfig:showController(i)
	local cla_node
	local urcc_node
	if i == 1 then
		WorldNode_setEnabled(self.drc_controller_node, true)
		self.select_controller_avilable[1] = self.select_controller_buttons[1][1]
		LabelComponent_setText(self.drc_controller_label, Localization_getText(self.select_controller_drc_label_texts[self.controllers[1].playerId]))
	else
		WorldNode_setEnabled(self.select_controller_nodes[i], true)
		cla_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "cla_button")
		urcc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "urcc_button")
		if self.controllers[i].controller_type == "cla" then
			WorldNode_setEnabled(cla_node, true)
			WorldNode_setEnabled(urcc_node, false)
			self.select_controller_avilable[i] = self.select_controller_buttons[i][1]
		elseif self.controllers[i].controller_type == "urcc" then
			WorldNode_setEnabled(cla_node, false)
			WorldNode_setEnabled(urcc_node, true)
			self.select_controller_avilable[i] = self.select_controller_buttons[i][2]
		end
	end
	AnimatorComponent_stop(self.anims_hide_controller[i])
	self:launchAnimation(self.anims_show_controller[i])
end

function KeyConfig:hideController(i)
	self.select_controller_avilable[i] = nil
	AnimatorComponent_stop(self.anims_show_controller[i])
	self:launchAnimation(self.anims_hide_controller[i])
end

function KeyConfig:switchController(i)
	local cla_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "cla_button")
	local urcc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "urcc_button")
	if self.controllers[i].controller_type == "cla" then
		WorldNode_setEnabled(cla_node, true)
		WorldNode_setEnabled(urcc_node, false)
		self.select_controller_avilable[i] = self.select_controller_buttons[i][1]
	elseif self.controllers[i].controller_type == "urcc" then
		WorldNode_setEnabled(cla_node, false)
		WorldNode_setEnabled(urcc_node, true)
		self.select_controller_avilable[i] = self.select_controller_buttons[i][2]
	end
end

function KeyConfig:layoutControllers()
	local controller_count = 0
	local controller_widths_ref_table = { drc = 444, cla = 349, urcc = 374 }
	local controller_scale_table = {0.8, 0.8, 0.8, 0.62, 0.52}
	local gap = {120, 120, 55, 30, 24}
	local container_start_x = -600
	local container_end_x = 600
	local controller_start_x = 0
	local controller_widths = {}
	local controller_group_width = 0
	local controller_positions = {}
	for i = 1, 5 do
		if (next(self.controllers[i])) then
			controller_count = controller_count + 1
			if self.controllers[i].controller_type == "drc" then
				controller_widths[i] = controller_widths_ref_table.drc
			elseif self.controllers[i].controller_type == "cla" then
				controller_widths[i] = controller_widths_ref_table.cla
			elseif self.controllers[i].controller_type == "urcc" then
				controller_widths[i] = controller_widths_ref_table.urcc
			end
		else
			controller_widths[i] = 0
		end
		controller_group_width = controller_group_width + controller_widths[i]
	end
	if controller_count ~= 0 then
		controller_group_width = controller_group_width * controller_scale_table[controller_count] + (controller_count-1) * gap[controller_count]
		controller_start_x = (container_end_x - container_start_x - controller_group_width)/2 + container_start_x
		for i = 1, 5 do
			if controller_widths[i] ~= 0 then
				controller_positions[i] = controller_start_x + controller_widths[i]*controller_scale_table[controller_count]/2
				controller_start_x = controller_positions[i] + controller_widths[i]*controller_scale_table[controller_count]/2 + gap[controller_count]
			else
				controller_positions[i] = 0
			end
		end
		return controller_positions, controller_scale_table[controller_count]
	else 
		return {0, 0, 0, 0, 0}, 0
	end
end

function KeyConfig:controllersStatusChanged()
	local result = {}
	for i = 1, 5 do 
		if self.controllers[i].controller_type ~= connectedControllers[i].controller_type then
			if self.controllers[i].controller_type == nil then
				result[i] = 1
			elseif connectedControllers[i].controller_type == nil then
				result[i] = -1
			else
				result[i] = 2
			end
		else
			result[i] = 0
		end
	end
	self.controllers = copyTab(connectedControllers)
	return result
end

function KeyConfig:controllersLayoutUpdate(dt)
	local stiffness_pos = 15
	local stiffness_sca = 5
	for i = 1, 5 do
		if(next(self.controllers[i])) then
			self.controllers_currentPos[i], self.controllers_currentPosVel[i] = springSmoothUpdate(self.controllers_currentPos[i], self.controllers_currentPosVel[i], self.controllers_targetPos[i], stiffness_pos, dt)
			WorldNode_setLocalPosition(self.select_controller_nodes[i], self.controllers_currentPos[i], -235)
			self.controllers_currentScale, self.controllers_currentScaleVel = springSmoothUpdate(self.controllers_currentScale, self.controllers_currentScaleVel, self.controllers_targetScale, stiffness_sca,dt)
			WorldNode_setLocalScale(self.select_controller_scale_nodes[i], self.controllers_currentScale, self.controllers_currentScale )
		end
	end
end

function KeyConfig:launchAnimation(animator, new_state)
	Component_enable(animator)
	AnimatorComponent_play(animator)
	if new_state then
		self.state = new_state
		self.animation_controller = animator
	end
end

function KeyConfig:onElementFocus(element)
	local drc_node
	local cla_node
	local urcc_node
	for i = 1, 5 do
		if element == self.select_controller_avilable[i] then
			drc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "drc_button")
			cla_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "cla_button")
			urcc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "urcc_button")
			if drc_node ~= nil then
				WorldNode_setLocalScale(drc_node, 1.1, 1.1)
			else
				WorldNode_setLocalScale(cla_node, 1.1, 1.1)
				WorldNode_setLocalScale(urcc_node, 1.1, 1.1)
			end
		end
	end
end

function KeyConfig:onElementUnfocus(element)
	local drc_node
	local cla_node
	local urcc_node
	for i = 1, 5 do
		if element == self.select_controller_avilable[i] then
			drc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "drc_button")
			cla_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "cla_button")
			urcc_node = WorldNode_getChildByName(self.select_controller_scale_nodes[i], "urcc_button")
			if drc_node ~= nil then
				WorldNode_setLocalScale(drc_node, 1, 1)
			else
				WorldNode_setLocalScale(cla_node, 1, 1)
				WorldNode_setLocalScale(urcc_node, 1, 1)
			end
		end
	end
end

function KeyConfig:isMenuKeyAssigned(playerId)
	local configure = set(g_keyConfigurations[2*playerId])
	return configure[13]
end

function KeyConfig:onPopupClose(popup, answer)
	print("popup close with answer " .. answer)
	if answer == "ok" or answer == "yes" then
		if self.current_dialog.popup_id == POPUP_ID_CONFIRM_NO_MENU_KEY then
			self.no_menu_key_confirm = "yes"
		end
	else
		if self.current_dialog.popup_id == POPUP_ID_CONFIRM_NO_MENU_KEY then
			self.no_menu_key_confirm = "no"
		end
	end
end


-- Callback when object starts colliding with another
function KeyConfig:beginCollision(componentA, componentB)

end

-- Callback when object finishes colliding with another
function KeyConfig:endCollision(componentA, componentB)

end

-- Callback when object is a button and is pressed
function KeyConfig:onButtonClick(pressedButton)
	self.clicked[pressedButton] = true
end

-- Callback when object is a button and is released
function KeyConfig:onReleased()
end

-- Callback when object is a button and is pressed
function KeyConfig:onButtonPress(button)
	VC_playSoundId(SOUND_SELECT)
	-- VC_playSound(g_menu.sound_select, 1.0)
end

-- function KeyConfig:onPopupOpen(popup)
-- end