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
local overlay = {}
local layout = rb.file.read_json("assets/layout.json")
local art = {}
for name in layout.sizes do
art[name] = rb.file.read_bytes("assets/" .. name .. ".png")
end
local cfg, keys, actions
function overlay.init(settings, key_names, menu_actions)
cfg, keys, actions = settings, key_names, menu_actions
end
local function image(name, x, y, w)
return { kind = "image", png = art[name], x = x, y = y, w = w, h = if w then layout.sizes[name][2] else nil }
end
local PIECE_GAP = 5
local KEY_TEXT_SIZE = 13
local KEY_MIN_TEXT = 12
local TEXT_SIZE = 13
local KEY_END = layout.sizes.keycap_l[1]
local KEY_HEIGHT = layout.sizes.keycap_l[2]
local function piece_width(piece)
if type(piece) == "string" then
return layout.sizes[piece][1]
end
if piece.key then
local width = rb.overlay.measure(piece.key, KEY_TEXT_SIZE, true)
return 2 * KEY_END + math.max(width, KEY_MIN_TEXT) + 6
end
if piece.text then
return (rb.overlay.measure(piece.text, piece.size or TEXT_SIZE, true))
end
return piece.space
end
local function line_width(pieces)
local width = PIECE_GAP * (#pieces - 1)
for _, piece in pieces do
width += piece_width(piece)
end
return width
end
local function line(pieces, x, mid_y)
local ops = {}
for _, piece in pieces do
local width = piece_width(piece)
if type(piece) == "string" then
table.insert(ops, image(piece, x, mid_y - layout.sizes[piece][2] // 2))
elseif piece.key then
local top = mid_y - KEY_HEIGHT // 2
table.insert(ops, image("keycap_l", x, top))
table.insert(ops, image("keycap_m", x + KEY_END, top, width - 2 * KEY_END))
table.insert(ops, image("keycap_r", x + width - KEY_END, top))
local text_width, text_height = rb.overlay.measure(piece.key, KEY_TEXT_SIZE, true)
table.insert(ops, {
kind = "text", text = piece.key, size = KEY_TEXT_SIZE, bold = true, color = "#6ee8ff",
x = x + (width - text_width) // 2, y = top + (KEY_HEIGHT - 2 - text_height) // 2,
})
elseif piece.text then
local size = piece.size or TEXT_SIZE
local _, text_height = rb.overlay.measure(piece.text, size, true)
table.insert(ops, {
kind = "text", text = piece.text, size = size, bold = true, color = piece.color or "#f0e2c0",
x = x, y = mid_y - text_height // 2,
})
end
x += width + PIECE_GAP
end
return ops
end
local function append(ops, more)
for _, op in more do
table.insert(ops, op)
end
return ops
end
local function place(name, where)
local w = rb.system.window()
local width, height = layout.sizes[name][1], layout.sizes[name][2]
local x = if where.x == "left" then 0 elseif where.x == "right" then w.width - width else (w.width - width) // 2
local y = if where.y == "top" then 0 elseif where.y == "bottom" then w.height - height else (w.height - height) // 2
return w.x + x + (where.dx or 0), w.y + y + (where.dy or 0)
end
local function surface(name, where, ops)
local x, y = place(name, where)
return rb.overlay.surface({
x = x, y = y,
width = layout.sizes[name][1],
height = layout.sizes[name][2],
capturable = true,
ops = ops,
})
end
local function chip_ops(name, pieces)
local ops = { image(name, 0, 0) }
if pieces then
append(ops, line(pieces, layout.chip_detail.x, layout.chip_detail.y))
end
return ops
end
local CHIP_HEIGHT = layout.sizes.chip_welcome[2]
local STATUS = { x = "center", y = "top", dy = 6 }
local NOTICE = { x = "center", y = "top", dy = 6 + CHIP_HEIGHT }
local ROTATION = { x = "center", y = "top", dy = 6 + 2 * CHIP_HEIGHT }
local MIDDLE = { x = "center", y = "center" }
local STATUS_GAP = 4
local slots = {
chat = { where = STATUS },
afk = { where = STATUS },
rotation = { where = ROTATION },
recording = { where = ROTATION },
}
local visible = true
local function open(slot)
slot.surface = surface(slot.name, slot.where, slot.ops)
end
local function arrange_status()
local shown = {}
for _, slot in { slots.chat, slots.afk } do
if slot.name then
table.insert(shown, slot)
end
end
local width = layout.sizes.chip_welcome[1]
local total = #shown * width + (#shown - 1) * STATUS_GAP
for i, slot in shown do
slot.where = { x = "center", y = "top", dy = STATUS.dy, dx = -total // 2 + (i - 1) * (width + STATUS_GAP) + width // 2 }
if slot.surface then
local x, y = place(slot.name, slot.where)
slot.surface:set({ x = x, y = y })
end
end
end
local function set_slot(slot, name, ops, key)
key = key or name
if slot.key == key then
return
end
slot.key, slot.name, slot.ops = key, name, ops
if not name then
if slot.surface then
slot.surface:remove()
slot.surface = nil
end
elseif slot.surface then
slot.surface:draw(ops)
elseif visible then
open(slot)
end
end
local NOTICE_MS = 2500
local notice = nil
local function show_notice(name, ops, ms)
if notice then
notice:remove()
notice = nil
end
if not visible then
return
end
local shown = surface(name, NOTICE, ops)
notice = shown
rb.timer.after(ms, function()
if notice == shown then
notice:remove()
notice = nil
end
end)
end
function overlay.notice(name, pieces, ms)
show_notice(name, chip_ops(name, pieces), ms or NOTICE_MS)
end
local TOAST_MS = 5000
local TOAST_DETAIL_CHARS = 44
function overlay.toast(installed, detail)
local name = if installed then "toast_ok" else "toast_fail"
local ops = { image(name, 0, 0) }
if detail then
table.insert(ops, {
kind = "text", x = layout.toast_detail.x, y = layout.toast_detail.y, size = 12,
color = "#f0e2c0", text = detail:sub(1, TOAST_DETAIL_CHARS),
})
end
show_notice(name, ops, TOAST_MS)
end
function overlay.playing(on)
local pieces = { "d_move_to_stop", { key = keys.play }, "d_restarts" }
set_slot(slots.rotation, if on then "chip_playing" else nil, if on then chip_ops("chip_playing", pieces) else nil)
end
function overlay.recording(presses)
if presses == nil then
set_slot(slots.recording, nil)
return
end
local r = layout.record
local ops = { image("record", 0, 0) }
append(ops, line({ { key = "Enter" }, "b_save", { space = 14 }, { key = "Esc" }, "b_cancel" }, r.keys_x, r.line_y))
local count = { { text = tostring(presses), color = "#f0c060", size = 18 }, "b_presses" }
append(ops, line(count, r.count_right - line_width(count), r.line_y))
set_slot(slots.recording, "record", ops, "record " .. presses)
end
local strip_ready = false
local GOOD, BAD, NUMBER, DIM, ARROW = "#78f08c", "#ff6e64", "#f0c060", "#968ca0", "#6ee8ff"
local ROWS = {
{
kind = "toggle", field = "enabled", key = "turbo",
hint = function() return { "h_turbo" } end,
},
{
kind = "speed",
hint = function() return { "h_speed" } end,
},
{
kind = "toggle", field = "afk_enabled", key = "afk",
hint = function() return { "h_afk", { key = keys.afk_key }, "h_when_idle" } end,
},
{
kind = "rotation", key = "play",
hint = function()
local first = actions.length() == nil
return { if first then "h_rotation_first" else "h_rotation", { key = keys.play }, "h_to_play" }
end,
},
{
kind = "addon",
hint = function() return { if strip_ready then "h_addon_ready" else "h_addon" } end,
},
}
local GAP_MIN, GAP_MAX, GAP_STEP = 10, 400, 10
local menu = nil
local function value(row)
if row.kind == "toggle" then
return if cfg[row.field] then "[ON]" else "[OFF]", if cfg[row.field] then GOOD else BAD
elseif row.kind == "speed" then
return string.format("[%.1f/s]", 1000 / (cfg.hold + cfg.gap)), NUMBER
elseif row.kind == "rotation" then
local length = actions.length()
return if length then "[" .. length .. "]" else "[NONE]", if length then NUMBER else DIM
end
return if strip_ready then "[READY]" else "[MISSING]", if strip_ready then GOOD else BAD
end
local function draw_menu()
local state = { menu.selected }
for _, row in ROWS do
table.insert(state, (value(row)))
end
state = table.concat(state, "|")
if menu.drawn == state then
return
end
menu.drawn = state
local m = layout.menu
local menu_width = layout.sizes.menu[1]
local ops = {
image("menu", 0, 0),
image("menu_highlight", m.highlight_x, m.rows[menu.selected] + 2),
image("menu_labels", 0, 0),
}
for i, row in ROWS do
local mid = m.rows[i] + m.row_height // 2
if row.key then
append(ops, line({ { key = keys[row.key] } }, m.key_x, mid))
end
local tag, color = value(row)
local pieces = { { text = tag, color = color } }
if row.kind == "speed" and i == menu.selected then
pieces = { { text = "<", color = ARROW }, pieces[1], { text = ">", color = ARROW } }
end
append(ops, line(pieces, m.value_right - line_width(pieces), mid))
end
local hint = ROWS[menu.selected].hint()
append(ops, line(hint, (menu_width - line_width(hint)) // 2, m.hint_y))
menu.surface:draw(ops)
end
function overlay.menu_open()
return menu ~= nil
end
function overlay.close_menu()
if menu then
menu.animation:stop()
menu.banner:remove()
menu.surface:remove()
menu = nil
end
end
function overlay.toggle_menu()
if menu then
overlay.close_menu()
return
end
menu = { selected = 1, frame = 0, surface = surface("menu", MIDDLE, {}) }
menu.banner = surface("menu", MIDDLE, { image("banner_0", 0, 0) })
menu.animation = rb.timer.every(layout.banner.frame_ms, function()
menu.frame = (menu.frame + 1) % layout.banner.frames
menu.banner:draw({ image("banner_" .. menu.frame, 0, 0) })
end)
draw_menu()
end
function overlay.menu_key(key)
if key == "Escape" then
overlay.close_menu()
return
end
local row = ROWS[menu.selected]
if key == "Up" then
menu.selected = (menu.selected - 2) % #ROWS + 1
elseif key == "Down" then
menu.selected = menu.selected % #ROWS + 1
elseif (key == "Left" or key == "Right") and row.kind == "speed" then
local step = if key == "Right" then -GAP_STEP else GAP_STEP
cfg.gap = math.clamp(cfg.gap + step, GAP_MIN, GAP_MAX)
elseif key == "Enter" and row.kind == "toggle" then
cfg[row.field] = not cfg[row.field]
elseif key == "Enter" and (row.kind == "rotation" or row.kind == "addon") then
overlay.close_menu()
if row.kind == "rotation" then
actions.record()
else
actions.install()
end
return
end
draw_menu()
end
function overlay.sync(chat)
strip_ready = chat ~= nil
local chat_chip = nil
if cfg.chat_detect and cfg.chat_overlay then
chat_chip = if chat == nil then "chip_nostrip" elseif chat then "chip_typing" else "chip_idle"
end
local afk_chip = if cfg.afk_enabled and cfg.afk_overlay then "chip_afk" else nil
set_slot(slots.chat, chat_chip, chat_chip and chip_ops(chat_chip))
set_slot(slots.afk, afk_chip, afk_chip and chip_ops(afk_chip))
arrange_status()
if menu then
draw_menu()
end
end
function overlay.hide()
visible = false
overlay.close_menu()
for _, slot in slots do
if slot.surface then
slot.surface:remove()
slot.surface = nil
end
end
if notice then
notice:remove()
notice = nil
end
end
function overlay.show()
visible = true
arrange_status()
for _, slot in slots do
if slot.name and not slot.surface then
open(slot)
end
end
end
return overlay