function CheckIfPointIsInSquare(N,p,f)return p.X<=N.X and(N.X<=f.X and(p.Y<=N.Y and N.Y<=f.Y))end local N=false local p=false local f=0 local z=0 local D=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local y=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local H=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")function DoBackgroundFadeIn(p)z=tick()N=false D:Fire()ChatWindow:FadeInBackground(p or ChatSettings.ChatDefaultFadeDuration)local f=ChatWindow:GetCurrentChannel()if f then local N=MessageLogDisplay.Scroller N.ScrollingEnabled=true N.ScrollBarThickness=moduleMessageLogDisplay.ScrollBarThickness end end function DoBackgroundFadeOut(p)z=tick()N=true D:Fire()ChatWindow:FadeOutBackground(p or ChatSettings.ChatDefaultFadeDuration)local f=ChatWindow:GetCurrentChannel()if f then local N=MessageLogDisplay.Scroller N.ScrollingEnabled=false N.ScrollBarThickness=0 end end function DoTextFadeIn(N)f=tick()p=false D:Fire()ChatWindow:FadeInText((N or ChatSettings.ChatDefaultFadeDuration)*0)end function DoTextFadeOut(N)f=tick()p=true D:Fire()ChatWindow:FadeOutText(N or ChatSettings.ChatDefaultFadeDuration)end function DoFadeInFromNewInformation()DoTextFadeIn()if ChatSettings.ChatShouldFadeInFromNewInformation then DoBackgroundFadeIn()end end function InstantFadeIn()DoBackgroundFadeIn(0)DoTextFadeIn(0)end function InstantFadeOut()DoBackgroundFadeOut(0)DoTextFadeOut(0)end local d=nil function UpdateFadingForMouseState(N)d=N y:Fire()if ChatBar:IsFocused()then return end if N then DoBackgroundFadeIn()DoTextFadeIn()else DoBackgroundFadeIn()end end spawn(function()while true do RunService.RenderStepped:wait()while d or ChatBar:IsFocused()do if d then y.Event:wait()end if ChatBar:IsFocused()then H.Event:wait()end end if not N then local N=tick()-z if N>ChatSettings.ChatWindowBackgroundFadeOutTime then DoBackgroundFadeOut()end elseif not p then local N=tick()-f if N>ChatSettings.ChatWindowTextFadeOutTime then DoTextFadeOut()end else D.Event:wait()end end end)function getClassicChatEnabled()if ChatSettings.ClassicChatEnabled~=nil then return ChatSettings.ClassicChatEnabled end return Players.ClassicChat end function getBubbleChatEnabled()if ChatSettings.BubbleChatEnabled~=nil then return ChatSettings.BubbleChatEnabled end return Players.BubbleChat end function bubbleChatOnly()return not getClassicChatEnabled()and getBubbleChatEnabled()end function UpdateMousePosition(N)if not(moduleApiTable.Visible and(moduleApiTable.IsCoreGuiEnabled and(moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)))then return end if bubbleChatOnly()then return end local p=ChatWindow.GuiObject.AbsolutePosition local f=ChatWindow.GuiObject.AbsoluteSize local z=CheckIfPointIsInSquare(N,p,p+f)if z~=d then UpdateFadingForMouseState(z)end end UserInputService.InputChanged:connect(function(N)if N.UserInputType==Enum.UserInputType.MouseMovement then local p=Vector2.new(N.Position.X,N.Position.Y)UpdateMousePosition(p)end end)UserInputService.TouchTap:connect(function(N,p)UpdateMousePosition(N[1])end)UserInputService.TouchMoved:connect(function(N,p)local f=Vector2.new(N.Position.X,N.Position.Y)UpdateMousePosition(f)end)UserInputService.Changed:connect(function(N)if N=="\077\111\117\115\101\066\101\104\097\118\105\111\114"then if UserInputService.MouseBehavior==Enum.MouseBehavior.LockCenter then local N=ChatWindow.GuiObject.AbsolutePosition local p=ChatWindow.GuiObject.AbsoluteSize local f=GuiParent.AbsoluteSize local z=CheckIfPointIsInSquare(f/2,N,N+p)if z then UserInputService.MouseBehavior=Enum.MouseBehavior.Default end end end end)
--////////////////////////////////////////////////////////////////////////////////////////////
--////////////////////////////////////////////////////////////// Code to do chat window fading
--////////////////////////////////////////////////////////////////////////////////////////////
function CheckIfPointIsInSquare(checkPos, topLeft, bottomRight)
	return (
		topLeft.X <= checkPos.X
		and checkPos.X <= bottomRight.X
		and topLeft.Y <= checkPos.Y
		and checkPos.Y <= bottomRight.Y
	)
end

local backgroundIsFaded = false
local textIsFaded = false
local lastTextFadeTime = 0
local lastBackgroundFadeTime = 0

local fadedChanged = Instance.new("BindableEvent")
local mouseStateChanged = Instance.new("BindableEvent")
local chatBarFocusChanged = Instance.new("BindableEvent")

