altosui/altosdroid: Change message in forbidden map tiles
[fw/altos] / altosuilib / AltosUIMap.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altosuilib_13;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.awt.image.*;
24 import javax.swing.*;
25 import java.io.*;
26 import java.lang.Math;
27 import java.awt.geom.*;
28 import java.util.*;
29 import java.util.concurrent.*;
30 import javax.imageio.*;
31 import org.altusmetrum.altoslib_13.*;
32
33 public class AltosUIMap extends JComponent implements AltosFlightDisplay, AltosMapInterface {
34
35         AltosMap        map;
36         Graphics2D      g;
37         Font            tile_font;
38         Font            line_font;
39
40         static Point2D.Double point2d(AltosPointDouble pt) {
41                 return new Point2D.Double(pt.x, pt.y);
42         }
43
44         static final AltosPointDouble point_double(Point pt) {
45                 return new AltosPointDouble(pt.x, pt.y);
46         }
47
48         class MapMark extends AltosMapMark {
49                 public void paint(AltosMapTransform t) {
50                         AltosPointDouble pt = t.screen(lat_lon);
51
52                         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
53                                            RenderingHints.VALUE_ANTIALIAS_ON);
54                         g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
55
56                         if (0 <= state && state < AltosUIMap.stateColors.length)
57                                 g.setColor(AltosUIMap.stateColors[state]);
58                         else
59                                 g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
60
61                         g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
62                         g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
63                         g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
64                 }
65
66                 MapMark(double lat, double lon, int state) {
67                         super(lat, lon, state);
68                 }
69         }
70
71         class MapView extends JComponent implements MouseMotionListener, MouseListener, ComponentListener, MouseWheelListener {
72
73                 private VolatileImage create_back_buffer() {
74                         return getGraphicsConfiguration().createCompatibleVolatileImage(getWidth(), getHeight());
75                 }
76
77                 private void do_paint(Graphics my_g) {
78                         g = (Graphics2D) my_g;
79
80                         map.paint();
81                 }
82
83                 public void paint(Graphics my_g) {
84                         VolatileImage   back_buffer = create_back_buffer();
85
86                         Graphics2D      top_g = (Graphics2D) my_g;
87
88                         do {
89                                 GraphicsConfiguration gc = getGraphicsConfiguration();
90                                 int code = back_buffer.validate(gc);
91                                 if (code == VolatileImage.IMAGE_INCOMPATIBLE)
92                                         back_buffer = create_back_buffer();
93
94                                 Graphics g_back = back_buffer.getGraphics();
95                                 g_back.setClip(top_g.getClip());
96                                 do_paint(g_back);
97                                 g_back.dispose();
98
99                                 top_g.drawImage(back_buffer, 0, 0, this);
100                         } while (back_buffer.contentsLost());
101                         back_buffer.flush();
102                 }
103
104                 public void repaint(AltosRectangle damage) {
105                         repaint(damage.x, damage.y, damage.width, damage.height);
106                 }
107
108                 private boolean is_drag_event(MouseEvent e) {
109                         return e.getModifiersEx() == InputEvent.BUTTON1_DOWN_MASK;
110                 }
111
112                 /* MouseMotionListener methods */
113
114                 public void mouseDragged(MouseEvent e) {
115                         map.touch_continue(e.getPoint().x, e.getPoint().y, is_drag_event(e));
116                 }
117
118                 public void mouseMoved(MouseEvent e) {
119                 }
120
121                 /* MouseListener methods */
122                 public void mouseClicked(MouseEvent e) {
123                 }
124
125                 public void mouseEntered(MouseEvent e) {
126                 }
127
128                 public void mouseExited(MouseEvent e) {
129                 }
130
131                 public void mousePressed(MouseEvent e) {
132                         map.touch_start(e.getPoint().x, e.getPoint().y, is_drag_event(e));
133                 }
134
135                 public void mouseReleased(MouseEvent e) {
136                 }
137
138                 /* MouseWheelListener methods */
139
140                 public void mouseWheelMoved(MouseWheelEvent e) {
141                         int     zoom_change = e.getWheelRotation();
142
143                         map.set_zoom_centre(map.get_zoom() - zoom_change, new AltosPointInt(e.getPoint().x, e.getPoint().y));
144                 }
145
146                 /* ComponentListener methods */
147
148                 public void componentHidden(ComponentEvent e) {
149                 }
150
151                 public void componentMoved(ComponentEvent e) {
152                 }
153
154                 public void componentResized(ComponentEvent e) {
155                         map.set_transform();
156                 }
157
158                 public void componentShown(ComponentEvent e) {
159                         map.set_transform();
160                 }
161
162                 MapView() {
163                         addComponentListener(this);
164                         addMouseMotionListener(this);
165                         addMouseListener(this);
166                         addMouseWheelListener(this);
167                 }
168         }
169
170         class MapLine extends AltosMapLine {
171
172                 public void paint(AltosMapTransform t) {
173
174                         if (start == null || end == null)
175                                 return;
176
177                         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
178
179                         Line2D.Double line = new Line2D.Double(point2d(t.screen(start)),
180                                                                point2d(t.screen(end)));
181
182                         g.setColor(Color.WHITE);
183                         g.setStroke(new BasicStroke(stroke_width+4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
184                         g.draw(line);
185
186                         g.setColor(Color.BLUE);
187                         g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
188                         g.draw(line);
189
190                         String  message = line_dist();
191                         Rectangle2D     bounds;
192                         bounds = line_font.getStringBounds(message, g.getFontRenderContext());
193
194                         float x = (float) line.x1;
195                         float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
196
197                         if (line.x1 < line.x2) {
198                                 x -= (float) bounds.getWidth() + 2.0f;
199                         } else {
200                                 x += 2.0f;
201                         }
202
203                         g.setFont(line_font);
204                         g.setColor(Color.WHITE);
205                         for (int dy = -2; dy <= 2; dy += 2)
206                                 for (int dx = -2; dx <= 2; dx += 2)
207                                         g.drawString(message, x + dx, y + dy);
208                         g.setColor(Color.BLUE);
209                         g.drawString(message, x, y);
210                 }
211
212                 public MapLine() {
213                 }
214         }
215
216         class MapPath extends AltosMapPath {
217                 public void paint(AltosMapTransform t) {
218                         Point2D.Double  prev = null;
219
220                         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
221                                            RenderingHints.VALUE_ANTIALIAS_ON);
222                         g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
223
224                         for (AltosMapPathPoint point : points) {
225                                 Point2D.Double  cur = point2d(t.screen(point.lat_lon));
226                                 if (prev != null) {
227                                         Line2D.Double   line = new Line2D.Double (prev, cur);
228                                         Rectangle       bounds = line.getBounds();
229
230                                         if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
231                                                 if (0 <= point.state && point.state < AltosUIMap.stateColors.length)
232                                                         g.setColor(AltosUIMap.stateColors[point.state]);
233                                                 else
234                                                         g.setColor(AltosUIMap.stateColors[AltosLib.ao_flight_invalid]);
235
236                                                 g.draw(line);
237                                         }
238                                 }
239                                 prev = cur;
240                         }
241                 }
242         }
243
244         class MapTile extends AltosMapTile {
245                 public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
246                         super(cache, upper_left, center, zoom, maptype, px_size, scale);
247                 }
248
249                 public void paint(AltosMapTransform t) {
250
251                         AltosPointDouble        point_double = t.screen(upper_left);
252                         Point                   point = new Point((int) (point_double.x + 0.5),
253                                                                   (int) (point_double.y + 0.5));
254
255                         if (!g.hitClip(point.x, point.y, px_size, px_size))
256                                 return;
257
258                         AltosImage      altos_image = get_image();
259                         AltosUIImage    ui_image = (AltosUIImage) altos_image;
260                         Image           image = null;
261
262                         if (ui_image != null)
263                                 image = ui_image.image;
264
265                         if (image != null) {
266                                 g.drawImage(image, point.x, point.y, null);
267                         } else {
268                                 g.setColor(Color.GRAY);
269                                 g.fillRect(point.x, point.y, px_size, px_size);
270
271                                 if (t.has_location()) {
272                                         String  message = null;
273                                         switch (status) {
274                                         case AltosMapTile.fetching:
275                                                 message = "Fetching...";
276                                                 break;
277                                         case AltosMapTile.bad_request:
278                                                 message = "Internal error";
279                                                 break;
280                                         case AltosMapTile.failed:
281                                                 message = "Network error";
282                                                 break;
283                                         case AltosMapTile.forbidden:
284                                                 message = "Outside of known launch areas";
285                                                 break;
286                                         }
287                                         if (message != null && tile_font != null) {
288                                                 g.setFont(tile_font);
289                                                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
290                                                 Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
291
292                                                 float x = px_size / 2.0f;
293                                                 float y = px_size / 2.0f;
294                                                 x = x - (float) bounds.getWidth() / 2.0f;
295                                                 y = y + (float) bounds.getHeight() / 2.0f;
296                                                 g.setColor(Color.BLACK);
297                                                 g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
298                                         }
299                                 }
300                         }
301                 }
302         }
303
304         public static final Color stateColors[] = {
305                 Color.WHITE,  // startup
306                 Color.WHITE,  // idle
307                 Color.WHITE,  // pad
308                 Color.RED,    // boost
309                 Color.PINK,   // fast
310                 Color.YELLOW, // coast
311                 Color.CYAN,   // drogue
312                 Color.BLUE,   // main
313                 Color.BLACK,  // landed
314                 Color.BLACK,  // invalid
315                 Color.CYAN,   // stateless
316         };
317
318         /* AltosMapInterface functions */
319
320         public AltosMapPath new_path() {
321                 return new MapPath();
322         }
323
324         public AltosMapLine new_line() {
325                 return new MapLine();
326         }
327
328         public AltosImage load_image(File file) throws Exception {
329                 return new AltosUIImage(ImageIO.read(file));
330         }
331
332         public AltosMapMark new_mark(double lat, double lon, int state) {
333                 return new MapMark(lat, lon, state);
334         }
335
336         public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
337                 return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
338         }
339
340         public int width() {
341                 return view.getWidth();
342         }
343
344         public int height() {
345                 return view.getHeight();
346         }
347
348         public void repaint() {
349                 view.repaint();
350         }
351
352         public void repaint(AltosRectangle damage) {
353                 view.repaint(damage);
354         }
355
356         public void set_zoom_label(String label) {
357                 zoom_label.setText(label);
358         }
359
360         public void select_object(AltosLatLon latlon) {
361                 debug("select at %f,%f\n", latlon.lat, latlon.lon);
362         }
363
364         public void debug(String format, Object ... arguments) {
365                 if (AltosUIPreferences.serial_debug())
366                         System.out.printf(format, arguments);
367         }
368
369
370         /* AltosFlightDisplay interface */
371
372         public void set_font() {
373                 tile_font = AltosUILib.value_font;
374                 line_font = AltosUILib.status_font;
375         }
376
377         public void font_size_changed(int font_size) {
378                 set_font();
379                 repaint();
380         }
381
382         public void units_changed(boolean imperial_units) {
383                 repaint();
384         }
385
386         JLabel  zoom_label;
387
388         public void set_maptype(int type) {
389 /*
390                 map.set_maptype(type);
391                 maptype_combo.setSelectedIndex(type);
392 */
393         }
394
395         /* AltosUIMapPreload functions */
396
397         public void set_zoom(int zoom) {
398                 map.set_zoom(zoom);
399         }
400
401         public void add_mark(double lat, double lon, int status) {
402                 map.add_mark(lat, lon, status);
403         }
404
405         public void clear_marks() {
406                 map.clear_marks();
407         }
408
409         /* AltosFlightDisplay interface */
410         public void reset() {
411                 // nothing
412         }
413
414         public void show(AltosState state, AltosListenerState listener_state) {
415                 map.show(state, listener_state);
416         }
417
418         public void show(AltosGPS gps, int state) {
419                 map.show(gps, state);
420         }
421
422         public String getName() {
423                 return "Map";
424         }
425
426         /* AltosGraphUI interface */
427         public void centre(AltosState state) {
428                 map.centre(state);
429         }
430
431         public void centre(AltosGPS gps) {
432                 map.centre(gps);
433         }
434
435         /* internal layout bits */
436         private GridBagLayout layout = new GridBagLayout();
437
438 /*
439         JComboBox<String>       maptype_combo;
440 */
441
442         MapView view;
443
444         public AltosUIMap() {
445
446                 set_font();
447
448                 view = new MapView();
449
450                 view.setPreferredSize(new Dimension(500,500));
451                 view.setVisible(true);
452                 view.setEnabled(true);
453
454                 GridBagLayout   my_layout = new GridBagLayout();
455
456                 setLayout(my_layout);
457
458                 GridBagConstraints c = new GridBagConstraints();
459                 c.anchor = GridBagConstraints.CENTER;
460                 c.fill = GridBagConstraints.BOTH;
461                 c.gridx = 0;
462                 c.gridy = 0;
463                 c.gridwidth = 1;
464                 c.gridheight = 10;
465                 c.weightx = 1;
466                 c.weighty = 1;
467                 add(view, c);
468
469                 int     y = 0;
470
471                 zoom_label = new JLabel("", JLabel.CENTER);
472
473                 c = new GridBagConstraints();
474                 c.anchor = GridBagConstraints.CENTER;
475                 c.fill = GridBagConstraints.HORIZONTAL;
476                 c.gridx = 1;
477                 c.gridy = y++;
478                 c.weightx = 0;
479                 c.weighty = 0;
480                 add(zoom_label, c);
481
482                 JButton zoom_reset = new JButton("0");
483                 zoom_reset.addActionListener(new ActionListener() {
484                                 public void actionPerformed(ActionEvent e) {
485                                         map.set_zoom(map.default_zoom);
486                                 }
487                         });
488
489                 c = new GridBagConstraints();
490                 c.anchor = GridBagConstraints.CENTER;
491                 c.fill = GridBagConstraints.HORIZONTAL;
492                 c.gridx = 1;
493                 c.gridy = y++;
494                 c.weightx = 0;
495                 c.weighty = 0;
496                 add(zoom_reset, c);
497
498                 JButton zoom_in = new JButton("+");
499                 zoom_in.addActionListener(new ActionListener() {
500                                 public void actionPerformed(ActionEvent e) {
501                                         map.set_zoom(map.get_zoom() + 1);
502                                 }
503                         });
504
505                 c = new GridBagConstraints();
506                 c.anchor = GridBagConstraints.CENTER;
507                 c.fill = GridBagConstraints.HORIZONTAL;
508                 c.gridx = 1;
509                 c.gridy = y++;
510                 c.weightx = 0;
511                 c.weighty = 0;
512                 add(zoom_in, c);
513
514                 JButton zoom_out = new JButton("-");
515                 zoom_out.addActionListener(new ActionListener() {
516                                 public void actionPerformed(ActionEvent e) {
517                                         map.set_zoom(map.get_zoom() - 1);
518                                 }
519                         });
520                 c = new GridBagConstraints();
521                 c.anchor = GridBagConstraints.CENTER;
522                 c.fill = GridBagConstraints.HORIZONTAL;
523                 c.gridx = 1;
524                 c.gridy = y++;
525                 c.weightx = 0;
526                 c.weighty = 0;
527                 add(zoom_out, c);
528
529 /*
530                 maptype_combo = new JComboBox<String>(map.maptype_labels);
531
532                 maptype_combo.setEditable(false);
533                 maptype_combo.setMaximumRowCount(maptype_combo.getItemCount());
534                 maptype_combo.addItemListener(new ItemListener() {
535                                 public void itemStateChanged(ItemEvent e) {
536                                         map.set_maptype(maptype_combo.getSelectedIndex());
537                                 }
538                         });
539
540                 c = new GridBagConstraints();
541                 c.anchor = GridBagConstraints.CENTER;
542                 c.fill = GridBagConstraints.HORIZONTAL;
543                 c.gridx = 1;
544                 c.gridy = y++;
545                 c.weightx = 0;
546                 c.weighty = 0;
547                 add(maptype_combo, c);
548 */
549                 map = new AltosMap(this);
550         }
551 }