Skip to content

Commit b13c616

Browse files
fix bugs in page navigation and video playback
1 parent ee01fad commit b13c616

2 files changed

Lines changed: 49 additions & 29 deletions

File tree

site/src/TimeManager.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,48 @@ export class TimeManager {
7676

7777
advancePage(numPages : number, updateSource: UpdateSource) {
7878
console.log("advancePage", numPages);
79-
const currentPage = bar_to_page[this.scoreTime.act - 1][this.scoreTime.bar].page - 1
80-
+ act_starting_pages[this.scoreTime.act - 1];
81-
let targetPage = currentPage + numPages;
82-
if (targetPage < act_starting_pages[0]) {
83-
targetPage = act_starting_pages[0];
84-
} else if (targetPage >= 486) {
85-
targetPage = 486;
86-
}
8779

88-
for (let act = 0; act < bar_to_page.length; ++act) {
89-
if (targetPage >= act_starting_pages[act] && targetPage < act_starting_pages[act + 1]) {
90-
targetPage -= act_starting_pages[act];
91-
targetPage += 1;
92-
for (const bar in bar_to_page[act]) {
93-
if (bar_to_page[act][bar].page === targetPage) {
94-
this.goToTime(act + 1, Number(bar), this.scoreTime.beat, updateSource);
95-
return;
96-
}
80+
const firstPage = act_starting_pages[0];
81+
const lastPage = 486;
82+
83+
// Compute current absolute page (act-relative page is 1-based, so subtract 1 before adding act offset)
84+
const currentActIndex = this.scoreTime.act - 1;
85+
const currentWithinActPage = bar_to_page[currentActIndex][this.scoreTime.bar].page;
86+
const currentAbsolutePage = currentWithinActPage - 1 + act_starting_pages[currentActIndex];
87+
88+
const targetAbsolutePage = Math.max(firstPage, Math.min(lastPage, currentAbsolutePage + numPages));
89+
90+
// Search outward from targetAbsolutePage in the direction of travel until we find a
91+
// page that has at least one bar starting on it. This handles pages that fall in
92+
// the middle of a bar (no bar starts there) without silently doing nothing.
93+
const direction = numPages >= 0 ? 1 : -1;
94+
95+
for (let page = targetAbsolutePage; page >= firstPage && page <= lastPage; page += direction) {
96+
// Determine which act this absolute page belongs to (last act whose start ≤ page)
97+
let actIndex = -1;
98+
for (let a = act_starting_pages.length - 1; a >= 0; a--) {
99+
if (page >= act_starting_pages[a]) {
100+
actIndex = a;
101+
break;
102+
}
103+
}
104+
if (actIndex === -1) continue;
105+
106+
const withinActPage = page - act_starting_pages[actIndex] + 1;
107+
108+
// Numeric keys in JS objects iterate in ascending order, so the first match is
109+
// the lowest-numbered (i.e. first) bar that starts on this page.
110+
for (const barStr in bar_to_page[actIndex]) {
111+
if (bar_to_page[actIndex][barStr].page === withinActPage) {
112+
this.goToTime(actIndex + 1, Number(barStr), this.scoreTime.beat, updateSource);
113+
return;
97114
}
98115
}
99116
}
100117
}
101118

102119
notifyListeners(updateSource: UpdateSource) {
120+
console.log("updateSource", updateSource, "time", this.scoreTime);
103121
for (const listener of this.listeners) {
104122
listener.timeUpdated(this.scoreTime, updateSource);
105123
}

site/src/VideoPlayerManager.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,24 @@ export class VideoPlayerManager extends TimeManagerListener {
3939
}
4040

4141
navigateToCurrentTime() {
42-
const time : number | null = this.player?.getCurrentTime() ?? null;
43-
let gotoAct = 1;
44-
let gotoBar = 1;
45-
if (time !== null) {
46-
for (const act in recordingTimestamps) {
47-
for (const bar in recordingTimestamps[act]) {
48-
if (recordingTimestamps[act][bar] > time) {
49-
this.timeManager.goToTime(gotoAct, gotoBar, 1, "video-playhead");
50-
return;
42+
if (this.player?.getPlayerState() === 1) {
43+
const time : number | null = this.player?.getCurrentTime() ?? null;
44+
let gotoAct = 1;
45+
let gotoBar = 1;
46+
if (time !== null) {
47+
for (const act in recordingTimestamps) {
48+
for (const bar in recordingTimestamps[act]) {
49+
if (recordingTimestamps[act][bar] > time) {
50+
this.timeManager.goToTime(gotoAct, gotoBar, 1, "video-playhead");
51+
return;
52+
}
53+
gotoAct = Number(act);
54+
gotoBar = Number(bar);
5155
}
52-
gotoAct = Number(act);
53-
gotoBar = Number(bar);
5456
}
5557
}
58+
this.timeManager.goToTime(gotoAct, gotoBar, 1, "video-playhead");
5659
}
57-
this.timeManager.goToTime(gotoAct, gotoBar, 1, "video-playhead");
5860
}
5961

6062
seekTo(seconds: number) {

0 commit comments

Comments
 (0)