Add new create syntax

This commit is contained in:
11poe 2024-11-16 12:26:57 +00:00 committed by centau
parent 8799988851
commit dccb0e3b66
2 changed files with 66 additions and 40 deletions

View file

@ -20,8 +20,8 @@ setmetatable(ctor_cache :: any, {
end end
local function ctor(properties: Props): Instance local function ctor(properties: Props): Instance
return apply(instance:Clone(), properties) return apply(instance:Clone(), properties)
end end
self[class] = ctor self[class] = ctor
return ctor return ctor
@ -40,44 +40,53 @@ local function clone_instance(instance: Instance)
end end
end end
local function create(class_or_instance: string|Instance): (Props) -> Instance local function create(class_or_instance: string | Instance, props: Props?): ((Props) -> Instance) | Instance
if type(class_or_instance) == "string" then local result: (Props) -> Instance
return create_instance(class_or_instance) if type(class_or_instance) == "string" then
elseif typeof(class_or_instance) == "Instance" then result = create_instance(class_or_instance)
return clone_instance(class_or_instance) elseif typeof(class_or_instance) == "Instance" then
else result = clone_instance(class_or_instance)
throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance)) else
return nil :: never throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance))
end return nil :: never
end
if props then
return result(props)
end
return result
end end
type Props = { [any]: any } type Props = { [any]: any }
return (create :: any) ::
( <T>(T & Instance) -> (Props) -> T ) & type Create<Name, Instance> = ((Name, Props) -> Instance) & ((Name) -> (Props) -> Instance)
( ("Folder") -> (Props) -> Folder ) &
( ("BillboardGui") -> (Props) -> BillboardGui ) & return (create :: any) ::
( ("CanvasGroup") -> (Props) -> CanvasGroup ) & & ( <T>(T & Instance) -> (Props) -> T )
( ("Frame") -> (Props) -> Frame ) & & ( <T>(T & Instance, Props) -> T )
( ("ImageButton") -> (Props) -> ImageButton ) & & Create<"Folder", Folder>
( ("ImageLabel") -> (Props) -> ImageLabel ) & & Create<"BillboardGui", BillboardGui>
( ("ScreenGui") -> (Props) -> ScreenGui ) & & Create<"CanvasGroup", CanvasGroup>
( ("ScrollingFrame") -> (Props) -> ScrollingFrame ) & & Create<"Frame", Frame>
( ("SurfaceGui") -> (Props) -> SurfaceGui ) & & Create<"ImageButton", ImageButton>
( ("TextBox") -> (Props) -> TextBox ) & & Create<"ImageLabel", ImageLabel>
( ("TextButton") -> (Props) -> TextButton ) & & Create<"ScreenGui", ScreenGui>
( ("TextLabel") -> (Props) -> TextLabel ) & & Create<"ScrollingFrame", ScrollingFrame>
( ("UIAspectRatioConstraint") -> (Props) -> UIAspectRatioConstraint ) & & Create<"SurfaceGui", SurfaceGui>
( ("UICorner") -> (Props) -> UICorner ) & & Create<"TextBox", TextBox>
( ("UIGradient") -> (Props) -> UIGradient ) & & Create<"TextButton", TextButton>
( ("UIGridLayout") -> (Props) -> UIGridLayout ) & & Create<"TextLabel", TextLabel>
( ("UIListLayout") -> (Props) -> UIListLayout ) & & Create<"UIAspectRatioConstraint", UIAspectRatioConstraint>
( ("UIPadding") -> (Props) -> UIPadding ) & & Create<"UICorner", UICorner>
( ("UIPageLayout") -> (Props) -> UIPageLayout ) & & Create<"UIGradient", UIGradient>
( ("UIScale") -> (Props) -> UIScale ) & & Create<"UIGridLayout", UIGridLayout>
( ("UISizeConstraint") -> (Props) -> UISizeConstraint ) & & Create<"UIListLayout", UIListLayout>
( ("UIStroke") -> (Props) -> UIStroke ) & & Create<"UIPadding", UIPadding>
( ("UITableLayout") -> (Props) -> UITableLayout ) & & Create<"UIPageLayout", UIPageLayout>
( ("UITextSizeConstraint") -> (Props) -> UITextSizeConstraint ) & & Create<"UIScale", UIScale>
( ("VideoFrame") -> (Props) -> VideoFrame ) & & Create<"UISizeConstraint", UISizeConstraint>
( ("ViewportFrame") -> (Props) -> ViewportFrame ) & & Create<"UIStroke", UIStroke>
( (string) -> (Props) -> Instance ) & Create<"UITableLayout", UITableLayout>
& Create<"UITextSizeConstraint", UITextSizeConstraint>
& Create<"VideoFrame", VideoFrame>
& Create<"ViewportFrame", ViewportFrame>
& Create<string, Instance>

View file

@ -721,6 +721,23 @@ TEST("create()", wrap_root(function()
local source = vide.source local source = vide.source
local cleanup = vide.cleanup local cleanup = vide.cleanup
do CASE "create(\"ClassName\", props) syntax"
local frame = create("Frame", { BackgroundTransparency = 0.5, Size = UDim2.fromScale(1, 1) })
CHECK(typeof(frame) == "Instance")
CHECK(frame:IsA("Frame"))
CHECK(frame.BackgroundTransparency == 0.5)
CHECK(frame.Size == UDim2.fromScale(1, 1))
end
do CASE "create(Instance, props) syntax"
local frame0 = create("Frame", { BackgroundTransparency = 0.5, Size = UDim2.fromScale(1, 1) })
local frame = create(frame0, { BackgroundTransparency = 1 })
CHECK(typeof(frame) == "Instance")
CHECK(frame:IsA("Frame"))
CHECK(frame.BackgroundTransparency == 1)
CHECK(frame.Size == UDim2.fromScale(1, 1))
end
do CASE "apply default properties" do CASE "apply default properties"
local defaults = require "../src/defaults" local defaults = require "../src/defaults"
local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any } local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any }