Browse Source

fix cursor broken after pageformat change, improve cursor current page calculation (#817)

slight fix and improvement to the solution in #817
sschmid 4 years ago
parent
commit
ff3563e8dc

+ 9 - 14
src/OpenSheetMusicDisplay/Cursor.ts

@@ -11,7 +11,6 @@ import {Fraction} from "../Common/DataObjects/Fraction";
 import { EngravingRules } from "../MusicalScore/Graphical/EngravingRules";
 import { EngravingRules } from "../MusicalScore/Graphical/EngravingRules";
 import { SourceMeasure } from "../MusicalScore/VoiceData/SourceMeasure";
 import { SourceMeasure } from "../MusicalScore/VoiceData/SourceMeasure";
 import { StaffLine } from "../MusicalScore/Graphical/StaffLine";
 import { StaffLine } from "../MusicalScore/Graphical/StaffLine";
-import { GraphicalMeasure } from "../MusicalScore/Graphical/GraphicalMeasure";
 
 
 /**
 /**
  * A cursor which can iterate through the music sheet.
  * A cursor which can iterate through the music sheet.
@@ -54,13 +53,11 @@ export class Cursor {
   private graphic: GraphicalMusicSheet;
   private graphic: GraphicalMusicSheet;
   public hidden: boolean = true;
   public hidden: boolean = true;
   public currentPageNumber: number = 1;
   public currentPageNumber: number = 1;
-  public pageStartingGraphicalMeasures: GraphicalMeasure[] = [];
 
 
   /** Initialize the cursor. Necessary before using functions like show() and next(). */
   /** Initialize the cursor. Necessary before using functions like show() and next(). */
   public init(manager: MusicPartManager, graphic: GraphicalMusicSheet): void {
   public init(manager: MusicPartManager, graphic: GraphicalMusicSheet): void {
     this.manager = manager;
     this.manager = manager;
     this.graphic = graphic;
     this.graphic = graphic;
-    this.indexPages();
     this.reset();
     this.reset();
     this.hidden = true;
     this.hidden = true;
     this.hide();
     this.hide();
@@ -204,14 +201,7 @@ export class Cursor {
    */
    */
   public next(): void {
   public next(): void {
     this.iterator.moveToNext();
     this.iterator.moveToNext();
-    for (let i: number = 0; i < this.pageStartingGraphicalMeasures.length; i++) {
-      const gMeasure: GraphicalMeasure = this.pageStartingGraphicalMeasures[i];
-      if (this.iterator.CurrentMeasure === gMeasure.parentSourceMeasure) {
-        this.currentPageNumber = i + 1;
-        // this.openSheetMusicDisplay.enableOrDisableCursor(true, true);
-        // break;
-      }
-    }
+    this.updateCurrentPage();
     this.update();
     this.update();
   }
   }
 
 
@@ -220,6 +210,7 @@ export class Cursor {
    */
    */
   public reset(): void {
   public reset(): void {
     this.resetIterator();
     this.resetIterator();
+    this.updateCurrentPage();
     //this.iterator.moveToNext();
     //this.iterator.moveToNext();
     this.update();
     this.update();
   }
   }
@@ -266,10 +257,14 @@ export class Cursor {
     return notes;
     return notes;
   }
   }
 
 
-  private indexPages(): void {
-    this.pageStartingGraphicalMeasures = [];
+  public updateCurrentPage(): number {
+    const timestamp: Fraction = this.iterator.currentTimeStamp;
     for (const page of this.graphic.MusicPages) {
     for (const page of this.graphic.MusicPages) {
-      this.pageStartingGraphicalMeasures.push(page.MusicSystems[0].GraphicalMeasures[0][0]);
+      const lastSystemTimestamp: Fraction = page.MusicSystems.last().GetSystemsLastTimeStamp();
+      if (lastSystemTimestamp.gt(timestamp)) {
+        return this.currentPageNumber = page.PageNumber;
+      }
     }
     }
+    return 1;
   }
   }
 }
 }

+ 9 - 5
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -704,12 +704,16 @@ export class OpenSheetMusicDisplay {
             // save previous cursor state
             // save previous cursor state
             const hidden: boolean = this.cursor?.Hidden;
             const hidden: boolean = this.cursor?.Hidden;
             const previousIterator: MusicPartManagerIterator = this.cursor?.Iterator;
             const previousIterator: MusicPartManagerIterator = this.cursor?.Iterator;
-            const previousPageNumber: number = this.cursor?.currentPageNumber ?? 1;
-
             this.cursor?.hide();
             this.cursor?.hide();
+
+            let backendToDrawOn: VexFlowBackend = this.drawer?.Backends[0];
+            if (backendToDrawOn && this.rules.RestoreCursorAfterRerender && this.cursor) {
+                const newPageNumber: number = this.cursor.updateCurrentPage();
+                backendToDrawOn = this.drawer.Backends[newPageNumber - 1];
+            }
             // create new cursor
             // create new cursor
-            if (this.drawer?.Backends?.length >= 1 && this.drawer.Backends[0].getRenderElement()) {
-                this.cursor = new Cursor(this.drawer.Backends[previousPageNumber - 1].getRenderElement(), this);
+            if (backendToDrawOn && backendToDrawOn.getRenderElement()) {
+                this.cursor = new Cursor(backendToDrawOn.getRenderElement(), this);
             }
             }
             if (this.sheet && this.graphic && this.cursor) { // else init is called in load()
             if (this.sheet && this.graphic && this.cursor) { // else init is called in load()
                 this.cursor.init(this.sheet.MusicPartManager, this.graphic);
                 this.cursor.init(this.sheet.MusicPartManager, this.graphic);
@@ -720,7 +724,7 @@ export class OpenSheetMusicDisplay {
                 this.cursor.hidden = hidden;
                 this.cursor.hidden = hidden;
                 if (previousIterator) {
                 if (previousIterator) {
                     this.cursor.iterator = previousIterator;
                     this.cursor.iterator = previousIterator;
-                    this.cursor.currentPageNumber = previousPageNumber;
+                    this.cursor.updateCurrentPage();
                 }
                 }
                 if (switchedPage && !hidden) {
                 if (switchedPage && !hidden) {
                     this.cursor.show();
                     this.cursor.show();