-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.lua
More file actions
471 lines (433 loc) · 16.2 KB
/
library.lua
File metadata and controls
471 lines (433 loc) · 16.2 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
-- Core library
local CORE_LIBRARY = {
{
entryType = "actorBlueprint",
title = "wall",
description = "A rectangular solid that doesn't move.",
actorBlueprint = {
components = {
Drawing = {
url = "assets/rectangle.svg"
},
Body = {},
Solid = {}
}
}
},
{
entryType = "actorBlueprint",
title = "ball",
description = "A circular solid that falls.",
actorBlueprint = {
components = {
Drawing = {
url = "assets/circle.svg"
},
Body = {gravityScale = 1},
CircleShape = {},
Solid = {},
Falling = {}
}
}
}
}
if SHOW_TEXT_ACTORS then
table.insert(
CORE_LIBRARY,
{
entryType = "actorBlueprint",
title = "text",
description = "A humble block of text, pinned to the bottom of the card.",
actorBlueprint = {
components = {
Text = {
content = "Once upon a time..."
}
}
}
}
)
end
local assetNames = require "asset_names"
for _, assetName in ipairs(assetNames) do
-- SVG?
if assetName:match("%.svg$") then
table.insert(
CORE_LIBRARY,
{
entryType = "drawing",
title = assetName:gsub("%.svg", ""),
description = "A drawing from the default asset library.",
drawing = {
url = "assets/" .. assetName
}
}
)
end
end
-- Start / stop
function Common:startLibrary()
self.library = {} -- `entryId` -> entry
for _, entrySpec in pairs(CORE_LIBRARY) do
local entryId = self:generateId()
local entry = util.deepCopyTable(entrySpec)
entry.entryId = entryId
entry.isCore = true
self.library[entryId] = entry
end
end
-- Message receivers
function Common.receivers:addLibraryEntry(time, entryId, entry)
local entryCopy = util.deepCopyTable(entry)
entryCopy.entryId = entryId
self.library[entryId] = entryCopy
end
function Common.receivers:removeLibraryEntry(time, entryId)
self.library[entryId] = nil
end
function Common.receivers:updateLibraryEntry(time, clientId, entryId, newEntry, opts)
local oldEntry = self.library[entryId]
assert(oldEntry.entryType == newEntry.entryType, "updateLibraryEntry: cannot change entry type")
if self.clientId == clientId then
if newEntry.entryType == "actorBlueprint" and opts.updateActors then
local oldBp = oldEntry.actorBlueprint
local newBp = newEntry.actorBlueprint
local function valueEqual(value1, value2)
if type(value1) == "table" and type(value2) == "table" then
-- Quick hack for checking table equality...
return serpent.dump(value1, {sortkeys = true}) == serpent.dump(value2, {sortkeys = true})
else
return value1 == value2
end
end
-- Collect list of changes
local changes = {}
for behaviorName, newComponentBp in pairs(newBp.components) do
local oldComponentBp = oldBp.components[behaviorName]
if oldComponentBp then -- Component already existed, check value changes
for key, newValue in pairs(newComponentBp) do
if not valueEqual(oldComponentBp[key], newValue) then
table.insert(
changes,
{
changeType = "value",
behaviorName = behaviorName,
key = key,
newValue = newValue
}
)
end
end
for key, oldValue in pairs(oldComponentBp) do
if newComponentBp[key] == nil then -- Check for `nil`'d values skipped above
table.insert(
changes,
{
changeType = "value",
behaviorName = behaviorName,
key = key,
newValue = nil
}
)
end
end
else -- Component newly added
table.insert(
changes,
{
changeType = "add component",
behaviorName = behaviorName,
newComponentBp = newComponentBp
}
)
end
end
for behaviorName, oldComponentBp in pairs(oldBp.components) do
if newBp.components[behaviorName] == nil then -- Check for removed components skipped above
table.insert(
changes,
{
changeType = "remove component",
behaviorName = behaviorName
}
)
end
end
-- Update actors
for actorId, actor in pairs(self.actors) do
if actorId ~= opts.skipActorId and actor.parentEntryId == entryId then
local bp = self:blueprintActor(actorId) -- Start with old blueprint and merge changes
for _, change in ipairs(changes) do
local changeType, behaviorName, key = change.changeType, change.behaviorName, change.key
if changeType == "value" then
if bp.components[behaviorName] then
if
valueEqual(
oldBp.components[behaviorName][key],
bp.components[behaviorName][key]
)
then
-- Only change value if not overridden
bp.components[behaviorName][key] = change.newValue
end
end
elseif changeType == "add component" then
bp.components[behaviorName] = change.newComponentBp
elseif changeType == "remove component" then
bp.components[behaviorName] = nil
end
end
self:send("removeActor", self.clientId, actorId)
self:sendAddActor(
bp,
{
actorId = actorId,
parentEntryId = entryId
}
)
end
end
end
end
local newEntryCopy = util.deepCopyTable(newEntry)
newEntryCopy.entryId = entryId
self.library[entryId] = newEntryCopy
end
-- UI
local PAGE_SIZE = 10
local currPage = {}
function Client:uiLibrary(opts)
-- Reusable library UI component
opts = opts or {}
opts.id = opts.id or "library"
local order = {}
-- Add regular library entries
for entryId, entry in pairs(self.library) do
local skip = false
if opts.filter then
if not opts.filter(entry) then
skip = true
end
elseif opts.filterType then
if entry.entryType ~= opts.filterType then
skip = true
end
end
if not skip then
table.insert(order, entry)
end
end
-- Add behaviors unless filtered out
if not opts.filterType or opts.filterType == "behavior" then
for behaviorId, behavior in pairs(self.behaviors) do
if not opts.filterBehavior or opts.filterBehavior(behavior) then
local dependencyNames = {}
for name, dependency in pairs(behavior.dependencies) do
if name ~= "Body" then
table.insert(dependencyNames, "**" .. dependency:getUiName() .. "**")
end
end
local requiresLine = ""
if next(dependencyNames) then
requiresLine = "Needs " .. table.concat(dependencyNames, ", ") .. ".\n\n"
end
local shortDescription = (behavior.description and behavior.description:match("^[\n ]*[^\n]*")) or ""
table.insert(
order,
{
entryId = tostring(behaviorId),
entryType = "behavior",
title = behavior:getUiName(),
description = requiresLine .. shortDescription,
behaviorId = behaviorId
}
)
end
end
end
-- Sort
table.sort(
order,
function(entry1, entry2)
if entry1.behaviorId and entry2.behaviorId then
return entry1.behaviorId < entry2.behaviorId
end
return entry1.title:upper() < entry2.title:upper()
end
)
-- Paginate
if #order > PAGE_SIZE then
currPage[opts.id] = currPage[opts.id] or 1
local numPages = math.ceil(#order / PAGE_SIZE)
local newOrder = {}
for i = 1, PAGE_SIZE do
local j = PAGE_SIZE * (currPage[opts.id] - 1) + i
if j > #order then
break
end
table.insert(newOrder, order[j])
end
order = newOrder
ui.box(
"top page buttons",
{
flexDirection = "row"
},
function()
ui.button(
"previous page",
{
icon = "arrow-bold-left",
iconFamily = "Entypo",
hideLabel = true,
onClick = function()
currPage[opts.id] = math.max(1, currPage[opts.id] - 1)
end
}
)
ui.box(
"spacer",
{flex = 1},
function()
end
)
ui.markdown("page " .. currPage[opts.id] .. " of " .. numPages)
ui.box(
"spacer",
{flex = 1},
function()
end
)
ui.button(
"previous page",
{
icon = "arrow-bold-right",
iconFamily = "Entypo",
hideLabel = true,
onClick = function()
currPage[opts.id] = math.min(currPage[opts.id] + 1, numPages)
end
}
)
end
)
end
-- Scrolling view of current page
ui.scrollBox(
"scrollBox" .. opts.id .. (currPage[opts.id] or 1),
{
padding = 2,
margin = 2,
flex = 1
},
function()
-- Empty?
if #order == 0 then
ui.box(
"empty text",
{
paddingLeft = 4,
margin = 4
},
function()
ui.markdown(opts.emptyText or "No entries!")
end
)
end
for _, entry in ipairs(order) do
-- Entry box
ui.box(
entry.entryId,
{
borderWidth = 1,
borderColor = "#292929",
borderRadius = 4,
padding = 4,
margin = 4,
marginBottom = 8,
flexDirection = "row",
alignItems = "center"
},
function()
local imageUrl
-- Figure out image based on type
if entry.entryType == "actorBlueprint" then
local actorBp = entry.actorBlueprint
if actorBp.components.Image and actorBp.components.Image.url then
imageUrl = actorBp.components.Image.url
end
if actorBp.components.Drawing and actorBp.components.Drawing.url then
imageUrl = actorBp.components.Drawing.url
end
end
if entry.entryType == "image" then
imageUrl = entry.image.url
end
if entry.entryType == "drawing" then
imageUrl = entry.drawing.url
end
-- Show image if applies
if imageUrl then
ui.box(
"image container",
{
width = "28%",
aspectRatio = 1,
margin = 4,
marginLeft = 8,
backgroundColor = "white"
},
function()
ui.image(CHECKERBOARD_IMAGE_URL, {flex = 1, margin = 0})
ui.image(
imageUrl,
{
position = "absolute",
left = 0,
top = 0,
bottom = 0,
right = 0,
margin = 0
}
)
end
)
ui.box(
"spacer",
{width = 8},
function()
end
)
end
ui.box(
"text buttons",
{flex = 1},
function()
-- Title, short description
ui.markdown("## " .. entry.title .. "\n" .. (entry.description or ""))
-- Buttons
if opts.buttons then
ui.box(
"buttons",
{flexDirection = "row"},
function()
opts.buttons(entry)
end
)
end
end
)
end
)
end
if opts.bottomSpace then
ui.box(
"bottom space",
{height = opts.bottomSpace},
function()
end
)
end
end
)
end