Selaa lähdekoodia

Add ability to retreive notes and voices under cursor

Jerko Steiner 6 vuotta sitten
vanhempi
commit
a482309246
2 muutettua tiedostoa jossa 72 lisäystä ja 0 poistoa
  1. 19 0
      src/OpenSheetMusicDisplay/Cursor.ts
  2. 53 0
      test/Common/OSMD/OSMD_Test.ts

+ 19 - 0
src/OpenSheetMusicDisplay/Cursor.ts

@@ -5,6 +5,8 @@ import {VexFlowStaffEntry} from "../MusicalScore/Graphical/VexFlow/VexFlowStaffE
 import {MusicSystem} from "../MusicalScore/Graphical/MusicSystem";
 import {OpenSheetMusicDisplay} from "./OpenSheetMusicDisplay";
 import {GraphicalMusicSheet} from "../MusicalScore/Graphical/GraphicalMusicSheet";
+import {Instrument} from "../MusicalScore/Instrument";
+import {Note} from "../MusicalScore/VoiceData/Note";
 
 /**
  * A cursor which can iterate through the music sheet.
@@ -167,4 +169,21 @@ export class Cursor {
   public get Hidden(): boolean {
     return this.hidden;
   }
+
+  public get AllVoicesUnderCursor(): VoiceEntry[] {
+    return this.VoicesUnderCursor();
+  }
+
+  public VoicesUnderCursor(instrument?: Instrument): VoiceEntry[] {
+    return this.iterator.CurrentVisibleVoiceEntries(instrument);
+  }
+
+  public NotesUnderCursor(instrument?: Instrument): Note[] {
+    const voiceEntries: VoiceEntry[]  = this.VoicesUnderCursor(instrument);
+    const notes: Note[] = [];
+    voiceEntries.forEach(voiceEntry => {
+      notes.push.apply(notes, voiceEntry.Notes);
+    });
+    return notes;
+  }
 }

+ 53 - 0
test/Common/OSMD/OSMD_Test.ts

@@ -1,5 +1,8 @@
 import chai = require("chai");
+import {Instrument} from "../../../src/MusicalScore/Instrument";
+import {Note} from "../../../src/MusicalScore/VoiceData/Note";
 import {OpenSheetMusicDisplay} from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
+import {VoiceEntry} from "../../../src/MusicalScore/VoiceData/VoiceEntry";
 import {TestUtils} from "../../Util/TestUtils";
 
 describe("OpenSheetMusicDisplay Main Export", () => {
@@ -192,4 +195,54 @@ describe("OpenSheetMusicDisplay Main Export", () => {
             done
         ).catch(done);
     });
+
+    describe("cursor", () => {
+        let opensheetmusicdisplay: OpenSheetMusicDisplay;
+        beforeEach((done: MochaDone) => {
+            const div: HTMLElement = container1;
+            opensheetmusicdisplay = TestUtils.createOpenSheetMusicDisplay(div);
+            const score: Document = TestUtils.getScore("MuzioClementi_SonatinaOpus36No1_Part1.xml");
+            opensheetmusicdisplay.load(score).then(
+                (_: {}) => {
+                    opensheetmusicdisplay.render();
+                    opensheetmusicdisplay.cursor.show();
+                    done();
+                },
+                done
+            ).catch(done);
+        });
+
+        describe("get AllVoicesUnderCursor", () => {
+            it("retreives all voices under cursor", () => {
+                const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.AllVoicesUnderCursor;
+                chai.expect(voiceEntries.length).to.equal(2);
+            });
+        });
+
+        describe("VoicesUnderCursor", () => {
+            it("retreives voices for a specific instrument under cursor", () => {
+                const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
+                chai.expect(voiceEntries.length).to.equal(2);
+            });
+            it("retreives all voices under cursor when instrument not specified", () => {
+                const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[1];
+                const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor(instrument);
+                chai.expect(voiceEntries.length).to.equal(1);
+            });
+        });
+
+        describe("NotesUnderCursor", () => {
+            it("gets notes for a specific instrument under cursor", () => {
+                const instrument: Instrument = opensheetmusicdisplay.Sheet.Instruments[0];
+                const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor(instrument);
+                chai.expect(notes.length).to.equal(1);
+            });
+
+            it("gets all notes under cursor when instrument unspecified", () => {
+                const notes: Note[] = opensheetmusicdisplay.cursor.NotesUnderCursor();
+                chai.expect(notes.length).to.equal(2);
+            });
+        });
+    });
+
 });