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