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

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)
