Merge branch 'buttonbox' into sitemap
[fw/altos] / ao-tools / altosui / AltosSiteMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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 altosui;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.imageio.ImageIO;
25 import javax.swing.table.*;
26 import java.io.*;
27 import java.util.*;
28 import java.text.*;
29 import java.util.prefs.*;
30 import java.lang.Math;
31 import java.awt.geom.Point2D;
32 import java.awt.geom.Line2D;
33
34 public class AltosSiteMapTile extends JLayeredPane {
35     Point2D.Double coord_pt;
36     Point2D.Double last_pt;
37
38     JLabel mapLabel;
39     JLabel draw;
40     Graphics2D g2d;
41
42     public void loadMap(ImageIcon icn) {
43         mapLabel.setIcon(icn);
44     }
45
46     static Color stateColors[] = {
47         Color.WHITE,  // startup
48         Color.WHITE,  // idle
49         Color.WHITE,  // pad
50         Color.RED,    // boost
51         Color.PINK,   // fast
52         Color.YELLOW, // coast
53         Color.CYAN,   // drogue
54         Color.BLUE,   // main
55         Color.BLACK   // landed
56     };
57
58     boolean drawn_landed_circle = false;
59     boolean drawn_boost_circle = false;
60     public void show(AltosState state, int crc_errors, Point2D.Double pt) {
61         if (last_pt == null) {
62             // setLocation(state.pad_lat, state.pad_lon);
63             // loadMap();
64             last_pt = pt;
65         }
66
67         if (pt != last_pt) {
68             if (0 <= state.state && state.state < stateColors.length) {
69                 g2d.setColor(stateColors[state.state]);
70             }
71             g2d.draw(new Line2D.Double(last_pt, pt));
72         }
73
74         int px_size = getWidth();
75         if (0 <= pt.x && pt.x < px_size) {
76             if (0 <= pt.y && pt.y < px_size) {
77                 int dx = 500, dy = 250;
78                 if (state.state > 2) {
79                     dx = Math.min(200, 20 + (int) Math.abs(last_pt.x - pt.x));
80                     dy = Math.min(100, 10 + (int) Math.abs(last_pt.y - pt.y));
81                 }
82                 Rectangle r = new Rectangle((int)pt.x-dx, (int)pt.y-dy, 
83                                             dx*2, dy*2);
84                 scrollRectToVisible(r);
85             }
86         }
87
88         if (state.state == 3 && !drawn_boost_circle) {
89             drawn_boost_circle = true;
90             g2d.setColor(Color.RED);
91             g2d.drawOval((int)last_pt.x-5, (int)last_pt.y-5, 10, 10);
92             g2d.drawOval((int)last_pt.x-20, (int)last_pt.y-20, 40, 40);
93             g2d.drawOval((int)last_pt.x-35, (int)last_pt.y-35, 70, 70);
94         }
95         if (state.state == 8 && !drawn_landed_circle) {
96             drawn_landed_circle = true;
97             g2d.setColor(Color.BLACK);
98             g2d.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
99             g2d.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
100             g2d.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
101         }
102
103         last_pt = pt;
104         repaint();
105     }
106
107     public static Graphics2D fillLabel(JLabel l, Color c, int px_size) {
108         BufferedImage img = new BufferedImage(px_size, px_size,
109                 BufferedImage.TYPE_INT_ARGB);
110         Graphics2D g = img.createGraphics();
111         g.setColor(c);
112         g.fillRect(0, 0, px_size, px_size);
113         l.setIcon(new ImageIcon(img));
114         return g;
115     }
116
117     public AltosSiteMapTile(int px_size) {
118         setPreferredSize(new Dimension(px_size, px_size));
119
120         mapLabel = new JLabel();
121         fillLabel(mapLabel, Color.GRAY, px_size);
122         mapLabel.setOpaque(true);
123         mapLabel.setBounds(0, 0, px_size, px_size);
124         add(mapLabel, new Integer(0));
125
126         draw = new JLabel();
127         g2d = fillLabel(draw, new Color(127, 127, 127, 0), px_size);
128         draw.setBounds(0, 0, px_size, px_size);
129         draw.setOpaque(false);
130
131         add(draw, new Integer(1));
132     }
133 }
134