diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-07 18:59:06 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-07 18:59:06 -0800 |
| commit | 8fd7556551ee6eeb353ac8e6815cdc78b361682b (patch) | |
| tree | 58037cdcdfc85a7551087be2077d1ba3d825a04e /osd.lua | |
| parent | c15719de25f65e8bb3f5c57960b60bc0ec17f7cd (diff) | |
| download | mpv-iptv-menu-8fd7556551ee6eeb353ac8e6815cdc78b361682b.tar.gz mpv-iptv-menu-8fd7556551ee6eeb353ac8e6815cdc78b361682b.tar.xz | |
clean up image drawing logic
Diffstat (limited to 'osd.lua')
| -rw-r--r-- | osd.lua | 117 |
1 files changed, 65 insertions, 52 deletions
@@ -167,6 +167,51 @@ function mt:option_path(opt, info) return str end +function mt:load_image() + local f = mp_utils.file_info(self.img.path) + if not f then + return false + end + + local cmd = 'magick \'' .. self.img.path .. '\'' .. + ' -background none' .. + ' -gravity northwest' .. + ' -depth 8' .. + ' -resize ' .. self.img.cmd.w .. 'x' .. self.img.cmd.h .. + ' -extent ' .. self.img.cmd.w .. 'x' .. self.img.cmd.h .. + ' tmp.bgra' + print('exec: ' .. cmd) + os.execute(cmd) + self.img.loaded = true + return true +end + +function mt:clear_image(purge) + if not self.img then + return + end + + mp.command_native({name = 'overlay-remove', id = self.img.cmd.id}) + if purge then + self.img = nil + end +end + +function mt:draw_image() + if not self.img then + return + end + + if not (self.img.loaded or self:load_image()) then + self:clear_image() + return + end + + if not self:is_hidden() then + mp.command_native(self.img.cmd) + end +end + function mt:update_image(path, menu_res) -- images require *real* dimensions and coordinates, unlike other OSD -- functions which scale the given values automatically @@ -196,28 +241,10 @@ function mt:update_image(path, menu_res) local x = math.floor(self.width - padding - w) local y = math.floor(padding) - local disp - if self.img and self.img.loaded and path == self.img.path and - w == self.img.cmd.w and h == self.img.cmd.h then - if x == self.img.cmd.x and y == self.img.cmd.y then - return - end - - disp = true - else - local f = mp_utils.file_info(path) - if f then - local cmd = 'magick \'' .. path .. '\'' .. - ' -background none' .. - ' -gravity northwest' .. - ' -depth 8' .. - ' -resize ' .. w .. 'x' .. h .. - ' -extent ' .. w .. 'x' .. h .. - ' tmp.bgra' - print('exec: ' .. cmd) - os.execute(cmd) - disp = true - end + local same = self.img and self.img.loaded and path == self.img.path and + w == self.img.cmd.w and h == self.img.cmd.h + if same and x == self.img.cmd.x and y == self.img.cmd.y then + return end self.img = { @@ -234,22 +261,8 @@ function mt:update_image(path, menu_res) offset = 0, stride = 4*w, }, + loaded = same, } - - if disp then - self.img.loaded = true - if not self.fg.hidden then - mp.command_native(self.img.cmd) - end - else - mp.command_native( - {name = 'overlay-remove', id = self.img.cmd.id}) - end -end - -function mt:remove_image() - mp.command_native({name = 'overlay-remove', id = self.img.cmd.id}) - self.img = nil end function mt:draw_scrollbar(state) @@ -289,7 +302,7 @@ function mt:redraw(state) local img if menu.img_url then - img = get_image_path(menu.img_url, true) + img = menu.img_url end for i = menu.view_top, math.min( @@ -317,7 +330,7 @@ function mt:redraw(state) self:option_path(opt, info) if selected and opt.image then - img = get_image_path(opt.image, true) + img = opt.image end end @@ -337,26 +350,26 @@ function mt:redraw(state) local res = self.fg:update() self.bg:update() if img then - self:update_image(img, res) - elseif self.img then - self:remove_image() + local path = get_image_path(img, true) + self:update_image(path, res) + self:draw_image() + else + self:clear_image(true) end end function mt:toggle_hidden() - self.fg.hidden = not self.fg.hidden + local hidden = not self:is_hidden() + + self.fg.hidden = hidden self.fg:update() - self.bg.hidden = self.fg.hidden + self.bg.hidden = hidden self.bg:update() - if self.img then - if self.fg.hidden then - mp.command_native({ - name = 'overlay-remove', - id = self.img.cmd.id}) - else - mp.command_native(self.img.cmd) - end + if hidden then + self:clear_image() + else + self:draw_image() end end |
