--
-- AllradV4
-- Specialization for Allrad
--
-- V2 by modelleicher & Made by Case_IH_MxU135 | FIX and V4 by DragonLord2007/modhoster
-- www.schwabenmodding.bplaced.net
-- V4 MP Fähig!Modelleicher 
-- Hud Modhosterteam und borutcebulj

-- Geändert am: 12.10.2013
-- Geändert von: Ifko[nator]
-- Geändert wurde: Die Inputs wurden geändert damit es zu keiner Tastenüberschreibung mit einem anderem Mod kommt der die lua auch verbaut hat.

-- Erneut geändert am: 14.06.2014
-- Geändert von: Ifko[nator]
-- Geädert wurde: Das Hud Overlay richtet sich nun nach den GUI Eintellungen Ingame.

AllradV4 = {};

function AllradV4.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations);
end;

function AllradV4:load(xmlFile)
    self.allradState = AllradV4.allradState;

    self.AllradV4Active = false;
	 
    self.hudAllradPosX = 0.6529;
    self.hudAllradWidth = 0.234;
    self.hudAllradPosY = 0.01238;
    self.hudAllradHeight = 0.04;
	
	self.infoPanelAllradONPathNew = Utils.getFilename("Schemas/allrad_symb_yes_new.dds", self.baseDirectory);
	self.hudAllradONOverlayNew = Overlay:new("allrad_symb_yes_new", self.infoPanelAllradONPathNew, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	
	self.infoPanelAllradOFFPathNew = Utils.getFilename("Schemas/allrad_symb_no_new.dds", self.baseDirectory);
	self.hudAllradOFFOverlayNew = Overlay:new("allrad_symb_no_new", self.infoPanelAllradOFFPathNew, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	
	self.infoPanelAllradONPathOld = Utils.getFilename("Schemas/allrad_symb_yes_old.dds", self.baseDirectory);
	self.hudAllradONOverlayOld = Overlay:new("allrad_symb_yes_old", self.infoPanelAllradONPathOld, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	
	self.infoPanelAllradOFFPathOld = Utils.getFilename("Schemas/allrad_symb_no_old.dds", self.baseDirectory);
	self.hudAllradOFFOverlayOld = Overlay:new("allrad_symb_no_old", self.infoPanelAllradOFFPathOld, self.hudAllradPosX, self.hudAllradPosY, self.hudAllradWidth, self.hudAllradHeight);
	
	self.showhudAllrad = false;
end;

function AllradV4:delete()
end;

function AllradV4:readStream(streamId, connection)  
     self:allradState(streamReadBool(streamId), true);  
end;

function AllradV4:writeStream(streamId, connection)  
    streamWriteBool(streamId, self.AllradV4Active);  
end;

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

function AllradV4:allradState(state, noEventSend)  
    AllradStateEvent.sendEvent(self, state, noEventSend);	
    self.AllradV4Active = state;
end;

function AllradV4:update(dt)
    if self:getIsActiveForInput(false) and not self:hasInputConflictWithSelection() then  
		if InputBinding.hasEvent(InputBinding.AllradIfkonator) then
		  self:allradState(not self.AllradV4Active);
		end;
    end;	
	if self:getIsActive() then	
   		if self.AllradV4Active == true then
			self.showhudAllrad = true;		
			self.wheels[1].driveMode = 2;
		 	self.wheels[2].driveMode = 2;
		 	self.wheels[3].driveMode = 2;
		 	self.wheels[4].driveMode = 2;			
		else                          
			self.showhudAllrad = false;
		 	self.wheels[1].driveMode = 0;
			self.wheels[2].driveMode = 0;
			self.wheels[3].driveMode = 2;
			self.wheels[4].driveMode = 2;			
		end; 	
    end; 
end;

function AllradV4:draw()
   if self.showhudAllrad then
		g_currentMission:addHelpButtonText(g_i18n:getText("AllradOFFIfkonator"), InputBinding.AllradIfkonator);	
		if g_newGUISkin then
			self.hudAllradONOverlayNew:render();
		else
			self.hudAllradONOverlayOld:render();
		end;
	else
		g_currentMission:addHelpButtonText(g_i18n:getText("AllradONIfkonator"), InputBinding.AllradIfkonator);	
		if g_newGUISkin then
			self.hudAllradOFFOverlayNew:render();
		else
			self.hudAllradOFFOverlayOld:render();
		end;
   end;
end;

AllradStateEvent = {};
AllradStateEvent_mt = Class(AllradStateEvent, Event);

InitEventClass(AllradStateEvent, "AllradStateEvent");

function AllradStateEvent:emptyNew() 
    local self = Event:new(AllradStateEvent_mt );
    self.className="AllradStateEvent";
    return self;
end;

function AllradStateEvent:new(vehicle, state) 
    self.vehicle = vehicle;
    self.state = state;
    return self;
end;

function AllradStateEvent:readStream(streamId, connection)  
    local id = streamReadInt32(streamId); 
    self.state = streamReadBool(streamId); 
    self.vehicle = networkGetObject(id); 
    self:run(connection);  
end;

function AllradStateEvent:writeStream(streamId, connection)   
    streamWriteInt32(streamId, networkGetObjectId(self.vehicle));	
    streamWriteBool(streamId, self.state );   
end;

function AllradStateEvent:run(connection)  
    self.vehicle:allradState(self.state, true);
	if not connection:getIsServer() then  
		g_server:broadcastEvent(AllradStateEvent:new(self.vehicle, self.state), nil, connection, self.object);
	end;
end;

function AllradStateEvent.sendEvent(vehicle, state, noEventSend)  
	if noEventSend == nil or noEventSend == false then
		if g_server ~= nil then  
			g_server:broadcastEvent(AllradStateEvent:new(vehicle, state), nil, nil, vehicle);
		else 
			g_client:getServerConnection():sendEvent(AllradStateEvent:new(vehicle, state));
		end;
	end;
end;

