menu.lua (1951B)
1 --[[ 2 Menu for mpvacious 3 4 Copyright (C) 2022 Ren Tatsumoto 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <https://www.gnu.org/licenses/>. 18 ]] 19 20 local mp = require('mp') 21 local help = require('helpers') 22 23 local Menu = { 24 active = false, 25 keybindings = {}, 26 overlay = mp.create_osd_overlay and mp.create_osd_overlay('ass-events'), 27 } 28 29 function Menu:new(o) 30 o = o or {} 31 setmetatable(o, self) 32 self.__index = self 33 return o 34 end 35 36 function Menu:with_update(params) 37 return function() 38 pcall(help.unpack(params)) 39 self:update() 40 end 41 end 42 43 function Menu:make_osd() 44 return nil 45 end 46 47 function Menu:update() 48 if self.active == false then return end 49 self.overlay.data = self:make_osd():get_text() 50 self.overlay:update() 51 end 52 53 function Menu:open() 54 if self.overlay == nil then 55 help.notify("OSD overlay is not supported in " .. mp.get_property("mpv-version"), "error", 5) 56 return 57 end 58 59 if self.active == true then 60 self:close() 61 return 62 end 63 64 for _, val in pairs(self.keybindings) do 65 mp.add_forced_key_binding(val.key, val.key, val.fn) 66 end 67 68 self.active = true 69 self:update() 70 end 71 72 function Menu:close() 73 if self.active == false then 74 return 75 end 76 77 for _, val in pairs(self.keybindings) do 78 mp.remove_key_binding(val.key) 79 end 80 81 self.overlay:remove() 82 self.active = false 83 end 84 85 return Menu