--[[

Interface: 1.4.4.0 1.4.4RC8

Author: GtX | Andy
Date: 30.07.2017
Version: 1.0

Support: http://ls-modcompany.com

History:
V 1.0 @ 30.07.2017 - Release

About: 
Ability to add sound to InteractiveWindows IC controls.

Designed for use with InteractiveControl.lua v4.1 and InteractiveWindows.lua v4.0

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 4 x Modes are available. Different sounds can be assigned to each group if needed.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

"singlePlay" 	-  This mode allows you to play a single continues sound for both directions of the animation. Sound is only played while animation is active and plays in a loop form.

"dualOC"  		-  This mode is designed for use with door type animations. <openSound /> is played when animation is started and <closedSound /> is played when animation returns to the start.

"dualPlay" 		-  This mode allows you to play a different continues sounds for both directions of the animation.
				   <openingSound /> is played when animation is started and <closingSound /> is played when animation is returning.

"triPlay"   	-  This mode is designed for use with hydrulic door type animations.
				   <openingSound /> is played when animation is started, <closingSound /> is played when animation is returning and <finishSound /> is played when animation returns to the start.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
HOW TO USE - "vehicle.xml" - Information:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

windowId			=		This is the InteractiveWindow you wish to assign the sounds too. The ID is the listed order each InteractiveControlWindow is listed starting at the top.

soundMode			=		This is the type of mode you want to use as listed above.

singleSound			=		This can only be used with "singlePlay" soundMode

openSound			=		This can only be used with "dualOC" soundMode

closedSound			=		This can only be used with "dualOC" soundMode

openingSound		=		This can only be used with "dualPlay" and "triPlay" soundMode

closingSound		=		This can only be used with "dualPlay" and "triPlay" soundMode

finishSound			=		This can only be used with "triPlay" soundMode

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Example for xml.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<interactiveSounds>

	<interactiveWindow windowId="1" soundMode="singlePlay">
		<singleSound file="sounds/powerWindow.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
	</interactiveWindow>

	<interactiveWindow windowId="2" soundMode="dualOC">
		<openSound file="sounds/doorOpening.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
		<closedSound file="sounds/doorClosing.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
	</interactiveWindow>
	
	<interactiveWindow windowId="5" soundMode="dualPlay">
		<openingSound  file="Sound/tuer_auf.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
		<closingSound file="Sound/tuer_zu.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
	</interactiveWindow>	
	
	<interactiveWindow windowId="3" soundMode="triPlay">
		<openingSound  file="Sound/doorOpen.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
		<closingSound file="Sound/tuer_zu.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
		<finishSound file="Sound/tuer_zu2.wav" pitchOffset="1" indoorVolumeFactor="1.0" indoorLowpassGain="1.0" outdoorVolumeFactor="0.5" outdoorLowpassGain="0.3"/>
	</interactiveWindow>
	
</interactiveSounds>

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
END:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

]]


InteractiveSounds = {}

InteractiveSounds.modDir = g_currentModDirectory;

function InteractiveSounds.prerequisitesPresent(specializations)
	if not SpecializationUtil.hasSpecialization(InteractiveControl, specializations) then print("Warning: Specialization InteractiveSounds v1.0 needs the specialization InteractiveControl v4.1 and specialization InteractiveWindows v4.0.");
	end;
	return SpecializationUtil.hasSpecialization(InteractiveControl, specializations) and SpecializationUtil.hasSpecialization(InteractiveWindows, specializations);
end;

function InteractiveSounds:load(savegame)

	self.icSounds = {}
	
	local i = 0
	
    while true do
		local icKey = string.format("vehicle.interactiveSounds.interactiveWindow(%d)", i)
		if not hasXMLProperty(self.xmlFile, icKey) then
            break
        end;
		
		local eic = {}
		eic.windowId = getXMLInt(self.xmlFile, icKey.."#windowId")
		eic.soundType = Utils.getNoNil(getXMLString (self.xmlFile, icKey.."#soundMode"), "single");
		eic.openSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".openSound", nil, self.baseDirectory);
		eic.closedSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".closedSound", nil, self.baseDirectory);
		eic.singleSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".singleSound", nil, self.baseDirectory);
		eic.finishSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".finishSound", nil, self.baseDirectory);
		eic.openingSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".openingSound", nil, self.baseDirectory);
		eic.closingSound = SoundUtil.loadSample(self.xmlFile, {}, icKey..".closingSound", nil, self.baseDirectory);

		table.insert(self.icSounds, eic)
		i = i + 1
		
	end;

end;

function InteractiveSounds:delete()
	for i=1, table.getn(self.icSounds) do
		if self.isClient then
			SoundUtil.deleteSample(self.icSounds[i].openSound);
			SoundUtil.deleteSample(self.icSounds[i].closedSound);
			SoundUtil.deleteSample(self.icSounds[i].singleSound);
			SoundUtil.deleteSample(self.icSounds[i].finishSound);
			SoundUtil.deleteSample(self.icSounds[i].openingSound);
			SoundUtil.deleteSample(self.icSounds[i].closingSound);
		end;
	end;
end;

