--
-- JD8400
-- Specialization for magnum tractors
--
-- @author  Knagsted
-- @date  15/03/10
--

-- Do not edit without my permission

--CAMERA 4 AXES 

--[[	SPECIALIZATION	ADD TO MODDESC & XML


		<specialization name="CAMMOVE"       className="CAMMOVE"       filename="SP/Cam4Axes.lua"/>
		
		<specialization name="CAMMOVE"/>

		<input name="MOVECAMLEFT" key1="KEY_f5" button="" />
		<input name="MOVECAMRIGHT" key1="KEY_f6" button="" />
		<input name="MOVECAMFORWARD" key1="KEY_f7" button="" />
		<input name="MOVECAMBACK" key1="KEY_f8" button="" />
		<input name="MOVECAMRESET" key1="KEY_r" button="" />
		<input name="CAMHUD" key1="KEY_KP_0" button="" />		
		
			
	<internalCamera index="11" />
	<cameraRotNode index="11|0|0" />
	
	<cameras count="2">
	    <camera1 index="11|0|0" rotatable="true" rotateNode="11|0|0" limit="false" rotMinX="0" rotMaxX="0" transMin="0" transMax="0" />	<!--INTERNAL-->
        <camera2 index="12|0" rotatable="true" rotateNode="12" limit="true" rotMinX="-1.4" rotMaxX="0" transMin="4" transMax="50" />
    </cameras>

]]

CAMMOVE = {}

function CAMMOVE.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Steerable, specializations);
end;

function CAMMOVE:load(xmlFile)

	-- Moveable camera
	self.internalCamera = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.internalCamera#index"));
	
	if self.camPositionX == nil then
		self.camPositionX = 0;
	end;
	
	if self.camPositionZ == nil then
		self.camPositionZ = 0;
	end;

	-- HUD --
	self.CAMERA = false;
	self.HUDpath_1 = Utils.getFilename("Scripts/HUD/Camera.png", self.baseDirectory);
	self.HUDWidth_1 = 0.13;
    self.HUDHeight_1 = 0.15;
	self.HUDPoxX_1 = 0.85;
    self.HUDPoxY_1 = 0.45;
    self.HUD_CAMERA = Overlay:new("HUD", self.HUDpath_1, self.HUDPoxX_1, self.HUDPoxY_1, self.HUDWidth_1, self.HUDHeight_1);
	
end;
      
function CAMMOVE:delete()
end;

function CAMMOVE:mouseEvent(posX, posY, isDown, isUp, button)
end;
	  
function CAMMOVE:keyEvent(unicode, sym, modifier, isDown)
end;

function CAMMOVE:update(dt)

	-- Input reading
	if self.isEntered then
	
		if InputBinding.isPressed(InputBinding.MFMOVECAMLEFT) and self.CAMERA then
				self.camPositionX = self.camPositionX + 0.02;
				if self.camPositionX >= 0.33 then
					self.camPositionX = 0.33;
				end;
		end;
		
		if InputBinding.isPressed(InputBinding.MFMOVECAMRIGHT) and self.CAMERA then
				self.camPositionX = self.camPositionX - 0.02;
				if self.camPositionX <= -0.33 then
					self.camPositionX = -0.33;
				end;
		end;
		
		if InputBinding.isPressed(InputBinding.MFMOVECAMFORWARD) and self.CAMERA then
				self.camPositionZ = self.camPositionZ + 0.02;
				if self.camPositionZ >= 0.35 then
					self.camPositionZ = 0.35;
				end;
		end;
		
		if InputBinding.isPressed(InputBinding.MFMOVECAMBACK) and self.CAMERA then
				self.camPositionZ = self.camPositionZ - 0.02;
				if self.camPositionZ <= -0.3 then
					self.camPositionZ = -0.3;
				end;
		end;
		
		if InputBinding.hasEvent(InputBinding.MFMOVECAMRESET) and self.CAMERA then
				self.camPositionX = 0;
				self.camPositionZ = 0;
		end;
		
		if InputBinding.hasEvent(InputBinding.CAMHUD) then
			self.CAMERA = not self.CAMERA;
		end;


	-- Camera translation
	if self.internalCamera ~= nil then
		setTranslation(self.internalCamera, self.camPositionX, 0, self.camPositionZ);
	end;

		
	end;
	
end;

function CAMMOVE:loadFromAttributesAndNodes(xmlFile, key, resetVehicles)
	self.camPositionX = Utils.getNoNil(getXMLFloat(xmlFile, key.."#camPositionX"), 0);
	self.camPositionZ = Utils.getNoNil(getXMLFloat(xmlFile, key.."#camPositionZ"), 0);
end;

function CAMMOVE:getSaveAttributesAndNodes(nodeIdent)
    local attributes = 'camPositionX="'..self.camPositionX..'" camPositionZ="'..self.camPositionZ..'"';
    return attributes, nil;
end;

function CAMMOVE:onLeave()
end;

function CAMMOVE:draw()
	--g_currentMission:addExtraPrintText(string.format("Keypad 0 : MOVE CAMERA ON/OFF"));
	if self.HUD_CAMERA ~= nil then
		if self.CAMERA then
			self.HUD_CAMERA:render();
			setTextBold(true);

			setTextColor(1, 1, 1, 1.0);
				renderText(0.01, 0.05, 0.020, string.format("Move camera mode[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.CAMHUD)));
				renderText(0.01, 0.03, 0.020, string.format("left[%s], right[%s], fwd[%s], back[%s], ",InputBinding.getKeyNamesOfDigitalAction(InputBinding.MFMOVECAMLEFT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.MFMOVECAMRIGHT), InputBinding.getKeyNamesOfDigitalAction(InputBinding.MFMOVECAMFORWARD), InputBinding.getKeyNamesOfDigitalAction(InputBinding.MFMOVECAMBACK)));
				renderText(0.01, 0.01, 0.020, string.format("Reset[%s]",InputBinding.getKeyNamesOfDigitalAction(InputBinding.MFMOVECAMRESET)));
	
		end;
	end;
end;
