forked from sionar/Botc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLight.ttslua
269 lines (257 loc) · 9.13 KB
/
Light.ttslua
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
function openUI()
self.UI.hide("open")
self.UI.show("panel")
end
function closeUI()
self.UI.hide("panel")
self.UI.show("open")
end
local state = {}
local toRgb = function(value)
return {
(tonumber(value:sub(2,3),16) or 0)/255,
(tonumber(value:sub(4,5),16) or 0)/255,
(tonumber(value:sub(6,7),16) or 0)/255
}
end
function powerOn()
local light = self.getComponentInChildren("Light")
light.set("enabled", true)
state.power = true
end
function powerOff()
local light = self.getComponentInChildren("Light")
light.set("enabled", false)
state.power = false
end
function collision()
local collider = self.getChildren()[1].getComponent("BoxCollider")
collider.set("enabled", not(collider.get("enabled")))
state.collider = not(collider.get("enabled"))
end
function setProperties(data)
if (data.intensity ~= nil) then
local value = tonumber(data.intensity);
if (value ~= nil) then
self.getComponentInChildren("Light").set("intensity", value)
state.intensity = value
end
self.UI.setAttribute("intensity", "text", data.intensity)
end
if (data.color ~= nil) then
local value = data.color;
if (type(value) == "string") then
if (value:len() == 7 and value:sub(1,1) == "#") then
local clr = toRgb(value)
self.getComponentInChildren("Light").set("color", clr)
self.UI.setAttribute("color", "colors", ""..value.."|"..value.."|"..value.."")
state.color = value
end
end
self.UI.setAttribute("color", "text", data.color)
end
if (data.range ~= nil) then
local value = tonumber(data.range)
if (value ~= nil) then
self.getComponentInChildren("Light").set("range", value)
state.range = value
end
self.UI.setAttribute("range", "text", data.range)
end
if (state.power) then
local c = toRgb(state.color)
local i = math.log(state.intensity + 1)
self.setColorTint({c[1] * i, c[2] * i, c[3] * i})
else
self.setColorTint({0,0,0})
end
end
function ui_intensity(player, value) setProperties({intensity=value}) end
function ui_color(player, value) setProperties({color=value}) end
function ui_range(player, value) setProperties({range=value}) end
function onSave()
return JSON.encode(state)
end
function onLoad(save)
state = JSON.decode(save) or {
power = false,
visibility = true,
color = "#ffffff",
intensity = 1,
range = 10,
collider = true
}
local r = tonumber(state.color:sub(2,3),16)/255 or ""
local g = tonumber(state.color:sub(4,5),16)/255 or ""
local b = tonumber(state.color:sub(6,7),16)/255 or ""
self.getComponentInChildren("Light").set("enabled", state.power)
self.getComponentInChildren("Light").set("intensity", state.intensity)
self.getComponentInChildren("Light").set("color", {r,g,b})
self.getComponentInChildren("Light").set("range", state.range)
self.getChildren()[1].getComponent("BoxCollider").set("enabled", state.collider or true)
self.setLock(true)
local btnColors = "#ccccccff|#ffffffff|#404040ff|#808080ff"
local txtColors = "#ffffffff|#ffffffff|#ffffffff|#808080ff"
local assets = {
{name="ui_close", url = "https://raw.githubusercontent.com/RobMayer/TTSLibrary/master/ui/close.png"},
{name="ui_settings", url = "https://raw.githubusercontent.com/RobMayer/TTSLibrary/master/ui/gear.png"},
{name="ui_power", url = "https://raw.githubusercontent.com/RobMayer/TTSLibrary/master/ui/power.png"},
{name="ui_cube", url = "https://raw.githubusercontent.com/RobMayer/TTSLibrary/master/ui/cube.png"},
}
local ui = {
{
tag="Button",
attributes={
id="open",
width=50,
height=50,
position="0 0 -60",
onClick="openUI",
image="ui_settings",
colors="#ccccccff|#ffffffff|#404040ff|#808080ff",
}
},
{
tag="Panel",
attributes={
id="panel",
width=200,
height=100,
position="0 0 -60",
color="#000000cc",
active=false,
alignment="UpperLeft",
},
children={
{
tag="Text",
attributes={
color="#ffffffff",
width=175,
height=20,
rectAlignment="UpperLeft",
alignment="MiddleLeft",
offsetXY="5 -5",
text="Point",
}
},
{
tag="Button",
attributes={
onClick="collision",
width=20,
height=20,
image="ui_cube",
colors=btnColors,
rectAlignment="UpperRight",
offsetXY="-45 -5",
tooltip="Toggle Collisions",
tooltipPosition="Above",
}
},
{
tag="Button",
attributes={
onClick="powerOn",
width=20,
height=20,
image="ui_power",
colors=btnColors,
rectAlignment="UpperRight",
offsetXY="-25 -5",
tooltip="Power",
tooltipPosition="Above",
}
},
{
tag="Button",
attributes={
onClick="closeUI",
width=20,
height=20,
image="ui_close",
colors=btnColors,
rectAlignment="UpperRight",
offsetXY="-5 -5",
tooltip="Close",
tooltipPosition="Above",
}
},
{
tag="InputField",
attributes={
id="intensity",
onValueChanged="ui_intensity",
tooltip="Intensity",
tooltipPosition="Left",
text=state.intensity,
width=92.5,
height=30,
characterValidation="Decimal",
colors=txtColors,
characterLimit=5,
textAlignment="MiddleCenter",
rectAlignment="UpperLeft",
offsetXY="5 -30",
}
},
{
tag="InputField",
attributes={
id="color",
onValueChanged="ui_color",
tooltip="Color",
tooltipPosition="Right",
text=state.color,
width=92.5,
height=30,
colors=txtColors,
characterLimit=7,
textAlignment="MiddleCenter",
rectAlignment="UpperRight",
offsetXY="-5 -30",
colors=""..state.color.."|"..state.color.."|"..state.color..""
}
},
{
tag="InputField",
attributes={
id="range",
onValueChanged="ui_range",
tooltip="Range",
tooltipPosition="Left",
text=state.range,
width=92.5,
height=30,
characterValidation="Decimal",
colors=txtColors,
characterLimit=9,
textAlignment="MiddleCenter",
rectAlignment="UpperLeft",
offsetXY="5 -65",
}
},
{
tag="InputField",
attributes={
interactable=false,
id="angle",
tooltip="Angle",
tooltipPosition="Right",
text="N/A",
width=92.5,
height=30,
colors=txtColors,
characterLimit=3,
textAlignment="MiddleCenter",
rectAlignment="UpperRight",
offsetXY="-5 -65",
}
},
}
}
}
self.UI.setCustomAssets(assets)
self.UI.setXmlTable(ui)
collision()
end