JAL-2611 Better
[jalview.git] / src / jalview / viewmodel / OverviewDimensionsHideHidden.java
1 package jalview.viewmodel;
2
3 import jalview.api.AlignmentColsCollectionI;
4 import jalview.api.AlignmentRowsCollectionI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.HiddenColumns;
7 import jalview.datamodel.HiddenSequences;
8 import jalview.datamodel.VisibleColsCollection;
9 import jalview.datamodel.VisibleRowsCollection;
10
11 public class OverviewDimensionsHideHidden extends OverviewDimensions
12 {
13   private ViewportRanges ranges;
14
15   public OverviewDimensionsHideHidden(ViewportRanges vpranges,
16           boolean showAnnotationPanel)
17   {
18     super(vpranges, showAnnotationPanel);
19     ranges = vpranges;
20     resetAlignmentDims();
21   }
22
23   @Override
24   public void updateViewportFromMouse(int mousex, int mousey,
25           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
26   {
27     int xAsRes = getLeftXFromCentreX(mousex, hiddenCols);
28     int yAsSeq = getTopYFromCentreY(mousey, hiddenSeqs);
29
30     updateViewportFromTopLeft(xAsRes, yAsSeq, hiddenSeqs, hiddenCols);
31
32   }
33
34   @Override
35   public void adjustViewportFromMouse(int mousex, int mousey,
36           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
37   {
38     // calculate translation in pixel terms:
39     // get mouse location in viewport coords, add translation in viewport
40     // coords, and update viewport as usual
41     int vpx = Math.round((float) mousex * alwidth / width);
42     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
43
44     updateViewportFromTopLeft(vpx + fixedX, vpy + fixedY, hiddenSeqs,
45             hiddenCols);
46
47   }
48
49   @Override
50   protected void updateViewportFromTopLeft(int xAsRes, int yAsSeq,
51           HiddenSequences hiddenSeqs, HiddenColumns hiddenCols)
52   {
53
54     resetAlignmentDims();
55
56     if (xAsRes < 0)
57     {
58       xAsRes = 0;
59     }
60
61     if (yAsSeq < 0)
62     {
63       yAsSeq = 0;
64     }
65
66     //
67     // Convert x value to residue position
68     //
69
70     // need to determine where scrollCol should be, given x
71     // to do this also need to know width of viewport, and some hidden column
72     // correction
73
74     // convert x to residues - this is an absolute position
75     // int xAsRes = Math.round((float) x * alwidth / width);
76
77     // get viewport width in residues
78     int vpwidth = ranges.getViewportWidth();
79
80     if (xAsRes + vpwidth > alwidth)
81     {
82       // went past the end of the alignment, adjust backwards
83
84       // if last position was before the end of the alignment, need to update
85       if (ranges.getStartRes() < alwidth)
86       {
87         xAsRes = alwidth - vpwidth;
88       }
89       else
90       {
91         xAsRes = ranges.getStartRes();
92       }
93     }
94
95
96     //
97     // Convert y value to sequence position
98     //
99
100     // get viewport height in sequences
101     // add 1 because height includes both endSeq and startSeq
102     int vpheight = ranges.getViewportHeight();
103
104     if (yAsSeq + vpheight > alheight)
105     {
106       // went past the end of the alignment, adjust backwards
107       if (ranges.getEndSeq() < alheight)
108       {
109         yAsSeq = alheight - vpheight;
110       }
111       else
112       {
113         yAsSeq = ranges.getStartSeq();
114       }
115     }
116
117     // update viewport
118     ranges.setStartRes(xAsRes);
119     ranges.setStartSeq(yAsSeq);
120
121   }
122
123   @Override
124   public void setBoxPosition(HiddenSequences hiddenSeqs,
125           HiddenColumns hiddenCols)
126   {
127     setBoxPosition(ranges.getStartRes(), ranges.getStartSeq(),
128             ranges.getViewportWidth(), ranges.getViewportHeight());
129   }
130
131   @Override
132   public AlignmentColsCollectionI getColumns(AlignmentI al)
133   {
134     return new VisibleColsCollection(0,
135             ranges.getAbsoluteAlignmentWidth() - 1, al);
136   }
137
138   @Override
139   public AlignmentRowsCollectionI getRows(AlignmentI al)
140   {
141     return new VisibleRowsCollection(0,
142             ranges.getAbsoluteAlignmentHeight() - 1, al);
143   }
144
145   @Override
146   protected void resetAlignmentDims()
147   {
148     alwidth = ranges.getVisibleAlignmentWidth();
149     alheight = ranges.getVisibleAlignmentHeight();
150   }
151
152   @Override
153   protected int getLeftXFromCentreX(int mousex, HiddenColumns hidden)
154   {
155     int vpx = Math.round((float) mousex * alwidth / width);
156     return vpx - ranges.getViewportWidth() / 2;
157   }
158
159   @Override
160   protected int getTopYFromCentreY(int mousey, HiddenSequences hidden)
161   {
162     int vpy = Math.round((float) mousey * alheight / sequencesHeight);
163     return vpy - ranges.getViewportHeight() / 2;
164   }
165
166   @Override
167   public void setDragPoint(int x, int y, HiddenSequences hiddenSeqs,
168           HiddenColumns hiddenCols)
169   {
170     // get alignment position of x and box (can get directly from vpranges) and
171     // calc difference
172     int vpx = Math.round((float) x * alwidth / width);
173
174     int vpy = Math.round((float) y * alheight / sequencesHeight);
175
176     fixedX = ranges.getStartRes() - vpx;
177     fixedY = ranges.getStartSeq() - vpy;
178
179   }
180
181 }