local T={}local q=.5 local H=false local r=Instance.new("\083\099\114\101\101\110\071\117\105")r.Name="\070\097\100\101\071\117\105"r.DisplayOrder=9 r.ResetOnSpawn=false r.IgnoreGuiInset=true local n=Instance.new("\070\114\097\109\101")n.Name="\070\097\100\101"n.Size=UDim2.new(1,0,1,0)n.Position=UDim2.new(0,0,0,0)n.BorderSizePixel=0 n.BackgroundColor3=Color3.new(0,0,0)n.BackgroundTransparency=1 n.Parent=r local s=Instance.new("\084\101\120\116\076\097\098\101\108")s.Name="\076\097\098\101\108"s.AnchorPoint=Vector2.new(.5,.5)s.BackgroundTransparency=1 s.Font=Enum.Font.SourceSans s.TextScaled=true s.TextWrapped=true s.Size=UDim2.new(1,0,1,0)s.Position=UDim2.new(.5,0,.5,0)s.TextColor3=Color3.new(1,1,1)s.Text=""s.Parent=n local W=Enum.EasingStyle.Quad local a local F local b="\083\116\097\114\116\101\100"local U="\069\110\100\101\100"function T.SetBackgroundColor(q,T)local H=typeof(T)if H=="\067\111\108\111\114\051"then n.BackgroundColor3=T elseif H=="\066\114\105\099\107\067\111\108\111\114"then n.BackgroundColor3=T.Color else error("\065\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \111\102 \116\121\112\101 \067\111\108\111\114\051 \111\114 \066\114\105\099\107\067\111\108\111\114")end end function T.SetTextColor(q,T)local H=typeof(T)if H=="\067\111\108\111\114\051"then s.TextColor3=T elseif H=="\066\114\105\099\107\067\111\108\111\114"then s.TextColor3=T.Color else error("\065\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \111\102 \116\121\112\101 \067\111\108\111\114\051 \111\114 \066\114\105\099\107\067\111\108\111\114")end end function T.SetText(q,T)s.Text=T==nil and""or tostring(T)end function T.SetTextSize(q,T)s.TextWrapped=false s.TextScaled=T==0 and true or false s.TextSize=T==0 and s.TextSize or T end function T.SetFont(q,T)s.Font=T end function T.ClearText(T)T:SetText(nil)end function T.In(H,T,q)H:FromTo(0,1,T,q)end function T.Out(H,T,q)H:FromTo(1,0,T,q)end function T.To(r,T,q,H)r:FromTo(n.BackgroundTransparency,T,q,H)end function T.FromTo(e,T,r,h,z)assert(type(T)=="\110\117\109\098\101\114","\'\102\114\111\109\084\114\097\110\115\112\097\114\101\110\099\121\' \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")assert(type(r)=="\110\117\109\098\101\114","\'\116\111\084\114\097\110\115\112\097\114\101\110\099\121\' \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")assert(h==nil or type(h)=="\110\117\109\098\101\114","\'\100\117\114\097\116\105\111\110\' \097\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114 \111\114 \110\105\108")h=h or q if h<=0 then e:FireEvent(b)n.BackgroundTransparency=r s.TextTransparency=r e:FireEvent(U)return end if z==nil then z=H end if F then F:Cancel()F=nil end e:FireEvent(b)local v=r-T local f=TweenInfo.new(h or q,W,T>r and Enum.EasingDirection.In or Enum.EasingDirection.Out)F=a.new(f,function(q)local H=T+v*q n.BackgroundTransparency=H s.TextTransparency=H end)F:Play()local function I()F.Completed:Wait()e:FireEvent(U)end if z then(coroutine.wrap(I))()else I()end end function T.SetEasingStyle(q,T)assert(typeof(T)=="\069\110\117\109\073\116\101\109","\065\114\103\117\109\101\110\116 \109\117\115\116 \098\101 \111\102 \116\121\112\101 \069\110\117\109\073\116\101\109")W=T end function T.GetScreenGui(T)return r end function T.GetFrame(T)return n end function T.GetLabel(T)return s end function T.Start(T)r.Parent=(game:GetService("\080\108\097\121\101\114\115")).LocalPlayer:WaitForChild("\080\108\097\121\101\114\071\117\105")end function T.Init(T)a=T.Modules.Tween T:RegisterEvent(b)T:RegisterEvent(U)end return T
-- Fade
-- Crazyman32
-- February 4, 2017

