|
| 1 | +--[[ |
| 2 | +This file is part of mpv. |
| 3 | +
|
| 4 | +mpv is free software; you can redistribute it and/or |
| 5 | +modify it under the terms of the GNU Lesser General Public |
| 6 | +License as published by the Free Software Foundation; either |
| 7 | +version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | +mpv is distributed in the hope that it will be useful, |
| 10 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU Lesser General Public |
| 15 | +License along with mpv. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +]] |
| 17 | + |
| 18 | +local options = { |
| 19 | + suppress_osd = false, |
| 20 | +} |
| 21 | + |
| 22 | +require "mp.options".read_options(options) |
| 23 | + |
| 24 | +mp.register_script_message("pan", function (axis, amount) |
| 25 | + local dims = mp.get_property_native("osd-dimensions") |
| 26 | + local dimension = axis == "x" and dims.w - dims.ml - dims.mr or dims.h - dims.mt - dims.mb |
| 27 | + local osd_dimension = axis == "x" and dims.w or dims.h |
| 28 | + -- 1 video-align shifts the OSD by (dimension - osd_dimension) / 2 pixels, |
| 29 | + -- so the equation to find how much video-align to add to offset the OSD by |
| 30 | + -- osd_dimension is: |
| 31 | + -- x/1 = osd_dimension / ((dimension - osd_dimension) / 2) |
| 32 | + if dimension ~= osd_dimension then |
| 33 | + mp.commandv("add", "video-align-" .. axis, |
| 34 | + amount * 2 * osd_dimension / (dimension - osd_dimension)) |
| 35 | + end |
| 36 | +end) |
| 37 | + |
| 38 | +mp.add_key_binding(nil, "drag-to-pan", function (t) |
| 39 | + if t.event == "up" then |
| 40 | + mp.remove_key_binding("drag-to-pan-mouse-move") |
| 41 | + return |
| 42 | + end |
| 43 | + |
| 44 | + local dims = mp.get_property_native("osd-dimensions") |
| 45 | + local old_mouse_pos = mp.get_property_native("mouse-pos") |
| 46 | + local old_align_x = mp.get_property_native("video-align-x") |
| 47 | + local old_align_y = mp.get_property_native("video-align-y") |
| 48 | + |
| 49 | + mp.add_forced_key_binding("MOUSE_MOVE", "drag-to-pan-mouse-move", function() |
| 50 | + local mouse_pos = mp.get_property_native("mouse-pos") |
| 51 | + -- 1 video-align shifts the OSD by (dimension - osd_dimension) / 2 pixels, |
| 52 | + -- so the equation to find how much video-align to add to offset the OSD |
| 53 | + -- by the difference in mouse position is: |
| 54 | + -- x/1 = (mouse_pos - old_mouse_pos) / ((dimension - osd_dimension) / 2) |
| 55 | + local align = old_align_x + 2 * (mouse_pos.x - old_mouse_pos.x) |
| 56 | + / (dims.ml + dims.mr) |
| 57 | + mp.set_property("video-align-x", math.min(1, math.max(align, -1))) |
| 58 | + align = old_align_y + 2 * (mouse_pos.y - old_mouse_pos.y) |
| 59 | + / (dims.mt + dims.mb) |
| 60 | + mp.set_property("video-align-y", math.min(1, math.max(align, -1))) |
| 61 | + end) |
| 62 | +end, { complex = true }) |
| 63 | + |
| 64 | +mp.add_key_binding(nil, "align-to-cursor", function (t) |
| 65 | + if t.event == "up" then |
| 66 | + mp.remove_key_binding("align-to-cursor-mouse-move") |
| 67 | + return |
| 68 | + end |
| 69 | + |
| 70 | + local dims = mp.get_property_native("osd-dimensions") |
| 71 | + mp.add_forced_key_binding("MOUSE_MOVE", "align-to-cursor-mouse-move", function() |
| 72 | + local mouse_pos = mp.get_property_native("mouse-pos") |
| 73 | + mp.set_property("video-align-x", (mouse_pos.x * 2 - dims.w) / dims.w) |
| 74 | + mp.set_property("video-align-y", (mouse_pos.y * 2 - dims.h) / dims.h) |
| 75 | + end) |
| 76 | +end, { complex = true }) |
| 77 | + |
| 78 | +mp.register_script_message("cursor-centric-zoom", function (amount) |
| 79 | + local command = (options.suppress_osd and "no-osd " or "") .. |
| 80 | + "add video-zoom " .. amount .. ";" |
| 81 | + |
| 82 | + local x, y |
| 83 | + local touch_positions = mp.get_property_native("touch-pos") |
| 84 | + if next(touch_positions) then |
| 85 | + for _, position in pairs(touch_positions) do |
| 86 | + x = x + position.x |
| 87 | + y = y + position.y |
| 88 | + end |
| 89 | + x = x / #touch_positions |
| 90 | + y = y / #touch_positions |
| 91 | + else |
| 92 | + local mouse_pos = mp.get_property_native("mouse-pos") |
| 93 | + x = mouse_pos.x |
| 94 | + y = mouse_pos.y |
| 95 | + end |
| 96 | + |
| 97 | + local dims = mp.get_property_native("osd-dimensions") |
| 98 | + local width = (dims.w - dims.ml - dims.mr) * 2^amount |
| 99 | + local height = (dims.h - dims.mt - dims.mb) * 2^amount |
| 100 | + |
| 101 | + local old_cursor_ml = dims.ml - x |
| 102 | + local cursor_ml = old_cursor_ml * 2^amount |
| 103 | + local ml = cursor_ml + x |
| 104 | + -- video/out/aspect.c:src_dst_split_scaling() defines ml as: |
| 105 | + -- ml = (osd-width - width) * (video-align-x + 1) / 2 + pan-x * width |
| 106 | + -- So video-align-x is: |
| 107 | + local align = 2 * (ml - mp.get_property_native("video-pan-x") * width) |
| 108 | + / (dims.w - width) - 1 |
| 109 | + command = command .. "no-osd set video-align-x " .. |
| 110 | + math.min(1, math.max(align, -1)) .. ";" |
| 111 | + |
| 112 | + local mt = (dims.mt - y) * 2^amount + y |
| 113 | + align = 2 * (mt - mp.get_property_native("video-pan-y") * height) |
| 114 | + / (dims.h - height) - 1 |
| 115 | + command = command .. "no-osd set video-align-y " .. |
| 116 | + math.min(1, math.max(align, -1)) |
| 117 | + |
| 118 | + mp.command(command) |
| 119 | +end) |
0 commit comments