VexFlowStaffEntry.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {GraphicalStaffEntry} from "../GraphicalStaffEntry";
  2. import {VexFlowMeasure} from "./VexFlowMeasure";
  3. import {SourceStaffEntry} from "../../VoiceData/SourceStaffEntry";
  4. import {unitInPixels} from "./VexFlowMusicSheetDrawer";
  5. export class VexFlowStaffEntry extends GraphicalStaffEntry {
  6. constructor(measure: VexFlowMeasure, sourceStaffEntry: SourceStaffEntry, staffEntryParent: VexFlowStaffEntry) {
  7. super(measure, sourceStaffEntry, staffEntryParent);
  8. }
  9. // The corresponding VexFlow.StaveNotes
  10. public vfNotes: { [voiceID: number]: Vex.Flow.StaveNote; } = {};
  11. /**
  12. * Calculates the staff entry positions from the VexFlow stave information and the tickabels inside the staff.
  13. * This is needed in order to set the OSMD staff entries (which are almost the same as tickables) to the correct positionts.
  14. * It is also needed to be done after formatting!
  15. */
  16. public calculateXPosition(): void {
  17. const vfNotes: { [voiceID: number]: Vex.Flow.StaveNote; } = this.vfNotes;
  18. const stave: Vex.Flow.Stave = (this.parentMeasure as VexFlowMeasure).getVFStave();
  19. let tickablePosition: number = 0;
  20. let numberOfValidTickables: number = 0;
  21. for (const voiceId in vfNotes) {
  22. if (vfNotes.hasOwnProperty(voiceId)) {
  23. const tickable: Vex.Flow.StaveNote = vfNotes[voiceId];
  24. // This will let the tickable know how to calculate it's bounding box
  25. tickable.setStave(stave);
  26. // The middle of the tickable is also the OSMD BoundingBox center
  27. const staveNote: Vex.Flow.StaveNote = (<Vex.Flow.StaveNote>tickable);
  28. tickablePosition += staveNote.getNoteHeadEndX() - staveNote.getGlyphWidth() / 2;
  29. numberOfValidTickables++;
  30. }
  31. }
  32. tickablePosition = tickablePosition / numberOfValidTickables;
  33. // Calculate parent absolute position and reverse calculate the relative position
  34. // All the modifiers signs, clefs, you name it have an offset in the measure. Therefore remove it.
  35. // NOTE: Somehow vexflows shift is off by 25px.
  36. const modifierOffset: number = stave.getModifierXShift() - (this.parentMeasure.MeasureNumber === 1 ? 25 : 0);
  37. // const modifierOffset: number = 0;
  38. // sets the vexflow x positions back into the bounding boxes of the staff entries in the osmd object model.
  39. // The positions are needed for cursor placement and mouse/tap interactions
  40. this.PositionAndShape.RelativePosition.x = (tickablePosition - stave.getNoteStartX() + modifierOffset) / unitInPixels;
  41. }
  42. }