Skip to content

Commit

Permalink
Ensure correct BoundsLookup state CoderLine#960
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielku15 authored and ChiHoc committed Nov 7, 2022
1 parent 74e0976 commit a5b7e3c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
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

0 comments on commit a5b7e3c

Please sign in to comment.