altosuilib: Add multiple zoom levels and content types to map
[fw/altos] / altosuilib / 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 org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.swing.*;
23 import javax.imageio.*;
24 import java.awt.geom.*;
25 import java.io.*;
26 import java.util.*;
27 import java.awt.RenderingHints.*;
28 import org.altusmetrum.altoslib_4.*;
29
30 class AltosPoint {
31         Point2D.Double  pt;
32         int             state;
33
34         AltosPoint(Point2D.Double pt, int state) {
35                 this.pt = pt;
36                 this.state = state;
37         }
38 }
39
40 public class AltosSiteMapTile extends JComponent {
41         int px_size;
42         File file;
43         int status;
44
45         Point2D.Double  boost;
46         Point2D.Double  landed;
47
48         LinkedList<AltosPoint>  points;
49
50         public synchronized void queue_repaint() {
51                 SwingUtilities.invokeLater(new Runnable() {
52                                 public void run() {
53                                         repaint();
54                                 }
55                         });
56         }
57
58         public void load_map(File pngFile) {
59                 file = pngFile;
60                 queue_repaint();
61         }
62
63         private Font    font = null;
64
65         public void set_font(Font font) {
66                 this.font = font;
67                 this.status = AltosSiteMapCache.success;
68                 queue_repaint();
69         }
70
71         public void set_status(int status) {
72                 file = null;
73                 this.status = status;
74                 queue_repaint();
75         }
76
77         public void clearMap() {
78                 boost = null;
79                 landed = null;
80                 points = null;
81                 file = null;
82                 status = AltosSiteMapCache.success;
83                 queue_repaint();
84         }
85
86         static Color stateColors[] = {
87                 Color.WHITE,  // startup
88                 Color.WHITE,  // idle
89                 Color.WHITE,  // pad
90                 Color.RED,    // boost
91                 Color.PINK,   // fast
92                 Color.YELLOW, // coast
93                 Color.CYAN,   // drogue
94                 Color.BLUE,   // main
95                 Color.BLACK   // landed
96         };
97
98         private void draw_circle(Graphics g, Point2D.Double pt) {
99                 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
100                 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
101                 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
102         }
103
104         public void set_boost(Point2D.Double boost) {
105                 this.boost = boost;
106                 queue_repaint();
107         }
108
109         public void paint(Graphics g) {
110                 Graphics2D      g2d = (Graphics2D) g;
111                 AltosPoint      prev = null;
112                 Image           img = null;
113
114                 if (file != null)
115                         img = AltosSiteMapCache.get_image(this, file, px_size, px_size);
116
117                 if (img != null) {
118                         g2d.drawImage(img, 0, 0, null);
119                 } else {
120                         g2d.setColor(Color.GRAY);
121                         g2d.fillRect(0, 0, getWidth(), getHeight());
122                         String  message = null;
123                         switch (status) {
124                         case AltosSiteMapCache.loading:
125                                 message = "Loading...";
126                                 break;
127                         case AltosSiteMapCache.bad_request:
128                                 message = "Internal error";
129                                 break;
130                         case AltosSiteMapCache.failed:
131                                 message = "Network error, check connection";
132                                 break;
133                         case AltosSiteMapCache.forbidden:
134                                 message = "Too many requests, try later";
135                                 break;
136                         }
137                         if (message != null && font != null) {
138                                 g2d.setFont(font);
139                                 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
140                                 Rectangle2D     bounds;
141                                 bounds = font.getStringBounds(message, g2d.getFontRenderContext());
142
143                                 float x = getWidth() / 2.0f;
144                                 float y = getHeight() / 2.0f;
145                                 x = x - (float) bounds.getWidth() / 2.0f;
146                                 y = y - (float) bounds.getHeight() / 2.0f;
147                                 g2d.setColor(Color.BLACK);
148                                 g2d.drawString(message, x, y);
149                         }
150                 }
151
152                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
153                                    RenderingHints.VALUE_ANTIALIAS_ON);
154                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
155
156                 if (points != null) {
157                         for (AltosPoint point : points) {
158                                 if (prev != null) {
159                                         if (0 <= point.state && point.state < stateColors.length)
160                                                 g2d.setColor(stateColors[point.state]);
161                                         g2d.draw(new Line2D.Double(prev.pt, point.pt));
162                                 }
163                                 prev = point;
164                         }
165                 }
166                 if (boost != null) {
167                         g2d.setColor(Color.RED);
168                         draw_circle(g2d, boost);
169                 }
170                 if (landed != null) {
171                         g2d.setColor(Color.BLACK);
172                         draw_circle(g2d, landed);
173                 }
174         }
175
176         public synchronized void show(int state, Point2D.Double last_pt, Point2D.Double pt)
177         {
178                 if (points == null)
179                         points = new LinkedList<AltosPoint>();
180
181                 points.add(new AltosPoint(pt, state));
182
183                 if (state == AltosLib.ao_flight_boost && boost == null)
184                         boost = pt;
185                 if (state == AltosLib.ao_flight_landed && landed == null)
186                         landed = pt;
187                 queue_repaint();
188         }
189
190         public AltosSiteMapTile(int in_px_size) {
191                 px_size = in_px_size;
192                 setPreferredSize(new Dimension(px_size, px_size));
193         }
194 }