]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/gui/map/MapCanvas.java
Version 17, September 2014
[GpsPrune.git] / tim / prune / gui / map / MapCanvas.java
index a6d5a8861fb62600b9e006f36b29d7466d9dd456..7a6bce81a5dc285050a8a1e97a5ed3db4b84db37 100644 (file)
@@ -1,5 +1,6 @@
 package tim.prune.gui.map;
 
+import java.awt.AlphaComposite;
 import java.awt.BasicStroke;
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -31,6 +32,7 @@ import javax.swing.JMenuItem;
 import javax.swing.JPanel;
 import javax.swing.JPopupMenu;
 import javax.swing.JSlider;
+import javax.swing.SwingUtilities;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
@@ -57,12 +59,14 @@ import tim.prune.function.compress.MarkPointsInRectangleFunction;
 import tim.prune.function.edit.FieldEdit;
 import tim.prune.function.edit.FieldEditList;
 import tim.prune.gui.IconManager;
+import tim.prune.gui.colour.PointColourer;
+import tim.prune.tips.TipManager;
 
 /**
  * Class for the map canvas, to display a background map and draw on it
  */
 public class MapCanvas extends JPanel implements MouseListener, MouseMotionListener, DataSubscriber,
-       KeyListener, MouseWheelListener
+       KeyListener, MouseWheelListener, TileConsumer
 {
        /** App object for callbacks */
        private App _app = null;
@@ -82,6 +86,8 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
        private MapTileManager _tileManager = new MapTileManager(this);
        /** Image to display */
        private BufferedImage _mapImage = null;
+       /** Second image for drawing track (only needed for alpha blending) */
+       private BufferedImage _trackImage = null;
        /** Slider for transparency */
        private JSlider _transparencySlider = null;
        /** Checkbox for scale bar */
@@ -292,6 +298,8 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                add(_scaleBar, BorderLayout.SOUTH);
                // Make popup menu
                makePopup();
+               // Get currently selected map from Config, pass to MapTileManager
+               _tileManager.setMapSource(Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX));
        }
 
 
@@ -480,7 +488,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                else if (px > (getWidth()-PAN_DISTANCE)) {
                        panX = AUTOPAN_DISTANCE + px - getWidth();
                }
-               if (py < PAN_DISTANCE) {
+               if (py < (2*PAN_DISTANCE)) {
                        panY = py - AUTOPAN_DISTANCE;
                }
                if (py > (getHeight()-PAN_DISTANCE)) {
@@ -508,8 +516,19 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                g.fillRect(0, 0, getWidth(), getHeight());
 
                // Check whether maps are on or not
-               boolean showMap = Config.getConfigBoolean(Config.KEY_SHOW_MAP);
+               final boolean showMap = Config.getConfigBoolean(Config.KEY_SHOW_MAP);
                _mapCheckBox.setSelected(showMap);
+               // Check whether disk cache is on or not
+               final boolean usingDiskCache = Config.getConfigString(Config.KEY_DISK_CACHE) != null;
+               // Show tip to recommend setting up a cache
+               if (showMap && !usingDiskCache && Config.getConfigBoolean(Config.KEY_ONLINE_MODE))
+               {
+                       SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       _app.showTip(TipManager.Tip_UseAMapCache);
+                               }
+                       });
+               }
 
                // reset error message
                if (!showMap) {_shownOsmErrorAlready = false;}
@@ -544,7 +563,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                                                // Loop over layers
                                                for (int l=0; l<numLayers; l++)
                                                {
-                                                       Image image = _tileManager.getTile(l, tileX, tileY);
+                                                       Image image = _tileManager.getTile(l, tileX, tileY, true);
                                                        if (image != null) {
                                                                g.drawImage(image, x, y, 256, 256, null);
                                                        }
@@ -565,24 +584,58 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                        }
                }
 
