
ZoomSpeed = 100;
MinZoominDistance = 30;
CameraMaxPos = CVector(300, 300, 500);
CameraMinPos = CVector(-300, -300, 50);
MoveScaleMultiplier = 1/50;
MouseWheelZoomMultiplier = 0.3;
HotDistance = 0.03;
dofile("../data/programs/Common.lua");
PrevMousPos = CVector(CVector.ZERO);
 function Initialize()
		Camera:SetPos(CVector(0, 0.1, 300));
	Camera:LookAt(CVector(0, 0, 0));
		Camera:SetPos(Camera:GetPrevModeLastPos());
		PrevMousPos = MousePosition;
end
function CheckBeforeMovement(NewPos)
		if(NewPos.x < CameraMinPos.x)then NewPos.x = CameraMinPos.x;end
	if(NewPos.y < CameraMinPos.y)then NewPos.y = CameraMinPos.y;end
	if(NewPos.z < CameraMinPos.z)then NewPos.z = CameraMinPos.z;end
	if(NewPos.x > CameraMaxPos.x)then NewPos.x = CameraMaxPos.x;end
	if(NewPos.y > CameraMaxPos.y)then NewPos.y = CameraMaxPos.y;end
	if(NewPos.z > CameraMaxPos.z)then NewPos.z = CameraMaxPos.z;end
	
		Camera:SetPos(NewPos);
end
function MoveWithEvents()
		MoveScale = Camera:GetMoveScale() * MoveScaleMultiplier;
	if(Camera:IsSlowMovement())then
		MoveScale = MoveScale * 0.1;
	end
	if(Camera:IsAcceleratedMovement())then
				Camera:SetAcceleration(Camera:GetAcceleration() + Camera:GetAccelerationInc());
		MoveScale = MoveScale * Camera:GetAcceleration();
	end
	Translation = CVector(0, 0, 0);
		if(Camera:IsMovingIn(ICamera.MOVING_FORWARD))then
		Translation.z = Translation.z - 1;
	end
	if(Camera:IsMovingIn(ICamera.MOVING_BACKWARDS))then
		Translation.z = Translation.z + 1;
	end
	if(Camera:IsMovingIn(ICamera.MOVING_LEFT))then
		Translation.x = Translation.x - 1;
	end
	if(Camera:IsMovingIn(ICamera.MOVING_RIGHT))then
		Translation.x = Translation.x + 1;
	end
	if(Camera:IsMovingIn(ICamera.MOVING_DOWN))then
		Translation.y = Translation.y - 1;
	end
	if(Camera:IsMovingIn(ICamera.MOVING_UP))then
		Translation.y = Translation.y + 1;
	end
		CheckBeforeMovement(CamPos + (Translation * MoveScale));
end
function MoveWithMouse()
		Translation = CVector(0, 0, 0);
		LeftDistance = MousePosition.x;
	RightDistance = 1 - MousePosition.x;
	TopDistance = MousePosition.y;
	BottomDistance = 1 - MousePosition.y;
		MouseRelChange = MousePosition - PrevMousPos;
			Translation.z = Translation.z + MouseRelChange.z * MouseWheelZoomMultiplier;
		PrevMousPos = MousePosition;
	if(LeftDistance < HotDistance)then
				NormDistance = 1 - LeftDistance / HotDistance;
		Translation.x = Translation.x - NormDistance * NormDistance;
	end
	if(RightDistance < HotDistance)then
				NormDistance = 1 - RightDistance / HotDistance;
		Translation.x = Translation.x + NormDistance * NormDistance;
	end
	if(BottomDistance < HotDistance)then
				NormDistance = 1 - BottomDistance / HotDistance;
		Translation.y = Translation.y - NormDistance * NormDistance;
	end
	if(TopDistance < HotDistance)then
				NormDistance = 1 - TopDistance / HotDistance;
		Translation.y = Translation.y + NormDistance * NormDistance;
	end
		Translation = Translation * Camera:GetMoveScale() / 10;
		if(Translation:length() > 0.1)then
				CheckBeforeMovement(Camera:GetPos() + Translation);
	end
end
function Update()
		CamPos = Camera:GetPos();
	if(not Camera:IsMoving())then
				Camera:SetAcceleration(1);
				MoveWithMouse();
	else
				MoveWithEvents();
	end
end
