MusicSystem.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import {StaffLine} from "./StaffLine";
  2. import {Instrument} from "../Instrument";
  3. import {BoundingBox} from "./BoundingBox";
  4. import {Fraction} from "../../Common/DataObjects/fraction";
  5. import {SourceMeasure} from "../VoiceData/SourceMeasure";
  6. import {InstrumentalGroup} from "../InstrumentalGroup";
  7. import {TextAlignment} from "../../Common/Enums/TextAlignment";
  8. import {GraphicalMusicPage} from "./GraphicalMusicPage";
  9. import {GraphicalLabel} from "./GraphicalLabel";
  10. import {StaffMeasure} from "./StaffMeasure";
  11. import {GraphicalObject} from "./GraphicalObject";
  12. import {EngravingRules} from "./EngravingRules";
  13. import {PointF2D} from "../../Common/DataObjects/PointF2D";
  14. import {GraphicalStaffEntry} from "./GraphicalStaffEntry";
  15. import {SystemLinesEnum} from "./SystemLinesEnum";
  16. export class MusicSystem extends GraphicalObject {
  17. public needsToBeRedrawn: boolean = true;
  18. protected parent: GraphicalMusicPage;
  19. protected id: number;
  20. protected staffLines: StaffLine[] = [];
  21. protected graphicalMeasures: StaffMeasure[][] = [];
  22. protected labels: Dictionary<GraphicalLabel, Instrument> = new Dictionary<GraphicalLabel, Instrument>();
  23. protected measureNumberLabels: GraphicalLabel[] = [];
  24. protected maxLabelLength: number;
  25. protected objectsToRedraw: [Object[], Object][] = [];
  26. constructor(parent: GraphicalMusicPage, id: number) {
  27. this.parent = parent;
  28. this.id = id;
  29. this.boundingBox = new BoundingBox(parent.PositionAndShape, this);
  30. this.maxLabelLength = 0.0;
  31. }
  32. public get Parent(): GraphicalMusicPage {
  33. return this.parent;
  34. }
  35. public set Parent(value: GraphicalMusicPage) {
  36. this.parent = value;
  37. }
  38. public get StaffLines(): StaffLine[] {
  39. return this.staffLines;
  40. }
  41. public get GraphicalMeasures(): StaffMeasure[][] {
  42. return this.graphicalMeasures;
  43. }
  44. public get MeasureNumberLabels(): GraphicalLabel[] {
  45. return this.measureNumberLabels;
  46. }
  47. public get Labels(): GraphicalLabel[] {
  48. return this.labels.GetKeys();
  49. }
  50. public get ObjectsToRedraw(): [Object[], Object][] {
  51. return this.objectsToRedraw;
  52. }
  53. public get Id(): number {
  54. return this.id;
  55. }
  56. public createSystemLeftVerticalLineObject(lineWidth: number, systemLabelsRightMargin: number): void {
  57. throw new Error("not implemented");
  58. }
  59. public createVerticalLineForMeasure(position: number, lineType: SystemLinesEnum, lineWidth: number, index: number): void {
  60. throw new Error("not implemented");
  61. }
  62. public setYPositionsToVerticalLineObjectsAndCreateLines(rules: EngravingRules): void {
  63. throw new Error("not implemented");
  64. }
  65. public calculateBorders(rules: EngravingRules): void {
  66. throw new Error("not implemented");
  67. }
  68. public alignBeginInstructions(): void {
  69. throw new Error("not implemented");
  70. }
  71. public GetLeftBorderAbsoluteXPosition(): number {
  72. return this.StaffLines[0].PositionAndShape.AbsolutePosition.x + this.StaffLines[0].Measures[0].beginInstructionsWidth;
  73. }
  74. public GetRightBorderAbsoluteXPosition(): number {
  75. return this.StaffLines[0].PositionAndShape.AbsolutePosition.x + this.StaffLines[0].StaffLines[0].End.x;
  76. }
  77. public AddStaffMeasures(graphicalMeasures: StaffMeasure[]): void {
  78. for (let idx: number = 0, len: number = graphicalMeasures.length; idx < len; ++idx) {
  79. let graphicalMeasure: StaffMeasure = graphicalMeasures[idx];
  80. graphicalMeasure.parentMusicSystem = this;
  81. }
  82. this.graphicalMeasures.push(graphicalMeasures);
  83. }
  84. public GetSystemsFirstTimeStamp(): Fraction {
  85. return this.graphicalMeasures[0][0].parentSourceMeasure.AbsoluteTimestamp;
  86. }
  87. public GetSystemsLastTimeStamp(): Fraction {
  88. let m: SourceMeasure = this.graphicalMeasures[this.graphicalMeasures.length - 1][0].parentSourceMeasure;
  89. return m.AbsoluteTimestamp + m.Duration;
  90. }
  91. public createInstrumentBrackets(instruments: Instrument[], staffHeight: number): void {
  92. for (let idx: number = 0, len: number = instruments.length; idx < len; ++idx) {
  93. let instrument: Instrument = instruments[idx];
  94. if (instrument.Staves.length > 1) {
  95. let firstStaffLine: StaffLine = undefined, lastStaffLine: StaffLine = undefined;
  96. for (let idx2: number = 0, len2: number = this.staffLines.length; idx2 < len2; ++idx2) {
  97. let staffLine: StaffLine = this.staffLines[idx2];
  98. if (staffLine.ParentStaff === instrument.Staves[0]) {
  99. firstStaffLine = staffLine;
  100. }
  101. if (staffLine.ParentStaff === instrument.Staves[instrument.Staves.length - 1]) {
  102. lastStaffLine = staffLine;
  103. }
  104. }
  105. if (firstStaffLine !== undefined && lastStaffLine !== undefined) {
  106. let rightUpper: PointF2D = new PointF2D(
  107. firstStaffLine.PositionAndShape.RelativePosition.x,
  108. firstStaffLine.PositionAndShape.RelativePosition.y
  109. );
  110. let rightLower: PointF2D = new PointF2D(
  111. lastStaffLine.PositionAndShape.RelativePosition.x,
  112. lastStaffLine.PositionAndShape.RelativePosition.y + staffHeight
  113. );
  114. this.createInstrumentBracket(rightUpper, rightLower);
  115. }
  116. }
  117. }
  118. }
  119. public createGroupBrackets(instrumentGroups: InstrumentalGroup[], staffHeight: number, recursionDepth: number): void {
  120. for (let idx: number = 0, len: number = instrumentGroups.length; idx < len; ++idx) {
  121. let instrumentGroup: InstrumentalGroup = instrumentGroups[idx];
  122. if (instrumentGroup.InstrumentalGroups.length < 1) {
  123. continue;
  124. }
  125. let instrument1: Instrument = this.findFirstVisibleInstrumentInInstrumentalGroup(instrumentGroup);
  126. let instrument2: Instrument = this.findLastVisibleInstrumentInInstrumentalGroup(instrumentGroup);
  127. if (instrument1 === undefined || instrument2 === undefined) {
  128. continue;
  129. }
  130. let firstStaffLine: StaffLine = undefined, lastStaffLine: StaffLine = undefined;
  131. for (let idx2: number = 0, len2: number = this.staffLines.length; idx2 < len2; ++idx2) {
  132. let staffLine: StaffLine = this.staffLines[idx2];
  133. if (staffLine.ParentStaff === instrument1.Staves[0]) {
  134. firstStaffLine = staffLine;
  135. }
  136. if (staffLine.ParentStaff === instrument2.Staves.Last()) {
  137. lastStaffLine = staffLine;
  138. }
  139. }
  140. if (firstStaffLine !== undefined && lastStaffLine !== undefined) {
  141. let rightUpper: PointF2D = new PointF2D(
  142. firstStaffLine.PositionAndShape.RelativePosition.x,
  143. firstStaffLine.PositionAndShape.RelativePosition.y
  144. );
  145. let rightLower: PointF2D = new PointF2D(
  146. lastStaffLine.PositionAndShape.RelativePosition.x,
  147. lastStaffLine.PositionAndShape.RelativePosition.y + staffHeight
  148. );
  149. this.createGroupBracket(rightUpper, rightLower, staffHeight, recursionDepth);
  150. }
  151. if (instrumentGroup.InstrumentalGroups.length < 1) {
  152. continue;
  153. }
  154. this.createGroupBrackets(instrumentGroup.InstrumentalGroups, staffHeight, recursionDepth + 1);
  155. }
  156. }
  157. public createMusicSystemLabel(instrumentLabelTextHeight: number, systemLabelsRightMargin: number, labelMarginBorderFactor: number): void {
  158. if (this.parent === this.parent.Parent.MusicPages[0] && this === this.parent.MusicSystems[0]) {
  159. let instruments: Instrument[] = this.parent.Parent.ParentMusicSheet.getVisibleInstruments();
  160. for (let idx: number = 0, len: number = instruments.length; idx < len; ++idx) {
  161. let instrument: Instrument = instruments[idx];
  162. let graphicalLabel: GraphicalLabel = new GraphicalLabel(
  163. instrument.NameLabel, instrumentLabelTextHeight, TextAlignment.LeftCenter, this.boundingBox
  164. );
  165. graphicalLabel.setLabelPositionAndShapeBorders();
  166. this.labels.push(graphicalLabel, instrument);
  167. this.boundingBox.ChildElements.push(graphicalLabel.PositionAndShape);
  168. graphicalLabel.PositionAndShape.RelativePosition = new PointF2D(0.0, 0.0);
  169. }
  170. this.maxLabelLength = 0.0;
  171. let labels: GraphicalLabel[] = this.labels.Keys;
  172. for (let idx: number = 0, len: number = labels.length; idx < len; ++idx) {
  173. let label: GraphicalLabel = labels[idx];
  174. if (label.PositionAndShape.Size.width > this.maxLabelLength) {
  175. this.maxLabelLength = label.PositionAndShape.Size.width;
  176. }
  177. }
  178. this.updateMusicSystemStaffLineXPosition(systemLabelsRightMargin);
  179. }
  180. }
  181. public setMusicSystemLabelsYPosition(): void {
  182. if (this.parent === this.parent.Parent.MusicPages[0] && this === this.parent.MusicSystems[0]) {
  183. let labels: KeyValuePair<GraphicalLabel, Instrument>[] = this.labels;
  184. for (let idx: number = 0, len: number = labels.length; idx < len; ++idx) {
  185. let entry: KeyValuePair<GraphicalLabel, Instrument> = labels[idx];
  186. let ypositionSum: number = 0;
  187. let staffCounter: number = 0;
  188. for (let i: number = 0; i < this.staffLines.length; i++) {
  189. if (this.staffLines[i].ParentStaff.ParentInstrument === entry.Value) {
  190. for (let j: number = i; j < this.staffLines.length; j++) {
  191. let staffLine: StaffLine = this.staffLines[j];
  192. if (staffLine.ParentStaff.ParentInstrument !== entry.Value) {
  193. break;
  194. }
  195. ypositionSum += staffLine.PositionAndShape.RelativePosition.y;
  196. staffCounter++;
  197. }
  198. break;
  199. }
  200. }
  201. if (staffCounter > 0) {
  202. entry.Key.PositionAndShape.RelativePosition = new PointF2D(0.0, ypositionSum / staffCounter + 2.0);
  203. }
  204. }
  205. }
  206. }
  207. public checkStaffEntriesForStaffEntryLink(): boolean {
  208. let first: boolean = false;
  209. let second: boolean = false;
  210. for (let i: number = 0; i < this.staffLines.length - 1; i++) {
  211. for (let idx: number = 0, len: number = this.staffLines[i].Measures.length; idx < len; ++idx) {
  212. let measure: StaffMeasure = this.staffLines[i].Measures[idx];
  213. for (let idx2: number = 0, len2: number = measure.staffEntries.length; idx2 < len2; ++idx2) {
  214. let staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  215. if (staffEntry.sourceStaffEntry.Link !== undefined) {
  216. first = true;
  217. }
  218. }
  219. }
  220. for (let idx: number = 0, len: number = this.staffLines[i + 1].Measures.length; idx < len; ++idx) {
  221. let measure: StaffMeasure = this.staffLines[i + 1].Measures[idx];
  222. for (let idx2: number = 0, len2: number = measure.staffEntries.length; idx2 < len2; ++idx2) {
  223. let staffEntry: GraphicalStaffEntry = measure.staffEntries[idx2];
  224. if (staffEntry.sourceStaffEntry.Link !== undefined) {
  225. second = true;
  226. }
  227. }
  228. }
  229. }
  230. if (first && second) {
  231. return true;
  232. }
  233. return false;
  234. }
  235. protected calcInstrumentsBracketsWidth(): number {
  236. throw new Error("not implemented");
  237. }
  238. protected createInstrumentBracket(rightUpper: PointF2D, rightLower: PointF2D): void {
  239. throw new Error("not implemented");
  240. }
  241. protected createGroupBracket(rightUpper: PointF2D, rightLower: PointF2D, staffHeight: number, recursionDepth: number): void {
  242. throw new Error("not implemented");
  243. }
  244. private findFirstVisibleInstrumentInInstrumentalGroup(instrumentalGroup: InstrumentalGroup): Instrument {
  245. for (let idx: number = 0, len: number = instrumentalGroup.InstrumentalGroups.length; idx < len; ++idx) {
  246. let groupOrInstrument: InstrumentalGroup = instrumentalGroup.InstrumentalGroups[idx];
  247. if (groupOrInstrument instanceof Instrument) {
  248. if ((<Instrument>groupOrInstrument).Visible === true) {
  249. return <Instrument>groupOrInstrument;
  250. }
  251. continue;
  252. }
  253. return this.findFirstVisibleInstrumentInInstrumentalGroup(groupOrInstrument);
  254. }
  255. return undefined;
  256. }
  257. private findLastVisibleInstrumentInInstrumentalGroup(instrumentalGroup: InstrumentalGroup): Instrument {
  258. let groupOrInstrument: InstrumentalGroup;
  259. for (let i: number = instrumentalGroup.InstrumentalGroups.length - 1; i >= 0; i--) {
  260. groupOrInstrument = instrumentalGroup.InstrumentalGroups[i];
  261. if (groupOrInstrument instanceof Instrument) {
  262. if ((<Instrument>groupOrInstrument).Visible === true) {
  263. return <Instrument>groupOrInstrument;
  264. }
  265. continue;
  266. }
  267. return this.findLastVisibleInstrumentInInstrumentalGroup(groupOrInstrument);
  268. }
  269. return undefined;
  270. }
  271. private updateMusicSystemStaffLineXPosition(systemLabelsRightMargin: number): void {
  272. for (let idx: number = 0, len: number = this.StaffLines.length; idx < len; ++idx) {
  273. let staffLine: StaffLine = this.StaffLines[idx];
  274. let relative: PointF2D = staffLine.PositionAndShape.RelativePosition;
  275. relative.x = this.maxLabelLength + systemLabelsRightMargin;
  276. staffLine.PositionAndShape.RelativePosition = relative;
  277. staffLine.PositionAndShape.BorderRight = this.boundingBox.Size.width - this.maxLabelLength - systemLabelsRightMargin;
  278. for (let i: number = 0; i < staffLine.StaffLines.length; i++) {
  279. let lineEnd: PointF2D = new PointF2D(staffLine.PositionAndShape.Size.width, staffLine.StaffLines[i].End.y);
  280. staffLine.StaffLines[i].End = lineEnd;
  281. }
  282. }
  283. }
  284. }