-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableTop_FlexTableControl.lua
More file actions
513 lines (441 loc) · 15.9 KB
/
TableTop_FlexTableControl.lua
File metadata and controls
513 lines (441 loc) · 15.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
tableHeightOffset = -9
function onSave()
saved_data = JSON.encode({tid=tableImageData, cd=checkData})
--saved_data = ""
return saved_data
end
function onload(saved_data)
--Loads the tracking for if the game has started yet
if saved_data ~= "" then
local loaded_data = JSON.decode(saved_data)
tableImageData = loaded_data.tid
checkData = loaded_data.cd
else
tableImageData = {}
checkData = {move=false, scale=false}
end
--Disables interactable status of objects with GUID in list
for _, guid in ipairs(ref_noninteractable) do
local obj = getObjectFromGUID(guid)
if obj then obj.interactable = false end
end
--Establish references to table parts
obj_leg1 = getObjectFromGUID("afc863")
obj_leg2 = getObjectFromGUID("c8edca")
obj_leg3 = getObjectFromGUID("393bf7")
obj_leg4 = getObjectFromGUID("12c65e")
obj_surface = getObjectFromGUID("4ee1f2")
obj_side_top = getObjectFromGUID("35b95f")
obj_side_bot = getObjectFromGUID("f938a2")
obj_side_lef = getObjectFromGUID("9f95fd")
obj_side_rig = getObjectFromGUID("5af8f2")
controlActive = false
createOpenCloseButton()
end
--Activation/deactivation of control panel
--Activated by clicking on
function click_toggleControl(_, color)
if permissionCheck(color) then
if not controlActive then
--Activate control panel
controlActive = true
self.clearButtons()
createOpenCloseButton()
createSurfaceInput()
createSurfaceButtons()
createScaleInput()
createScaleButtons()
else
--Deactivate control panel
controlActive = false
self.clearButtons()
self.clearInputs()
createOpenCloseButton()
end
end
end
--Table surface control
--Changes table surface
function click_applySurface(_, color)
if permissionCheck(color) then
updateSurface()
broadcastToAll("New Table Image Applied", {0.2,0.9,0.2})
end
end
--Saves table surface
function click_saveSurface(_, color)
if permissionCheck(color) then
local nickname = self.getInputs()[1].value
local url = self.getInputs()[2].value
if nickname == "" then
--No nickname
broadcastToAll("Please supply a nickname for this save.", {0.9,0.2,0.2})
else
--Nickname exists
if findInImageDataIndex(url, nickname) == nil then
--Save doesn't exist already
table.insert(tableImageData, {url=url, name=nickname})
broadcastToAll("Image URL saved to memory.", {0.2,0.9,0.2})
--Refresh buttons
self.clearButtons()
createOpenCloseButton()
createSurfaceButtons()
createScaleButtons()
else
--Save exists already
broadcastToAll("Memory already contains a save with this Name or URL. Delete it first.", {0.9,0.2,0.2})
end
end
end
end
--Loads table surface
function click_loadMemory(_, color, index)
if permissionCheck(color) then
self.editInput({index=0, value=tableImageData[index].name})
self.editInput({index=1, value=tableImageData[index].url})
updateSurface()
broadcastToAll("Table Image Loaded", {0.2,0.9,0.2})
end
end
--Deletes table surface
function click_deleteMemory(_, color, index)
if permissionCheck(color) then
table.remove(tableImageData, index)
self.clearButtons()
createOpenCloseButton()
createSurfaceButtons()
createScaleButtons()
broadcastToAll("Element Removed from Memory", {0.2,0.9,0.2})
end
end
--Updates surface from the values in the input field
function updateSurface()
local customInfo = obj_surface.getCustomObject()
customInfo.diffuse = self.getInputs()[2].value
obj_surface.setCustomObject(customInfo)
obj_surface = obj_surface.reload()
end
--Table Scale control
--Applies Scale to table pieces
function click_applyScale(_, color)
if permissionCheck(color) then
local newWidth = tonumber(self.getInputs()[3].value)
local newDepth = tonumber(self.getInputs()[4].value)
if type(newWidth) ~= "number" then
broadcastToAll("Invalid Width", {0.9,0.2,0.2})
return
elseif type(newDepth) ~= "number" then
broadcastToAll("Invalid Depth", {0.9,0.2,0.2})
return
elseif newWidth<0.1 or newDepth<0.1 then
broadcastToAll("Scale cannot go below 0.1", {0.9,0.2,0.2})
return
elseif newWidth>12 or newDepth>12 then
broadcastToAll("Scale should not go over 12 (world size limitation)", {0.9,0.2,0.2})
return
else
changeTableScale(math.abs(newWidth), math.abs(newDepth))
broadcastToAll("Scale applied.", {0.2,0.9,0.2})
end
end
end
--Checks/unchecks move box for hands
function click_checkMove(_, color)
if permissionCheck(color) then
local find_func = function(o) return o.click_function=="click_checkMove" end
if checkData.move == true then
checkData.move = false
local buttonEntry = findButton(self, find_func)
self.editButton({index=buttonEntry.index, label=""})
else
checkData.move = true
local buttonEntry = findButton(self, find_func)
self.editButton({index=buttonEntry.index, label=string.char(10008)})
end
end
end
--Checks/unchecks scale box for hands
--This button was disabled for technical reasons
--[[
function click_checkScale(_, color)
if permissionCheck(color) then
local find_func = function(o) return o.click_function=="click_checkScale" end
if checkData.scale == true then
checkData.scale = false
local buttonEntry = findButton(self, find_func)
self.editButton({index=buttonEntry.index, label=""})
else
checkData.scale = true
local buttonEntry = findButton(self, find_func)
self.editButton({index=buttonEntry.index, label=string.char(10008)})
end
end
end
]]
--Alters scale of elements and moves them
function changeTableScale(width, depth)
--Scaling factors used to translate scale to position offset
local width2pos = (width-1) * 18
local depth2pos = (depth-1) * 18
--Hand zone movement
if checkData.move == true then
for _, pc in ipairs(ref_playerColor) do
if Player[pc].getHandCount() > 0 then
moveHandZone(Player[pc], width2pos, depth2pos)
end
end
end
--Hand zone scaling
--The button to enable this was disabled for technical reasons
if checkData.scale == true then
for _, pc in ipairs(ref_playerColor) do
if Player[pc].getHandCount() > 0 then
scaleHandZone(Player[pc], width, depth)
end
end
end
--Resizing table elements
obj_side_top.setScale({width, 1, 1})
obj_side_bot.setScale({width, 1, 1})
obj_side_lef.setScale({depth, 1, 1})
obj_side_rig.setScale({depth, 1, 1})
obj_surface.setScale({width, 1, depth})
--Moving table elements to accomodate new scale
obj_side_lef.setPosition({-width2pos,tableHeightOffset,0})
obj_side_rig.setPosition({ width2pos,tableHeightOffset,0})
obj_side_top.setPosition({0,tableHeightOffset, depth2pos})
obj_side_bot.setPosition({0,tableHeightOffset,-depth2pos})
obj_leg1.setPosition({-width2pos,tableHeightOffset,-depth2pos})
obj_leg2.setPosition({-width2pos,tableHeightOffset, depth2pos})
obj_leg3.setPosition({ width2pos,tableHeightOffset, depth2pos})
obj_leg4.setPosition({ width2pos,tableHeightOffset,-depth2pos})
self.setPosition(obj_leg4.positionToWorld({-22.12, 8.74,-19.16}))
--Only enabled when changing tableHeightOffset
--obj_surface.setPosition({0,tableHeightOffset,0})
end
--Move hand zone, p=player reference, facts are scaling factors
function moveHandZone(p, width2pos, depth2pos)
local widthX = obj_side_rig.getPosition().x
local depthZ = obj_side_top.getPosition().z
for i=1, p.getHandCount() do
local handT = p.getHandTransform()
local pos = handT.position
local y = handT.rotation.y
if y<45 or y>320 or y>135 and y<225 then
if pos.z > 0 then
pos.z = pos.z + depth2pos - depthZ
else
pos.z = pos.z - depth2pos + depthZ
end
else
if pos.x > 0 then
pos.x = pos.x + width2pos - widthX
else
pos.x = pos.x - width2pos + widthX
end
end
--Only enabled when changing tableHeightOffset
--pos.y = tableHeightOffset + 14
handT.position = pos
p.setHandTransform(handT, i)
end
end
---Scales hand zones, p=player reference, facts are scaling factors
function scaleHandZone(p, width, depth)
local widthFact = width / obj_side_top.getScale().x
local depthFact = depth / obj_side_lef.getScale().x
for i=1, p.getHandCount() do
local handT = p.getHandTransform()
local scale = handT.scale
local y = handT.rotation.y
if y<45 or y>320 or y>135 and y<225 then
scale.x = scale.x * widthFact
else
scale.x = scale.x * depthFact
end
handT.scale = scale
p.setHandTransform(handT, i)
end
end
--Information gathering
--Checks if a color is promoted or host
function permissionCheck(color)
if Player[color].host==true or Player[color].promoted==true then
return true
else
return false
end
end
--Locates a string saved within memory file
function findInImageDataIndex(...)
for _, str in ipairs({...}) do
for i, v in ipairs(tableImageData) do
if v.url == str or v.name == str then
return i
end
end
end
return nil
end
--Round number (num) to the Nth decimal (dec)
function round(num, dec)
local mult = 10^(dec or 0)
return math.floor(num * mult + 0.5) / mult
end
--Locates a button with a helper function
function findButton(obj, func)
if func==nil then error("No func supplied to findButton") end
for _, v in ipairs(obj.getButtons()) do
if func(v) then
return v
end
end
return nil
end
--Creation of buttons/inputs
function createOpenCloseButton()
local tooltip = "Open Table Control Panel"
if controlActive then
tooltip = "Close Table Control Panel"
end
self.createButton({
click_function="click_toggleControl", function_owner=self,
position={0,0,0}, rotation={-45,0,0}, height=400, width=400,
color={1,1,1,0}, tooltip=tooltip
})
end
function createSurfaceInput()
local currentURL = obj_surface.getCustomObject().diffuse
local nickname = ""
if findInImageDataIndex(currentURL) ~= nil then
nickname = tableImageData[findInImageDataIndex(currentURL)].name
end
self.createInput({
label="Nickname", input_function="none", function_owner=self,
alignment=3, position={0,0,2}, height=224, width=4000,
font_size=200, tooltip="Enter nickname for table image (only used for save)",
value=nickname
})
self.createInput({
label="URL", input_function="none", function_owner=self,
alignment=3, position={0,0,3}, height=224, width=4000,
font_size=200, tooltip="Enter URL for tabletop image",
value=currentURL
})
end
function createSurfaceButtons()
--Label
self.createButton({
label="Tabletop Surface Image", click_function="none",
position={0,0,1}, height=0, width=0, font_size=300, font_color={1,1,1}
})
--Functional
self.createButton({
label="Apply Image\nTo Table", click_function="click_applySurface",
function_owner=self, tooltip="Apply URL as table image",
position={2,0,4}, height=440, width=1400, font_size=200,
})
self.createButton({
label="Save Image\nTo Memory", click_function="click_saveSurface",
function_owner=self, tooltip="Record URL into memory (requires nickname)",
position={-2,0,4}, height=440, width=1400, font_size=200,
})
--Label
self.createButton({
label="Load From Memory", click_function="none",
position={0,0,5.5}, height=0, width=0, font_size=300, font_color={1,1,1}
})
--Saves, created dynamically from memory file
for i, memoryEntry in ipairs(tableImageData) do
--Load
local funcName = i.."loadMemory"
local func = function(x,y) click_loadMemory(x,y,i) end
self.setVar(funcName, func)
self.createButton({
label=memoryEntry.name, click_function=funcName,
function_owner=self, tooltip=memoryEntry.url, font_size=200,
position={-0.6,0,6.5+0.5*(i-1)}, height=240, width=3300,
})
--Delete
local funcName = i.."deleteMemory"
local func = function(x,y) click_deleteMemory(x,y,i) end
self.setVar(funcName, func)
self.createButton({
label="DELETE", click_function=funcName,
function_owner=self, tooltip="",
position={3.6,0,6.5+0.5*(i-1)}, height=240, width=600,
font_size=160, font_color={1,0,0}, color={0.8,0.8,0.8}
})
end
end
function createScaleInput()
self.createInput({
label=string.char(8644), input_function="none", function_owner=self,
alignment=3, position={-8.5,0,2}, height=224, width=400,
font_size=200, tooltip="Table Width",
value=round(obj_side_top.getScale().x, 1)
})
self.createInput({
label=string.char(8645), input_function="none", function_owner=self,
alignment=3, position={-7.5,0,2}, height=224, width=400,
font_size=200, tooltip="Table Depth",
value=round(obj_side_lef.getScale().x, 1)
})
end
function createScaleButtons()
--Labels
self.createButton({
label="Table Scale", click_function="none",
position={-8,0,1}, height=0, width=0, font_size=300, font_color={1,1,1}
})
self.createButton({
label=string.char(8644).." "..string.char(8645),
click_function="none",
position={-8,0,2}, height=0, width=0, font_size=300, font_color={1,1,1}
})
self.createButton({
label="Move Hands:", click_function="none",
position={-8.3,0,3}, height=0, width=0, font_size=200, font_color={1,1,1}
})
--Disabled due to me removing the feature for technical reasons
--[[
self.createButton({
label="Scale Hands:", click_function="none",
position={-8.3,0,4}, height=0, width=0, font_size=200, font_color={1,1,1}
})
]]
--Checkboxes
local label = ""
if checkData.move == true then label = string.char(10008) end
self.createButton({
label=label, click_function="click_checkMove",
function_owner=self, tooltip="Check to move hands when table is rescaled",
position={-6.8,0,3}, height=224, width=224, font_size=200,
})
--[[
local label = ""
if checkData.scale == true then label = string.char(10008) end
self.createButton({
label=label, click_function="click_checkScale",
function_owner=self, tooltip="Check to scale the width of hands when table is rescaled",
position={-6.8,0,4}, height=224, width=224, font_size=200,
})
]]
--Apply button
self.createButton({
label="Apply Scale", click_function="click_applyScale",
function_owner=self, tooltip="Apply width/depth to table",
position={-8,0,4}, height=440, width=1400, font_size=200,
})
end
--Data tables
ref_noninteractable = {
"afc863","c8edca","393bf7","12c65e","f938a2","9f95fd","35b95f",
"5af8f2","4ee1f2","bd69bd"
}
ref_playerColor = {
"White", "Brown", "Red", "Orange", "Yellow",
"Green", "Teal", "Blue", "Purple", "Pink", "Black"
}
--Dummy function, absorbs unwanted triggers
function none() end