function CheckIfPointIsInSquare(y,c,T)return c.X<=y.X and(y.X<=T.X and(c.Y<=y.Y and y.Y<=T.Y))end local y=false local c=false local T=0 local F=0 local E=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")local l=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")function DoBackgroundFadeIn(c)F=tick()y=false E:Fire()ChatWindow:FadeInBackground(c or ChatSettings.ChatDefaultFadeDuration)local T=ChatWindow:GetCurrentChannel()if T then local y=MessageLogDisplay.Scroller y.ScrollingEnabled=true y.ScrollBarThickness=moduleMessageLogDisplay.ScrollBarThickness end end function DoBackgroundFadeOut(c)F=tick()y=true E:Fire()ChatWindow:FadeOutBackground(c or ChatSettings.ChatDefaultFadeDuration)local T=ChatWindow:GetCurrentChannel()if T then local y=MessageLogDisplay.Scroller y.ScrollingEnabled=false y.ScrollBarThickness=0 end end function DoTextFadeIn(y)T=tick()c=false E:Fire()ChatWindow:FadeInText((y or ChatSettings.ChatDefaultFadeDuration)*0)end function DoTextFadeOut(y)T=tick()c=true E:Fire()ChatWindow:FadeOutText(y 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 n=nil function UpdateFadingForMouseState(y)n=y H:Fire()if ChatBar:IsFocused()then return end if y then DoBackgroundFadeIn()DoTextFadeIn()else DoBackgroundFadeIn()end end spawn(function()while true do RunService.RenderStepped:Wait()while n or ChatBar:IsFocused()do if n then H.Event:Wait()end if ChatBar:IsFocused()then l.Event:Wait()end end if not y then local y=tick()-F if y>ChatSettings.ChatWindowBackgroundFadeOutTime then DoBackgroundFadeOut()end elseif not c then local y=tick()-T if y>ChatSettings.ChatWindowTextFadeOutTime then DoTextFadeOut()end else E.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(y)if not(moduleApiTable.Visible and(moduleApiTable.IsCoreGuiEnabled and(moduleApiTable.TopbarEnabled or ChatSettings.ChatOnWithTopBarOff)))then return end if bubbleChatOnly()then return end local c=ChatWindow.GuiObject.AbsolutePosition local T=ChatWindow.GuiObject.AbsoluteSize local F=CheckIfPointIsInSquare(y,c,c+T)if F~=n then UpdateFadingForMouseState(F)end end UserInputService.InputChanged:connect(function(y)if y.UserInputType==Enum.UserInputType.MouseMovement then local c=Vector2.new(y.Position.X,y.Position.Y)UpdateMousePosition(c)end end)UserInputService.TouchTap:connect(function(y,c)UpdateMousePosition(y[1])end)UserInputService.TouchMoved:connect(function(y,c)local T=Vector2.new(y.Position.X,y.Position.Y)UpdateMousePosition(T)end)UserInputService.Changed:connect(function(y)if y=="\077\111\117\115\101\066\101\104\097\118\105\111\114"then if UserInputService.MouseBehavior==Enum.MouseBehavior.LockCenter then local y=ChatWindow.GuiObject.AbsolutePosition local c=ChatWindow.GuiObject.AbsoluteSize local T=GuiParent.AbsoluteSize local F=CheckIfPointIsInSquare(T/2,y,y+c)if F 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)