-               // Paint the track points on top
-               int pointsPainted = 1;
-               try
+               // Work out track opacity according to slider
+               final float[] opacities = {1.0f, 0.75f, 0.5f, 0.3f, 0.15f, 0.0f};
+               float trackOpacity = 1.0f;
+               if (_transparencySlider.getValue() < 0) {
+                       trackOpacity = opacities[-1 - _transparencySlider.getValue()];
+               }
+
+               if (trackOpacity > 0.0f)
                {
-                       pointsPainted = paintPoints(g);
+                       // Paint the track points on top
+                       int pointsPainted = 1;
+                       try
+                       {
+                               if (trackOpacity > 0.9f)
+                               {
+                                       // Track is fully opaque, just draw it directly
+                                       pointsPainted = paintPoints(g);
+                                       _trackImage = null;
+                               }
+                               else
+                               {
+                                       // Track is partly transparent, so use a separate BufferedImage
+                                       if (_trackImage == null || _trackImage.getWidth() != getWidth() || _trackImage.getHeight() != getHeight())
+                                       {
+                                               _trackImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
+                                       }
+                                       // Clear to transparent
+                                       Graphics2D gTrack = _trackImage.createGraphics();
+                                       gTrack.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
+                                       gTrack.fillRect(0, 0, getWidth(), getHeight());
+                                       gTrack.setPaintMode();
+                                       // Draw the track onto this separate image
+                                       pointsPainted = paintPoints(gTrack);
+                                       ((Graphics2D) g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, trackOpacity));
+                                       g.drawImage(_trackImage, 0, 0, null);
+                               }
+                       }
+                       catch (NullPointerException npe) {} // ignore, probably due to data being changed during drawing
+                       catch (ArrayIndexOutOfBoundsException obe) {} // also ignore
+
+                       // Zoom to fit if no points found
+                       if (pointsPainted <= 0 && _checkBounds)
+                       {
+                               zoomToFit();
+                               _recalculate = true;
+                               repaint();
+                       }
                }
-               catch (NullPointerException npe) {} // ignore, probably due to data being changed during drawing
-               catch (ArrayIndexOutOfBoundsException obe) {} // also ignore
 
                // free g
                g.dispose();
 
-               // Zoom to fit if no points found
-               if (pointsPainted <= 0 && _checkBounds) {
-                       zoomToFit();
-                       _recalculate = true;
-                       repaint();
-               }
                _checkBounds = false;
                // enable / disable transparency slider
                _transparencySlider.setEnabled(showMap);
@@ -598,15 +651,12 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
        {
                // Set up colours
                final ColourScheme cs = Config.getColourScheme();
-               final int[] opacities = {255, 190, 130, 80, 40, 0};
-               int opacity = 255;
-               if (_transparencySlider.getValue() < 0)
-                       opacity = opacities[-1 - _transparencySlider.getValue()];
-               final Color pointColour  = makeTransparentColour(cs.getColour(ColourScheme.IDX_POINT), opacity);
-               final Color rangeColour  = makeTransparentColour(cs.getColour(ColourScheme.IDX_SELECTION), opacity);
-               final Color currentColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_PRIMARY), opacity);
-               final Color secondColour = makeTransparentColour(cs.getColour(ColourScheme.IDX_SECONDARY), opacity);
-               final Color textColour   = makeTransparentColour(cs.getColour(ColourScheme.IDX_TEXT), opacity);
+               final Color pointColour  = cs.getColour(ColourScheme.IDX_POINT);
+               final Color rangeColour  = cs.getColour(ColourScheme.IDX_SELECTION);
+               final Color currentColour = cs.getColour(ColourScheme.IDX_PRIMARY);
+               final Color secondColour = cs.getColour(ColourScheme.IDX_SECONDARY);
+               final Color textColour   = cs.getColour(ColourScheme.IDX_TEXT);
+               final PointColourer pointColourer = _app.getPointColourer();
 
                final int winWidth  = getWidth();
                final int winHeight = getHeight();
@@ -644,26 +694,34 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                        currPointVisible = px >= 0 && px < winWidth && py >= 0 && py < winHeight;
                        isWaypoint = _track.getPoint(i).isWaypoint();
                        anyWaypoints = anyWaypoints || isWaypoint;
