controls={}
joystick 		= {}

function controls:getJoys()
	if ljoystick and ljoystick.getJoystickCount()>0 then 
		joysticks = ljoystick.getJoysticks();
		joystick=joysticks[1];
	end
end

function controls:setMouseVars()
	mouse.clicks		= {}
	mouse.releases 	= {}
	mouse.line      = mouse.line or {}

	if #mouse.line>400 then 
		table.remove(mouse.line,#mouse.line)
		table.remove(mouse.line,#mouse.line)
	end

	local newx  		= lmouse.getRelativeMode() and screenWidth/2 or lmouse.getX()*game.resolution
	local newy  		= lmouse.getRelativeMode() and screenHeight/2 or lmouse.getY()*game.resolution

	mouse.px 			= mouse.x or newx
	mouse.py 			= mouse.y or newy
	mouse.x 			= newx or mouse.x
	mouse.y 			= newy or mouse.y
	mouse.moved   = (mouse.x-mouse.px)~=0 or (mouse.y-mouse.py)~=0
	
	if LOVE then 
		mouse.pressure= lmouse.getPressure()
	end

	mouse.change  = {x=mouse.x-mouse.px,y=mouse.y-mouse.py}
	mouse.dist  	= 	mouse.start.x and math.dist(mouse.x,mouse.y,mouse.start.x,mouse.start.y) or 0
	mouseScroll.x 		= easeTo(mouseScroll.x,0,loSpec and 8 or 2)	mouseScroll.y 		= easeTo(mouseScroll.y,0,loSpec and 8 or 2)
	
	if mouse.moved then 
		table.insert(mouse.line,1,mouse.y)
		table.insert(mouse.line,1,mouse.x)
	end

end

function controls:keysUpdate()

	keyTime = #keysDown>0 and 4 or math.max(0,keyTime-1)
	holdKeys()
	controls:setMouseVars()
	controls:toggleDeveloper()
	shiftDown 		= 	lkeyboard.isDown("lshift")	or lkeyboard.isDown("rshift")	
	altDown 		= 	lkeyboard.isDown("lalt")	or lkeyboard.isDown("ralt")		
	cmdDown 		= 	lkeyboard.isDown("lgui")	or lkeyboard.isDown("rgui")	
					or 	lkeyboard.isDown("lctrl")	or lkeyboard.isDown("rctrl")	
end

function controls:toggleDeveloper()
	if key == "=" and not editingLevel and not game.paused and not namer.enabled then
		developer = not developer;
	end
end

--uncomment to disable controller
function joystick:isDown(n) return false end 
function joystick:getAxis(n) return false end


function controls:update()

	if joystick=={} and ljoystick.getJoystickCount()>0 then 
		joysticks= ljoystick.getJoysticks()
		joystick = joysticks[1]
	else 
		joystick = joystick
	end	

	if buttons2~=nil then 
		buttons = {	
			 ["upButtonDown"]  			= upButtonDown  		 		
			,["downButtonDown"]  		= downButtonDown  	 		
			,["leftButtonDown"]  		= leftButtonDown  	 		
			,["rightButtonDown"]  		= rightButtonDown  	 	
			,["aButtonDown"]  			= aButtonDown  		 		
			,["bButtonDown"]  			= bButtonDown  		 		
			,["xButtonDown"]  			= xButtonDown  		 		
			,["yButtonDown"]  			= yButtonDown  		 		
			,["selectButtonDown"]  		= selectButtonDown  	 	
			,["startButtonDown"]  		= startButtonDown  	 	
			,["rightTrigButtonDown"]	= rightTrigButtonDown 
			,["leftTrigButtonDown"] 	= leftTrigButtonDown 
		}
	end

	--leftAnalogClicked	= joystick:isDown(2) or lkeyboard.isDown("1")
	--rightAnalogClicked= joystick:isDown(3) or lkeyboard.isDown("2")

	leftAnalogVert 		= (joystick:getAxis( 2 ) and math.abs(joystick:getAxis( 2 ))>0.1) and joystick:getAxis( 2 ) or ((upButtonDown 	) and -1 or (downButtonDown and 1 or 0))
	leftAnalogHori 		= (joystick:getAxis( 1 ) and math.abs(joystick:getAxis( 1 ))>0.1) and joystick:getAxis( 1 ) or ((leftButtonDown 	) and -1 or (rightButtonDown and 1 or 0))
	--rightAnalogVert 	= joystick:getAxis( 2 ) 
	--rightAnalogHori 	= joystick:getAxis( 1 )

	upButtonDown 		= (joystick:getAxis( 2 ) and joystick:getAxis( 2 )<-0.5 )	or joystick:isDown(5) or keyDown("up")
	downButtonDown 		= (joystick:getAxis( 2 ) and joystick:getAxis( 2 )>0.5 	)	or joystick:isDown(7) or keyDown("down") 
	leftButtonDown 		= (joystick:getAxis( 1 ) and joystick:getAxis( 1 )<-0.5 )	or joystick:isDown(8) or keyDown("left") 
	rightButtonDown 	= (joystick:getAxis( 1 ) and joystick:getAxis( 1 )>0.5  )	or joystick:isDown(6) or keyDown("right")

	aButtonDown 		= joystick:isDown(15) or  lkeyboard.isDown("a") 
	bButtonDown 		= joystick:isDown(14) or  lkeyboard.isDown("s")
	xButtonDown 		= joystick:isDown(16) or  lkeyboard.isDown("q") 
	yButtonDown 		= joystick:isDown(13) or  lkeyboard.isDown("w") 
	selectButtonDown 	= joystick:isDown(1) or lkeyboard.isDown("backspace")
	startButtonDown 	= joystick:isDown(4) or lkeyboard.isDown("return")
	rightTrigButtonDown = joystick:isDown(12) or joystick:isDown(10)or lkeyboard.isDown("rshift")
	leftTrigButtonDown 	= joystick:isDown(11) or joystick:isDown(9)	or lkeyboard.isDown("space")

	buttons2 = {		
		 ["upButtonDown"]  			= upButtonDown 		 		
		,["downButtonDown"]  		= downButtonDown  	 		
		,["leftButtonDown"]  		= leftButtonDown  	 		
		,["rightButtonDown"]  		= rightButtonDown  	 	
		,["aButtonDown"]  			= aButtonDown  		 		
		,["bButtonDown"]  			= bButtonDown  		 		
		,["xButtonDown"]  			= xButtonDown  		 		
		,["yButtonDown"]  			= yButtonDown  		 		
		,["selectButtonDown"]  		= selectButtonDown  	 	
		,["startButtonDown"]  		= startButtonDown  	 	
		,["rightTrigButtonDown"]	= rightTrigButtonDown 
		,["leftTrigButtonDown"] 	= leftTrigButtonDown  
	}

	if buttons~=nil  then
		for k,b in pairs(buttons2) do 
			local str
			if buttons[k] == false and b==true then 
				str = string.gsub(tostring(k),"ButtonDown","").."ButtonPressed"	;
				controls[ str	] = true 
				--log(str)
			end
			if buttons[k] == true and b==false then 
				str = string.gsub(tostring(k),"ButtonDown","").."ButtonReleased";
				controls[ str] = true	 
				--log(str)
			end
			if buttons[k] == true and b==true then 
				controls[ string.gsub(tostring(k),"ButtonDown","").."ButtonPressed"	] = false  
				controls[ string.gsub(tostring(k),"ButtonDown","").."ButtonReleased"] = false	 
			end
		end
	end
	controls.leftAnalogVert=leftAnalogVert
	controls.leftAnalogHori=leftAnalogHori
end

function controls:reset()
	if buttons~=nil  then
		for k,b in pairs(buttons2) do 
			controls[ string.gsub(tostring(k),"ButtonDown","").."ButtonPressed"	] = false  
			controls[ string.gsub(tostring(k),"ButtonDown","").."ButtonReleased"] = false	 
		end
	end
end

function controls:pushButton(button,kind,func)

end

function controls:controlZoom()

	if leftAnalogClicked or (cmdDown and keypressed("p")) then 
		if not exactScaleTog then
			exactScale=not exactScale;
		end 
		exactScaleToggle=true 
	else 
		exactScaleToggle=false 
	end
	
	exactScale=false
	
end