--[[
	
	
	METHODS:
	
		Fade:In([duration [, async] ])                  Fade in from black
		Fade:Out([duration [, async] ])                 Fade out to black
		
		Fade:To(transparency [, duration [, async] ])   Fade to the given transparency level
		Fade:FromTo(from, to [, duration [, async] ])   Fade from one transparency level to another
		
		Fade:SetText(text)                              Set text to show on fade screen
		Fade:ClearText()                                Sets text to a blank string
		Fade:SetTextSize(size)				Sets the text size (0 for auto scaling)
		Fade:SetFont(font)                              Sets the font
		
		Fade:SetBackgroundColor(color)                  Set the fade color (can be a Color3 or BrickColor)
		Fade:SetTextColor(color)                        Set the text color (can be a Color3 or BrickColor)
		
		Fade:SetEasingStyle(easingStyle)                Set the easing style (e.g. Enum.EasingStyle.Quad)
		
		Fade:GetScreenGui()                             Returns the ScreenGui for this fade system
		Fade:GetFrame()                                 Returns the overlay Frame
		Fade:GetLabel()                                 Returns the TextLabel used for showing text
	
	
	EVENTS:
		
		Fade.Started()
		Fade.Ended()
	
	
	
	EXAMPLES:
		
		-- Hello fade:
		Fade:SetText("Hello")
		Fade:Out()
		wait(1)
		Fade:In()
		
		-- Slow fade:
		Fade:ClearText()
		Fade:Out(5)
		wait(1)
		Fade:In(5)
		
		-- Half fade:
		Fade:To(0.5)
		
		-- Asynchronous w/ events:
		Fade.Ended:connect(function()
			print("Fade ended!")
		end)
		Fade:Out(1, true)
	
	
	
	
	Note: This module is dependent on the Tween module.
	
--]]



local Fade = {}


local DEFAULT_DURATION = 0.5
local DEFAULT_ASYNC    = false


-- ScreenGui:
local fadeGui = Instance.new("ScreenGui")
	fadeGui.Name = "FadeGui"
	fadeGui.DisplayOrder = 9
	fadeGui.ResetOnSpawn = false
	fadeGui.IgnoreGuiInset = true -- Now ignores the inset because what kind of monster would want this off?

-- Main overlay frame:
local fade = Instance.new("Frame")
	fade.Name = "Fade"
	fade.Size = UDim2.new(1, 0, 1, 0)
	fade.Position = UDim2.new(0, 0, 0, 0)
	fade.BorderSizePixel = 0
	fade.BackgroundColor3 = Color3.new(0, 0, 0)
	fade.BackgroundTransparency = 1
	fade.Parent = fadeGui

-- Text label:
local label = Instance.new("TextLabel")
	label.Name = "Label"
	label.AnchorPoint = Vector2.new(0.5, 0.5)
	label.BackgroundTransparency = 1
	label.Font = Enum.Font.SourceSans
	label.TextScaled = true
	label.TextWrapped = true
	label.Size = UDim2.new(1, 0, 1, 0)
	label.Position = UDim2.new(0.5, 0, 0.5, 0)
	label.TextColor3 = Color3.new(1, 1, 1)
	label.Text = ""
	label.Parent = fade


local easingStyle = Enum.EasingStyle.Quad

local Tween
local currentTween

--local fadeStarted
--local fadeEnded

local fadeStartedEvent = "Started"
local fadeEndedEvent = "Ended"


-- Set background color:
function Fade:SetBackgroundColor(c)
	local t = typeof(c)
	if (t == "Color3") then
		fade.BackgroundColor3 = c
	elseif (t == "BrickColor") then
		fade.BackgroundColor3 = c.Color
	else
		error("Argument must be of type Color3 or BrickColor")
	end
