Skip to content

Commit f804f2e

Browse files
committed
Chapters: add functionality to seek to the nearest chapter on click
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.
1 parent fe897fd commit f804f2e

File tree

6 files changed

+75
-14
lines changed

6 files changed

+75
-14
lines changed

src/ActivityZone.moon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class ActivityZone extends Rect
1515
@elements\remove element
1616

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

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

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

44-
if clickPending
45-
@clickHandler!
44+
if clickPending != false
45+
@clickHandler clickPending
4646

4747
return nowActive

src/Chapters.moon

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,32 @@ class Chapters extends BarBase
3939
@createMarkers!
4040
@animation = Animation 0, 1, @animationDuration, @\animate
4141

42+
clickHandler: ( button ) =>
43+
if button == 2
44+
@seekNearestChapter Mouse.clickX / Window.w
45+
return false
46+
47+
seekNearestChapter: ( frac ) =>
48+
chapters = mp.get_property_native 'chapter-list', { }
49+
if #chapters == 0
50+
return
51+
52+
duration = mp.get_property_number( 'duration', 0 )
53+
time = duration * frac
54+
55+
-- could make this a bisection ehhhhh
56+
mindist = duration
57+
minidx = #chapters
58+
for idx, chap in ipairs chapters
59+
dist = math.abs chap.time - time
60+
if dist < mindist
61+
mindist = dist
62+
minidx = idx
63+
elseif dist > mindist
64+
break
65+
66+
mp.set_property_native 'chapter', minidx - 1
67+
4268
resize: =>
4369
for i, marker in ipairs @markers
4470
marker\resize!

src/Mouse.moon

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,33 @@ class Mouse
1515
@x, @y = scaledPosition @
1616
if @dead and (oldX != @x or oldY != @y)
1717
@dead = false
18-
if not @dead and @clickPending
18+
if not @dead and @clickPending != false
19+
button = @clickPending
1920
@clickPending = false
20-
return true
21+
return button
2122
return false
2223

23-
@cacheClick: =>
24+
@cacheClick: (button) =>
2425
if not @dead
2526
@clickX, @clickY = scaledPosition @
26-
@clickPending = true
27+
@clickPending = button
2728
else
2829
@dead = false
2930

30-
mp.add_key_binding "mouse_btn0", "left-click", ->
31-
Mouse\cacheClick!
31+
mp.add_key_binding 'MBTN_LEFT', 'left-click', ->
32+
Mouse\cacheClick 0
33+
34+
35+
if settings['enable-chapter-seek']
36+
mp.add_key_binding settings['chapter-seek-button'], 'chapter-seek-click', ->
37+
Mouse\cacheClick 2
3238

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

37-
mp.add_forced_key_binding "mouse_leave", "mouse-leave", ->
43+
mp.add_forced_key_binding 'mouse_leave', 'mouse-leave', ->
3844
Mouse.inWindow = false
3945

40-
mp.add_forced_key_binding "mouse_enter", "mouse-enter", ->
46+
mp.add_forced_key_binding 'mouse_enter', 'mouse-enter', ->
4147
Mouse.inWindow = true

src/ProgressBar.moon

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ class ProgressBar extends BarBase
1515
@line[7] = [[]]
1616
@line[8] = @line[8]\format settings['bar-foreground-style']
1717

18-
clickHandler: =>
19-
mp.commandv "seek", Mouse.clickX*100/Window.w, seekString
18+
clickHandler: ( button ) =>
19+
if button == 0
20+
@seek Mouse.clickX * 100 / Window.w
21+
return false
22+
23+
seek: ( percent ) =>
24+
mp.commandv 'seek', percent, seekString
2025

2126
resize: =>
2227
super!

src/settings.moon

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,20 @@ number of chapters may slow down the script somewhat, but I have yet to run
345345
into this being a problem.
346346
]]
347347

348+
settings['enable-chapter-seek'] = false
349+
helpText['enable-chapter-seek'] = [[
350+
If enabled and the item being played back has chapters, using the
351+
`chapter-seek-button` while the progress bar is hovered will seek the video to
352+
the chapter that is closest to the mouse cursor's position.
353+
]]
354+
355+
settings['chapter-seek-button'] = 'MBTN_RIGHT'
356+
helpText['chapter-seek-button'] = [[
357+
The button to register for chapter seeking, if enabled. Since chapter seeking
358+
is based on the mouse position, this should probably be bound to a mouse button,
359+
but it doesn't have to be.
360+
]]
361+
348362
settings['chapter-marker-width'] = 2
349363
helpText['chapter-marker-width'] = [[
350364
Controls the width of each chapter marker when the progress bar is inactive.

torque-progressbar.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,16 @@ pause-indicator-background-style=\c&H2D2D2D&
245245
# into this being a problem.
246246
enable-chapter-markers=yes
247247

248+
# If enabled and the item being played back has chapters, using the
249+
# `chapter-seek-button` while the progress bar is hovered will seek the video to
250+
# the chapter that is closest to the mouse cursor's position.
251+
enable-chapter-seek=no
252+
253+
# The button to register for chapter seeking, if enabled. Since chapter seeking
254+
# is based on the mouse position, this should probably be bound to a mouse button,
255+
# but it doesn't have to be.
256+
chapter-seek-button=MBTN_RIGHT
257+
248258
# Controls the width of each chapter marker when the progress bar is inactive.
249259
chapter-marker-width=2
250260

0 commit comments

Comments
 (0)