
#include "Utilities.agc"

global rawMouseX as float
global rawMouseY as float
global lockMouse as integer
global mouseSet as integer
global cameraAngleX as float
global cameraAngleY as float
global oldCamAngleX as float
global oldCamAngleY as float

function FreeFlightCameraUsingMouseAndArrowKeys(  cameraID as integer, speed as float)
	if GetMouseExists() and lockMouse = 0 
		rawMouseX = 0.0
		rawMouseY = 0.0
		if mouseSet = 1
			rawMouseX = ( GetDeviceWidth() / 2 ) -  GetRawMouseX() 
			rawMouseY = ( GetDeviceHeight() / 2 ) -  GetRawMouseY() 
			oldCamAngleX = cameraAngleX
			oldCamAngleY = cameraAngleY
			cameraAngleY = WrapAngle( cameraAngleY - rawMouseX * 0.2)
			cameraAngleX = WrapAngle( cameraAngleX - rawMouseY * 0.2)
			if cameraAngleX <= 290.0 and cameraAngleX > 180.0  then cameraAngleX = 290.0
			if cameraAngleX >= 70.0 and cameraAngleX < 180.0 then cameraAngleX = 70.0
			cameraAngleX = CurveAngle( cameraAngleX, oldCamAngleX, speed )
			cameraAngleY = CurveAngle( cameraAngleY, oldCamAngleY, speed )
		endif
		SetRawMousePosition( GetDeviceWidth() / 2, GetDeviceHeight() / 2 )
		//SetRawMouseVisible does not work correctly bug in AGK
		//SetRawMouseVisible( 0 )
		mouseSet = 1
	endif
	SetCameraRotation( cameraID, cameraAngleX, cameraAngleY, 0.0 )
	if GetRawKeyReleased( 27 ) 
		lockMouse = lockMouse + 1
		if lockMouse = 2 then lockMouse = 0
		if GetMouseExists()  and lockMouse = 1 
			//SetRawMouseVisible does not work correctly bug in AGK
			//SetRawMouseVisible(1)
			mouseSet = 0
		endif
	endif
	if GetKeyboardExists()
		if GetRawKeyState( 37 )  then MoveCameraLocalX( cameraID, -1.0 * speed ) 
		if GetRawKeyState( 39 ) then MoveCameraLocalX( cameraID, 1.0 * speed ) 
		if GetRawKeyState( 38 ) then MoveCameraLocalZ( cameraID, 1.0 * speed ) 
		if GetRawKeyState( 40 ) then MoveCameraLocalZ( cameraID, -1.0 * speed ) 
	endif
endfunction

function FreeFlightCameraUsingTouch( cameraID as integer, speed as float )
	if getmultitouchexists() = 1
		local touchX as float
		local touchY as float
		camMove = 0.0
		camStrafe = 0.0
		howmany = GetRawTouchCount( 0 ) 
		id = getrawfirsttouchevent( 0 )
		for t = 1 to howmany step 1
			touchX = ( getrawtouchcurrentx( id ) - getrawtouchstartx( id ) ) / 75
			touchY = ( getrawtouchcurrenty( id ) - getrawtouchstarty( id ) ) / 75
			movectrl = 0
			if getrawtouchstartx( id ) < 200 then movectrl = 1
			//If the start of touch y is greater then 450 then its a movement touch not rotation.
			if movectrl = 1
				camMove = -touchY * 1 * speed
				camStrafe = touchX * 1 * speed
			endif
			if movectrl = 0
				oldCamAngleX = cameraAngleX
				oldCamAngleY = cameraAngleY
				cameraAngleY = WrapAngle( cameraAngleY + touchX )
				cameraAngleX = WrapAngle( cameraAngleX + touchY )
				if cameraAngleX <= 290.0 and cameraAngleX > 180.0  then cameraAngleX = 290.0
				if cameraAngleX >= 70.0 and cameraAngleX < 180.0 then cameraAngleX = 70.0
				cameraAngleX = CurveAngle( cameraAngleX, oldCamAngleX, speed )
				cameraAngleY = CurveAngle( cameraAngleY, oldCamAngleY, speed )
			endif
			id = getrawnexttouchevent()
		next t
		MovecameraLocalZ( cameraID, camMove )
		MovecameraLocalX( cameraID, camStrafe )
		SetCameraRotation( cameraID, cameraAngleX, cameraAngleY, 0.0 )
	endif
endfunction
