StaffLine.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 {GraphicalMeasure} from "./GraphicalMeasure";
  8. import {MusicSystem} from "./MusicSystem";
  9. import {StaffLineActivitySymbol} from "./StaffLineActivitySymbol";
  10. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  11. import {GraphicalLabel} from "./GraphicalLabel";
  12. import { SkyBottomLineCalculator } from "./SkyBottomLineCalculator";
  13. import { GraphicalOctaveShift } from "./GraphicalOctaveShift";
  14. import { GraphicalSlur } from "./GraphicalSlur";
  15. import { AbstractGraphicalExpression } from "./AbstractGraphicalExpression";
  16. import { EngravingRules } from "./EngravingRules";
  17. /**
  18. * A StaffLine contains the [[Measure]]s in one line of the music sheet
  19. * (one instrument, one line, until a line break)
  20. */
  21. export abstract class StaffLine extends GraphicalObject {
  22. protected measures: GraphicalMeasure[] = [];
  23. protected staffLines: GraphicalLine[] = new Array(5);
  24. protected parentMusicSystem: MusicSystem;
  25. protected parentStaff: Staff;
  26. protected octaveShifts: GraphicalOctaveShift[] = [];
  27. protected skyBottomLine: SkyBottomLineCalculator;
  28. protected lyricLines: GraphicalLine[] = [];
  29. protected lyricsDashes: GraphicalLabel[] = [];
  30. protected abstractExpressions: AbstractGraphicalExpression[] = [];
  31. /** The staff height in units */
  32. private staffHeight: number;
  33. // For displaying Slurs
  34. protected graphicalSlurs: GraphicalSlur[] = [];
  35. constructor(parentSystem: MusicSystem, parentStaff: Staff) {
  36. super();
  37. this.parentMusicSystem = parentSystem;
  38. this.parentStaff = parentStaff;
  39. this.boundingBox = new BoundingBox(this, parentSystem.PositionAndShape);
  40. this.skyBottomLine = new SkyBottomLineCalculator(this);
  41. this.staffHeight = EngravingRules.Rules.StaffHeight;
  42. if (this.parentStaff.isTab) {
  43. this.staffHeight = EngravingRules.Rules.TabStaffHeight;
  44. }
  45. }
  46. public get Measures(): GraphicalMeasure[] {
  47. return this.measures;
  48. }
  49. public set Measures(value: GraphicalMeasure[]) {
  50. this.measures = value;
  51. }
  52. public get StaffLines(): GraphicalLine[] {
  53. return this.staffLines;
  54. }
  55. public set StaffLines(value: GraphicalLine[]) {
  56. this.staffLines = value;
  57. }
  58. public get NextStaffLine(): StaffLine {
  59. const idxInParent: number = this.parentMusicSystem.StaffLines.indexOf(this);
  60. return idxInParent !== this.parentMusicSystem.StaffLines.length ? this.parentMusicSystem.StaffLines[idxInParent + 1] : undefined;
  61. }
  62. public get LyricLines(): GraphicalLine[] {
  63. return this.lyricLines;
  64. }
  65. public get AbstractExpressions(): AbstractGraphicalExpression[] {
  66. return this.abstractExpressions;
  67. }
  68. public set AbstractExpressions(value: AbstractGraphicalExpression[]) {
  69. this.abstractExpressions = value;
  70. }
  71. public set LyricLines(value: GraphicalLine[]) {
  72. this.lyricLines = value;
  73. }
  74. public get LyricsDashes(): GraphicalLabel[] {
  75. return this.lyricsDashes;
  76. }
  77. public set LyricsDashes(value: GraphicalLabel[]) {
  78. this.lyricsDashes = value;
  79. }
  80. public get ParentMusicSystem(): MusicSystem {
  81. return this.parentMusicSystem;
  82. }
  83. public set ParentMusicSystem(value: MusicSystem) {
  84. this.parentMusicSystem = value;
  85. }
  86. public get ParentStaff(): Staff {
  87. return this.parentStaff;
  88. }
  89. public set ParentStaff(value: Staff) {
  90. this.parentStaff = value;
  91. }
  92. public get SkyBottomLineCalculator(): SkyBottomLineCalculator {
  93. return this.skyBottomLine;
  94. }
  95. public get SkyLine(): number[] {
  96. return this.skyBottomLine.SkyLine;
  97. }
  98. public get BottomLine(): number[] {
  99. return this.skyBottomLine.BottomLine;
  100. }
  101. public get OctaveShifts(): GraphicalOctaveShift[] {
  102. return this.octaveShifts;
  103. }
  104. public set OctaveShifts(value: GraphicalOctaveShift[]) {
  105. this.octaveShifts = value;
  106. }
  107. public get StaffHeight(): number {
  108. return this.staffHeight;
  109. }
  110. // get all Graphical Slurs of a staffline
  111. public get GraphicalSlurs(): GraphicalSlur[] {
  112. return this.graphicalSlurs;
  113. }
  114. /**
  115. * Add a given Graphical Slur to the staffline
  116. * @param gSlur
  117. */
  118. public addSlurToStaffline(gSlur: GraphicalSlur): void {
  119. this.graphicalSlurs.push(gSlur);
  120. }
  121. public addActivitySymbolClickArea(): void {
  122. const activitySymbol: StaffLineActivitySymbol = new StaffLineActivitySymbol(this);
  123. const staffLinePsi: BoundingBox = this.PositionAndShape;
  124. activitySymbol.PositionAndShape.RelativePosition =
  125. new PointF2D(staffLinePsi.RelativePosition.x + staffLinePsi.BorderRight + 0.5, staffLinePsi.RelativePosition.y + 0.5);
  126. activitySymbol.PositionAndShape.Parent = this.parentMusicSystem.PositionAndShape;
  127. }
  128. /**
  129. * True iff [[StaffLine]] belongs to an [[Instrument]] with more than one [[Staff]].
  130. * @returns {boolean}
  131. */
  132. public isPartOfMultiStaffInstrument(): boolean {
  133. const instrument: Instrument = this.parentStaff.ParentInstrument;
  134. if (instrument.Staves.length > 1) {
  135. return true;
  136. }
  137. return false;
  138. }
  139. /**
  140. * Find the [[GraphicalStaffEntry]] closest to the given xPosition.
  141. * @param xPosition
  142. * @returns {GraphicalStaffEntry}
  143. */
  144. public findClosestStaffEntry(xPosition: number): GraphicalStaffEntry {
  145. let closestStaffentry: GraphicalStaffEntry = undefined;
  146. for (let idx: number = 0, len: number = this.Measures.length; idx < len; ++idx) {
  147. const graphicalMeasure: GraphicalMeasure = this.Measures[idx];
  148. for (let idx2: number = 0, len2: number = graphicalMeasure.staffEntries.length; idx2 < len2; ++idx2) {
  149. const graphicalStaffEntry: GraphicalStaffEntry = graphicalMeasure.staffEntries[idx2];
  150. if (
  151. Math.abs(graphicalStaffEntry.PositionAndShape.RelativePosition.x - xPosition + graphicalMeasure.PositionAndShape.RelativePosition.x) < 5.0
  152. ) {
  153. closestStaffentry = graphicalStaffEntry;
  154. }
  155. }
  156. }
  157. return closestStaffentry;
  158. }
  159. }