inputNames={"cross", "square", "circle", "triangle", "l", "r", "start", "select", "up", "down", "left", "right"}

inputWatcher={}
for key, value in pairs(inputNames) do
	inputWatcher[value]={}
	inputWatcher[value]["timer"]=Timer.new(0)
	inputWatcher[value]["timer"]:reset()
	inputWatcher[value]["firstPress"]=false
	inputWatcher[value]["isPressed"]=false
	inputWatcher[value]["repeatTimer"]=Timer.new(0)
	inputWatcher[value]["repeatTimer"]:reset()
	inputWatcher[value]["justReleased"]=false
end
--special case for the analog stick, we just want to know if it's touched
inputWatcher["analog"]=false

inputNames=nil

--might be doable with a loop
function updateInputs(pad)		
	if pad:cross() then
		time=inputWatcher["cross"]["timer"]:time()
		if time==0 then --if he just pressed cross
			inputWatcher["cross"]["timer"]:start()
			inputWatcher["cross"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["cross"]["firstPress"]=false
		end
		inputWatcher["cross"]["isPressed"]=true
	else --doesn't push cross so stop it in the case he released it	
		inputWatcher["cross"]["timer"]:reset()
		inputWatcher["cross"]["firstPress"]=false
		if inputWatcher["cross"]["isPressed"]==true then
			inputWatcher["cross"]["justReleased"]=true
		else
			inputWatcher["cross"]["justReleased"]=false
		end
		inputWatcher["cross"]["isPressed"]=false
		inputWatcher["cross"]["repeatTimer"]:reset()
	end
	
	if pad:square() then
		time=inputWatcher["square"]["timer"]:time()
		if time==0 then --if he just pressed square
			inputWatcher["square"]["timer"]:start()
			inputWatcher["square"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["square"]["firstPress"]=false
		end
		inputWatcher["square"]["isPressed"]=true
	else --doesn't push square so stop it in the case he released it	
		inputWatcher["square"]["timer"]:reset()
		inputWatcher["square"]["firstPress"]=false
		if inputWatcher["square"]["isPressed"]==true then
			inputWatcher["square"]["justReleased"]=true
		else
			inputWatcher["square"]["justReleased"]=false
		end
		inputWatcher["square"]["isPressed"]=false
		inputWatcher["square"]["repeatTimer"]:reset()
	end
	
	if pad:triangle() then
		time=inputWatcher["triangle"]["timer"]:time()
		if time==0 then --if he just pressed triangle
			inputWatcher["triangle"]["timer"]:start()
			inputWatcher["triangle"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["triangle"]["firstPress"]=false
		end
		inputWatcher["triangle"]["isPressed"]=true
	else --doesn't push triangle so stop it in the case he released it	
		inputWatcher["triangle"]["timer"]:reset()
		inputWatcher["triangle"]["firstPress"]=false
		if inputWatcher["triangle"]["isPressed"]==true then
			inputWatcher["triangle"]["justReleased"]=true
		else
			inputWatcher["triangle"]["justReleased"]=false
		end
		inputWatcher["triangle"]["isPressed"]=false
		inputWatcher["triangle"]["repeatTimer"]:reset()
	end
	
	if pad:circle() then
		time=inputWatcher["circle"]["timer"]:time()
		if time==0 then --if he just pressed circle
			inputWatcher["circle"]["timer"]:start()
			inputWatcher["circle"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["circle"]["firstPress"]=false
		end
		inputWatcher["circle"]["isPressed"]=true
	else --doesn't push circle so stop it in the case he released it	
		inputWatcher["circle"]["timer"]:reset()
		inputWatcher["circle"]["firstPress"]=false
		if inputWatcher["circle"]["isPressed"]==true then
			inputWatcher["circle"]["justReleased"]=true
		else
			inputWatcher["circle"]["justReleased"]=false
		end
		inputWatcher["circle"]["isPressed"]=false
		inputWatcher["circle"]["repeatTimer"]:reset()
	end
	
	if pad:up() then
		time=inputWatcher["up"]["timer"]:time()
		if time==0 then --if he just pressed up
			inputWatcher["up"]["timer"]:start()
			inputWatcher["up"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["up"]["firstPress"]=false
		end
		inputWatcher["up"]["isPressed"]=true
	else --doesn't push up so stop it in the case he released it	
		inputWatcher["up"]["timer"]:reset()
		inputWatcher["up"]["firstPress"]=false
		if inputWatcher["up"]["isPressed"]==true then
			inputWatcher["up"]["justReleased"]=true
		else
			inputWatcher["up"]["justReleased"]=false
		end
		inputWatcher["up"]["isPressed"]=false
		inputWatcher["up"]["repeatTimer"]:reset()
	end
	
	if pad:down() then
		time=inputWatcher["down"]["timer"]:time()
		if time==0 then --if he just pressed down
			inputWatcher["down"]["timer"]:start()
			inputWatcher["down"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["down"]["firstPress"]=false
		end
		inputWatcher["down"]["isPressed"]=true
	else --doesn't push down so stop it in the case he released it	
		inputWatcher["down"]["timer"]:reset()
		inputWatcher["down"]["firstPress"]=false
		if inputWatcher["down"]["isPressed"]==true then
			inputWatcher["down"]["justReleased"]=true
		else
			inputWatcher["down"]["justReleased"]=false
		end
		inputWatcher["down"]["isPressed"]=false
		inputWatcher["down"]["repeatTimer"]:reset()
	end
	
	if pad:left() then
		time=inputWatcher["left"]["timer"]:time()
		if time==0 then --if he just pressed left
			inputWatcher["left"]["timer"]:start()
			inputWatcher["left"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["left"]["firstPress"]=false
		end
		inputWatcher["left"]["isPressed"]=true
	else --doesn't push left so stop it in the case he released it	
		inputWatcher["left"]["timer"]:reset()
		inputWatcher["left"]["firstPress"]=false
		if inputWatcher["left"]["isPressed"]==true then
			inputWatcher["left"]["justReleased"]=true
		else
			inputWatcher["left"]["justReleased"]=false
		end
		inputWatcher["left"]["isPressed"]=false
		inputWatcher["left"]["repeatTimer"]:reset()
	end
	
	if pad:right() then
		time=inputWatcher["right"]["timer"]:time()
		if time==0 then --if he just pressed right
			inputWatcher["right"]["timer"]:start()
			inputWatcher["right"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["right"]["firstPress"]=false
		end
		inputWatcher["right"]["isPressed"]=true
	else --doesn't push right so stop it in the case he released it	
		inputWatcher["right"]["timer"]:reset()
		inputWatcher["right"]["firstPress"]=false
		if inputWatcher["right"]["isPressed"]==true then
			inputWatcher["right"]["justReleased"]=true
		else
			inputWatcher["right"]["justReleased"]=false
		end
		inputWatcher["right"]["isPressed"]=false
		inputWatcher["right"]["repeatTimer"]:reset()
	end
	
	if pad:l() then
		time=inputWatcher["l"]["timer"]:time()
		if time==0 then --if he just pressed l
			inputWatcher["l"]["timer"]:start()
			inputWatcher["l"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["l"]["firstPress"]=false
		end
		inputWatcher["l"]["isPressed"]=true
	else --doesn't push l so stop it in the case he released it	
		inputWatcher["l"]["timer"]:reset()
		inputWatcher["l"]["firstPress"]=false
		if inputWatcher["l"]["isPressed"]==true then
			inputWatcher["l"]["justReleased"]=true
		else
			inputWatcher["l"]["justReleased"]=false
		end
		inputWatcher["l"]["isPressed"]=false
		inputWatcher["l"]["repeatTimer"]:reset()
	end
	
	if pad:r() then
		time=inputWatcher["r"]["timer"]:time()
		if time==0 then --if he just pressed r
			inputWatcher["r"]["timer"]:start()
			inputWatcher["r"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["r"]["firstPress"]=false
		end
		inputWatcher["r"]["isPressed"]=true
	else --doesn't push r so stop it in the case he released it	
		inputWatcher["r"]["timer"]:reset()
		inputWatcher["r"]["firstPress"]=false
		if inputWatcher["r"]["isPressed"]==true then
			inputWatcher["r"]["justReleased"]=true
		else
			inputWatcher["r"]["justReleased"]=false
		end
		inputWatcher["r"]["isPressed"]=false
		inputWatcher["r"]["repeatTimer"]:reset()
	end
	
	if pad:start() then
		time=inputWatcher["start"]["timer"]:time()
		if time==0 then --if he just pressed start
			inputWatcher["start"]["timer"]:start()
			inputWatcher["start"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["start"]["firstPress"]=false
		end
		inputWatcher["start"]["isPressed"]=true
	else --doesn't push start so stop it in the case he released it	
		inputWatcher["start"]["timer"]:reset()
		inputWatcher["start"]["firstPress"]=false
		if inputWatcher["start"]["isPressed"]==true then
			inputWatcher["start"]["justReleased"]=true
		else
			inputWatcher["start"]["justReleased"]=false
		end
		inputWatcher["start"]["isPressed"]=false
		inputWatcher["start"]["repeatTimer"]:reset()
	end
	
	if pad:select() then
		time=inputWatcher["select"]["timer"]:time()
		if time==0 then --if he just pressed select
			inputWatcher["select"]["timer"]:start()
			inputWatcher["select"]["firstPress"]=true
		else --it is pressed since some time
			inputWatcher["select"]["firstPress"]=false
		end
		inputWatcher["select"]["isPressed"]=true
	else --doesn't push select so stop it in the case he released it	
		inputWatcher["select"]["timer"]:reset()
		inputWatcher["select"]["firstPress"]=false
		if inputWatcher["select"]["isPressed"]==true then
			inputWatcher["select"]["justReleased"]=true
		else
			inputWatcher["select"]["justReleased"]=false
		end
		inputWatcher["select"]["isPressed"]=false
		inputWatcher["select"]["repeatTimer"]:reset()
	end
	
	if 	pad:analogY()>GAMEPREF["analogSensibility"] or pad:analogY()<-GAMEPREF["analogSensibility"]
		or pad:analogX()>GAMEPREF["analogSensibility"] or pad:analogX()<-GAMEPREF["analogSensibility"] then
		inputWatcher["analog"]=true
	else
		inputWatcher["analog"]=false
	end
	
end

function inputFirstPress(inp)
	if inputWatcher[inp]["firstPress"] then
		inputWatcher[inp]["firstPress"]=false
		return true
	end
	return false
end

function inputLongPress(inp)
	if GAMEPREF["longPress"]<=inputWatcher[inp]["timer"]:time() then
		return true
	end
	return false
end

function inputIsPressed(inp)
	return inputWatcher[inp]["isPressed"]
end

function inputIsRepeating(inp)
	if inputLongPress(inp) then
		--starts the timer if not already
		if inputWatcher[inp]["repeatTimer"]:start()>=GAMEPREF["repeatInterval"] then
			inputWatcher[inp]["repeatTimer"]:reset()
			inputWatcher[inp]["repeatTimer"]:start()
			return true
		end
	end
	return false
end

function inputJustReleased(inp)
	return inputWatcher[inp]["justReleased"]
end

function inputAnalogIsTouched()
	return inputWatcher["analog"]
end

















--should be used only for debugging purposes
function inputTimePressed(inp)
	return inputWatcher[inp]["timer"]:time()
end