function InteractiveSounds:update(dt)
	for i=1, table.getn(self.icSounds) do
		local object = self.interactiveObjects[self.icSounds[i].windowId];
		
		if object.animation ~= nil then
			if self.icSounds[i].singleSound ~= nil then
				if self:getIsAnimationPlaying(object.animation) and self:getIsActiveForSound() and self.icSounds[i].soundType == "singlePlay" then
					SoundUtil.playSample(self.icSounds[i].singleSound, 0, 0, nil);
				else
					SoundUtil.stopSample(self.icSounds[i].singleSound)
				end;
			end;
		end;
			
			
		if self.icSounds[i].openSound ~= nil and self.icSounds[i].closedSound ~= nil then
			if self:getIsAnimationPlaying(object.animation) and self:getIsActiveForSound() and self.icSounds[i].soundType == "dualOC" then
				if self.animations[object.animation].currentSpeed > 0 and self.animations[object.animation].currentTime < 50 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].openSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].openSound, 1, 0, nil);
					end;
						SoundUtil.stopSample(self.icSounds[i].closedSound)
				elseif self.animations[object.animation].currentSpeed < 0 and self.animations[object.animation].currentTime < 100 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].closedSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].closedSound, 1, 0, nil);
					end;
					SoundUtil.stopSample(self.icSounds[i].openSound)
				end;
			end;
		end;
		
		
		if self.icSounds[i].openingSound ~= nil and self.icSounds[i].closingSound ~= nil then
			if self:getIsAnimationPlaying(object.animation) and self:getIsActiveForSound() and self.icSounds[i].soundType == "dualPlay" then
				if self.animations[object.animation].currentSpeed == 1 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].openingSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].openingSound, 0, 0, nil);
					end
					SoundUtil.stopSample(self.icSounds[i].closingSound)
				elseif self.animations[object.animation].currentSpeed == -1 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].closingSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].closingSound, 0, 0, nil);
					end
					SoundUtil.stopSample(self.icSounds[i].openingSound)
				end;
			else
				SoundUtil.stopSample(self.icSounds[i].openingSound)
				SoundUtil.stopSample(self.icSounds[i].closingSound)
			end;
		end;
		
		if self.icSounds[i].openingSound ~= nil and self.icSounds[i].closingSound ~= nil and self.icSounds[i].finishSound ~= nil then
			if self:getIsAnimationPlaying(object.animation) and self:getIsActiveForSound() and self.icSounds[i].soundType == "triPlay" then
				if self.animations[object.animation].currentSpeed == 1 and self.animations[object.animation].currentTime < 50 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].openingSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].openingSound, 1, 0, nil);
					end;
					SoundUtil.stopSample(self.icSounds[i].closingSound)					
				elseif self.animations[object.animation].currentSpeed == -1 and self.animations[object.animation].currentTime > 50 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].closingSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].closingSound, 1, 0, nil);
					end;
					SoundUtil.stopSample(self.icSounds[i].openingSound)
				end
				
				if self.animations[object.animation].currentSpeed < 0 and self.animations[object.animation].currentTime < 100 then
					if not SoundUtil.isSamplePlaying(self.icSounds[i].finishSound, 1.5*dt) then
						SoundUtil.playSample(self.icSounds[i].finishSound, 1, 0, nil);
						SoundUtil.stopSample(self.icSounds[i].closingSound)
					end;
				end;
			end;
		end;
			
	end;
end;

function InteractiveSounds:onLeave()
	for i=1, table.getn(self.icSounds) do
		if self.icSounds[i].openSound ~= nil then  
			if SoundUtil.isSamplePlaying(self.icSounds[i].openSound) then
				SoundUtil.stopSample(self.icSounds[i].openSound)
			end;	
		end;
		if self.icSounds[i].closedSound ~= nil then
			if SoundUtil.isSamplePlaying(self.icSounds[i].closedSound) then
				SoundUtil.stopSample(self.icSounds[i].closedSound)
			end;
		end;
		if self.icSounds[i].singleSound ~= nil then
			if SoundUtil.isSamplePlaying(self.icSounds[i].singleSound) then
				SoundUtil.stopSample(self.icSounds[i].singleSound)
			end;
		end;
		if self.icSounds[i].closingSound ~= nil then
			if SoundUtil.isSamplePlaying(self.icSounds[i].closingSound) then
				SoundUtil.stopSample(self.icSounds[i].closingSound)
			end;
		end;
		if self.icSounds[i].openingSound ~= nil then
			if SoundUtil.isSamplePlaying(self.icSounds[i].openingSound) then
				SoundUtil.stopSample(self.icSounds[i].openingSound)
			end;
		end;
		if self.icSounds[i].finishSound ~= nil then
			if SoundUtil.isSamplePlaying(self.icSounds[i].finishSound) then
				SoundUtil.stopSample(self.icSounds[i].finishSound)
			end;
		end;
	end;
end;

function InteractiveSounds:onEnter()end;
function InteractiveSounds:draw()end;
function InteractiveSounds:keyEvent(unicode, sym, modifier, isDown)end;
function InteractiveSounds:mouseEvent(posX, posY, isDown, isUp, button)end;
function InteractiveSounds:readStream(streamId, connection)end;
function InteractiveSounds:writeStream(streamId, connection)end;