Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created August 22, 2021 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howmanysmall/c75702340cd05bd0506927d8e7a0f03c to your computer and use it in GitHub Desktop.
Save howmanysmall/c75702340cd05bd0506927d8e7a0f03c to your computer and use it in GitHub Desktop.
local Workspace = game:GetService("Workspace")
local GuiService = game:GetService("GuiService")
local Fusion = require("Fusion")
local Janitor = require("Janitor")
local TopInset, BottomInset = GuiService:GetGuiInset()
export type Properties = {
Scale: number?,
Size: Vector2,
}
type RequiredProperties = {
Scale: number,
Size: Vector2,
}
local function Llama_Dictionary_join(TableA, TableB)
local NewTable = {}
for Index, Value in next, TableA do
NewTable[Index] = Value
end
for Index, Value in next, TableB do
NewTable[Index] = Value
end
return NewTable
end
local DEFAULT_PROPERTIES = {Scale = 1}
local function Scale(BaseProperties: Properties)
local Properties = Llama_Dictionary_join(DEFAULT_PROPERTIES, BaseProperties) :: RequiredProperties
local CurrentScale = Fusion.State(1)
local ScaleJanitor = Janitor.new()
local CurrentCamera = Workspace.CurrentCamera
local function Update()
local CurrentSize = Properties.Size
local ViewportSize = CurrentCamera.ViewportSize - TopInset + BottomInset
CurrentScale:set(1 / math.max(CurrentSize.X / ViewportSize.X, CurrentSize.Y / ViewportSize.Y))
end
Update()
ScaleJanitor:Add(CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(Update), "Disconnect", "ViewportSize")
ScaleJanitor:Add(Workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
CurrentCamera = Workspace.CurrentCamera
ScaleJanitor:Add(CurrentCamera:GetPropertyChangedSignal("ViewportSize"):Connect(Update), "Disconnect", "ViewportSize")
end), "Disconnect")
local UIScale = Fusion.New("UIScale") {
Scale = Fusion.Computed(function()
return CurrentScale:get() * Properties.Scale
end);
} :: UIScale
ScaleJanitor:LinkToInstance(UIScale)
return UIScale
end
return Scale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment