local h=game:GetService("\071\117\105\083\101\114\118\105\099\101")local C=require(script.Parent.Parent.Roact)local A=C.Component:extend("\085\073\083\099\097\108\101\100\083\099\114\101\101\110\071\117\105")function A.init(h)h.ref=C.createRef()h.scaleBinding,h.updateScaleBinding=C.createBinding(1)end A.defaultProps={minAxisSize=320;maxAxisSize=1080,minScaleRatio=.3,maxScaleRatio=1}function A.render(h)return C.createElement("\085\073\083\099\097\108\101",{Scale=h.scaleBinding;[C.Ref]=h.ref,[C.Event.AncestryChanged]=function()h:updateScale()end})end function A.calculateRatio(t,C,A)local B=h:GetGuiInset()local E=t.props.minAxisSize if not A then E=E-B.Y end local n=t.props.maxAxisSize local c=n-E local i=((C-E)/c)*(t.props.maxScaleRatio-t.props.minScaleRatio)+t.props.minScaleRatio return math.clamp(i,.3,1)end function A.clearListener(h)if h.viewportChangedListener then h.viewportChangedListener:Disconnect()h.viewportChangedListener=nil end end function A.updateScale(h)h:clearListener()local C=h.ref:getValue()if C then local A=C:FindFirstAncestorWhichIsA("\083\099\114\101\101\110\071\117\105")or C:FindFirstAncestorWhichIsA("\068\111\099\107\087\105\100\103\101\116\080\108\117\103\105\110\071\117\105")if A then local C=A.AbsoluteSize.X local t=A.AbsoluteSize.Y local B=not A:IsA("\068\111\099\107\087\105\100\103\101\116\080\108\117\103\105\110\071\117\105")and A.IgnoreGuiInset h.updateScaleBinding(h:calculateRatio(math.min(C,t),B))h.viewportChangedListener=(A:GetPropertyChangedSignal("\065\098\115\111\108\117\116\101\083\105\122\101")):Connect(function()h.updateScaleBinding(h:calculateRatio(math.min(A.AbsoluteSize.X,A.AbsoluteSize.Y),B))end)end end end function A.didMount(h)h:updateScale()end function A.didUpdate(h)h:updateScale()end function A.willUnmount(h)h:clearListener()end return A
local GuiService = game:GetService("GuiService")
local Roact = require(script.Parent.Parent.Roact)
local AutoUIScale = Roact.Component:extend("UIScaledScreenGui")

function AutoUIScale:init()
	self.ref = Roact.createRef()
	self.scaleBinding, self.updateScaleBinding = Roact.createBinding(1)
end

AutoUIScale.defaultProps = {
	minAxisSize = 320, -- minimum supported resolution (on small axis)
	maxAxisSize = 1080, -- max supported resolution (on small axis)
	minScaleRatio = 0.3, -- min scale ratio
	maxScaleRatio = 1, -- max scale ratio
}

function AutoUIScale:render()
	return Roact.createElement("UIScale", {
		Scale = self.scaleBinding,
		[Roact.Ref] = self.ref,
		[Roact.Event.AncestryChanged] = function()
			self:updateScale()
		end,
	})
end

function AutoUIScale:calculateRatio(minAxis, ignoreGuiInset)
	local guiInset = GuiService:GetGuiInset()
	local minAxisSize = self.props.minAxisSize
	if not ignoreGuiInset then
		minAxisSize = minAxisSize - guiInset.Y
	end
	local maxAxisSize = self.props.maxAxisSize
	local delta = maxAxisSize - minAxisSize
	local ratio = (minAxis - minAxisSize) / delta * (self.props.maxScaleRatio - self.props.minScaleRatio)
		+ self.props.minScaleRatio
	return math.clamp(ratio, 0.3, 1)
end

function AutoUIScale:clearListener()
	if self.viewportChangedListener then
		self.viewportChangedListener:Disconnect()
		self.viewportChangedListener = nil
	end
end

function AutoUIScale:updateScale()
	self:clearListener()
	local ref = self.ref:getValue()
	if ref then
		local viewport = ref:FindFirstAncestorWhichIsA("ScreenGui")
			or ref:FindFirstAncestorWhichIsA("DockWidgetPluginGui")
		if viewport then
			local absX = viewport.AbsoluteSize.X
			local absY = viewport.AbsoluteSize.Y
			local ignoredGuiInset = (not viewport:IsA("DockWidgetPluginGui") and viewport.IgnoreGuiInset)
			self.updateScaleBinding(self:calculateRatio(math.min(absX, absY), ignoredGuiInset))
			self.viewportChangedListener = viewport:GetPropertyChangedSignal("AbsoluteSize"):Connect(function()
				self.updateScaleBinding(
					self:calculateRatio(math.min(viewport.AbsoluteSize.X, viewport.AbsoluteSize.Y), ignoredGuiInset)
				)
			end)
		end
	end
end

function AutoUIScale:didMount()
	self:updateScale()
end

function AutoUIScale:didUpdate()
	self:updateScale()
end

function AutoUIScale:willUnmount()
	self:clearListener()
end

return AutoUIScale
