Changeset 15511 in josm


Ignore:
Timestamp:
2019-11-05T16:15:59+01:00 (5 years ago)
Author:
stoecker
Message:

better handling of smooth scrolling in case of long distances and overlapping scrolls, why must Java sometimes be so complicated...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r15484 r15511  
    735735
    736736    /**
     737     * Thread class for smooth scrolling. Made a separate class, so we can safely terminate it.
     738     */
     739    private class SmoothScrollThread extends Thread {
     740        private boolean doStop = false;
     741        private final EastNorth oldCenter = getCenter();
     742        private final EastNorth finalNewCenter;
     743        private final double frames;
     744        private final long sleepTime;
     745
     746        SmoothScrollThread(EastNorth newCenter, double frameNum, int fps) {
     747            super("smooth-scroller");
     748            finalNewCenter = newCenter;
     749            frames = frameNum;
     750            sleepTime = 1000L / fps;
     751        }
     752
     753        @Override
     754        public void run() {
     755            try {
     756                for (int i = 0; i < frames && !doStop; i++) {
     757                    zoomTo(oldCenter.interpolate(finalNewCenter, (i+1) / frames));
     758                    Thread.sleep(sleepTime);
     759                }
     760            } catch (InterruptedException ex) {
     761                Logging.warn("Interruption during smooth scrolling");
     762            }
     763        }
     764
     765        public void stopIt() {
     766            doStop = true;
     767        }
     768    }
     769
     770    /**
    737771     * Create a thread that moves the viewport to the given center in an animated fashion.
    738772     * @param newCenter new east/north center
     
    749783                milliseconds = maxtime;
    750784            }
    751             final double frames = milliseconds * fps / 1000;
    752             final EastNorth finalNewCenter = newCenter;
    753 
    754             new Thread("smooth-scroller") {
    755                 @Override
    756                 public void run() {
    757                     for (int i = 0; i < frames; i++) {
    758                         // FIXME - do not use zoom history here
    759                         zoomTo(oldCenter.interpolate(finalNewCenter, (i+1) / frames));
    760                         try {
    761                             Thread.sleep(1000L / fps);
    762                         } catch (InterruptedException ex) {
    763                             Logging.warn("InterruptedException in "+NavigatableComponent.class.getSimpleName()+" during smooth scrolling");
    764                             Thread.currentThread().interrupt();
    765                         }
    766                     }
    767                 }
    768             }.start();
     785
     786            ThreadGroup group = Thread.currentThread().getThreadGroup();
     787            Thread[] threads = new Thread[group.activeCount()];
     788            group.enumerate(threads, true);
     789            boolean stopped = false;
     790            for (Thread t : threads) {
     791                if (t instanceof SmoothScrollThread) {
     792                    ((SmoothScrollThread)t).stopIt();
     793                    /* handle this case outside in case there is more than one smooth thread */
     794                    stopped = true;
     795                }
     796            }
     797            if (stopped && milliseconds > maxtime/2.0) { /* we aren't fast enough, skip smooth */
     798                Logging.warn("Skip smooth scrolling");
     799                zoomTo(newCenter);
     800            } else {
     801                new SmoothScrollThread(newCenter, milliseconds * fps / 1000, fps).start();
     802            }
    769803        }
    770804    }
Note: See TracChangeset for help on using the changeset viewer.