add initial version of drag-zoom

This commit is contained in:
Eyck Jentzsch 2020-03-13 21:06:53 +01:00
parent 2b7c9a240f
commit 674aa7d3ce
2 changed files with 47 additions and 16 deletions

View File

@ -229,6 +229,11 @@ public class WaveformCanvas extends Canvas{
}
public void setZoomLevel(int level) {
long tc=cursorPainters.get(0).getTime(); // cursor time
setZoomLevel(level, tc);
}
public void setZoomLevel(int level, long centerTime) {
long oldScaleFactor=scaleFactor;
if(level<Constants.unitMultiplier.length*Constants.unitString.length){
this.level = level;
@ -242,10 +247,9 @@ public class WaveformCanvas extends Canvas{
* xcn = tc/newScaleFactor
* t0n = (xcn-xoffs)*scaleFactor
*/
long tc=cursorPainters.get(0).getTime(); // cursor time
long xc=tc/oldScaleFactor; // cursor total x-offset
long xc=centerTime/oldScaleFactor; // cursor total x-offset
long xoffs=xc+origin.x; // cursor offset relative to left border
long xcn=tc/scaleFactor; // new total x-offset
long xcn=centerTime/scaleFactor; // new total x-offset
long originX=xcn-xoffs;
if(originX>0) {
origin.x=(int) -originX; // new cursor time offset relative to left border
@ -557,7 +561,7 @@ public class WaveformCanvas extends Canvas{
return (getClientArea().width+origin.x)*scaleFactor;
}
long getOriginTime() {
long getMinVisibleTime() {
return origin.x * scaleFactor;
}
}

View File

@ -194,26 +194,60 @@ public class WaveformViewer implements IWaveformViewer {
if((e.stateMask&SWT.MODIFIER_MASK&~SWT.SHIFT)!=0) return; //don't react on modifier except shift
if(!start.equals(end)){
asyncUpdate(e.widget);
long startTime = waveformCanvas.getTimeForOffset(start.x);
long endTime = waveformCanvas.getTimeForOffset(end.x);
long targetTimeRange = endTime-startTime;
long currentTimeRange = waveformCanvas.getMaxVisibleTime()-waveformCanvas.getMinVisibleTime();
if(targetTimeRange==0) return;
long relation = currentTimeRange/targetTimeRange;
long i = 1;
int level=0;//relation>0?0:0;
do {
if(relation<0 ) {
if(-relation<i) {
break;
}
level--;
if(-relation<i*3) {
break;
}
level--;
} else {
if(relation<i) {
break;
}
level++;
if(relation<i*3) {
break;
}
level++;
}
i=i*10;
} while(i<10000);
if(i<10000) {
int curLevel = waveformCanvas.getZoomLevel();
waveformCanvas.setZoomLevel(curLevel-level, (startTime+endTime)/2);
}
} else if (e.button == 1 && ((e.stateMask&SWT.SHIFT)==0)) {
// set cursor (button 1 and no shift)
if(Math.abs(e.x-start.x)<3 && Math.abs(e.y-start.y)<3){
// first set cursor time
setCursorTime(snapOffsetToEvent(e));
setCursorTime(snapOffsetToEvent(start));
// then set selection and reveal
setSelection(new StructuredSelection(initialSelected));
asyncUpdate(e.widget);
}
}else if (e.button == 2 ||(e.button==1 && (e.stateMask&SWT.SHIFT)!=0)) {
// set marker (button 1 and shift)
setMarkerTime(snapOffsetToEvent(e), selectedMarker);
setMarkerTime(snapOffsetToEvent(start), selectedMarker);
asyncUpdate(e.widget);
}
}
protected long snapOffsetToEvent(MouseEvent e) {
long time= waveformCanvas.getTimeForOffset(e.x);
protected long snapOffsetToEvent(Point p) {
long time= waveformCanvas.getTimeForOffset(p.x);
long scaling=5*waveformCanvas.getScaleFactor();
for(Object o:waveformCanvas.getClicked(start)){
for(Object o:waveformCanvas.getClicked(p)){
Entry<Long, ?> floorEntry=null, ceilEntry=null;
if(o instanceof TrackEntry){
TrackEntry entry = (TrackEntry) o;
@ -1270,13 +1304,6 @@ public class WaveformViewer implements IWaveformViewer {
if(percent<-100) percent=-100;
if(percent>100) percent=100;
int diff = (waveformCanvas.getWidth()*percent)/100;
// ScrollBar sb = waveformCanvas.getHorizontalBar();
// int x = sb.getSelection();
// System.out.println("Setting sb to "+ (x+diff));
// if((x+diff)>0)
// sb.setSelection(x+diff);
// else
// sb.setSelection(0);
Point o = waveformCanvas.getOrigin();
waveformCanvas.setOrigin(o.x-diff, o.y);
waveformCanvas.redraw();