Parcourir la source

Merge branch 'jeremija/get-notes-under-cursor' of https://github.com/jeremija/opensheetmusicdisplay into jeremija-jeremija/get-notes-under-cursor

# Conflicts:
#	test/Common/OSMD/OSMD_Test.ts

helping #538
sschmidTU il y a 6 ans
Parent
commit
dce75c7db9
2 fichiers modifiés avec 66 ajouts et 0 suppressions
  1. 16 0
      src/OpenSheetMusicDisplay/Cursor.ts
  2. 50 0
      test/Common/OSMD/OSMD_Test.ts

+ 16 - 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.
@@ -168,4 +170,18 @@ export class Cursor {
   public get Hidden(): boolean {
     return this.hidden;
   }
+
+  /** returns voices under the current Cursor position. Without instrument argument, all voices are returned. */
+  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;
+  }
 }

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

@@ -1,6 +1,7 @@
 import chai = require("chai");
 import { OpenSheetMusicDisplay } from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
 import { TestUtils } from "../../Util/TestUtils";
+import { VoiceEntry, Instrument, Note } from "../../../src";
 
 describe("OpenSheetMusicDisplay Main Export", () => {
     let container1: HTMLElement;
@@ -229,4 +230,53 @@ describe("OpenSheetMusicDisplay Main Export", () => {
             }
         });
     });
+    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("retrieves all voices under cursor", () => {
+                const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
+                chai.expect(voiceEntries.length).to.equal(2);
+            });
+        });
+
+        describe("VoicesUnderCursor", () => {
+            it("retrieves voices for a specific instrument under cursor", () => {
+                const voiceEntries: VoiceEntry[] = opensheetmusicdisplay.cursor.VoicesUnderCursor();
+                chai.expect(voiceEntries.length).to.equal(2);
+            });
+            it("retrieves 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);
+            });
+        });
+    });
+
 });