altosdroid: Add telemetry rate support
[fw/altos] / altosuilib / AltosUIMapPath.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.lang.Math;
25 import java.awt.geom.*;
26 import java.util.*;
27 import java.util.concurrent.*;
28 import org.altusmetrum.altoslib_5.*;
29
30 class PathPoint {
31         AltosUILatLon   lat_lon;
32         int             state;
33
34         public PathPoint(AltosUILatLon lat_lon, int state) {
35                 this.lat_lon = lat_lon;
36                 this.state = state;
37         }
38
39         public boolean equals(PathPoint other) {
40                 if (other == null)
41                         return false;
42
43                 return lat_lon.equals(other.lat_lon) && state == other.state;
44         }
45 }
46
47 public class AltosUIMapPath {
48
49         LinkedList<PathPoint>   points = new LinkedList<PathPoint>();
50         PathPoint               last_point = null;
51
52         static public int stroke_width = 6;
53
54         public void paint(Graphics2D g, AltosUIMapTransform t) {
55                 Point2D.Double  prev = null;
56
57                 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
58                                    RenderingHints.VALUE_ANTIALIAS_ON);
59                 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
60
61                 for (PathPoint point : points) {
62                         Point2D.Double  cur = t.screen(point.lat_lon);
63                         if (prev != null) {
64                                 Line2D.Double   line = new Line2D.Double (prev, cur);
65                                 Rectangle       bounds = line.getBounds();
66
67                                 if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
68                                         if (0 <= point.state && point.state < AltosUIMap.stateColors.length)
69                                                 g.setColor(AltosUIMap.stateColors[point.state]);
70                                         else
71                                                 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
72
73                                         g.draw(line);
74                                 }
75                         }
76                         prev = cur;
77                 }
78         }
79
80         public AltosUIMapRectangle add(double lat, double lon, int state) {
81                 PathPoint               point = new PathPoint(new AltosUILatLon (lat, lon), state);
82                 AltosUIMapRectangle     rect = null;
83
84                 if (!point.equals(last_point)) {
85                         if (last_point != null)
86                                 rect = new AltosUIMapRectangle(last_point.lat_lon, point.lat_lon);
87                         points.add (point);
88                         last_point = point;
89                 }
90                 return rect;
91         }
92
93         public void clear () {
94                 points = new LinkedList<PathPoint>();
95         }
96 }