GraphicalVoiceEntry.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { GraphicalObject } from "./GraphicalObject";
  2. import { VoiceEntry } from "../VoiceData/VoiceEntry";
  3. import { BoundingBox } from "./BoundingBox";
  4. import { GraphicalNote } from "./GraphicalNote";
  5. import { GraphicalStaffEntry } from "./GraphicalStaffEntry";
  6. import { OctaveEnum } from "../VoiceData/Expressions/ContinuousExpressions/OctaveShift";
  7. import { VexFlowVoiceEntry } from "./VexFlow/VexFlowVoiceEntry";
  8. import { EngravingRules } from "./EngravingRules";
  9. /**
  10. * The graphical counterpart of a [[VoiceEntry]].
  11. */
  12. export class GraphicalVoiceEntry extends GraphicalObject {
  13. constructor(parentVoiceEntry: VoiceEntry, parentStaffEntry: GraphicalStaffEntry) {
  14. super();
  15. this.parentVoiceEntry = parentVoiceEntry;
  16. this.parentStaffEntry = parentStaffEntry;
  17. this.PositionAndShape = new BoundingBox(this, parentStaffEntry ? parentStaffEntry.PositionAndShape : undefined, true);
  18. this.notes = [];
  19. }
  20. public parentVoiceEntry: VoiceEntry;
  21. public parentStaffEntry: GraphicalStaffEntry;
  22. public notes: GraphicalNote[];
  23. /** Contains octave shifts affecting this voice entry, caused by octave brackets. */
  24. public octaveShiftValue: OctaveEnum;
  25. /** Sort this entry's notes by pitch.
  26. * Notes need to be sorted for Vexflow StaveNote creation.
  27. * Note that Vexflow needs the reverse order, see VexFlowConverter.StaveNote().
  28. */
  29. public sort(): void {
  30. this.notes.sort((a, b) => {
  31. return b.sourceNote.Pitch.getHalfTone() - a.sourceNote.Pitch.getHalfTone();
  32. });
  33. }
  34. /** (Re-)color notes and stems by setting their Vexflow styles.
  35. * Could be made redundant by a Vexflow PR, but Vexflow needs more solid and permanent color methods/variables for that
  36. * See VexFlowConverter.StaveNote()
  37. */
  38. public color(): void {
  39. const defaultColorNotehead: string = EngravingRules.Rules.DefaultColorNotehead;
  40. const defaultColorRest: string = EngravingRules.Rules.DefaultColorRest;
  41. const defaultColorStem: string = EngravingRules.Rules.DefaultColorStem;
  42. const vfStaveNote: any = (<VexFlowVoiceEntry>(this as any)).vfStaveNote;
  43. for (let i: number = 0; i < this.notes.length; i++) {
  44. const note: GraphicalNote = this.notes[i];
  45. let noteheadColor: string = note.sourceNote.NoteheadColorXml;
  46. // DEBUG runtime coloring test
  47. /*const testColor: string = "#FF0000";
  48. if (i === 2 && Math.random() < 0.1 && note.sourceNote.NoteheadColor !== testColor) {
  49. const measureNumber: number = note.parentVoiceEntry.parentStaffEntry.parentMeasure.MeasureNumber;
  50. noteheadColor = testColor;
  51. console.log("color changed to " + noteheadColor + " of this note:\n" + note.sourceNote.Pitch.ToString() +
  52. ", in measure #" + measureNumber);
  53. }*/
  54. if (!noteheadColor) {
  55. if (!note.sourceNote.isRest() && defaultColorNotehead) {
  56. noteheadColor = defaultColorNotehead;
  57. } else if (note.sourceNote.isRest() && defaultColorRest) {
  58. noteheadColor = defaultColorRest;
  59. }
  60. }
  61. if (noteheadColor) {
  62. note.sourceNote.NoteheadColor = noteheadColor;
  63. } else {
  64. continue;
  65. }
  66. if (vfStaveNote) {
  67. if (vfStaveNote.note_heads) { // see VexFlowConverter, needs Vexflow PR
  68. const notehead: any = vfStaveNote.note_heads[i];
  69. if (notehead) {
  70. vfStaveNote.note_heads[i].setStyle({ fillStyle: noteheadColor, strokeStyle: noteheadColor });
  71. }
  72. }
  73. }
  74. }
  75. // color stems
  76. let stemColor: string = this.parentVoiceEntry.StemColorXml;
  77. if (!stemColor && defaultColorStem) {
  78. stemColor = defaultColorStem;
  79. }
  80. const stemStyle: Object = { fillStyle: stemColor, strokeStyle: stemColor };
  81. if (stemColor && vfStaveNote.setStemStyle) {
  82. this.parentVoiceEntry.StemColor = stemColor;
  83. vfStaveNote.setStemStyle(stemStyle);
  84. if (vfStaveNote.flag && vfStaveNote.setFlagStyle && EngravingRules.Rules.ColorFlags) {
  85. vfStaveNote.setFlagStyle(stemStyle);
  86. }
  87. }
  88. }
  89. }