StaffLine.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {Staff} from "../VoiceData/Staff";
  2. import {BoundingBox} from "./BoundingBox";
  3. import {Instrument} from "../Instrument";
  4. import {GraphicalLine} from "./GraphicalLine";
  5. import {GraphicalStaffEntry} from "./GraphicalStaffEntry";
  6. import {GraphicalObject} from "./GraphicalObject";
  7. import {StaffMeasure} from "./StaffMeasure";
  8. import {MusicSystem} from "./MusicSystem";
  9. import {StaffLineActivitySymbol} from "./StaffLineActivitySymbol";
  10. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  11. import {GraphicalLabel} from "./GraphicalLabel";
  12. /**
  13. * A StaffLine contains the [[Measure]]s in one line of the music sheet
  14. * (one instrument, one line, until a line break)
  15. */
  16. export abstract class StaffLine extends GraphicalObject {
  17. protected measures: StaffMeasure[] = [];
  18. protected staffLines: GraphicalLine[] = new Array(5);
  19. protected parentMusicSystem: MusicSystem;
  20. protected parentStaff: Staff;
  21. protected skyLine: number[];
  22. protected bottomLine: number[];
  23. protected lyricLines: GraphicalLine[] = [];
  24. protected lyricsDashes: GraphicalLabel[] = [];
  25. constructor(parentSystem: MusicSystem, parentStaff: Staff) {
  26. super();
  27. this.parentMusicSystem = parentSystem;
  28. this.parentStaff = parentStaff;
  29. this.boundingBox = new BoundingBox(this, parentSystem.PositionAndShape);
  30. }
  31. public get Measures(): StaffMeasure[] {
  32. return this.measures;
  33. }
  34. public set Measures(value: StaffMeasure[]) {
  35. this.measures = value;
  36. }
  37. public get StaffLines(): GraphicalLine[] {
  38. return this.staffLines;
  39. }
  40. public set StaffLines(value: GraphicalLine[]) {
  41. this.staffLines = value;
  42. }
  43. public get NextStaffLine(): StaffLine {
  44. const idxInParent: number = this.parentMusicSystem.StaffLines.indexOf(this);
  45. return idxInParent !== this.parentMusicSystem.StaffLines.length ? this.parentMusicSystem.StaffLines[idxInParent + 1] : undefined;
  46. }
  47. public get LyricLines(): GraphicalLine[] {
  48. return this.lyricLines;
  49. }
  50. public set LyricLines(value: GraphicalLine[]) {
  51. this.lyricLines = value;
  52. }
  53. public get LyricsDashes(): GraphicalLabel[] {
  54. return this.lyricsDashes;
  55. }
  56. public set LyricsDashes(value: GraphicalLabel[]) {
  57. this.lyricsDashes = value;
  58. }
  59. public get ParentMusicSystem(): MusicSystem {
  60. return this.parentMusicSystem;
  61. }
  62. public set ParentMusicSystem(value: MusicSystem) {
  63. this.parentMusicSystem = value;
  64. }
  65. public get ParentStaff(): Staff {
  66. return this.parentStaff;
  67. }
  68. public set ParentStaff(value: Staff) {
  69. this.parentStaff = value;
  70. }
  71. public get SkyLine(): number[] {
  72. return this.skyLine;
  73. }
  74. public set SkyLine(value: number[]) {
  75. this.skyLine = value;
  76. }
  77. public get BottomLine(): number[] {
  78. return this.bottomLine;
  79. }
  80. public set BottomLine(value: number[]) {
  81. this.bottomLine = value;
  82. }
  83. public addActivitySymbolClickArea(): void {
  84. const activitySymbol: StaffLineActivitySymbol = new StaffLineActivitySymbol(this);
  85. const staffLinePsi: BoundingBox = this.PositionAndShape;
  86. activitySymbol.PositionAndShape.RelativePosition =
  87. new PointF2D(staffLinePsi.RelativePosition.x + staffLinePsi.BorderRight + 0.5, staffLinePsi.RelativePosition.y + 0.5);
  88. activitySymbol.PositionAndShape.Parent = this.parentMusicSystem.PositionAndShape;
  89. }
  90. /**
  91. * True iff [[StaffLine]] belongs to an [[Instrument]] with more than one [[Staff]].
  92. * @returns {boolean}
  93. */
  94. public isPartOfMultiStaffInstrument(): boolean {
  95. const instrument: Instrument = this.parentStaff.ParentInstrument;
  96. if (instrument.Staves.length > 1) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Find the [[GraphicalStaffEntry]] closest to the given xPosition.
  103. * @param xPosition
  104. * @returns {GraphicalStaffEntry}
  105. */
  106. public findClosestStaffEntry(xPosition: number): GraphicalStaffEntry {
  107. let closestStaffentry: GraphicalStaffEntry = undefined;
  108. for (let idx: number = 0, len: number = this.Measures.length; idx < len; ++idx) {
  109. const graphicalMeasure: StaffMeasure = this.Measures[idx];
  110. for (let idx2: number = 0, len2: number = graphicalMeasure.staffEntries.length; idx2 < len2; ++idx2) {
  111. const graphicalStaffEntry: GraphicalStaffEntry = graphicalMeasure.staffEntries[idx2];
  112. if (
  113. Math.abs(graphicalStaffEntry.PositionAndShape.RelativePosition.x - xPosition + graphicalMeasure.PositionAndShape.RelativePosition.x) < 5.0
  114. ) {
  115. closestStaffentry = graphicalStaffEntry;
  116. }
  117. }
  118. }
  119. return closestStaffentry;
  120. }
  121. }