Merge branch 'develop' into feature/JAL-2611
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsShowHidden.java
index 62e8000..5bd4bba 100644 (file)
@@ -32,6 +32,12 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
 {
   private ViewportRanges ranges;
 
+  private int xdiff; // when dragging, difference in alignment units between
+                     // start residue and original mouse click position
+
+  private int ydiff; // when dragging, difference in alignment units between
+                     // start sequence and original mouse click position
+
   /**
    * Create an OverviewDimensions object
    * 
@@ -66,46 +72,66 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void updateViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
-    int x = mousex;
-    int y = mousey;
+    // convert mousex and mousey to alignment units as well as
+    // translating to top left corner of viewport - this is an absolute position
+    int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
+    int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
 
-    resetAlignmentDims();
+    // convert to visible positions
+    int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
+    yAsSeq = hiddenSeqs.adjustForHiddenSeqs(
+            hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq));
+    yAsSeq = Math.max(yAsSeq, 0); // -1 if before first visible sequence
+    int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
+    visYAsSeq = Math.max(visYAsSeq, 0); // -1 if before first visible sequence
 
-    if (x < 0)
-    {
-      x = 0;
-    }
+    // update viewport accordingly
+    updateViewportFromTopLeft(visXAsRes, visYAsSeq, hiddenSeqs, hiddenCols);
+  }
+
+  @Override
+  public void adjustViewportFromMouse(int mousex, int mousey,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    // calculate translation in pixel terms:
+    // get mouse location in viewport coords, add translation in viewport
+    // coords,
+    // convert back to pixel coords
+    int vpx = Math.round((float) mousex * alwidth / width);
+    int visXAsRes = hiddenCols.findColumnPosition(vpx) + xdiff;
+
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy) + ydiff;
+
+    // update viewport accordingly
+    updateViewportFromTopLeft(visXAsRes, visYAsRes,
+            hiddenSeqs,
+            hiddenCols);
+  }
+
+  @Override
+  protected void updateViewportFromTopLeft(int leftx, int topy,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
+    int visXAsRes = leftx;
+    int visYAsSeq = topy;
+    resetAlignmentDims();
 
-    if (y < 0)
+    if (visXAsRes < 0)
     {
-      y = 0;
+      visXAsRes = 0;
     }
 
-    if (ranges.isWrappedMode())
+    if (visYAsSeq < 0)
     {
-      y = 0; // sorry, no vertical scroll when wrapped
+      visYAsSeq = 0;
     }
 
-    //
-    // Convert x value to residue position
-    //
-
-    // need to determine where scrollCol should be, given x
-    // to do this also need to know width of viewport, and some hidden column
-    // correction
-
-    // convert x to residues - this is an absolute position
-    int xAsRes = Math.round((float) x * alwidth / width);
+    // Determine where scrollCol should be, given visXAsRes
 
     // get viewport width in residues
     int vpwidth = ranges.getViewportWidth();
 
-    // get where x should be when accounting for hidden cols
-    // if x is in a hidden col region, shift to left - but we still need
-    // absolute position
-    // so convert back after getting visible region position
-    int visXAsRes = hiddenCols.findColumnPosition(xAsRes);
-
     // check in case we went off the edge of the alignment
     int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
     if (visXAsRes + vpwidth - 1 > visAlignWidth)
@@ -124,28 +150,14 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
       }
     }
 
-    //
-    // Convert y value to sequence position
-    //
-
-    // convert y to residues
-    int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
+    // Determine where scrollRow should be, given visYAsSeq
 
     // get viewport height in sequences
     int vpheight = ranges.getViewportHeight();
 
-    // get where y should be when accounting for hidden rows
-    // if y is in a hidden row region, shift up - but we still need absolute
-    // position,
-    // so convert back after getting visible region position
-    yAsSeq = hiddenSeqs.adjustForHiddenSeqs(hiddenSeqs
-            .findIndexWithoutHiddenSeqs(yAsSeq));
-    yAsSeq = Math.max(yAsSeq, 0); // -1 if before first visible sequence
-
     // check in case we went off the edge of the alignment
     int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
-    int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
-    visYAsSeq = Math.max(visYAsSeq, 0); // -1 if before first visible sequence
+
     if (visYAsSeq + vpheight - 1 > visAlignHeight)
     {
       // went past the end of the alignment, adjust backwards
@@ -213,4 +225,34 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     alwidth = ranges.getAbsoluteAlignmentWidth();
     alheight = ranges.getAbsoluteAlignmentHeight();
   }
+
+  @Override
+  protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
+  {
+    int vpx = Math.round((float) mousex * alwidth / width);
+    return hidden.subtractVisibleColumns(ranges.getViewportWidth() / 2,
+            vpx);
+  }
+
+  @Override
+  protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
+  {
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    return hidden.subtractVisibleRows(ranges.getViewportHeight() / 2, vpy);
+  }
+
+  @Override
+  public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
+          HiddenColumns hiddenCols)
+  {
+    // get alignment position of x and box (can get directly from vpranges) and
+    // calculate difference between the positions
+    int vpx = Math.round((float) x * alwidth / width);
+    int vpy = Math.round((float) y * alheight / sequencesHeight);
+
+    xdiff = ranges.getStartRes() - hiddenCols.findColumnPosition(vpx);
+    ydiff = ranges.getStartSeq()
+            - hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);
+  }
+
 }