]> git.gag.com Git - fw/altos/blobdiff - altosuilib/AltosSiteMapTile.java
altosuilib: Add distance measuring line to site map.
[fw/altos] / altosuilib / AltosSiteMapTile.java
index 1046d6bd0191af519a608f2a62a5e752a676b0c7..09f184a37869cf121f19e3f556b535f10ca7e4ea 100644 (file)
@@ -21,10 +21,10 @@ import java.awt.*;
 import java.awt.image.*;
 import javax.swing.*;
 import javax.imageio.*;
-import java.awt.geom.Point2D;
-import java.awt.geom.Line2D;
+import java.awt.geom.*;
 import java.io.*;
 import java.util.*;
+import java.awt.RenderingHints.*;
 import org.altusmetrum.altoslib_4.*;
 
 class AltosPoint {
@@ -40,15 +40,41 @@ class AltosPoint {
 public class AltosSiteMapTile extends JComponent {
        int px_size;
        File file;
+       int status;
 
        Point2D.Double  boost;
        Point2D.Double  landed;
+       Line2D.Double   line;
+       double          line_course;
+       double          line_dist;
 
        LinkedList<AltosPoint>  points;
 
-       public void loadMap(File pngFile) {
+       public synchronized void queue_repaint() {
+               SwingUtilities.invokeLater(new Runnable() {
+                               public void run() {
+                                       repaint();
+                               }
+                       });
+       }
+
+       public void load_map(File pngFile) {
                file = pngFile;
-               repaint();
+               queue_repaint();
+       }
+
+       private Font    font = null;
+
+       public void set_font(Font font) {
+               this.font = font;
+               this.status = AltosSiteMapCache.success;
+               queue_repaint();
+       }
+
+       public void set_status(int status) {
+               file = null;
+               this.status = status;
+               queue_repaint();
        }
 
        public void clearMap() {
@@ -56,6 +82,9 @@ public class AltosSiteMapTile extends JComponent {
                landed = null;
                points = null;
                file = null;
+               status = AltosSiteMapCache.success;
+               queue_repaint();
+               line = null;
        }
 
        static Color stateColors[] = {
@@ -78,7 +107,48 @@ public class AltosSiteMapTile extends JComponent {
 
        public void set_boost(Point2D.Double boost) {
                this.boost = boost;
-               repaint();
+               queue_repaint();
+       }
+
+       public void set_line(Line2D.Double line, double distance) {
+               this.line = line;
+               line_dist = distance;
+               queue_repaint();
+       }
+
+       private String line_dist() {
+               String  format;
+               double  distance = line_dist;
+
+               if (AltosConvert.imperial_units) {
+                       distance = AltosConvert.meters_to_feet(distance);
+                       if (distance < 10000) {
+                               format = "%4.0fft";
+                       } else {
+                               distance /= 5280;
+                               if (distance < 10)
+                                       format = "%5.3fmi";
+                               else if (distance < 100)
+                                       format = "%5.2fmi";
+                               else if (distance < 1000)
+                                       format = "%5.1fmi";
+                               else
+                                       format = "%5.0fmi";
+                       }
+               } else {
+                       if (distance < 10000) {
+                               format = "%4.0fm";
+                       } else {
+                               distance /= 1000;
+                               if (distance < 100)
+                                       format = "%5.2fkm";
+                               else if (distance < 1000)
+                                       format = "%5.1fkm";
+                               else
+                                       format = "%5.0fkm";
+                       }
+               }
+               return String.format(format, distance);
        }
 
        public void paint(Graphics g) {
@@ -94,6 +164,34 @@ public class AltosSiteMapTile extends JComponent {
                } else {
                        g2d.setColor(Color.GRAY);
                        g2d.fillRect(0, 0, getWidth(), getHeight());
+                       String  message = null;
+                       switch (status) {
+                       case AltosSiteMapCache.loading:
+                               message = "Loading...";
+                               break;
+                       case AltosSiteMapCache.bad_request:
+                               message = "Internal error";
+                               break;
+                       case AltosSiteMapCache.failed:
+                               message = "Network error, check connection";
+                               break;
+                       case AltosSiteMapCache.forbidden:
+                               message = "Too many requests, try later";
+                               break;
+                       }
+                       if (message != null && font != null) {
+                               g2d.setFont(font);
+                               g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+                               Rectangle2D     bounds;
+                               bounds = font.getStringBounds(message, g2d.getFontRenderContext());
+
+                               float x = getWidth() / 2.0f;
+                               float y = getHeight() / 2.0f;
+                               x = x - (float) bounds.getWidth() / 2.0f;
+                               y = y + (float) bounds.getHeight() / 2.0f;
+                               g2d.setColor(Color.BLACK);
+                               g2d.drawString(message, x, y);
+                       }
                }
 
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
@@ -118,21 +216,41 @@ public class AltosSiteMapTile extends JComponent {
                        g2d.setColor(Color.BLACK);
                        draw_circle(g2d, landed);
                }
+
+               if (line != null) {
+                       g2d.setColor(Color.BLUE);
+                       g2d.draw(line);
+
+                       String  message = line_dist();
+                       g2d.setFont(font);
+                       g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
+                       Rectangle2D     bounds;
+                       bounds = font.getStringBounds(message, g2d.getFontRenderContext());
+
+                       float x = (float) line.x1;
+                       float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
+
+                       if (line.x1 < line.x2) {
+                               x -= (float) bounds.getWidth() + 2.0f;
+                       } else {
+                               x += 2.0f;
+                       }
+                       g2d.drawString(message, x, y);
+               }
        }
 
-       public synchronized void show(AltosState state, AltosListenerState listener_state,
-                                     Point2D.Double last_pt, Point2D.Double pt)
+       public synchronized void show(int state, Point2D.Double last_pt, Point2D.Double pt)
        {
                if (points == null)
                        points = new LinkedList<AltosPoint>();
 
-               points.add(new AltosPoint(pt, state.state));
+               points.add(new AltosPoint(pt, state));
 
-               if (state.state == AltosLib.ao_flight_boost && boost == null)
+               if (state == AltosLib.ao_flight_boost && boost == null)
                        boost = pt;
-               if (state.state == AltosLib.ao_flight_landed && landed == null)
+               if (state == AltosLib.ao_flight_landed && landed == null)
                        landed = pt;
-               repaint();
+               queue_repaint();
        }
 
        public AltosSiteMapTile(int in_px_size) {