Przeglądaj źródła

feat(Cursor): restore cursor state after resize and re-render (#734) if option set (default true)

new osmd.rules.RestoreCursorAfterRerender setting, no OSMDOption for it yet

fix #734
sschmid 5 lat temu
rodzic
commit
a08e9578c2

+ 8 - 0
src/MusicalScore/Graphical/EngravingRules.ts

@@ -218,6 +218,7 @@ export class EngravingRules {
     private pageFormat: PageFormat;
     private pageBackgroundColor: string; // vexflow-color-string (#FFFFFF). Default undefined/transparent.
     private renderSingleHorizontalStaffline: boolean;
+    private restoreCursorAfterRerender: boolean;
 
     private static fixStafflineBoundingBox: boolean; // TODO temporary workaround
 
@@ -445,6 +446,7 @@ export class EngravingRules {
         this.fingeringInsideStafflines = false;
         this.newSystemAtXMLNewSystemAttribute = false;
         this.newPageAtXMLNewPageAttribute = false;
+        this.restoreCursorAfterRerender = true;
 
         EngravingRules.FixStafflineBoundingBox = false; // TODO temporary workaround
 
@@ -1595,6 +1597,12 @@ export class EngravingRules {
     public set RenderSingleHorizontalStaffline(value: boolean) {
         this.renderSingleHorizontalStaffline = value;
     }
+    public get RestoreCursorAfterRerender(): boolean {
+        return this.restoreCursorAfterRerender;
+    }
+    public set RestoreCursorAfterRerender(value: boolean) {
+        this.restoreCursorAfterRerender = value;
+    }
 
     /**
      * This method maps NoteDurations to Distances and DistancesScalingFactors.

+ 5 - 5
src/OpenSheetMusicDisplay/Cursor.ts

@@ -29,16 +29,16 @@ export class Cursor {
   private openSheetMusicDisplay: OpenSheetMusicDisplay;
   private rules: EngravingRules;
   private manager: MusicPartManager;
-  protected iterator: MusicPartManagerIterator;
+  public iterator: MusicPartManagerIterator;
   private graphic: GraphicalMusicSheet;
-  private hidden: boolean = true;
+  public hidden: boolean = true;
   private cursorElement: HTMLImageElement;
 
   /** Initialize the cursor. Necessary before using functions like show() and next(). */
   public init(manager: MusicPartManager, graphic: GraphicalMusicSheet): void {
     this.manager = manager;
-    this.reset();
     this.graphic = graphic;
+    this.reset();
     this.hidden = true;
     this.hide();
   }
@@ -84,10 +84,10 @@ export class Cursor {
 
   public update(): void {
     // Warning! This should NEVER call this.openSheetMusicDisplay.render()
-    if (this.hidden) {
+    if (this.hidden || this.hidden === undefined) {
       return;
     }
-    this.graphic.Cursors.length = 0;
+    // this.graphic?.Cursors?.length = 0;
     const iterator: MusicPartManagerIterator = this.iterator;
     // TODO when measure draw range (drawUpToMeasureNumber) was changed, next/update can fail to move cursor. but of course it can be reset before.
 

+ 14 - 0
src/OpenSheetMusicDisplay/OpenSheetMusicDisplay.ts

@@ -21,6 +21,7 @@ import { NoteEnum } from "..";
 import { AutoColorSet, GraphicalMusicPage } from "../MusicalScore";
 import jspdf = require("jspdf-yworks/dist/jspdf.min");
 import svg2pdf = require("svg2pdf.js/dist/svg2pdf.min");
+import { MusicPartManagerIterator } from "../MusicalScore/MusicParts";
 
 /**
  * The main class and control point of OpenSheetMusicDisplay.<br>
@@ -675,10 +676,23 @@ export class OpenSheetMusicDisplay {
     public enableOrDisableCursor(enable: boolean): void {
         this.drawingParameters.drawCursors = enable;
         if (enable) {
+            // save previous cursor state
+            const hidden: boolean = this.cursor?.Hidden;
+            const previousIterator: MusicPartManagerIterator = this.cursor?.Iterator;
+
+            // create new cursor
             this.cursor = new Cursor(this.drawer.Backends[0].getInnerElement(), this);
             if (this.sheet && this.graphic) { // else init is called in load()
                 this.cursor.init(this.sheet.MusicPartManager, this.graphic);
             }
+
+            // restore old cursor state
+            if (this.rules.RestoreCursorAfterRerender) {
+                this.cursor.hidden = hidden;
+                if (previousIterator) {
+                    this.cursor.iterator = previousIterator;
+                }
+            }
         } else { // disable cursor
             if (!this.cursor) {
                 return;