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
local cfg = rb.ui.schema({
games = rb.ui.text("", {
label = "Games",
tooltip = "Show the overlay only while one of these is the focused window:"
.. " app or process names, or words from the window title, separated"
.. " by commas. Empty: show it everywhere.",
placeholder = "empty: everywhere, for example cs2, valorant",
}),
anchor = rb.ui.select("bottom-left", {
"bottom-left",
"bottom",
"bottom-right",
"left",
"right",
"top-left",
"top",
"top-right",
}, { label = "Position" }),
monitor = rb.ui.select("focused window", {
"focused window",
"primary",
"under the mouse",
}, { label = "Monitor" }),
offset_x = rb.ui.slider(16, {
label = "Distance from the side",
tooltip = "No effect when Position is top, bottom, or centred",
min = 0,
max = 1000,
step = 4,
}),
offset_y = rb.ui.slider(16, {
label = "Distance from the top or bottom",
tooltip = "No effect when Position is left or right",
min = 0,
max = 1000,
step = 4,
}),
scale = rb.ui.slider(100, {
label = "Size",
min = 50,
max = 150,
step = 10,
suffix = "%",
}),
color = rb.ui.color("#ffffff", {
label = "Key color",
tooltip = "Outlines, labels, and pressed keys",
}),
background = rb.ui.slider(100, {
label = "Background opacity",
tooltip = "The black panel behind the keys; 0% leaves only the keys",
min = 0,
max = 100,
step = 5,
suffix = "%",
}),
mouse = rb.ui.toggle(true, { label = "Show mouse" }),
timers = rb.ui.toggle(true, {
label = "Show hold times",
tooltip = "How long each held key has been down",
}),
capturable = rb.ui.toggle(true, {
label = "Show in screenshots and recordings",
tooltip = "Off: only you see it, and OBS and screenshots leave it out",
}),
})
local SCROLL_MS = 150
local TIMER_MS = 100
local TIMER_SIZE = 11
local CHECK_MS = 500
local DISPLAYS = {
["focused window"] = "active",
primary = "primary",
["under the mouse"] = "mouse",
}
local layout
local parts = {}
local part_of = {}
local surface = nil
local showing = false
local dirty = true
local drawn_settings = ""
local drawn_at = 0
local checked_at = -CHECK_MS
local scroll_until = 0
local tinted_with = nil
local CRC = {}
for n = 0, 255 do
local c = n
for _ = 1, 8 do
c = if bit32.band(c, 1) ~= 0
then bit32.bxor(0xEDB88320, bit32.rshift(c, 1))
else bit32.rshift(c, 1)
end
CRC[n] = c
end
local function crc32(s)
local c = 0xFFFFFFFF
for i = 1, #s do
local index = bit32.band(bit32.bxor(c, string.byte(s, i)), 0xFF)
c = bit32.bxor(CRC[index], bit32.rshift(c, 8))
end
return bit32.bxor(c, 0xFFFFFFFF)
end
local function tint(png, r, g, b)
local at = 9
while at <= #png do
local length, kind = string.unpack(">I4c4", png, at)
if kind == "PLTE" then
local out = {}
for i = at + 8, at + 7 + length, 3 do
local v = string.byte(png, i) / 255
table.insert(
out,
string.char(
math.floor(r * v + 0.5),
math.floor(g * v + 0.5),
math.floor(b * v + 0.5)
)
)
end
local data = "PLTE" .. table.concat(out)
return string.sub(png, 1, at + 3)
.. data
.. string.pack(">I4", crc32(data))
.. string.sub(png, at + 12 + length)
end
at += 12 + length
end
return png
end
local function retint()
if cfg.color == tinted_with then
return
end
tinted_with = cfg.color
local hex = string.match(cfg.color, "^#(%x%x%x%x%x%x)") or "ffffff"
local r = tonumber(string.sub(hex, 1, 2), 16)
local g = tonumber(string.sub(hex, 3, 4), 16)
local b = tonumber(string.sub(hex, 5, 6), 16)
for _, part in parts do
part.up = tint(part.art_up, r, g, b)
part.down = part.art_down and tint(part.art_down, r, g, b)
end
end
local function in_game()
local words = {}
for _, word in string.split(cfg.games, ",") do
word = string.lower(string.match(word, "^%s*(.-)%s*$"))
if word ~= "" then
table.insert(words, word)
end
end
if #words == 0 then
return true
end
local ok, window = pcall(rb.system.window)
if not ok then
return false
end
local process = string.lower(window.process or "")
local title = string.lower(window.title or "")
for _, word in words do
if
string.find(process, word, 1, true)
or string.find(title, word, 1, true)
then
return true
end
end
return false
end
local function since(part)
local first = nil
for _, at in part.held do
first = if first then math.min(first, at) else at
end
return first
end
local function lit(part, now)
if part.id == "mouse-wheel" and now < scroll_until then
return true
end
return next(part.held) ~= nil
end
local function clock(ms)
if ms < 10000 then
return string.format("%.1fs", ms / 1000)
elseif ms < 60000 then
return string.format("%ds", ms // 1000)
end
local s = ms // 1000
return string.format("%d:%02d", s // 60, s % 60)
end
local function timer_op(part, text, k)
local size = math.max(6, TIMER_SIZE * k)
local w = rb.overlay.measure(text, size, true)
local x, y, color
if part.timer then
x, y, color = part.timer.x, part.timer.y, cfg.color
else
local face = part.w - layout.slant
x = part.x + face / 2 + layout.slant * 0.2
y = part.y + part.h - 17
color = "#000000"
end
return {
kind = "text",
x = x * k - w / 2,
y = y * k,
size = size,
bold = true,
text = text,
color = color,
}
end
local function shown(part)
return cfg.mouse or string.sub(part.id, 1, 6) ~= "mouse-"
end
local function draw(now)
retint()
local k = cfg.scale / 100
local pad = layout.pad
local right = 0
for _, part in parts do
if shown(part) then
right = math.max(right, part.x + part.w)
end
end
local width = (right + layout.margin) * k
local alpha = math.floor(cfg.background / 100 * 255 + 0.5)
local ops = {
{
kind = "rect",
w = width,
h = layout.height * k,
fill = string.format("#000000%02x", alpha),
radius = 6 * k,
},
}
local timers = {}
for _, part in parts do
if not shown(part) then
continue
end
table.insert(ops, {
kind = "image",
x = (part.x - pad) * k,
y = (part.y - pad) * k,
w = (part.w + 2 * pad) * k,
h = (part.h + 2 * pad) * k,
png = if lit(part, now) then part.down else part.up,
})
local at = since(part)
if cfg.timers and at then
table.insert(timers, timer_op(part, clock(now - at), k))
end
end
table.move(timers, 1, #timers, #ops + 1, ops)
local fields = {
anchor = cfg.anchor,
display = DISPLAYS[cfg.monitor] or "active",
offset_x = cfg.offset_x,
offset_y = cfg.offset_y,
width = width,
height = layout.height * k,
capturable = cfg.capturable,
ops = ops,
}
if surface then
surface:set(fields)
else
surface = rb.overlay.surface(fields)
end
end
rb.on("start", function()
layout = rb.file.read_json("layout.json")
for _, part in layout.parts do
part.art_up = rb.file.read_bytes("caps/" .. part.id .. ".png")
if #part.keys > 0 then
part.art_down = rb.file.read_bytes("caps/" .. part.id .. "-down.png")
end
part.held = {}
for _, key in part.keys do
part_of[key] = part
end
table.insert(parts, part)
end
end)
rb.on("input_down", function(key)
local part = part_of[key]
if part and not part.held[key] then
part.held[key] = rb.system.time()
dirty = true
end
end)
rb.on("input_up", function(key)
local part = part_of[key]
if part and part.held[key] then
part.held[key] = nil
dirty = true
end
end)
rb.on("input_scroll", function()
scroll_until = rb.system.time() + SCROLL_MS
dirty = true
end)
local function any_held()
for _, part in parts do
if next(part.held) ~= nil then
return true
end
end
return false
end
rb.on("tick", function()
local now = rb.system.time()
if now - checked_at >= CHECK_MS then
checked_at = now
local wanted = in_game()
if wanted ~= showing then
showing = wanted
if not showing and surface then
surface:remove()
surface = nil
end
dirty = true
end
end
if not showing then
return
end
local settings = table.concat({
cfg.anchor,
cfg.monitor,
cfg.offset_x,
cfg.offset_y,
cfg.scale,
cfg.color,
cfg.background,
tostring(cfg.mouse),
tostring(cfg.timers),
tostring(cfg.capturable),
}, "/")
if scroll_until > 0 and now >= scroll_until then
scroll_until = 0
dirty = true
end
if cfg.timers and now - drawn_at >= TIMER_MS and any_held() then
dirty = true
end
if dirty or settings ~= drawn_settings then
dirty, drawn_settings, drawn_at = false, settings, now
draw(now)
end
end)