AltosSiteMap: add targeting circles around landing site
[fw/altos] / ao-tools / altosui / AltosSiteMap.java
index 6f33aabd152d0e696183274a7cf7222907167fba..25b7779243f4fda1bea9b4be51a2e0380e11007f 100644 (file)
@@ -40,7 +40,8 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
 
     Graphics2D g2d;
 
-    private void setLocation(double new_lat, double new_lng, int new_zoom) {
+    private boolean setLocation(double new_lat, double new_lng) {
+        int new_zoom = 15;
         lat = new_lat;
         lng = new_lng;
         zoom = new_zoom;
@@ -50,6 +51,19 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
         coord_pt.x = 320-coord_pt.x;
         coord_pt.y = 320-coord_pt.y;
         last_pt = null;
+
+        try {
+            File pngfile = new File(AltosPreferences.logdir(), 
+                                    FileCoord(lat, lng, zoom));
+            System.out.printf("Trying file %s\n", pngfile);
+            BufferedImage myPicture = ImageIO.read(pngfile);
+            picLabel.setIcon(new ImageIcon( myPicture ));
+            g2d = myPicture.createGraphics();
+        } catch (Exception e) { 
+            // throw new RuntimeException(e);
+            return false;
+        }
+        return true;
     }
 
     private static double limit(double v, double lo, double hi) {
@@ -60,6 +74,16 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
         return v;
     }
 
+    private static String FileCoord(double lat, double lng, int zoom) {
+        char chlat = lat < 0 ? 'S' : 'N';
+        char chlng = lng < 0 ? 'E' : 'W';
+        if (lat < 0) lat = -lat;
+        if (lng < 0) lng = -lng;
+        return String.format("map-%c%.3f,%c%.3f-%d.png",
+                chlat, lat, chlng, lng, zoom);
+    }
+
+
     // based on google js
     //  http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/10/main.js
     // search for fromLatLngToPoint and fromPointToLatLng
@@ -94,9 +118,23 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
         Color.BLACK   // landed
     };
 
+    boolean drawn_landed_circle = false;
+    boolean nomaps = false;
     public void show(AltosState state, int crc_errors) {
-        if (!state.gps_ready)
+        if (nomaps)
+            return;
+        if (!state.gps_ready && state.pad_lat == 0 && state.pad_lon == 0)
             return;
+        double plat = (int)(state.pad_lat*200)/200.0;
+        double plon = (int)(state.pad_lon*200)/200.0;
+
+        if (last_pt == null) {
+            if (!setLocation(plat, plon)) {
+                nomaps = true;
+                return;
+            }
+        }
+
         Point2D.Double pt = pt(state.gps.lat, state.gps.lon);
         if (last_pt != null && pt != last_pt) {
             if (0 <= state.state && state.state < stateColors.length) {
@@ -104,34 +142,36 @@ public class AltosSiteMap extends JComponent implements AltosFlightDisplay {
             }
             g2d.draw(new Line2D.Double(last_pt, pt));
         }
+
+        if (state.state == 8 && !drawn_landed_circle) {
+            drawn_landed_circle = true;
+            g2d.setColor(Color.RED);
+            g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
+            g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
+            g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
+        }
+
         last_pt = pt;
         repaint();
     }
-    
+   
+    JLabel picLabel = new JLabel();
+
     public AltosSiteMap() {
         GridBagLayout layout = new GridBagLayout();
         setLayout(layout);
 
         GridBagConstraints c = new GridBagConstraints();
 
-        setLocation(-27.850, 152.960, 15);
-        String pngfile = "/home/aj/qrs-S27.850,W152.960-15.png";
-
         c.gridx = 0; c.gridy = 0;
         c.weightx = 1; c.weighty = 1;
         c.anchor = GridBagConstraints.CENTER;
         c.fill = GridBagConstraints.BOTH;
+        picLabel = new JLabel();
+        JScrollPane scrollPane = new JScrollPane(picLabel);
+        layout.setConstraints(scrollPane, c);
+        add(scrollPane);
 
-        try {
-            BufferedImage myPicture = ImageIO.read(new File(pngfile));
-            g2d = myPicture.createGraphics();
-            JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
-            JScrollPane scrollPane = new JScrollPane(picLabel);
-            layout.setConstraints(scrollPane, c);
-            add(scrollPane);
-        } catch (Exception e) { 
-            throw new RuntimeException(e);
-        };
     }
 }