-- ================================================================================
-- LOCAL FUNCTIONS
-- ================================================================================

function Reset()
	down = {}
	state = {}
end -- Reset()

function GetState()
	-- Map KeyCodes to corresponding InputObjects on the gamepad:
	local inputObjects = UserInputService:GetGamepadState(GAMEPAD1)
	for _,inputObject in pairs(inputObjects) do
		state[inputObject.KeyCode] = inputObject
	end
end -- GetState()

function InputBegan(input, processed)
	if input.UserInputType == GAMEPAD1 then
		down[input.KeyCode] = true
		buttonDown:Fire(input.KeyCode)
	end
end -- InputBegan()

function InputEnded(input, processed)
	if input.UserInputType == GAMEPAD1 then
		down[input.KeyCode] = false
		buttonUp:Fire(input.KeyCode)
	end
end -- InputEnded()

function InputChanged(input, processed)
	if input.UserInputType == GAMEPAD1 then
		changed:Fire(input.KeyCode)
	end
end -- InputChanged()

function GamepadConnected(gamepad)
	if gamepad == GAMEPAD1 then
		Gamepad.IsConnected = true
		GetState()
		connected:Fire()
	end
end -- GamepadConnected

function GamepadDisconnected(gamepad)
	if gamepad == GAMEPAD1 then
		Gamepad.IsConnected = false
		Reset()
		disconnected:Fire()
	end
end -- GamepadDisconnected