Ignore:
Timestamp:
2011-04-07T00:03:40+02:00 (14 years ago)
Author:
glebius
Message:

New autocentering technique, based on position of cursor on the
map. User configures the tunable:

livegps.center_factor=80 (80% - default value)

It represents a value (in percent) of a rectangular screen
area where the cursor should be present. If cursor leaves the
area, centering is performed.

Time based centering can now be turned off by default with:

livegps.center_interval_msec=0

But is turned on by default for compatibility.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/livegps/src/livegps/LiveGpsLayer.java

    r25808 r25809  
    66import java.awt.Graphics2D;
    77import java.awt.Point;
     8import java.awt.geom.Point2D;
     9import java.awt.Rectangle;
    810import java.beans.PropertyChangeEvent;
    911import java.beans.PropertyChangeListener;
     
    2830    private static final int DEFAULT_REFRESH_INTERVAL = 250;
    2931    private static final int DEFAULT_CENTER_INTERVAL = 5000;
     32    private static final int DEFAULT_CENTER_FACTOR = 80;
    3033    private static final String oldC_REFRESH_INTERVAL = "livegps.refreshinterval";     /* in seconds */
    3134    private static final String C_REFRESH_INTERVAL = "livegps.refresh_interval_msec";  /* in msec */
    3235    private static final String C_CENTER_INTERVAL = "livegps.center_interval_msec";  /* in msec */
     36    private static final String C_CENTER_FACTOR = "livegps.center_factor" /* in percent */;
    3337    private int refreshInterval;
    3438    private int centerInterval;
     39    private double centerFactor;
    3540    private long lastRedraw = 0;
    3641    private long lastCenter = 0;
     
    6974        lastPoint.attr.put("time", dateFormat.format(new Date()));
    7075        trackSegment.addWaypoint(lastPoint);
    71         if (autocenter && allowCenter())
    72             center();
     76
     77        conditionalCenter(thisPos);
    7378    }
    7479
     
    7681        if (lastPoint != null)
    7782            Main.map.mapView.zoomTo(lastPoint.getCoor());
     83    }
     84
     85    public void conditionalCenter(LatLon Pos) {
     86        Point2D P = Main.map.mapView.getPoint2D(Pos);
     87        Rectangle rv = Main.map.mapView.getBounds(null);
     88        Date date = new Date();
     89        long current = date.getTime();
     90
     91        rv.grow(-(int)(rv.getHeight() * centerFactor), -(int)(rv.getWidth() * centerFactor));
     92
     93        if (!rv.contains(P) || (centerInterval > 0 && current - lastCenter >= centerInterval)) {
     94                Main.map.mapView.zoomTo(Pos);
     95                lastCenter = current;
     96        }
    7897    }
    7998
     
    170189
    171190    /**
    172      * Check, if a autocentering is currently allowed.
    173      *
    174      * @return true, if a autocentering is permitted, false, if a autocentering
    175      * should be suppressed.
    176      */
    177     private boolean allowCenter() {
    178         Date date = new Date();
    179         long current = date.getTime();
    180 
    181         if (current - lastCenter >= centerInterval) {
    182                 lastCenter = current;
    183                 return true;
    184         } else
    185                 return false;
    186     }
    187 
    188     /**
    189191     * Retrieve the refreshInterval and centerInterval from the configuration. Be compatible
    190192     * with old version that stored refreshInterval in seconds. If no such configuration key
     
    192194     */
    193195    private void initIntervals() {
    194         if ((refreshInterval = Main.pref.getInteger(C_REFRESH_INTERVAL, 0)) == 0) {
    195                 if ((refreshInterval = Main.pref.getInteger(oldC_REFRESH_INTERVAL, 0)) != 0) {
    196                         refreshInterval *= 1000;
    197                         Main.pref.put(oldC_REFRESH_INTERVAL, null);
    198                 } else
    199                         refreshInterval = DEFAULT_REFRESH_INTERVAL;
    200         }
    201 
    202         if ((centerInterval = Main.pref.getInteger(C_CENTER_INTERVAL, 0)) == 0)
    203                 centerInterval = DEFAULT_CENTER_INTERVAL;
     196        if ((refreshInterval = Main.pref.getInteger(oldC_REFRESH_INTERVAL, 0)) != 0) {
     197                refreshInterval *= 1000;
     198                Main.pref.put(oldC_REFRESH_INTERVAL, null);
     199        } else
     200                refreshInterval = Main.pref.getInteger(C_REFRESH_INTERVAL, DEFAULT_REFRESH_INTERVAL);
     201
     202        centerInterval = Main.pref.getInteger(C_CENTER_INTERVAL, DEFAULT_CENTER_INTERVAL);
     203        centerFactor = Main.pref.getInteger(C_CENTER_FACTOR, DEFAULT_CENTER_FACTOR);
     204        if (centerFactor <= 1 || centerFactor >= 99)
     205                centerFactor = DEFAULT_CENTER_FACTOR;
    204206
    205207        Main.pref.putInteger(C_REFRESH_INTERVAL, refreshInterval);
    206208        Main.pref.putInteger(C_CENTER_INTERVAL, centerInterval);
     209        Main.pref.putInteger(C_CENTER_FACTOR, (int )centerFactor);
     210
     211        /*
     212         * Do one time conversion of factor: user value means "how big is inner rectangle
     213         * comparing to screen in percent", machine value means "what is the shrink ratio
     214         * for each dimension on _both_ sides".
     215         */
     216
     217        centerFactor = (100 - centerFactor) / 2 / 100;
    207218    }
    208219}
Note: See TracChangeset for help on using the changeset viewer.