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
local DEFAULTS = {
start_mode = "Size",
invert = false,
ps_opacity_wait = 600,
show_status = true,
}
local DIAL_DEFAULT = "Mouse5"
local BOARD_DEFAULT = "LCtrl+LAlt+H"
local cfg = rb.ui.schema({
dial_key = rb.ui.keybind(DIAL_DEFAULT, {
label = "Dial button",
tooltip = "Hold it and scroll to turn the dial; tap it to switch mode. Use one key or mouse button, not a chord. Rebind restarts the script to apply a change made here. Photoshop keys are from Adobe's pages via search, not a live read, so check before use.",
group = "Hotkeys",
}),
board = rb.ui.keybind(BOARD_DEFAULT, {
label = "Edit hotkeys",
tooltip = "Opens the Bind Board over the app: pick a hotkey, then press the new key.",
group = "Hotkeys",
}),
start_mode = rb.ui.select(DEFAULTS.start_mode, { "Size", "Hardness", "Opacity" }, {
label = "Start in mode",
tooltip = "The mode the dial starts in. Takes effect after you restart the script.",
group = "Behavior",
}),
invert = rb.ui.toggle(DEFAULTS.invert, {
label = "Invert scroll direction",
tooltip = "Off: scroll up makes it bigger, harder or more opaque.",
group = "Behavior",
}),
ps_opacity_wait = rb.ui.slider(DEFAULTS.ps_opacity_wait, {
label = "Photoshop opacity settle time",
min = 300,
max = 1500,
step = 50,
suffix = " ms",
tooltip = "Photoshop reads two number keys pressed in quick succession as one value (4 then 5 = 45%). The dial waits this long after your last scroll step before it sends a single number key.",
group = "Behavior",
}),
show_status = rb.ui.toggle(DEFAULTS.show_status, { label = "Show the mode pill", group = "Display" }),
reset = rb.ui.toggle(false, {
label = "Reset to defaults",
tooltip = "Resets the Behavior and Display settings. Hotkeys reset from Edit hotkeys or this tab.",
group = "Reset",
}),
})
local MAC = rb.info.platform == "macos"
local CMD = MAC and "LWin" or "LCtrl"
local MODES = { "Size", "Hardness", "Opacity" }
local mode = 1
for i, m in MODES do
if m == cfg.start_mode then
mode = i
end
end
local function appNow()
local ok, w = pcall(rb.system.window)
local p = (ok and w and w.process or ""):lower()
if p:find("photoshop", 1, true) then
return "ps", "Photoshop"
end
if p:find("krita", 1, true) then
return "krita", "Krita"
end
if p:find("clip", 1, true) then
return "csp", "Clip Studio"
end
return nil, "?"
end
local KEYS = {
ps = {
Size = { down = "LeftBrace", up = "RightBrace" },
Hardness = { down = "LShift+LeftBrace", up = "LShift+RightBrace" },
},
krita = {
Size = { down = "LeftBrace", up = "RightBrace" },
Opacity = { down = "I", up = "O" },
},
csp = {
Size = { down = "LeftBrace", up = "RightBrace" },
Opacity = { down = CMD .. "+LeftBrace", up = CMD .. "+RightBrace" },
},
}
local function supported(app, m)
if not app then
return false
end
if app == "ps" and m == "Opacity" then
return true
end
return KEYS[app][m] ~= nil
end
local modePill = nil
local function render()
if not cfg.show_status then
if modePill then
modePill.remove()
modePill = nil
end
return
end
if not modePill then
modePill = rb.overlay.status("Brush Dial", { anchor = "top" })
end
local app, appName = appNow()
local m = MODES[mode]
local ok = supported(app, m)
modePill.set({
{ "Brush Dial ", "dim" },
{ m:upper(), ok and "accent" or "warning", true },
{ ok and (" · " .. appName) or (" · no " .. m:lower() .. " key in " .. appName), "dim" },
})
end
local psLevel = 10
local psTimer = nil
local function psSendOpacity()
psTimer = nil
local key = psLevel == 10 and "0" or tostring(psLevel)
rb.hid.combo(key)
rb.overlay.toast("Opacity " .. (psLevel * 10) .. "%", { title = "Brush Dial", duration = 1200 })
end
local scrolledWhileHeld = false
local dialPlain = (tostring(cfg.dial_key):gsub("@.*$", ""))
local function step(dir)
scrolledWhileHeld = true
if cfg.invert then
dir = -dir
end
local app = appNow()
local m = MODES[mode]
if not supported(app, m) then
return
end
if app == "ps" and m == "Opacity" then
psLevel = math.clamp(psLevel + dir, 1, 10)
if psTimer then
psTimer:stop()
end
psTimer = rb.timer.after(cfg.ps_opacity_wait, psSendOpacity)
return
end
local k = KEYS[app][m]
rb.hid.combo(dir > 0 and k.up or k.down)
end
local BB = { rows = {}, order = {}, busy = false }
local MODS = { "LCtrl", "RCtrl", "LShift", "RShift", "LAlt", "RAlt", "LWin", "RWin" }
local IS_MOD = {}
for _, m in MODS do
IS_MOD[m] = true
end
local REFUSE = { Escape = "Esc always cancels", Mouse1 = "a plain left click can't be a hotkey" }
local function cur(id)
return rb.ui.get(id) or BB.rows[id].default
end
local function shown(k)
return (string.match(k, "^[^@]+"))
end
local function chord(key)
local parts = {}
for _, m in MODS do
if m ~= key and rb.input.is_down(m) then
table.insert(parts, m)
end
end
table.insert(parts, key)
return table.concat(parts, "+")
end
local function apply(id, key)
local r = BB.rows[id]
local ok, h = pcall(rb.bind, key, r.fn, r.opts)
if not ok then
return false
end
if r.handle then
r.handle:stop()
end
r.handle = h
rb.ui.set(id, key)
return true
end
local function guarded(opts)
local g = {}
for k, v in opts or {} do
g[k] = v
end
local orig = g.when
g.when = function(d)
return not BB.busy and (orig == nil or orig(d))
end
return g
end
function BB.bind(id, label, default, fn, opts)
opts = guarded(opts)
local r = { label = label, default = default, fn = fn, opts = opts }
BB.rows[id] = r
table.insert(BB.order, id)
local key = cur(id)
local ok, h = pcall(rb.bind, key, fn, opts)
if not ok and key ~= default then
ok, h = pcall(rb.bind, default, fn, opts)
if ok then
rb.ui.set(id, default)
rb.overlay.toast(label .. " couldn't use " .. shown(key) .. ", so it's back to " .. default .. ".")
end
end
if ok then
r.handle = h
else
rb.overlay.toast(label .. " has no hotkey. Set one in Edit hotkeys or the Options tab.")
end
end
local function restart_menu(text)
rb.overlay.menu({
{ label = "Restart script now", action = function() rb.script.reload() end },
{ label = "Later" },
}, { title = text, subtitle = "Restart to apply" })
end
local function saved(r, key)
rb.overlay.toast(r.label .. " is now " .. shown(key) .. ".", { title = "Hotkey saved" })
end
local function swap(id, key, clash, mine)
local r, r2 = BB.rows[id], BB.rows[clash]
if r2.handle then
r2.handle:stop()
end
r2.handle = nil
rb.ui.set(clash, mine)
if not apply(id, key) then
rb.ui.set(clash, key)
local ok, h = pcall(rb.bind, key, r2.fn, r2.opts)
if ok then
r2.handle = h
end
rb.overlay.toast("Couldn't use " .. shown(key) .. ". Nothing changed.")
return
end
if apply(clash, mine) then
saved(r, key)
return
end
restart_menu("Swapped. " .. r2.label .. " is saved as " .. shown(mine))
end
local function set_key(id, key)
local r, clash = BB.rows[id], nil
for _, o in BB.order do
if o ~= id and cur(o) == key then
clash = o
end
end
if not clash then
if apply(id, key) then
saved(r, key)
return
end
rb.ui.set(id, key)
restart_menu("Saved " .. shown(key) .. " for " .. r.label)
return
end
local mine = cur(id)
rb.overlay.menu({
{ label = "Cancel" },
{ label = "Swap the two keys", hint = BB.rows[clash].label .. " → " .. shown(mine),
action = function() swap(id, key, clash, mine) end },
}, { title = shown(key) .. " is already " .. BB.rows[clash].label, subtitle = "Nothing changes unless you pick Swap" })
end
local function capture(id)
local r = BB.rows[id]
local status = rb.overlay.status("Press the new key for " .. r.label .. " · Esc cancels")
local hDown, hScroll, timer
local function done()
BB.busy = false
status.remove()
if hDown then
hDown:stop()
end
if hScroll then
hScroll:stop()
end
if timer then
timer:stop()
end
end
hDown = rb.on("down", function(key)
if not BB.busy then
return
end
if IS_MOD[key] then
return
end
done()
if key == "Escape" then
rb.overlay.toast("Nothing changed.")
return false
end
if REFUSE[key] and chord(key) == key then
rb.overlay.toast("Can't use " .. key .. ": " .. REFUSE[key] .. ".")
return false
end
set_key(id, chord(key))
return false
end)
hScroll = rb.on("scroll", function(delta)
if not BB.busy then
return
end
done()
set_key(id, chord(delta > 0 and "WheelUp" or "WheelDown"))
return false
end)
timer = rb.timer.after(10000, function()
if not BB.busy then
return
end
done()
rb.overlay.toast("No key pressed. Nothing changed.")
end)
end
local function reset_all()
local failed = {}
for _, id in BB.order do
if not apply(id, BB.rows[id].default) then
table.insert(failed, BB.rows[id].label)
end
end
if #failed == 0 then
rb.overlay.toast("Every hotkey is back to its default.")
else
rb.overlay.toast("Couldn't reset: " .. table.concat(failed, ", ") .. ".")
end
end
function BB.open()
if BB.busy then
return
end
local items = {}
for _, id in BB.order do
table.insert(items, {
label = BB.rows[id].label,
hint = shown(cur(id)),
action = function()
BB.busy = true
capture(id)
end,
})
end
table.insert(items, { label = "Reset all to defaults", action = reset_all })
table.insert(items, { label = "Done" })
local m, why = rb.overlay.menu(items, { title = "Edit hotkeys", subtitle = "Pick one, then press the new key" })
if not m then
rb.overlay.toast("Can't open the editor: " .. why .. ".")
end
end
function BB.board(id, default)
BB.bind(id, "Edit hotkeys", default, BB.open)
end
BB.bind("board", "Edit hotkeys", BOARD_DEFAULT, BB.open, {
when = function()
return appNow() ~= nil
end,
})
local function dialHeld()
return rb.input.is_down(dialPlain)
end
rb.bind("WheelUp", function()
step(1)
end, { when = dialHeld })
rb.bind("WheelDown", function()
step(-1)
end, { when = dialHeld })
local okBind, why = pcall(rb.bind, cfg.dial_key, function()
scrolledWhileHeld = false
end, {
release = function()
if not scrolledWhileHeld then
mode = mode % #MODES + 1
render()
end
end,
})
if not okBind then
rb.log.warn("dial key not bound: " .. tostring(why))
end
rb.timer.every(500, function()
if cfg.reset then
for k, v in DEFAULTS do
rb.ui.set(k, v)
end
cfg.reset = false
rb.ui.notify("Behavior and Display settings reset to defaults.", "success")
end
render()
end)
render()
rb.log.info("Brush Dial ready: hold " .. tostring(cfg.dial_key) .. " and scroll; tap it to switch mode")