-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.lua
86 lines (68 loc) · 1.75 KB
/
create.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local Runtime = require(script.Parent.Runtime)
--[=[
@within Plasma
@function create
@param className string -- The class name of the Instance to create
@param props CreateProps
@return Instance -- The created instance
@tag utilities
A function that creates an Instance tree.
CreateProps is a table:
- String keys are interpreted as properties to set
- Numerical keys are interpreted as children
- Function values are interpreted as event handlers
- Table keys can be used to get references to instances deep in the tree, the value becomes the key in the table
This function doesn't do anything special. It just creates an instance.
```lua
create("Frame", {
BackgroundTransparency = 1,
Name = "Checkbox",
create("TextButton", {
BackgroundColor3 = Color3.fromRGB(54, 54, 54),
Size = UDim2.new(0, 30, 0, 30),
create("UICorner", {
CornerRadius = UDim.new(0, 8),
}),
Activated = function()
setClicked(true)
end,
}),
})
```
Getting references to instances deep in a tree:
```lua
local ref = {}
create("Frame", {
create("TextButton", {
[ref] = "button",
Text = "hi"
})
})
print(ref.button.Text) --> hi
```
]=]
local function create(className, props)
props = props or {}
local eventCallback = Runtime.useEventCallback()
local instance = Instance.new(className)
for key, value in pairs(props) do
if type(value) == "function" then
if eventCallback then
eventCallback(instance, key, value)
else
instance[key]:Connect(value)
end
elseif type(key) == "number" then
value.Parent = instance
elseif type(key) == "table" then
key[value] = instance
if props.Name == nil then
instance.Name = value
end
else
instance[key] = value
end
end
return instance
end
return create