function module.new(s,U,p)local c=RecurseAdd(s,U)local B=setmetatable({record=RecurseAdd(s,U),state=0,duration=p},module)return B end function RemoveTween(s,U)local p=s.sourceInstance if U then p=U end for s,U in pairs(s.pairs)do p[s]=U.src end for U,p in pairs(s.children)do local c=s.sourceInstance if c then RemoveTween(p,c)end end end function module.RemoveTween(s)RemoveTween(s.record)end function SetTween(s,U)for p,c in pairs(s.pairs)do s.sourceInstance[p]=c.func(c.src,c.dst,U)end for p,c in pairs(s.children)do local B=s.sourceInstance if B then SetTween(c,U)end end end function module.SetTween(U,s)s=math.clamp(s,0,1)SetTween(U.record,s)end function module.RunTween(p,s,U)local c=p.state if U==true then p.state+=s/p.duration else p.state-=s/p.duration end p.state=math.clamp(p.state,0,1)if c~=p.state then p:SetTween(p.state)end end return module
--Exported

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