function DoBackgroundFadeIn(setFadingTime)
	lastBackgroundFadeTime = tick()
	backgroundIsFaded = false
	fadedChanged:Fire()
	ChatWindow:FadeInBackground((setFadingTime or ChatSettings.ChatDefaultFadeDuration))

	local currentChannelObject = ChatWindow:GetCurrentChannel()
	if currentChannelObject then
		local Scroller = MessageLogDisplay.Scroller
		Scroller.ScrollingEnabled = true
		Scroller.ScrollBarThickness = moduleMessageLogDisplay.ScrollBarThickness
	end
end

function DoBackgroundFadeOut(setFadingTime)
	lastBackgroundFadeTime = tick()
	backgroundIsFaded = true
	fadedChanged:Fire()
	ChatWindow:FadeOutBackground((setFadingTime or ChatSettings.ChatDefaultFadeDuration))

	local currentChannelObject = ChatWindow:GetCurrentChannel()
	if currentChannelObject then
		local Scroller = MessageLogDisplay.Scroller
		Scroller.ScrollingEnabled = false
		Scroller.ScrollBarThickness = 0
	end
end

function DoTextFadeIn(setFadingTime)
	lastTextFadeTime = tick()
	textIsFaded = false
	fadedChanged:Fire()
	ChatWindow:FadeInText((setFadingTime or ChatSettings.ChatDefaultFadeDuration) * 0)
end

function DoTextFadeOut(setFadingTime)
	lastTextFadeTime = tick()
	textIsFaded = true
	fadedChanged:Fire()
	ChatWindow:FadeOutText((setFadingTime or ChatSettings.ChatDefaultFadeDuration))
end

function DoFadeInFromNewInformation()
	DoTextFadeIn()
	if ChatSettings.ChatShouldFadeInFromNewInformation then
		DoBackgroundFadeIn()
	end
end

function InstantFadeIn()
	DoBackgroundFadeIn(0)
	DoTextFadeIn(0)
end

function InstantFadeOut()
	DoBackgroundFadeOut(0)
	DoTextFadeOut(0)
end

local mouseIsInWindow = nil
function UpdateFadingForMouseState(mouseState)
	mouseIsInWindow = mouseState

	mouseStateChanged:Fire()

	if ChatBar:IsFocused() then
		return
	end

	if mouseState then
		DoBackgroundFadeIn()
		DoTextFadeIn()
	else
		DoBackgroundFadeIn()
	end
end

spawn(function()
	while true do
		RunService.RenderStepped:wait()

		while mouseIsInWindow or ChatBar:IsFocused() do
			if mouseIsInWindow then
				mouseStateChanged.Event:wait()
			end
			if ChatBar:IsFocused() then
				chatBarFocusChanged.Event:wait()
			end
		end

		if not backgroundIsFaded then
			local timeDiff = tick() - lastBackgroundFadeTime
			if timeDiff > ChatSettings.ChatWindowBackgroundFadeOutTime then
				DoBackgroundFadeOut()
			end
		elseif not textIsFaded then
			local timeDiff = tick() - lastTextFadeTime
			if timeDiff > ChatSettings.ChatWindowTextFadeOutTime then
				DoTextFadeOut()
			end
		else
			fadedChanged.Event:wait()
		end
	end
end)

function getClassicChatEnabled()
	if ChatSettings.ClassicChatEnabled ~= nil then
		return ChatSettings.ClassicChatEnabled
	end
	return Players.ClassicChat
end

function getBubbleChatEnabled()
	if ChatSettings.BubbleChatEnabled ~= nil then
		return ChatSettings.BubbleChatEnabled
	end
	return Players.BubbleChat
end

function bubbleChatOnly()
	return not getClassicChatEnabled() and getBubbleChatEnabled()
end

function UpdateMousePosition(mousePos)
	if
		not (
			moduleApiTable.Visible
			and moduleApiTable.IsCoreGuiEnabled
			and (moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)
		)
	then
		return
	end

	if bubbleChatOnly() then
		return
	end

	local windowPos = ChatWindow.GuiObject.AbsolutePosition
	local windowSize = ChatWindow.GuiObject.AbsoluteSize

	local newMouseState = CheckIfPointIsInSquare(mousePos, windowPos, windowPos + windowSize)
	if newMouseState ~= mouseIsInWindow then
		UpdateFadingForMouseState(newMouseState)
	end
end

UserInputService.InputChanged:connect(function(inputObject)
	if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
		local mousePos = Vector2.new(inputObject.Position.X, inputObject.Position.Y)
		UpdateMousePosition(mousePos)
	end
end)

UserInputService.TouchTap:connect(function(tapPos, gameProcessedEvent)
	UpdateMousePosition(tapPos[1])
end)

UserInputService.TouchMoved:connect(function(inputObject, gameProcessedEvent)
	local tapPos = Vector2.new(inputObject.Position.X, inputObject.Position.Y)
	UpdateMousePosition(tapPos)
end)

UserInputService.Changed:connect(function(prop)
	if prop == "MouseBehavior" then
		if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
			local windowPos = ChatWindow.GuiObject.AbsolutePosition
			local windowSize = ChatWindow.GuiObject.AbsoluteSize
			local screenSize = GuiParent.AbsoluteSize

			local centerScreenIsInWindow = CheckIfPointIsInSquare(screenSize / 2, windowPos, windowPos + windowSize)
			if centerScreenIsInWindow then
				UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			end
		end
	end
end)
