PauseIndicator.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. local Element = require('elements/Element')
  2. ---@class PauseIndicator : Element
  3. local PauseIndicator = class(Element)
  4. function PauseIndicator:new() return Class.new(self) --[[@as PauseIndicator]] end
  5. function PauseIndicator:init()
  6. Element.init(self, 'pause_indicator', {render_order = 3})
  7. self.ignores_curtain = true
  8. self.paused = state.pause
  9. self.opacity = 0
  10. self.fadeout = false
  11. self:init_options()
  12. end
  13. function PauseIndicator:init_options()
  14. self.base_icon_opacity = options.pause_indicator == 'flash' and 1 or 0.8
  15. self.type = options.pause_indicator
  16. self:on_prop_pause()
  17. end
  18. function PauseIndicator:flash()
  19. -- Can't wait for pause property event listener to set this, because when this is used inside a binding like:
  20. -- cycle pause; script-binding uosc/flash-pause-indicator
  21. -- The pause event is not fired fast enough, and indicator starts rendering with old icon.
  22. self.paused = mp.get_property_native('pause')
  23. self.fadeout, self.opacity = false, 1
  24. self:tween_property('opacity', 1, 0, 300)
  25. end
  26. -- Decides whether static indicator should be visible or not.
  27. function PauseIndicator:decide()
  28. self.paused = mp.get_property_native('pause') -- see flash() for why this line is necessary
  29. self.fadeout, self.opacity = self.paused, self.paused and 1 or 0
  30. request_render()
  31. -- Workaround for an mpv race condition bug during pause on windows builds, which causes osd updates to be ignored.
  32. -- .03 was still loosing renders, .04 was fine, but to be safe I added 10ms more
  33. mp.add_timeout(.05, function() osd:update() end)
  34. end
  35. function PauseIndicator:on_prop_pause()
  36. if Elements:v('timeline', 'pressed') then return end
  37. if options.pause_indicator == 'flash' then
  38. if self.paused ~= state.pause then self:flash() end
  39. elseif options.pause_indicator == 'static' then
  40. self:decide()
  41. end
  42. end
  43. function PauseIndicator:on_options()
  44. self:init_options()
  45. if self.type == 'flash' then self.opacity = 0 end
  46. end
  47. function PauseIndicator:render()
  48. if self.opacity == 0 then return end
  49. local ass = assdraw.ass_new()
  50. -- Background fadeout
  51. if self.fadeout then
  52. ass:rect(0, 0, display.width, display.height, {color = bg, opacity = self.opacity * 0.3})
  53. end
  54. -- Icon
  55. local size = round(math.min(display.width, display.height) * (self.fadeout and 0.20 or 0.15))
  56. size = size + size * (1 - self.opacity)
  57. if self.paused then
  58. ass:icon(display.width / 2, display.height / 2, size, 'pause',
  59. {border = 1, opacity = self.base_icon_opacity * self.opacity}
  60. )
  61. else
  62. ass:icon(display.width / 2, display.height / 2, size * 1.2, 'play_arrow',
  63. {border = 1, opacity = self.base_icon_opacity * self.opacity}
  64. )
  65. end
  66. return ass
  67. end
  68. return PauseIndicator