-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclick-drag.lua
52 lines (47 loc) · 1.6 KB
/
click-drag.lua
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
local state = nil
local o = {
gap = 20, -- number of pixels between steps
step = 1, -- step factor and precision level
dBmin = -60, -- silence threshold in decibels
fmt = '', -- print format
x = true, -- whether to use the x or y axis
}
(require 'mp.options').read_options(o)
local dBmin, dBmax = o.dBmin, 0
local prec = math.abs(o.step)
if o.fmt == '' then
o.fmt = tostring(o.step)
local i = o.fmt:find('%.')
o.fmt = '.'..(i and o.fmt:sub(i + 1):len() or 0)..'f'
end
local k = (mp.get_property('ao') == 'pulse') and 60 or 20
local function drag(_, pos)
local dif = o.x and (pos.x - state.pos.x) or (state.pos.y - pos.y)
dif = math.floor(dif / o.gap + 0.5)
if state.dif ~= dif then
state.dif = dif
local dB = math.min(state.dB + (dif * o.step), dBmax)
local s = (dB <= dBmin and '-∞' or string.format('%+'..o.fmt, dB))..' dB'
mp.commandv('osd-bar', 'set', 'ao-volume',
dB <= dBmin and 0 or 10 ^ (2 + dB / k))
mp.osd_message(string.format('AO-Volume: %s%s', s,
mp.get_property_bool('ao-mute') and ' (Muted)' or ''), 1)
end
end
local function click(t)
if t.event == 'down' then
local pos = mp.get_property_native('mouse-pos')
local vol = mp.get_property_number('ao-volume')
if not (pos and vol) then
return
end
local dB = math.max(dBmin, k * math.log(vol / 100, 10))
dB = math.floor(dB / prec + 0.5) * prec
state = { pos=pos, dif=0, dB=dB }
mp.observe_property('mouse-pos', 'native', drag)
elseif t.event == 'up' then
mp.unobserve_property(drag)
end
end
mp.add_key_binding('MBTN_MID', click, {complex=true})
-- mp.add_key_binding('Ctrl+MBTN_LEFT', click, {complex=true})