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
|
local opts = {
drag_to_pan_margin = 50,
drag_to_pan_move_if_full_view=false,
pan_follows_cursor_margin = 50,
cursor_centric_zoom_margin = 50,
cursor_centric_zoom_auto_center = true,
cursor_centric_zoom_dezoom_if_full_view = false,
}
local options = require 'mp.options'
local msg = require 'mp.msg'
local assdraw = require 'mp.assdraw'
options.read_options(opts, nil, function() end)
function clamp(value, low, high)
if value <= low then
return low
elseif value >= high then
return high
else
return value
end
end
local cleanup = nil -- function set up by drag-to-pan/pan-follows cursor and must be called to clean lingering state
function drag_to_pan_handler(table)
if cleanup then
cleanup()
cleanup = nil
end
if table["event"] == "down" then
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local mouse_pos_origin, video_pan_origin = {}, {}
local moved = false
mouse_pos_origin[1], mouse_pos_origin[2] = mp.get_mouse_pos()
video_pan_origin[1] = mp.get_property_number("video-pan-x")
video_pan_origin[2] = mp.get_property_number("video-pan-y")
local video_size = { dim.w - dim.ml - dim.mr, dim.h - dim.mt - dim.mb }
local margin = opts.drag_to_pan_margin
local move_up = true
local move_lateral = true
if not opts.drag_to_pan_move_if_full_view then
if dim.ml >= 0 and dim.mr >= 0 then
move_lateral = false
end
if dim.mt >= 0 and dim.mb >= 0 then
move_up = false
end
end
if not move_up and not move_lateral then return end
local idle = function()
if moved then
local mX, mY = mp.get_mouse_pos()
local pX = video_pan_origin[1]
local pY = video_pan_origin[2]
if move_lateral then
pX = video_pan_origin[1] + (mX - mouse_pos_origin[1]) / video_size[1]
if 2 * margin > dim.ml + dim.mr then
pX = clamp(pX,
(-margin + dim.w / 2) / video_size[1] - 0.5,
(margin - dim.w / 2) / video_size[1] + 0.5)
else
pX = clamp(pX,
(margin - dim.w / 2) / video_size[1] + 0.5,
(-margin + dim.w / 2) / video_size[1] - 0.5)
end
end
if move_up then
pY = video_pan_origin[2] + (mY - mouse_pos_origin[2]) / video_size[2]
if 2 * margin > dim.mt + dim.mb then
pY = clamp(pY,
(-margin + dim.h / 2) / video_size[2] - 0.5,
(margin - dim.h / 2) / video_size[2] + 0.5)
else
pY = clamp(pY,
(margin - dim.h / 2) / video_size[2] + 0.5,
(-margin + dim.h / 2) / video_size[2] - 0.5)
end
end
mp.command("no-osd set video-pan-x " .. clamp(pX, -3, 3) .. "; no-osd set video-pan-y " .. clamp(pY, -3, 3))
moved = false
end
end
mp.register_idle(idle)
mp.add_forced_key_binding("mouse_move", "image-viewer-mouse-move", function() moved = true end)
cleanup = function()
mp.remove_key_binding("image-viewer-mouse-move")
mp.unregister_idle(idle)
end
end
end
function pan_follows_cursor_handler(table)
if cleanup then
cleanup()
cleanup = nil
end
if table["event"] == "down" then
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local video_size = { dim.w - dim.ml - dim.mr, dim.h - dim.mt - dim.mb }
local moved = true
local idle = function()
if moved then
local mX, mY = mp.get_mouse_pos()
local x = math.min(1, math.max(- 2 * mX / dim.w + 1, -1))
local y = math.min(1, math.max(- 2 * mY / dim.h + 1, -1))
local command = ""
local margin = opts.pan_follows_cursor_margin
if dim.ml + dim.mr < 0 then
command = command .. "no-osd set video-pan-x " .. clamp(x * (2 * margin - dim.ml - dim.mr) / (2 * video_size[1]), -3, 3) .. ";"
elseif mp.get_property_number("video-pan-x") ~= 0 then
command = command .. "no-osd set video-pan-x " .. "0;"
end
if dim.mt + dim.mb < 0 then
command = command .. "no-osd set video-pan-y " .. clamp(y * (2 * margin - dim.mt - dim.mb) / (2 * video_size[2]), -3, 3) .. ";"
elseif mp.get_property_number("video-pan-y") ~= 0 then
command = command .. "no-osd set video-pan-y " .. "0;"
end
if command ~= "" then
mp.command(command)
end
moved = false
end
end
mp.register_idle(idle)
mp.add_forced_key_binding("mouse_move", "image-viewer-mouse-move", function() moved = true end)
cleanup = function()
mp.remove_key_binding("image-viewer-mouse-move")
mp.unregister_idle(idle)
end
end
end
function cursor_centric_zoom_handler(amt)
local zoom_inc = tonumber(amt)
if not zoom_inc or zoom_inc == 0 then return end
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local margin = opts.cursor_centric_zoom_margin
local video_size = { dim.w - dim.ml - dim.mr, dim.h - dim.mt - dim.mb }
-- the size in pixels of the (in|de)crement
local diff_width = (2 ^ zoom_inc - 1) * video_size[1]
local diff_height = (2 ^ zoom_inc - 1) * video_size[2]
if not opts.cursor_centric_zoom_dezoom_if_full_view and
zoom_inc < 0 and
video_size[1] + diff_width + 2 * margin <= dim.w and
video_size[2] + diff_height + 2 * margin <= dim.h
then
-- the zoom decrement is too much, reduce it such that the full image is visible, no more, no less
-- in addition, this should take care of trying too zoom out while everything is already visible
local new_zoom_inc_x = math.log((dim.w - 2 * margin) / video_size[1]) / math.log(2)
local new_zoom_inc_y = math.log((dim.h - 2 * margin) / video_size[2]) / math.log(2)
local new_zoom_inc = math.min(0, math.min(new_zoom_inc_x, new_zoom_inc_y))
zoom_inc = new_zoom_inc
diff_width = (2 ^ zoom_inc - 1) * video_size[1]
diff_height = (2 ^ zoom_inc - 1) * video_size[2]
end
local new_width = video_size[1] + diff_width
local new_height = video_size[2] + diff_height
local mouse_pos_origin = {}
mouse_pos_origin[1], mouse_pos_origin[2] = mp.get_mouse_pos()
local new_pan_x, new_pan_y
-- some additional constraints:
-- if image can be fully visible (in either direction), set pan to 0
-- if border would show on either side, then prefer adjusting the pan even if not cursor-centric
local auto_c = opts.cursor_centric_zoom_auto_center
if auto_c and new_width <= dim.w then
new_pan_x = 0
else
local pan_x = mp.get_property("video-pan-x")
local rx = (dim.ml + video_size[1] / 2 - mouse_pos_origin[1]) / (video_size[1] / 2)
new_pan_x = (pan_x * video_size[1] + rx * diff_width / 2) / new_width
if auto_c then
new_pan_x = clamp(new_pan_x, (dim.w - 2 * margin) / (2 * new_width) - 0.5, - (dim.w - 2 * margin) / (2 * new_width) + 0.5)
end
end
if auto_c and new_height <= dim.h then
new_pan_y = 0
else
local pan_y = mp.get_property("video-pan-y")
local ry = (dim.mt + video_size[2] / 2 - mouse_pos_origin[2]) / (video_size[2] / 2)
new_pan_y = (pan_y * video_size[2] + ry * diff_height / 2) / new_height
if auto_c then
new_pan_y = clamp(new_pan_y, (dim.h - 2 * margin) / (2 * new_height) - 0.5, - (dim.h - 2 * margin) / (2 * new_height) + 0.5)
end
end
local zoom_origin = mp.get_property("video-zoom")
mp.command("no-osd set video-zoom " .. zoom_origin + zoom_inc .. "; no-osd set video-pan-x " .. clamp(new_pan_x, -3, 3) .. "; no-osd set video-pan-y " .. clamp(new_pan_y, -3, 3))
end
function align_border(x, y)
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local video_size = { dim.w - dim.ml - dim.mr, dim.h - dim.mt - dim.mb }
local x, y = tonumber(x), tonumber(y)
local command = ""
if x then
command = command .. "no-osd set video-pan-x " .. clamp(- x * (dim.ml + dim.mr) / (2 * video_size[1]), -3, 3) .. ";"
end
if y then
command = command .. "no-osd set video-pan-y " .. clamp(- y * (dim.mt + dim.mb) / (2 * video_size[2]), -3, 3) .. ";"
end
if command ~= "" then
mp.command(command)
end
end
function pan_image(axis, amount, zoom_invariant, image_constrained)
amount = tonumber(amount)
if not amount or amount == 0 or axis ~= "x" and axis ~= "y" then return end
if zoom_invariant == "yes" then
amount = amount / 2 ^ mp.get_property_number("video-zoom")
end
local prop = "video-pan-" .. axis
local old_pan = mp.get_property_number(prop)
if image_constrained == "yes" then
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local margin =
(axis == "x" and amount > 0) and dim.ml
or (axis == "x" and amount < 0) and dim.mr
or (amount > 0) and dim.mt
or (amount < 0) and dim.mb
local vid_size = (axis == "x") and (dim.w - dim.ml - dim.mr) or (dim.h - dim.mt - dim.mb)
local pixels_moved = math.abs(amount) * vid_size
-- the margin is already visible, no point going further
if margin >= 0 then
return
elseif margin + pixels_moved > 0 then
amount = -(math.abs(amount) / amount) * margin / vid_size
end
end
mp.set_property_number(prop, old_pan + amount)
end
function rotate_video(amt)
local rot = mp.get_property_number("video-rotate")
rot = (rot + amt) % 360
mp.set_property_number("video-rotate", rot)
end
function reset_pan_if_visible()
local dim = mp.get_property_native("osd-dimensions")
if not dim then return end
local command = ""
if (dim.ml + dim.mr >= 0) then
command = command .. "no-osd set video-pan-x 0" .. ";"
end
if (dim.mt + dim.mb >= 0) then
command = command .. "no-osd set video-pan-y 0" .. ";"
end
if command ~= "" then
mp.command(command)
end
end
mp.add_key_binding(nil, "drag-to-pan", drag_to_pan_handler, {complex = true})
mp.add_key_binding(nil, "pan-follows-cursor", pan_follows_cursor_handler, {complex = true})
mp.add_key_binding(nil, "cursor-centric-zoom", cursor_centric_zoom_handler)
mp.add_key_binding(nil, "align-border", align_border)
mp.add_key_binding(nil, "pan-image", pan_image)
mp.add_key_binding(nil, "rotate-video", rotate_video)
mp.add_key_binding(nil, "reset-pan-if-visible", reset_pan_if_visible)
mp.add_key_binding(nil, "force-print-filename", force_print_filename)
|