JAL-2611 Still not happy with behaviour
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsShowHidden.java
index 3506332..4ab27de 100644 (file)
@@ -66,19 +66,141 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void updateViewportFromMouse(int mousex, int mousey,
           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
   {
-    int x = mousex;
-    int y = mousey;
+    int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
+    int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
+
+    updateViewportFromTopLeft(xAsRes, yAsSeq, 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);
+
+    int vpy = Math.round((float) mousey * alheight / sequencesHeight);
+    // int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);
+
+    System.out.println("vpx: " + vpx);
+    // System.out.println("VisXAsRes: " + visXAsRes);
+    System.out.println("transX: " + transX);
+    // updateViewportFromTopLeft(vpx + transX, vpy + transY,
+    // hiddenSeqs,
+    // hiddenCols);
+
+    int xAsRes = vpx + transX;
+    int yAsSeq = vpy + transY;
+
+    resetAlignmentDims();
+
+    if (xAsRes < 0)
+    {
+      xAsRes = 0;
+    }
+
+    if (yAsSeq < 0)
+    {
+      yAsSeq = 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);
+
+    // 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);
+    int visXAsRes = xAsRes;
+
+    // check in case we went off the edge of the alignment
+    int visAlignWidth = hiddenCols.findColumnPosition(alwidth - 1);
+    /*    if (visXAsRes + vpwidth - 1 > visAlignWidth)
+    {
+      // went past the end of the alignment, adjust backwards
+    
+      // if last position was before the end of the alignment, need to update
+      if (ranges.getEndRes() < visAlignWidth)
+      {
+        visXAsRes = hiddenCols.findColumnPosition(hiddenCols
+                .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
+      }
+      else
+      {
+        visXAsRes = ranges.getStartRes();
+      }
+    }*/
+
+    //
+    // Convert y value to sequence position
+    //
+
+    // convert y to residues
+    // int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
+
+    // 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));
+
+    // check in case we went off the edge of the alignment
+    int visAlignHeight = hiddenSeqs.findIndexWithoutHiddenSeqs(alheight);
+    int visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(yAsSeq);
+    if (visYAsSeq + vpheight - 1 > visAlignHeight)
+    {
+      // went past the end of the alignment, adjust backwards
+      if (ranges.getEndSeq() < visAlignHeight)
+      {
+        visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(
+                hiddenSeqs.subtractVisibleRows(vpheight - 1, alheight - 1));
+      }
+      else
+      {
+        visYAsSeq = ranges.getStartSeq();
+      }
+    }
+
+    // update viewport
+    ranges.setStartRes(visXAsRes);
+    ranges.setStartSeq(visYAsSeq);
+  }
+
+  @Override
+  protected void updateViewportFromTopLeft(int xAsRes, int yAsSeq,
+          HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
+  {
 
     resetAlignmentDims();
 
-    if (x < 0)
+    if (xAsRes < 0)
     {
-      x = 0;
+      xAsRes = 0;
     }
 
-    if (y < 0)
+    if (yAsSeq < 0)
     {
-      y = 0;
+      yAsSeq = 0;
     }
 
     //
@@ -90,7 +212,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     // correction
 
     // convert x to residues - this is an absolute position
-    int xAsRes = Math.round((float) x * alwidth / width);
+    // int xAsRes = Math.round((float) x * alwidth / width);
 
     // get viewport width in residues
     int vpwidth = ranges.getViewportWidth();
@@ -108,7 +230,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
       // went past the end of the alignment, adjust backwards
 
       // if last position was before the end of the alignment, need to update
-      if ((ranges.getEndRes() + vpwidth - 1) < visAlignWidth)
+      if (ranges.getEndRes() < visAlignWidth)
       {
         visXAsRes = hiddenCols.findColumnPosition(hiddenCols
                 .subtractVisibleColumns(vpwidth - 1, alwidth - 1));
@@ -124,7 +246,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     //
 
     // convert y to residues
-    int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
+    // int yAsSeq = Math.round((float) y * alheight / sequencesHeight);
 
     // get viewport height in sequences
     int vpheight = ranges.getViewportHeight();
@@ -142,7 +264,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
     if (visYAsSeq + vpheight - 1 > visAlignHeight)
     {
       // went past the end of the alignment, adjust backwards
-      if ((ranges.getEndSeq() + vpheight - 1) < visAlignHeight)
+      if (ranges.getEndSeq() < visAlignHeight)
       {
         visYAsSeq = hiddenSeqs.findIndexWithoutHiddenSeqs(hiddenSeqs
                 .subtractVisibleRows(vpheight - 1, alheight - 1));
@@ -174,6 +296,7 @@ public class OverviewDimensionsShowHidden extends OverviewDimensions
   public void setBoxPosition(HiddenSequences hiddenSeqs,
           HiddenColumns hiddenCols)
   {
+
     // work with absolute values of startRes and endRes
     int startRes = hiddenCols.adjustForHiddenColumns(ranges.getStartRes());
     int endRes = hiddenCols.adjustForHiddenColumns(ranges.getEndRes());
@@ -207,4 +330,37 @@ 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 calc difference
+      int vpx = Math.round((float) x * alwidth / width);
+      int visXAsRes = hiddenCols.findColumnPosition(vpx);
+      
+      int vpy = Math.round((float) y * alheight / sequencesHeight);
+      int visYAsRes = hiddenSeqs.findIndexWithoutHiddenSeqs(vpy);
+
+      transX = ranges.getStartRes() - visXAsRes;
+      transY = ranges.getStartSeq() - visYAsRes;
+    }
+  }
+
 }