Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct BoundsLookup state #960

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/platform/javascript/AlphaTabWorkerScoreRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class AlphaTabWorkerScoreRenderer<T> implements IScoreRenderer {
break;
case 'alphaTab.postRenderFinished':
this.boundsLookup = BoundsLookup.fromJson(data.boundsLookup, this._api.score!);
this.boundsLookup.finish();
(this.postRenderFinished as EventEmitter).trigger();
break;
case 'alphaTab.error':
Expand Down
1 change: 1 addition & 0 deletions src/rendering/ScoreRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export class ScoreRenderer implements IScoreRenderer {
public readonly error: IEventEmitterOfT<Error> = new EventEmitterOfT<Error>();

private onRenderFinished() {
this.boundsLookup?.finish();
const e = new RenderFinishedEventArgs();
e.totalHeight = this.layout!.height;
e.totalWidth = this.layout!.width;
Expand Down
7 changes: 7 additions & 0 deletions src/rendering/utils/BarBounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ export class BarBounds {
}
return beat;
}

/**
* Finishes the lookup object and optimizes itself for fast access.
*/
public finish(): void {
this.beats.sort((a, b) => a.realBounds.x - b.realBounds.x);
}
}
9 changes: 8 additions & 1 deletion src/rendering/utils/MasterBarBounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ export class MasterBarBounds {
*/
public findBeatAtPos(x: number, y: number): Beat | null {
let beat: BeatBounds | null = null;
let distance = 10000000;
for (let bar of this.bars) {
let b = bar.findBeatAtPos(x);
if (b && (!beat || beat.realBounds.x < b.realBounds.x)) {
beat = b;
const newDistance = Math.abs(b.realBounds.x - x);
if (!beat || newDistance < distance) {
beat = b;
}
}
}
return !beat ? null : beat.beat;
Expand All @@ -88,6 +92,9 @@ export class MasterBarBounds {
}
return 0;
});
for (const bar of this.bars) {
bar.finish();
}
}

/**
Expand Down