-                       if (currPointVisible)
+                       if (!isWaypoint)
                        {
-                               if (!isWaypoint)
+                               if (currPointVisible || prevPointVisible)
                                {
-                                       // Draw rectangle for track point
+                                       // For track points, work out which colour to use
                                        if (_track.getPoint(i).getDeleteFlag()) {
                                                inG.setColor(currentColour);
                                        }
-                                       else {
+                                       else if (pointColourer != null)
+                                       {  // use the point colourer if there is one
+                                               Color trackColour = pointColourer.getColour(i);
+                                               inG.setColor(trackColour);
+                                       }
+                                       else
+                                       {
                                                inG.setColor(pointColour);
                                        }
-                                       inG.drawRect(px-2, py-2, 3, 3);
-                                       pointsPainted++;
+
+                                       // Draw rectangle for track point if it's visible
+                                       if (currPointVisible)
+                                       {
+                                               inG.drawRect(px-2, py-2, 3, 3);
+                                               pointsPainted++;
+                                       }
                                }
-                       }
-                       if (!isWaypoint)
-                       {
+
                                // Connect track points if either of them are visible
-                               if (connectPoints && (currPointVisible || prevPointVisible)
-                                && !(prevX == -1 && prevY == -1)
+                               if (connectPoints && !(prevX == -1 && prevY == -1)
                                 && !_track.getPoint(i).getSegmentStart())
                                {
                                        inG.drawLine(prevX, prevY, px, py);
@@ -844,38 +902,29 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                }
        }
 
-       /**
-        * Make a semi-transparent colour for drawing with
-        * @param inColour base colour (fully opaque)
-        * @param inOpacity opacity where 0=invisible and 255=full
-        * @return new colour object
-        */
-       private static Color makeTransparentColour(Color inColour, int inOpacity)
-       {
-               if (inOpacity > 240) return inColour;
-               return new Color(inColour.getRed(), inColour.getGreen(), inColour.getBlue(), inOpacity);
-       }
-
        /**
         * Inform that tiles have been updated and the map can be repainted
         * @param inIsOk true if data loaded ok, false for error
         */
-       public synchronized void tilesUpdated(boolean inIsOk)
+       public void tilesUpdated(boolean inIsOk)
        {
-               // Show message if loading failed (but not too many times)
-               if (!inIsOk && !_shownOsmErrorAlready && _mapCheckBox.isSelected())
+               synchronized(this)
                {
-                       _shownOsmErrorAlready = true;
-                       // use separate thread to show message about failing to load osm images
-                       new Thread(new Runnable() {
-                               public void run() {
-                                       try {Thread.sleep(500);} catch (InterruptedException ie) {}
-                                       _app.showErrorMessage("error.osmimage.dialogtitle", "error.osmimage.failed");
-                               }
-                       }).start();
+                       // Show message if loading failed (but not too many times)
+                       if (!inIsOk && !_shownOsmErrorAlready && _mapCheckBox.isSelected())
+                       {
+                               _shownOsmErrorAlready = true;
+                               // use separate thread to show message about failing to load osm images
+                               new Thread(new Runnable() {
+                                       public void run() {
+                                               try {Thread.sleep(500);} catch (InterruptedException ie) {}
+                                               _app.showErrorMessage("error.osmimage.dialogtitle", "error.osmimage.failed");
+                                       }
+                               }).start();
+                       }
+                       _recalculate = true;
+                       repaint();
                }
-               _recalculate = true;
-               repaint();
        }
 
        /**
@@ -1032,8 +1081,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                                        else if (_drawMode == MODE_DRAW_POINTS_CONT)
                                        {
                                                DataPoint point = createPointFromClick(inE.getX(), inE.getY());
-                                               _app.createPoint(point);
-                                               point.setSegmentStart(false);
+                                               _app.createPoint(point, false); // not a new segment
                                        }
                                }
                                else if (inE.getClickCount() == 2)
@@ -1168,7 +1216,7 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                        _app.setCurrentMode(App.AppMode.NORMAL);
                        _drawMode = MODE_DEFAULT;
                        // Call a function to mark the points
-                       MarkPointsInRectangleFunction marker = new MarkPointsInRectangleFunction(_app);
+                       MarkPointsInRectangleFunction marker = (MarkPointsInRectangleFunction) FunctionLibrary.FUNCTION_MARK_IN_RECTANGLE;
                        double lon1 = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(_dragFromX, getWidth()));
                        double lat1 = MapUtils.getLatitudeFromY(_mapPosition.getYFromPixels(_dragFromY, getHeight()));
                        double lon2 = MapUtils.getLongitudeFromX(_mapPosition.getXFromPixels(_dragToX, getWidth()));
@@ -1298,7 +1346,8 @@ public class MapCanvas extends JPanel implements MouseListener, MouseMotionListe
                        _checkBounds = true;
                }
                if ((inUpdateType & DataSubscriber.MAPSERVER_CHANGED) > 0) {
-                       _tileManager.resetConfig();
+                       // Get the selected map source index and pass to tile manager
+                       _tileManager.setMapSource(Config.getConfigInt(Config.KEY_MAPSOURCE_INDEX));
                }
                if ((inUpdateType & (DataSubscriber.DATA_ADDED_OR_REMOVED + DataSubscriber.DATA_EDITED)) > 0) {
                        _midpoints.updateData(_track);