function module.new(V,q,d)local g=RecurseAdd(V,q)local P=setmetatable({record=RecurseAdd(V,q),state=0;duration=d},module)return P end function RemoveTween(V,q)local d=V.sourceInstance if q then d=q end for V,q in pairs(V.pairs)do d[V]=q.src end for q,d in pairs(V.children)do local g=V.sourceInstance if g then RemoveTween(d,g)end end end function module.RemoveTween(V)RemoveTween(V.record)end function SetTween(V,q)for d,g in pairs(V.pairs)do V.sourceInstance[d]=g.func(g.src,g.dst,q)end for d,g in pairs(V.children)do local P=V.sourceInstance if P then SetTween(g,q)end end end function module.SetTween(q,V)V=math.clamp(V,0,1)SetTween(q.record,V)end function module.RunTween(d,V,q)local g=d.state if q==true then d.state+=V/d.duration else d.state-=V/d.duration end d.state=math.clamp(d.state,0,1)if g~=d.state then d:SetTween(d.state)end end return module


function module.new(sourceInstance, data, duration)
	
	local record = RecurseAdd(sourceInstance, data)
	local self = setmetatable(
		{
			record = RecurseAdd(sourceInstance, data),
			state = 0,
			duration  = duration
			
		}, module)
	
	
	return self
end


function RemoveTween(tweenRecord, instance)
	local sourceInstance = tweenRecord.sourceInstance
	if (instance) then
		sourceInstance = instance
	end

	for key,value in pairs(tweenRecord.pairs) do
		sourceInstance[key] = value.src		
	end
	for key,value in pairs(tweenRecord.children) do

		local child = tweenRecord.sourceInstance
		if (child) then
			RemoveTween(value, child)
		end
	end
end

function module:RemoveTween()
	RemoveTween(self.record)
end

function SetTween(tweenRecord, frac)

	for key,value in pairs(tweenRecord.pairs) do
		tweenRecord.sourceInstance[key] = value.func(value.src,value.dst, frac)		
	end
	for key,value in pairs(tweenRecord.children) do

		local child = tweenRecord.sourceInstance
		if (child) then
			SetTween(value, frac)
		end
	end
end

function module:SetTween(frac)
	
	frac = math.clamp(frac,0,1)
 	SetTween(self.record, frac)
end

function module:RunTween(deltaTime, grow)
	
		
	local oldFrac = self.state
	if (grow == true) then
		self.state += deltaTime / self.duration
	else
		self.state -= deltaTime / self.duration
	end
	self.state = math.clamp(self.state,0,1)

	if (oldFrac ~= self.state) then
		self:SetTween(self.state)
	end
	
end

return module