end


-- Set text color:
function Fade:SetTextColor(c)
	local t = typeof(c)
	if (t == "Color3") then
		label.TextColor3 = c
	elseif (t == "BrickColor") then
		label.TextColor3 = c.Color
	else
		error("Argument must be of type Color3 or BrickColor")
	end
end


-- Set text:
function Fade:SetText(text)
	label.Text = (text == nil and "" or tostring(text))
end


-- Set text size:
function Fade:SetTextSize(size)
	-- Turn off text wrapping no matter what because if we turn on text scaling, wrapping will automatically turn back on
	label.TextWrapped = false
	label.TextScaled = (size == 0 and true or false)
	label.TextSize = (size == 0 and label.TextSize or size) -- Don't bother changing text size if we turn on auto scaling
end


-- Set font:
function Fade:SetFont(font)
	label.Font = font
end


-- Clear text:
function Fade:ClearText()
	self:SetText(nil)
end


-- Fade in (fade the transparency frame out of the picture)
function Fade:In(duration, async)
	self:FromTo(0, 1, duration, async)
end


--  Fade out (fade the transparency frame into picture)
function Fade:Out(duration, async)
	self:FromTo(1, 0, duration, async)
end


-- Fade to a transparency, starting at whatever transparency level it is currently:
function Fade:To(transparency, duration, async)
	self:FromTo(fade.BackgroundTransparency, transparency, duration, async)
end


-- Fade from a transparency to another:
function Fade:FromTo(fromTransparency, toTransparency, duration, async)
	
	assert(type(fromTransparency) == "number", "'fromTransparency' argument must be a number")
	assert(type(toTransparency) == "number", "'toTransparency' argument must be a number")
	assert(duration == nil or type(duration) == "number", "'duration' argument must be a number or nil")
	
	duration = (duration or DEFAULT_DURATION)
	
	if (duration <= 0) then
		-- Instant fade; skip everything else:
		self:FireEvent(fadeStartedEvent)
		fade.BackgroundTransparency = toTransparency
		label.TextTransparency = toTransparency
		self:FireEvent(fadeEndedEvent)
		return
	end
	
	if (async == nil) then
		async = DEFAULT_ASYNC
	end
	
	-- If already fading, stop fading so we can prioritize this new fade:
	if (currentTween) then
		currentTween:Cancel()
		currentTween = nil
	end
	
	-- Fire Started event:
	self:FireEvent(fadeStartedEvent)
	
	local deltaTransparency = (toTransparency - fromTransparency)
	
	-- Fade operation:
	local tweenInfo = TweenInfo.new(
		(duration or DEFAULT_DURATION),
		easingStyle,
		(fromTransparency > toTransparency and Enum.EasingDirection.In or Enum.EasingDirection.Out)
	)
	currentTween = Tween.new(tweenInfo, function(ratio)
		local transparency = (fromTransparency + (deltaTransparency * ratio))
		fade.BackgroundTransparency = transparency
		label.TextTransparency = transparency
	end)
	
	-- Start fading:
	currentTween:Play()
	
	-- Await fade to end, then fire Ended event:
	local function AwaitEnd()
		currentTween.Completed:Wait()
		self:FireEvent(fadeEndedEvent)
	end
	
	if (async) then
		coroutine.wrap(AwaitEnd)()
	else
		AwaitEnd()
	end
	
end


function Fade:SetEasingStyle(style)
	assert(typeof(style) == "EnumItem", "Argument must be of type EnumItem")
	easingStyle = style
end


function Fade:GetScreenGui()
	return fadeGui
end


function Fade:GetFrame()
	return fade
end


function Fade:GetLabel()
	return label
end


function Fade:Start()
	fadeGui.Parent = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
end


function Fade:Init()
	Tween = self.Modules.Tween
	self:RegisterEvent(fadeStartedEvent)
	self:RegisterEvent(fadeEndedEvent)
end


return Fade
