Skip to content

Commit

Permalink
Chapters: add functionality to seek to the nearest chapter on click
Browse files Browse the repository at this point in the history
This is not implemented particularly gracefully. Currently, it must be
bound to a separate button than standard click to seek, and it will
always seek to a chapter, even if the mouse is not particularly close
to a chapter marker.

It might make sense to have a setting that seeks to a chapter if the
mouse is within a certain threshold of the chapter marker, but that's
more difficult to implement and also requires pixels-on-screen
computation for the hitbox, which is messy.

This feature is disabled by default for backward compatibility, since
the default keybindings have MBTN_RIGHT bound to cycle pause.
  • Loading branch information
torque committed Jun 19, 2024
1 parent fe897fd commit f804f2e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/ActivityZone.moon
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class ActivityZone extends Rect
@elements\remove element

-- bottom-up click propagation does not deal with mouse down/up events.
clickHandler: =>
clickHandler: ( button ) =>
unless @containsPoint Mouse.clickX, Mouse.clickY
return

for _, element in ipairs @elements
-- if clickHandler returns false, the click stops propagating.
if element.clickHandler and not element\clickHandler!
if element.clickHandler and element\clickHandler( button ) == false
break

activityCheck: ( displayRequested ) =>
Expand All @@ -41,7 +41,7 @@ class ActivityZone extends Rect
for id, element in ipairs @elements
element\activate nowActive

if clickPending
@clickHandler!
if clickPending != false
@clickHandler clickPending

return nowActive
26 changes: 26 additions & 0 deletions src/Chapters.moon
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ class Chapters extends BarBase
@createMarkers!
@animation = Animation 0, 1, @animationDuration, @\animate

clickHandler: ( button ) =>
if button == 2
@seekNearestChapter Mouse.clickX / Window.w
return false

seekNearestChapter: ( frac ) =>
chapters = mp.get_property_native 'chapter-list', { }
if #chapters == 0
return

duration = mp.get_property_number( 'duration', 0 )
time = duration * frac

-- could make this a bisection ehhhhh
mindist = duration
minidx = #chapters
for idx, chap in ipairs chapters
dist = math.abs chap.time - time
if dist < mindist
mindist = dist
minidx = idx
elseif dist > mindist
break

mp.set_property_native 'chapter', minidx - 1

resize: =>
for i, marker in ipairs @markers
marker\resize!
Expand Down
22 changes: 14 additions & 8 deletions src/Mouse.moon
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@ class Mouse
@x, @y = scaledPosition @
if @dead and (oldX != @x or oldY != @y)
@dead = false
if not @dead and @clickPending
if not @dead and @clickPending != false
button = @clickPending
@clickPending = false
return true
return button
return false

@cacheClick: =>
@cacheClick: (button) =>
if not @dead
@clickX, @clickY = scaledPosition @
@clickPending = true
@clickPending = button
else
@dead = false

mp.add_key_binding "mouse_btn0", "left-click", ->
Mouse\cacheClick!
mp.add_key_binding 'MBTN_LEFT', 'left-click', ->
Mouse\cacheClick 0


if settings['enable-chapter-seek']
mp.add_key_binding settings['chapter-seek-button'], 'chapter-seek-click', ->
Mouse\cacheClick 2

mp.observe_property 'fullscreen', 'bool', ->
Mouse\update!
Mouse.dead = true

mp.add_forced_key_binding "mouse_leave", "mouse-leave", ->
mp.add_forced_key_binding 'mouse_leave', 'mouse-leave', ->
Mouse.inWindow = false

mp.add_forced_key_binding "mouse_enter", "mouse-enter", ->
mp.add_forced_key_binding 'mouse_enter', 'mouse-enter', ->
Mouse.inWindow = true
9 changes: 7 additions & 2 deletions src/ProgressBar.moon
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ class ProgressBar extends BarBase
@line[7] = [[]]
@line[8] = @line[8]\format settings['bar-foreground-style']

clickHandler: =>
mp.commandv "seek", Mouse.clickX*100/Window.w, seekString
clickHandler: ( button ) =>
if button == 0
@seek Mouse.clickX * 100 / Window.w
return false

seek: ( percent ) =>
mp.commandv 'seek', percent, seekString

resize: =>
super!
Expand Down
14 changes: 14 additions & 0 deletions src/settings.moon
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,20 @@ number of chapters may slow down the script somewhat, but I have yet to run
into this being a problem.
]]

settings['enable-chapter-seek'] = false
helpText['enable-chapter-seek'] = [[
If enabled and the item being played back has chapters, using the
`chapter-seek-button` while the progress bar is hovered will seek the video to
the chapter that is closest to the mouse cursor's position.
]]

settings['chapter-seek-button'] = 'MBTN_RIGHT'
helpText['chapter-seek-button'] = [[
The button to register for chapter seeking, if enabled. Since chapter seeking
is based on the mouse position, this should probably be bound to a mouse button,
but it doesn't have to be.
]]

settings['chapter-marker-width'] = 2
helpText['chapter-marker-width'] = [[
Controls the width of each chapter marker when the progress bar is inactive.
Expand Down
10 changes: 10 additions & 0 deletions torque-progressbar.conf
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ pause-indicator-background-style=\c&H2D2D2D&
# into this being a problem.
enable-chapter-markers=yes

# If enabled and the item being played back has chapters, using the
# `chapter-seek-button` while the progress bar is hovered will seek the video to
# the chapter that is closest to the mouse cursor's position.
enable-chapter-seek=no

# The button to register for chapter seeking, if enabled. Since chapter seeking
# is based on the mouse position, this should probably be bound to a mouse button,
# but it doesn't have to be.
chapter-seek-button=MBTN_RIGHT

# Controls the width of each chapter marker when the progress bar is inactive.
chapter-marker-width=2

Expand Down

0 comments on commit f804f2e

Please sign in to comment.