2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altosuilib_14;
22 import java.awt.event.*;
23 import java.awt.image.*;
26 import java.lang.Math;
27 import java.awt.geom.*;
29 import java.util.concurrent.*;
30 import javax.imageio.*;
31 import org.altusmetrum.altoslib_14.*;
33 public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosMapInterface {
39 AltosMapMark nearest_mark;
41 static Point2D.Double point2d(AltosPointDouble pt) {
42 return new Point2D.Double(pt.x, pt.y);
45 static final AltosPointDouble point_double(Point pt) {
46 return new AltosPointDouble(pt.x, pt.y);
49 class MapMark extends AltosMapMark {
50 public void paint(AltosMapTransform t) {
51 AltosPointDouble pt = t.screen(lat_lon);
53 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
54 RenderingHints.VALUE_ANTIALIAS_ON);
55 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
57 if (0 <= state && state < AltosUIMap.stateColors.length)
58 g.setColor(AltosUIMap.stateColors[state]);
60 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
62 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
63 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
64 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
67 MapMark(double lat, double lon, int state) {
68 super(lat, lon, state);
72 class MapView extends JComponent implements MouseMotionListener, MouseListener, ComponentListener, MouseWheelListener {
74 private VolatileImage create_back_buffer() {
75 return getGraphicsConfiguration().createCompatibleVolatileImage(getWidth(), getHeight());
78 private void do_paint(Graphics my_g) {
79 g = (Graphics2D) my_g;
84 public void paint(Graphics my_g) {
85 VolatileImage back_buffer = create_back_buffer();
87 Graphics2D top_g = (Graphics2D) my_g;
90 GraphicsConfiguration gc = getGraphicsConfiguration();
91 int code = back_buffer.validate(gc);
92 if (code == VolatileImage.IMAGE_INCOMPATIBLE)
93 back_buffer = create_back_buffer();
95 Graphics g_back = back_buffer.getGraphics();
96 g_back.setClip(top_g.getClip());
100 top_g.drawImage(back_buffer, 0, 0, this);
101 } while (back_buffer.contentsLost());
105 public void repaint(AltosRectangle damage) {
106 repaint(damage.x, damage.y, damage.width, damage.height);
109 private boolean is_drag_event(MouseEvent e) {
110 return e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK;
113 /* MouseMotionListener methods */
115 public void mouseDragged(MouseEvent e) {
116 map.touch_continue(e.getPoint().x, e.getPoint().y, is_drag_event(e));
119 String pos(double p, String pos, String neg) {
120 if (p == AltosLib.MISSING)
127 int deg = (int) Math.floor(p);
128 double min = (p - Math.floor(p)) * 60.0;
129 return String.format("%s %4d° %9.6f'", h, deg, min);
132 String height(double h, String label) {
133 if (h == AltosLib.MISSING)
135 return String.format(" %s%s",
136 AltosConvert.height.show(6, h),
140 String speed(double s, String label) {
141 if (s == AltosLib.MISSING)
143 return String.format(" %s%s",
144 AltosConvert.speed.show(6, s),
148 public void mouseMoved(MouseEvent e) {
149 AltosMapPathPoint point = map.nearest(e.getPoint().x, e.getPoint().y);
152 if (nearest_mark == null)
153 nearest_mark = map.add_mark(point.gps.lat,
157 nearest_mark.lat_lon.lat = point.gps.lat;
158 nearest_mark.lat_lon.lon = point.gps.lon;
159 nearest_mark.state = point.state;
161 nearest_label.setText(String.format("%9.2f sec %s%s%s%s",
167 height(point.gps_height, ""),
168 speed(point.gps.ground_speed, "(h)"),
169 speed(point.gps.climb_rate, "(v)")));
171 nearest_label.setText("");
176 /* MouseListener methods */
177 public void mouseClicked(MouseEvent e) {
180 public void mouseEntered(MouseEvent e) {
183 public void mouseExited(MouseEvent e) {
186 public void mousePressed(MouseEvent e) {
187 map.touch_start(e.getPoint().x, e.getPoint().y, is_drag_event(e));
190 public void mouseReleased(MouseEvent e) {
193 /* MouseWheelListener methods */
195 public void mouseWheelMoved(MouseWheelEvent e) {
196 int zoom_change = e.getWheelRotation();
198 map.set_zoom_centre(map.get_zoom() - zoom_change, new AltosPointInt(e.getPoint().x, e.getPoint().y));
201 /* ComponentListener methods */
203 public void componentHidden(ComponentEvent e) {
206 public void componentMoved(ComponentEvent e) {
209 public void componentResized(ComponentEvent e) {
213 public void componentShown(ComponentEvent e) {
218 addComponentListener(this);
219 addMouseMotionListener(this);
220 addMouseListener(this);
221 addMouseWheelListener(this);
225 class MapLine extends AltosMapLine {
227 public void paint(AltosMapTransform t) {
229 if (start == null || end == null)
232 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
234 Line2D.Double line = new Line2D.Double(point2d(t.screen(start)),
235 point2d(t.screen(end)));
237 g.setColor(Color.WHITE);
238 g.setStroke(new BasicStroke(stroke_width+4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
241 g.setColor(Color.BLUE);
242 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
245 String message = line_dist();
247 bounds = line_font.getStringBounds(message, g.getFontRenderContext());
249 float x = (float) line.x1;
250 float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
252 if (line.x1 < line.x2) {
253 x -= (float) bounds.getWidth() + 2.0f;
258 g.setFont(line_font);
259 g.setColor(Color.WHITE);
260 for (int dy = -2; dy <= 2; dy += 2)
261 for (int dx = -2; dx <= 2; dx += 2)
262 g.drawString(message, x + dx, y + dy);
263 g.setColor(Color.BLUE);
264 g.drawString(message, x, y);
271 class MapPath extends AltosMapPath {
272 public void paint(AltosMapTransform t) {
273 Point2D.Double prev = null;
275 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
276 RenderingHints.VALUE_ANTIALIAS_ON);
277 g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
279 for (AltosMapPathPoint point : points) {
280 Point2D.Double cur = point2d(t.screen(point.gps.lat, point.gps.lon));
282 Line2D.Double line = new Line2D.Double (prev, cur);
283 Rectangle bounds = line.getBounds();
285 if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
286 if (0 <= point.state && point.state < AltosUIMap.stateColors.length)
287 g.setColor(AltosUIMap.stateColors[point.state]);
289 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
299 class MapTile extends AltosMapTile {
300 public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
301 super(cache, upper_left, center, zoom, maptype, px_size, scale);
304 public void paint(AltosMapTransform t) {
306 AltosPointDouble point_double = t.screen(upper_left);
307 Point point = new Point((int) (point_double.x + 0.5),
308 (int) (point_double.y + 0.5));
310 if (!g.hitClip(point.x, point.y, px_size, px_size))
313 AltosImage altos_image = get_image();
314 AltosUIImage ui_image = (AltosUIImage) altos_image;
317 if (ui_image != null)
318 image = ui_image.image;
321 g.drawImage(image, point.x, point.y, null);
323 * Useful when debugging map fetching problems
325 String message = String.format("%.6f %.6f", center.lat, center.lon);
326 g.setFont(tile_font);
327 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
328 Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
330 float x = px_size / 2.0f;
331 float y = px_size / 2.0f;
332 x = x - (float) bounds.getWidth() / 2.0f;
333 y = y + (float) bounds.getHeight() / 2.0f;
334 g.setColor(Color.RED);
335 g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
338 g.setColor(Color.GRAY);
339 g.fillRect(point.x, point.y, px_size, px_size);
341 if (t.has_location()) {
342 String message = null;
344 case AltosMapTile.fetching:
345 message = "Fetching...";
347 case AltosMapTile.bad_request:
348 message = "Internal error";
350 case AltosMapTile.failed:
351 message = "Network error";
353 case AltosMapTile.forbidden:
354 message = "Outside of known launch areas";
357 if (message != null && tile_font != null) {
358 g.setFont(tile_font);
359 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
360 Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
362 float x = px_size / 2.0f;
363 float y = px_size / 2.0f;
364 x = x - (float) bounds.getWidth() / 2.0f;
365 y = y + (float) bounds.getHeight() / 2.0f;
366 g.setColor(Color.BLACK);
367 g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
374 public static final Color stateColors[] = {
375 Color.WHITE, // startup
380 Color.YELLOW, // coast
381 Color.CYAN, // drogue
383 Color.BLACK, // landed
384 Color.BLACK, // invalid
385 Color.CYAN, // stateless
388 /* AltosMapInterface functions */
390 public AltosMapPath new_path() {
391 return new MapPath();
394 public AltosMapLine new_line() {
395 return new MapLine();
398 public AltosImage load_image(File file) throws Exception {
399 return new AltosUIImage(ImageIO.read(file));
402 public AltosMapMark new_mark(double lat, double lon, int state) {
403 return new MapMark(lat, lon, state);
406 public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
407 return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
411 return view.getWidth();
414 public int height() {
415 return view.getHeight();
418 public void repaint() {
422 public void repaint(AltosRectangle damage) {
423 view.repaint(damage);
426 public void set_zoom_label(String label) {
427 zoom_label.setText(label);
430 public void select_object(AltosLatLon latlon) {
431 debug("select at %f,%f\n", latlon.lat, latlon.lon);
434 public void debug(String format, Object ... arguments) {
435 if (AltosUIPreferences.serial_debug())
436 System.out.printf(format, arguments);
440 /* AltosFlightDisplay interface */
442 public void set_font() {
443 tile_font = AltosUILib.value_font;
444 line_font = AltosUILib.status_font;
445 if (nearest_label != null)
446 nearest_label.setFont(AltosUILib.value_font);
449 public void font_size_changed(int font_size) {
454 public void units_changed(boolean imperial_units) {
460 JLabel nearest_label;
462 public void set_maptype(int type) {
464 map.set_maptype(type);
465 maptype_combo.setSelectedIndex(type);
469 /* AltosUIMapPreload functions */
471 public void set_zoom(int zoom) {
475 public void add_mark(double lat, double lon, int status) {
476 map.add_mark(lat, lon, status);
479 public void clear_marks() {
483 /* AltosFlightDisplay interface */
484 public void reset() {
488 public void show(AltosState state, AltosListenerState listener_state) {
489 map.show(state, listener_state);
492 public void show(AltosGPS gps, double time, int state, double gps_height) {
493 map.show(gps, time, state, gps_height);
496 public String getName() {
500 /* AltosGraphUI interface */
501 public void centre(AltosState state) {
505 public void centre(AltosGPS gps) {
509 /* internal layout bits */
510 private GridBagLayout layout = new GridBagLayout();
513 JComboBox<String> maptype_combo;
518 public AltosUIMap() {
522 view = new MapView();
524 view.setPreferredSize(new Dimension(500,500));
525 view.setVisible(true);
526 view.setEnabled(true);
528 GridBagLayout my_layout = new GridBagLayout();
530 setLayout(my_layout);
532 GridBagConstraints c = new GridBagConstraints();
533 c.anchor = GridBagConstraints.CENTER;
534 c.fill = GridBagConstraints.BOTH;
545 zoom_label = new JLabel("", JLabel.CENTER);
547 c = new GridBagConstraints();
548 c.anchor = GridBagConstraints.CENTER;
549 c.fill = GridBagConstraints.HORIZONTAL;
556 JButton zoom_reset = new JButton("0");
557 zoom_reset.addActionListener(new ActionListener() {
558 public void actionPerformed(ActionEvent e) {
559 map.set_zoom(map.default_zoom);
563 c = new GridBagConstraints();
564 c.anchor = GridBagConstraints.CENTER;
565 c.fill = GridBagConstraints.HORIZONTAL;
572 JButton zoom_in = new JButton("+");
573 zoom_in.addActionListener(new ActionListener() {
574 public void actionPerformed(ActionEvent e) {
575 map.set_zoom(map.get_zoom() + 1);
579 c = new GridBagConstraints();
580 c.anchor = GridBagConstraints.CENTER;
581 c.fill = GridBagConstraints.HORIZONTAL;
588 JButton zoom_out = new JButton("-");
589 zoom_out.addActionListener(new ActionListener() {
590 public void actionPerformed(ActionEvent e) {
591 map.set_zoom(map.get_zoom() - 1);
594 c = new GridBagConstraints();
595 c.anchor = GridBagConstraints.CENTER;
596 c.fill = GridBagConstraints.HORIZONTAL;
604 nearest_label = new JLabel("", JLabel.LEFT);
605 nearest_label.setFont(tile_font);
607 c = new GridBagConstraints();
608 c.anchor = GridBagConstraints.CENTER;
609 c.fill = GridBagConstraints.HORIZONTAL;
616 add(nearest_label, c);
618 maptype_combo = new JComboBox<String>(map.maptype_labels);
620 maptype_combo.setEditable(false);
621 maptype_combo.setMaximumRowCount(maptype_combo.getItemCount());
622 maptype_combo.addItemListener(new ItemListener() {
623 public void itemStateChanged(ItemEvent e) {
624 map.set_maptype(maptype_combo.getSelectedIndex());
628 c = new GridBagConstraints();
629 c.anchor = GridBagConstraints.CENTER;
630 c.fill = GridBagConstraints.HORIZONTAL;
635 add(maptype_combo, c);
637 map = new AltosMap(this);