mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Add new create syntax
This commit is contained in:
parent
8799988851
commit
dccb0e3b66
2 changed files with 66 additions and 40 deletions
|
|
@ -40,44 +40,53 @@ local function clone_instance(instance: Instance)
|
|||
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
|
||||
local result: (Props) -> Instance
|
||||
if type(class_or_instance) == "string" then
|
||||
return create_instance(class_or_instance)
|
||||
result = create_instance(class_or_instance)
|
||||
elseif typeof(class_or_instance) == "Instance" then
|
||||
return clone_instance(class_or_instance)
|
||||
result = clone_instance(class_or_instance)
|
||||
else
|
||||
throw("bad argument #1, expected string or instance, got " .. typeof(class_or_instance))
|
||||
return nil :: never
|
||||
end
|
||||
if props then
|
||||
return result(props)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
type Props = { [any]: any }
|
||||
|
||||
type Create<Name, Instance> = ((Name, Props) -> Instance) & ((Name) -> (Props) -> Instance)
|
||||
|
||||
return (create :: any) ::
|
||||
( <T>(T & Instance) -> (Props) -> T ) &
|
||||
( ("Folder") -> (Props) -> Folder ) &
|
||||
( ("BillboardGui") -> (Props) -> BillboardGui ) &
|
||||
( ("CanvasGroup") -> (Props) -> CanvasGroup ) &
|
||||
( ("Frame") -> (Props) -> Frame ) &
|
||||
( ("ImageButton") -> (Props) -> ImageButton ) &
|
||||
( ("ImageLabel") -> (Props) -> ImageLabel ) &
|
||||
( ("ScreenGui") -> (Props) -> ScreenGui ) &
|
||||
( ("ScrollingFrame") -> (Props) -> ScrollingFrame ) &
|
||||
( ("SurfaceGui") -> (Props) -> SurfaceGui ) &
|
||||
( ("TextBox") -> (Props) -> TextBox ) &
|
||||
( ("TextButton") -> (Props) -> TextButton ) &
|
||||
( ("TextLabel") -> (Props) -> TextLabel ) &
|
||||
( ("UIAspectRatioConstraint") -> (Props) -> UIAspectRatioConstraint ) &
|
||||
( ("UICorner") -> (Props) -> UICorner ) &
|
||||
( ("UIGradient") -> (Props) -> UIGradient ) &
|
||||
( ("UIGridLayout") -> (Props) -> UIGridLayout ) &
|
||||
( ("UIListLayout") -> (Props) -> UIListLayout ) &
|
||||
( ("UIPadding") -> (Props) -> UIPadding ) &
|
||||
( ("UIPageLayout") -> (Props) -> UIPageLayout ) &
|
||||
( ("UIScale") -> (Props) -> UIScale ) &
|
||||
( ("UISizeConstraint") -> (Props) -> UISizeConstraint ) &
|
||||
( ("UIStroke") -> (Props) -> UIStroke ) &
|
||||
( ("UITableLayout") -> (Props) -> UITableLayout ) &
|
||||
( ("UITextSizeConstraint") -> (Props) -> UITextSizeConstraint ) &
|
||||
( ("VideoFrame") -> (Props) -> VideoFrame ) &
|
||||
( ("ViewportFrame") -> (Props) -> ViewportFrame ) &
|
||||
( (string) -> (Props) -> Instance )
|
||||
& ( <T>(T & Instance) -> (Props) -> T )
|
||||
& ( <T>(T & Instance, Props) -> T )
|
||||
& Create<"Folder", Folder>
|
||||
& Create<"BillboardGui", BillboardGui>
|
||||
& Create<"CanvasGroup", CanvasGroup>
|
||||
& Create<"Frame", Frame>
|
||||
& Create<"ImageButton", ImageButton>
|
||||
& Create<"ImageLabel", ImageLabel>
|
||||
& Create<"ScreenGui", ScreenGui>
|
||||
& Create<"ScrollingFrame", ScrollingFrame>
|
||||
& Create<"SurfaceGui", SurfaceGui>
|
||||
& Create<"TextBox", TextBox>
|
||||
& Create<"TextButton", TextButton>
|
||||
& Create<"TextLabel", TextLabel>
|
||||
& Create<"UIAspectRatioConstraint", UIAspectRatioConstraint>
|
||||
& Create<"UICorner", UICorner>
|
||||
& Create<"UIGradient", UIGradient>
|
||||
& Create<"UIGridLayout", UIGridLayout>
|
||||
& Create<"UIListLayout", UIListLayout>
|
||||
& Create<"UIPadding", UIPadding>
|
||||
& Create<"UIPageLayout", UIPageLayout>
|
||||
& Create<"UIScale", UIScale>
|
||||
& Create<"UISizeConstraint", UISizeConstraint>
|
||||
& Create<"UIStroke", UIStroke>
|
||||
& Create<"UITableLayout", UITableLayout>
|
||||
& Create<"UITextSizeConstraint", UITextSizeConstraint>
|
||||
& Create<"VideoFrame", VideoFrame>
|
||||
& Create<"ViewportFrame", ViewportFrame>
|
||||
& Create<string, Instance>
|
||||
|
|
|
|||
|
|
@ -721,6 +721,23 @@ TEST("create()", wrap_root(function()
|
|||
local source = vide.source
|
||||
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"
|
||||
local defaults = require "../src/defaults"
|
||||
local frame = create "Frame" {} :: Instance & { BorderSizePixel: any, BorderColor3: any }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue