altosuilib: Get rid of AltosUIMapNew.set_load_params
[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.notice_user_input();
143                         AltosLatLon     mouse_lat_lon = map.transform.screen_lat_lon(new AltosPointInt(e.getPoint().x, e.getPoint().y));
144                         map.set_zoom(map.get_zoom() - zoom_change);
145
146                         AltosPointDouble        new_mouse = map.transform.screen(mouse_lat_lon);
147
148                         int     dx = getWidth()/2 - e.getPoint().x;
149                         int     dy = getHeight()/2 - e.getPoint().y;
150
151                         AltosLatLon     new_centre = map.transform.screen_lat_lon(new AltosPointInt((int) new_mouse.x + dx, (int) new_mouse.y + dy));
152
153                         map.centre(new_centre);
154                 }
155
156                 /* ComponentListener methods */
157
158                 public void componentHidden(ComponentEvent e) {
159                 }
160
161                 public void componentMoved(ComponentEvent e) {
162                 }
163
164                 public void componentResized(ComponentEvent e) {
165                         map.set_transform();
166                 }
167
168                 public void componentShown(ComponentEvent e) {
169                         map.set_transform();
170                 }
171
172                 MapView() {
173                         addComponentListener(this);
174                         addMouseMotionListener(this);
175                         addMouseListener(this);
176                         addMouseWheelListener(this);
177                 }
178         }
179
180         class MapLine extends AltosMapLine {
181
182                 public void paint(AltosMapTransform t) {
183
184                         if (start == null || end == null)
185                                 return;
186
187                         g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
188
189                         Line2D.Double line = new Line2D.Double(point2d(t.screen(start)),
190                                                                point2d(t.screen(end)));
191
192                         g.setColor(Color.WHITE);
193                         g.setStroke(new BasicStroke(stroke_width+4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
194                         g.draw(line);
195
196                         g.setColor(Color.BLUE);
197                         g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
198                         g.draw(line);
199
200                         String  message = line_dist();
201                         Rectangle2D     bounds;
202                         bounds = line_font.getStringBounds(message, g.getFontRenderContext());
203
204                         float x = (float) line.x1;
205                         float y = (float) line.y1 + (float) bounds.getHeight() / 2.0f;
206
207                         if (line.x1 < line.x2) {
208                                 x -= (float) bounds.getWidth() + 2.0f;
209                         } else {
210                                 x += 2.0f;
211                         }
212
213                         g.setFont(line_font);
214                         g.setColor(Color.WHITE);
215                         for (int dy = -2; dy <= 2; dy += 2)
216                                 for (int dx = -2; dx <= 2; dx += 2)
217                                         g.drawString(message, x + dx, y + dy);
218                         g.setColor(Color.BLUE);
219                         g.drawString(message, x, y);
220                 }
221
222                 public MapLine() {
223                 }
224         }
225
226         class MapPath extends AltosMapPath {
227                 public void paint(AltosMapTransform t) {
228                         Point2D.Double  prev = null;
229
230                         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
231                                            RenderingHints.VALUE_ANTIALIAS_ON);
232                         g.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
233
234                         for (AltosMapPathPoint point : points) {
235                                 Point2D.Double  cur = point2d(t.screen(point.lat_lon));
236                                 if (prev != null) {
237                                         Line2D.Double   line = new Line2D.Double (prev, cur);
238                                         Rectangle       bounds = line.getBounds();
239
240                                         if (g.hitClip(bounds.x, bounds.y, bounds.width, bounds.height)) {
241                                                 if (0 <= point.state && point.state < AltosUIMapNew.stateColors.length)
242                                                         g.setColor(AltosUIMapNew.stateColors[point.state]);
243                                                 else
244                                                         g.setColor(AltosUIMapNew.stateColors[AltosLib.ao_flight_invalid]);
245
246                                                 g.draw(line);
247                                         }
248                                 }
249                                 prev = cur;
250                         }
251                 }
252         }
253
254         class MapTile extends AltosMapTile {
255                 public MapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
256                         super(listener, upper_left, center, zoom, maptype, px_size);
257                 }
258
259                 public void paint(AltosMapTransform t) {
260
261                         AltosPointDouble        point_double = t.screen(upper_left);
262                         Point                   point = new Point((int) (point_double.x + 0.5),
263                                                                   (int) (point_double.y + 0.5));
264
265                         if (!g.hitClip(point.x, point.y, px_size, px_size))
266                                 return;
267
268                         AltosImage altos_image = cache.get(this, store, px_size, px_size);
269
270                         AltosUIImage    ui_image = (AltosUIImage) altos_image;
271
272                         Image image = null;
273
274                         if (ui_image != null)
275                                 image = ui_image.image;
276
277                         if (image != null) {
278                                 g.drawImage(image, point.x, point.y, null);
279                         } else {
280                                 g.setColor(Color.GRAY);
281                                 g.fillRect(point.x, point.y, px_size, px_size);
282
283                                 if (t.has_location()) {
284                                         String  message = null;
285                                         switch (status) {
286                                         case AltosMapTile.loading:
287                                                 message = "Loading...";
288                                                 break;
289                                         case AltosMapTile.bad_request:
290                                                 message = "Internal error";
291                                                 break;
292                                         case AltosMapTile.failed:
293                                                 message = "Network error, check connection";
294                                                 break;
295                                         case AltosMapTile.forbidden:
296                                                 message = "Too many requests, try later";
297                                                 break;
298                                         }
299                                         if (message != null && tile_font != null) {
300                                                 g.setFont(tile_font);
301                                                 g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
302                                                 Rectangle2D bounds = tile_font.getStringBounds(message, g.getFontRenderContext());
303
304                                                 float x = px_size / 2.0f;
305                                                 float y = px_size / 2.0f;
306                                                 x = x - (float) bounds.getWidth() / 2.0f;
307                                                 y = y + (float) bounds.getHeight() / 2.0f;
308                                                 g.setColor(Color.BLACK);
309                                                 g.drawString(message, (float) point_double.x + x, (float) point_double.y + y);
310                                         }
311                                 }
312                         }
313                 }
314         }
315
316         public static final Color stateColors[] = {
317                 Color.WHITE,  // startup
318                 Color.WHITE,  // idle
319                 Color.WHITE,  // pad
320                 Color.RED,    // boost
321                 Color.PINK,   // fast
322                 Color.YELLOW, // coast
323                 Color.CYAN,   // drogue
324                 Color.BLUE,   // main
325                 Color.BLACK,  // landed
326                 Color.BLACK,  // invalid
327                 Color.CYAN,   // stateless
328         };
329
330         /* AltosMapInterface functions */
331
332         public AltosMapPath new_path() {
333                 return new MapPath();
334         }
335
336         public AltosMapLine new_line() {
337                 return new MapLine();
338         }
339
340         public AltosImage load_image(File file) throws Exception {
341                 return new AltosUIImage(ImageIO.read(file));
342         }
343
344         public AltosMapMark new_mark(double lat, double lon, int state) {
345                 return new MapMark(lat, lon, state);
346         }
347
348         public AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
349                 return new MapTile(listener, upper_left, center, zoom, maptype, px_size);
350         }
351
352         public int width() {
353                 return view.getWidth();
354         }
355
356         public int height() {
357                 return view.getHeight();
358         }
359
360         public void repaint() {
361                 view.repaint();
362         }
363
364         public void repaint(AltosRectangle damage) {
365                 view.repaint(damage);
366         }
367
368         public void set_zoom_label(String label) {
369                 zoom_label.setText(label);
370         }
371
372         /* AltosFlightDisplay interface */
373
374         public void set_font() {
375                 tile_font = AltosUILib.value_font;
376                 line_font = AltosUILib.status_font;
377         }
378
379         public void font_size_changed(int font_size) {
380                 set_font();
381                 repaint();
382         }
383
384         public void units_changed(boolean imperial_units) {
385                 repaint();
386         }
387
388         JLabel  zoom_label;
389
390         public void set_maptype(int type) {
391                 map.set_maptype(type);
392                 maptype_combo.setSelectedIndex(type);
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 String getName() {
419                 return "Map";
420         }
421
422         /* AltosGraphUI interface */
423         public void centre(AltosState state) {
424                 map.centre(state);
425         }
426
427         /* internal layout bits */
428         private GridBagLayout layout = new GridBagLayout();
429
430         JComboBox<String>       maptype_combo;
431
432         MapView view;
433
434         public AltosUIMapNew() {
435
436                 set_font();
437
438                 view = new MapView();
439
440                 view.setPreferredSize(new Dimension(500,500));
441                 view.setVisible(true);
442                 view.setEnabled(true);
443
444                 GridBagLayout   my_layout = new GridBagLayout();
445
446                 setLayout(my_layout);
447
448                 GridBagConstraints c = new GridBagConstraints();
449                 c.anchor = GridBagConstraints.CENTER;
450                 c.fill = GridBagConstraints.BOTH;
451                 c.gridx = 0;
452                 c.gridy = 0;
453                 c.gridwidth = 1;
454                 c.gridheight = 10;
455                 c.weightx = 1;
456                 c.weighty = 1;
457                 add(view, c);
458
459                 int     y = 0;
460
461                 zoom_label = new JLabel("", JLabel.CENTER);
462
463                 c = new GridBagConstraints();
464                 c.anchor = GridBagConstraints.CENTER;
465                 c.fill = GridBagConstraints.HORIZONTAL;
466                 c.gridx = 1;
467                 c.gridy = y++;
468                 c.weightx = 0;
469                 c.weighty = 0;
470                 add(zoom_label, c);
471
472                 JButton zoom_reset = new JButton("0");
473                 zoom_reset.addActionListener(new ActionListener() {
474                                 public void actionPerformed(ActionEvent e) {
475                                         map.set_zoom(map.default_zoom);
476                                 }
477                         });
478
479                 c = new GridBagConstraints();
480                 c.anchor = GridBagConstraints.CENTER;
481                 c.fill = GridBagConstraints.HORIZONTAL;
482                 c.gridx = 1;
483                 c.gridy = y++;
484                 c.weightx = 0;
485                 c.weighty = 0;
486                 add(zoom_reset, c);
487
488                 JButton zoom_in = new JButton("+");
489                 zoom_in.addActionListener(new ActionListener() {
490                                 public void actionPerformed(ActionEvent e) {
491                                         map.set_zoom(map.get_zoom() + 1);
492                                 }
493                         });
494
495                 c = new GridBagConstraints();
496                 c.anchor = GridBagConstraints.CENTER;
497                 c.fill = GridBagConstraints.HORIZONTAL;
498                 c.gridx = 1;
499                 c.gridy = y++;
500                 c.weightx = 0;
501                 c.weighty = 0;
502                 add(zoom_in, c);
503
504                 JButton zoom_out = new JButton("-");
505                 zoom_out.addActionListener(new ActionListener() {
506                                 public void actionPerformed(ActionEvent e) {
507                                         map.set_zoom(map.get_zoom() - 1);
508                                 }
509                         });
510                 c = new GridBagConstraints();
511                 c.anchor = GridBagConstraints.CENTER;
512                 c.fill = GridBagConstraints.HORIZONTAL;
513                 c.gridx = 1;
514                 c.gridy = y++;
515                 c.weightx = 0;
516                 c.weighty = 0;
517                 add(zoom_out, c);
518
519                 maptype_combo = new JComboBox<String>(map.maptype_labels);
520
521                 maptype_combo.setEditable(false);
522                 maptype_combo.setMaximumRowCount(maptype_combo.getItemCount());
523                 maptype_combo.addItemListener(new ItemListener() {
524                                 public void itemStateChanged(ItemEvent e) {
525                                         map.set_maptype(maptype_combo.getSelectedIndex());
526                                 }
527                         });
528
529                 c = new GridBagConstraints();
530                 c.anchor = GridBagConstraints.CENTER;
531                 c.fill = GridBagConstraints.HORIZONTAL;
532                 c.gridx = 1;
533                 c.gridy = y++;
534                 c.weightx = 0;
535                 c.weighty = 0;
536                 add(maptype_combo, c);
537
538                 map = new AltosMap(this);
539         }
540 }