
require "CIrp"

-- [I]nte[RP]olation[MAN]ager object 
--	an object that manages CIrp objects


-- sample usage 

--[[
	-- ease an object from 1 to 100 in 3 seconds following a 1.5 second delay:

	CIrpMan:Attach( self, true )

	local oIrp = self:CreateIrp( 1, 100, 3, CIrpFuncs.mEaseOutQuint, 1.5 )
	oIrp.m_oHookContext = self

	oIrp.mOnChange = function( i_oIrp, i_oContext )
		i_oContext.m_oTile:SetX( i_oIrp:GetVal() )
	end

	oIrp.mOnComplete = function( i_oIrp, i_oContext )
		log( "COMPLETE" )
	end
--]]

CIrpMan =
{
}

----------------------------------------------------------------------------------------
-- CREATE/ATTACH
----------------------------------------------------------------------------------------

-- iOpt_bAttachToOnUpdate		: update irps in function OnUpdate, assumes 1 / 60 sec irp update time 
--									(careful! will override previous OnUpdate)

function CIrpMan:Attach( i_oObj, iOpt_bAttachToOnUpdate )
	local oRet = CIrpMan:Bind( i_oObj )
	
	oRet:Init( iOpt_bAttachToOnUpdate )

	return oRet
end

function CIrpMan:Create()
	return CIrpMan:Attach( {} )
end

----------------------------------------------------------------------------------------
-- PUBLIC
----------------------------------------------------------------------------------------

function CIrpMan:Init( iOpt_bAttachToOnUpdate )
	self.m_aRegisteredIrps = {}
	
	if ( iOpt_bAttachToOnUpdate ~= nil
			and iOpt_bAttachToOnUpdate ) then
		self:SetOnUpdateEnabled( true )
		self.OnUpdate = function()
			self:UpdateIrps( 1 / 60 )
		end
	end
end

function CIrpMan:GetIrpCount()
	return table.getn( self.m_aRegisteredIrps )
end

-- creates an irp and automatically manages it
function CIrpMan:CreateIrp( i_fValStart, i_fValFinish, i_fTimeDuration, iOpt_mIrpFunc, iOpt_fDelay, iOpt_fContext )
	
	if ( iOpt_fContext == nil ) then
		iOpt_fContext = self
	end
	
	local oRet = CIrp:Create( i_fValStart, i_fValFinish, i_fTimeDuration, iOpt_mIrpFunc, iOpt_fDelay, iOpt_fContext )

	self:RegisterIrp( oRet )
	
	return oRet
end

-- main loop for registered tweens
function CIrpMan:UpdateIrps( i_fDeltaT )
	if ( i_fDeltaT == nil ) then
		i_fDeltaT = 1 / 60
	end
	
	for i = table.getn( self.m_aRegisteredIrps ), 1, -1 do

		local oIrp = self.m_aRegisteredIrps[ i ]
		
		if ( oIrp.m_bPlaying ) then
			oIrp:Update( i_fDeltaT )
		elseif ( oIrp:IsDone() ) then
			table.remove( self.m_aRegisteredIrps, i )
		end
	end
end

-- set iOpt_bComplete to true to Finish() all irps before killing (runs complete events on each irp .. careful of recursive traps!)
function CIrpMan:KillAllIrps( iOpt_bComplete )
	for i = 1, table.getn( self.m_aRegisteredIrps ), 1 do
		local oIrp = self.m_aRegisteredIrps[ i ]

		if ( iOpt_bComplete ~= nil and iOpt_bComplete ) then
			oIrp:Finish()
		else
			oIrp:Stop()
		end
	end
	
	self.m_aRegisteredIrps = {}
end

function CIrpMan:KillIrp( i_oIrp )
	for i = table.getn( self.m_aRegisteredIrps ), 1, -1 do

		local oIrp = self.m_aRegisteredIrps[ i ]
		
		if ( oIrp == i_oIrp ) then
			oIrp:Stop()
			table.remove( self.m_aRegisteredIrps, i )
			return true
		end
	end
	
	return false
end

function CIrpMan:RegisterIrp( i_oIrp )
	table.insert( self.m_aRegisteredIrps, i_oIrp )
end

function CIrpMan:UnregisterIrp( i_oIrp )
	
	for i = 1, table.getn( self.m_aRegisteredIrps ), 1 do
		if ( self.m_aRegisteredIrps[ i ] == i_oIrp ) then
			table.remove( self.m_aRegisteredIrps, i )
		end
	end
end


function CIrpMan:Bind( i_oObj )
	BindObj( CIrpMan, i_oObj )
	return i_oObj
end


----------------------------------------------------------------------------------------
-- PRIVATE
----------------------------------------------------------------------------------------


