RepetitionInstruction.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import {Repetition} from "../../MusicSource/Repetition";
  2. export class RepetitionInstructionComparer /*implements IComparer<RepetitionInstruction>*/ {
  3. public static Compare(x: RepetitionInstruction, y: RepetitionInstruction): number {
  4. if (x.parentRepetition !== undefined && y.parentRepetition !== undefined) {
  5. if (x.alignment === AlignmentType.End && y.alignment === AlignmentType.End) {
  6. if (x.parentRepetition.StartIndex < y.parentRepetition.StartIndex) {
  7. return 1;
  8. }
  9. if (x.parentRepetition.StartIndex > y.parentRepetition.StartIndex) {
  10. return -1;
  11. }
  12. }
  13. if (x.alignment === AlignmentType.Begin && y.alignment === AlignmentType.Begin) {
  14. if (x.parentRepetition.EndIndex < y.parentRepetition.EndIndex) {
  15. return 1;
  16. }
  17. if (x.parentRepetition.EndIndex > y.parentRepetition.EndIndex) {
  18. return -1;
  19. }
  20. }
  21. }
  22. return 0;
  23. }
  24. }
  25. export class RepetitionInstruction /*implements IComparable*/ {
  26. /* FIXME: Check constructor calling from other classes
  27. constructor(measureIndex: number, type: RepetitionInstructionEnum) {
  28. this(measureIndex, [], type, AlignmentType.End, undefined);
  29. if (type === RepetitionInstructionEnum.StartLine || type === RepetitionInstructionEnum.Segno || type === RepetitionInstructionEnum.Coda) {
  30. this.alignment = AlignmentType.Begin;
  31. }
  32. }
  33. constructor(measureIndex: number, type: RepetitionInstructionEnum, alignment: AlignmentType, parentRepetition: Repetition) {
  34. this(measureIndex, [], type, alignment, parentRepetition);
  35. }
  36. constructor(measureIndex: number, endingIndex: number, type: RepetitionInstructionEnum, alignment: AlignmentType, parentRepetition: Repetition) {
  37. this(measureIndex, [endingIndex], type, alignment, parentRepetition);
  38. }
  39. */
  40. constructor(measureIndex: number, type: RepetitionInstructionEnum, alignment: AlignmentType = AlignmentType.End,
  41. parentRepetition: Repetition = undefined, endingIndices: number[] = undefined) {
  42. this.measureIndex = measureIndex;
  43. this.endingIndices = endingIndices.slice();
  44. this.type = type;
  45. this.alignment = alignment;
  46. this.parentRepetition = parentRepetition;
  47. }
  48. public measureIndex: number;
  49. public endingIndices: number[];
  50. public type: RepetitionInstructionEnum;
  51. public alignment: AlignmentType;
  52. public parentRepetition: Repetition;
  53. public CompareTo(obj: Object): number {
  54. const other: RepetitionInstruction = <RepetitionInstruction>obj;
  55. if (this.measureIndex > other.measureIndex) {
  56. return 1;
  57. } else if (one.measureIndex < other.measureIndex) {
  58. return -1;
  59. }
  60. if (one.alignment === AlignmentType.Begin) {
  61. if (other.alignment === AlignmentType.End) {
  62. return -1;
  63. }
  64. switch (one.type) {
  65. case RepetitionInstructionEnum.Ending:
  66. return 1;
  67. case RepetitionInstructionEnum.StartLine:
  68. if (other.type === RepetitionInstructionEnum.Ending) {
  69. return -1;
  70. }
  71. return 1;
  72. case RepetitionInstructionEnum.Coda:
  73. case RepetitionInstructionEnum.Segno:
  74. if (other.type === RepetitionInstructionEnum.Coda) {
  75. return 1;
  76. }
  77. return -1;
  78. default:
  79. }
  80. } else {
  81. if (other.alignment === AlignmentType.Begin) {
  82. return 1;
  83. }
  84. switch (one.type) {
  85. case RepetitionInstructionEnum.Ending:
  86. return -1;
  87. case RepetitionInstructionEnum.Fine:
  88. case RepetitionInstructionEnum.ToCoda:
  89. if (other.type === RepetitionInstructionEnum.Ending) {
  90. return 1;
  91. }
  92. return -1;
  93. case RepetitionInstructionEnum.ForwardJump:
  94. switch (other.type) {
  95. case RepetitionInstructionEnum.Ending:
  96. case RepetitionInstructionEnum.Fine:
  97. case RepetitionInstructionEnum.ToCoda:
  98. return 1;
  99. default:
  100. }
  101. return -1;
  102. case RepetitionInstructionEnum.DalSegnoAlFine:
  103. case RepetitionInstructionEnum.DaCapoAlFine:
  104. case RepetitionInstructionEnum.DalSegnoAlCoda:
  105. case RepetitionInstructionEnum.DaCapoAlCoda:
  106. case RepetitionInstructionEnum.DaCapo:
  107. case RepetitionInstructionEnum.DalSegno:
  108. case RepetitionInstructionEnum.BackJumpLine:
  109. return 1;
  110. default:
  111. }
  112. }
  113. return 0;
  114. }
  115. public equals(other: RepetitionInstruction): boolean {
  116. if (
  117. this.measureIndex !== other.measureIndex
  118. || this.type !== other.type
  119. || this.alignment !== other.alignment
  120. || this.endingIndices.length !== other.endingIndices.length
  121. ) {
  122. return false;
  123. }
  124. for (let i: number = 0; i < this.endingIndices.length; i++) {
  125. if (this.endingIndices[i] !== other.endingIndices[i]) {
  126. return false;
  127. }
  128. }
  129. return true;
  130. }
  131. }
  132. export enum RepetitionInstructionEnum {
  133. StartLine,
  134. ForwardJump,
  135. BackJumpLine,
  136. Ending,
  137. DaCapo,
  138. DalSegno,
  139. Fine,
  140. ToCoda,
  141. DalSegnoAlFine,
  142. DaCapoAlFine,
  143. DalSegnoAlCoda,
  144. DaCapoAlCoda,
  145. Coda,
  146. Segno,
  147. None,
  148. }
  149. export enum AlignmentType {
  150. Begin,
  151. End,
  152. }