altosui: Display data for point nearest cursor in map view
[fw/altos] / altoslib / AltosMap.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.altoslib_13;
20
21 import java.io.*;
22 import java.lang.*;
23 import java.util.*;
24 import java.util.concurrent.*;
25
26 public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
27
28         public static final int px_size = 512;
29
30         public static final int maptype_hybrid = 0;
31         public static final int maptype_roadmap = 1;
32         public static final int maptype_satellite = 2;
33         public static final int maptype_terrain = 3;
34         public static final int maptype_default = maptype_hybrid;
35
36         public static final int default_zoom = 15;
37         public static final int min_zoom = 3;
38         public static final int max_zoom = 21;
39
40         public static final String[] maptype_names = {
41                 "hybrid",
42                 "roadmap",
43                 "satellite",
44                 "terrain"
45         };
46
47         public static final String[] maptype_labels = {
48                 "Hybrid",
49                 "Roadmap",
50                 "Satellite",
51                 "Terrain"
52         };
53
54         AltosMapInterface       map_interface;
55         int                     scale;
56
57         AltosMapCache           cache;
58
59         public AltosMapCache cache() { return cache; }
60
61         LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
62
63         AltosMapPath            path;
64         AltosMapLine            line;
65         public AltosLatLon      last_position;
66
67         boolean         have_boost = false;
68         boolean         have_landed = false;
69
70         ConcurrentHashMap<AltosPointInt,AltosMapTile> tiles = new ConcurrentHashMap<AltosPointInt,AltosMapTile>();
71         int             load_radius;
72         AltosLatLon     load_centre = null;
73         AltosMapTileListener    load_listener;
74
75         int             zoom = AltosMap.default_zoom;
76         int             maptype = AltosMap.maptype_default;
77
78         long            user_input_time;
79
80         /* Milliseconds to wait after user action before auto-scrolling
81          */
82         static final long auto_scroll_delay = 20 * 1000;
83
84         public AltosMapTransform        transform;
85         AltosLatLon             centre;
86
87         public void reset() {
88                 // nothing
89         }
90
91         /* MapInterface wrapping functions */
92
93         public void repaint(int x, int y, int w, int h) {
94                 map_interface.repaint(new AltosRectangle(x, y, w, h));
95         }
96
97         public void repaint(AltosMapRectangle damage, int pad) {
98                 AltosRectangle r = transform.screen(damage);
99                 repaint(r.x - pad, r.y - pad, r.width + pad * 2, r.height + pad * 2);
100         }
101
102         public void repaint() {
103                 map_interface.repaint();
104         }
105
106         public int width() {
107                 return map_interface.width();
108         }
109
110         public int height() {
111                 return map_interface.height();
112         }
113
114         public void debug(String format, Object ... arguments) {
115                 map_interface.debug(format, arguments);
116         }
117
118         static public AltosPointInt floor(AltosPointDouble point) {
119                 return new AltosPointInt ((int) Math.floor(point.x / AltosMap.px_size) * AltosMap.px_size,
120                                               (int) Math.floor(point.y / AltosMap.px_size) * AltosMap.px_size);
121         }
122
123         static public AltosPointInt ceil(AltosPointDouble point) {
124                 return new AltosPointInt ((int) Math.ceil(point.x / AltosMap.px_size) * AltosMap.px_size,
125                                               (int) Math.ceil(point.y / AltosMap.px_size) * AltosMap.px_size);
126         }
127
128         public void notice_user_input() {
129                 user_input_time = System.currentTimeMillis();
130         }
131
132         public boolean recent_user_input() {
133                 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
134         }
135
136         public boolean has_centre() {
137                 return centre != null;
138         }
139
140         public boolean far_from_centre(AltosLatLon lat_lon) {
141
142                 if (centre == null || transform == null)
143                         return true;
144
145                 AltosPointDouble        screen = transform.screen(lat_lon);
146
147                 int             width = width();
148                 int             dx = Math.abs ((int) (double) screen.x - width/2);
149
150                 if (dx > width / 4)
151                         return true;
152
153                 int             height = height();
154                 int             dy = Math.abs ((int) (double) screen.y - height/2);
155
156                 if (dy > height / 4)
157                         return true;
158
159                 return false;
160         }
161
162         public void set_transform() {
163                 if (centre != null) {
164                         transform = new AltosMapTransform(width(), height(), zoom, centre);
165                         repaint();
166                 }
167         }
168
169         private void set_zoom_label() {
170                 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
171         }
172
173
174         public boolean set_zoom(int zoom) {
175                 notice_user_input();
176                 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
177                         this.zoom = zoom;
178                         tiles.clear();
179                         set_transform();
180                         set_zoom_label();
181                         return true;
182                 }
183                 return false;
184         }
185
186         public boolean set_zoom_centre(int zoom, AltosPointInt centre) {
187                 AltosLatLon     mouse_lat_lon = null;
188                 boolean         ret;
189
190                 if (transform != null)
191                         mouse_lat_lon = transform.screen_lat_lon(centre);
192
193                 ret = set_zoom(zoom);
194
195                 if (ret && mouse_lat_lon != null) {
196                         AltosPointDouble        new_mouse = transform.screen(mouse_lat_lon);
197
198                         double  dx = width()/2.0 - centre.x;
199                         double  dy = height()/2.0 - centre.y;
200
201                         AltosLatLon     new_centre = transform.screen_lat_lon(new AltosPointDouble(new_mouse.x + dx, new_mouse.y + dy));
202
203                         centre(new_centre);
204                 }
205
206                 return ret;
207         }
208
209         public int get_zoom() {
210                 return zoom;
211         }
212
213         public boolean set_maptype(int maptype) {
214 /*
215                 if (maptype != this.maptype) {
216                         this.maptype = maptype;
217                         tiles.clear();
218                         repaint();
219                         return true;
220                 }
221 */
222                 return false;
223         }
224
225         public void show(AltosGPS gps, double time, int state) {
226
227                 /*
228                  * If insufficient gps data, nothing to update
229                  */
230
231                 if (gps == null)
232                         return;
233
234                 if (!gps.locked && gps.nsat < 4)
235                         return;
236
237                 switch (state) {
238                 case AltosLib.ao_flight_boost:
239                         if (!have_boost) {
240                                 add_mark(gps.lat, gps.lon, state);
241                                 have_boost = true;
242                         }
243                         break;
244                 case AltosLib.ao_flight_landed:
245                         if (!have_landed) {
246                                 add_mark(gps.lat, gps.lon, state);
247                                 have_landed = true;
248                         }
249                         break;
250                 }
251
252                 if (path != null) {
253                         AltosMapRectangle       damage = path.add(gps.lat, gps.lon, time, state);
254
255                         if (damage != null)
256                                 repaint(damage, AltosMapPath.stroke_width);
257                 }
258
259                 last_position = new AltosLatLon(gps.lat, gps.lon);
260
261                 maybe_centre(gps.lat, gps.lon);
262         }
263
264         public void show(AltosState state, AltosListenerState listener_state) {
265                 show(state.gps, state.time, state.state());
266         }
267
268         public void centre(AltosLatLon lat_lon) {
269                 centre = lat_lon;
270                 set_transform();
271         }
272
273         public void centre(double lat, double lon) {
274                 centre(new AltosLatLon(lat, lon));
275         }
276
277         public void centre(AltosGPS gps) {
278                 if (!gps.locked && gps.nsat < 4)
279                         return;
280                 centre(gps.lat, gps.lon);
281         }
282
283         public void centre(AltosState state) {
284                 centre(state.gps);
285         }
286
287         public void maybe_centre(double lat, double lon) {
288                 AltosLatLon     lat_lon = new AltosLatLon(lat, lon);
289                 if (centre == null || (!recent_user_input() && far_from_centre(lat_lon)))
290                         centre(lat_lon);
291         }
292
293         public AltosMapMark add_mark(double lat, double lon, int state) {
294                 AltosMapMark mark;
295                 synchronized(marks) {
296                         mark = map_interface.new_mark(lat, lon, state);
297                         if (mark != null)
298                                 marks.add(mark);
299                 }
300                 repaint();
301                 return mark;
302         }
303
304         public void del_mark(AltosMapMark mark) {
305                 marks.remove(mark);
306         }
307
308         public void clear_marks() {
309                 synchronized(marks) {
310                         marks.clear();
311                 }
312         }
313
314         private void make_tiles() {
315                 AltosPointInt   upper_left;
316                 AltosPointInt   lower_right;
317
318                 if (load_centre != null) {
319                         AltosPointInt centre = floor(transform.point(load_centre));
320
321                         upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
322                                                                centre.y - load_radius * AltosMap.px_size);
323                         lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
324                                                                 centre.y + load_radius * AltosMap.px_size);
325                 } else {
326                         upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
327                         lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
328                 }
329
330                 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
331
332                 while (keyEnumeration.hasMoreElements()) {
333                         AltosPointInt point = keyEnumeration.nextElement();
334                         if (point.x < upper_left.x || lower_right.x < point.x ||
335                             point.y < upper_left.y || lower_right.y < point.y) {
336                                 tiles.remove(point);
337                         }
338                 }
339
340                 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
341
342                 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
343                         for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
344                                 AltosPointInt   point = new AltosPointInt(x, y);
345
346                                 if (!tiles.containsKey(point)) {
347                                         AltosLatLon     ul = transform.lat_lon(point);
348                                         AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
349                                         AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size, scale);
350                                         int status = tile.store.status();
351                                         if (status == AltosMapTile.fetching)
352                                                 debug("Fetching %.6f %.6f %d\n", center.lat, center.lon, zoom);
353                                         tile.add_listener(this);
354                                         tiles.put(point, tile);
355                                 }
356                         }
357                 }
358         }
359
360         public void set_load_params(int new_zoom, int new_type, double lat, double lon, int radius, AltosMapTileListener listener) {
361                 if (AltosMap.min_zoom <= new_zoom && new_zoom <= AltosMap.max_zoom)
362                         zoom = new_zoom;
363 /*              maptype = new_type; */
364                 load_centre = new AltosLatLon(lat, lon);
365                 load_radius = radius;
366                 load_listener = listener;
367                 centre(lat, lon);
368                 tiles.clear();
369                 make_tiles();
370                 repaint();
371         }
372
373         public String getName() {
374                 return "Map";
375         }
376
377         public void paint() {
378                 if (centre != null)
379                         make_tiles();
380
381                 if (transform == null)
382                         return;
383
384                 for (AltosMapTile tile : tiles.values())
385                         tile.paint(transform);
386
387                 synchronized(marks) {
388                         for (AltosMapMark mark : marks)
389                                 mark.paint(transform);
390                 }
391
392                 if (path != null)
393                         path.paint(transform);
394
395                 if (line != null)
396                         line.paint(transform);
397         }
398
399         /* AltosMapTileListener methods */
400         public synchronized void notify_tile(AltosMapTile tile, int status) {
401                 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
402
403                 while (keyEnumeration.hasMoreElements()) {
404                         AltosPointInt point = keyEnumeration.nextElement();
405                         if (tile == tiles.get(point)) {
406                                 AltosPointInt   screen = transform.screen(point);
407                                 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
408                         }
409                 }
410         }
411
412         /* AltosMapStoreListener methods */
413         public synchronized void notify_store(AltosMapStore store, int status) {
414                 if (load_listener != null) {
415                         for (AltosMapTile tile : tiles.values())
416                                 if (store.equals(tile.store))
417                                         load_listener.notify_tile(tile, status);
418                 }
419         }
420
421         /* UI elements */
422
423         AltosPointInt   drag_start;
424
425         boolean         dragged;
426
427         static final double drag_far = 20;
428
429         private void drag(int x, int y) {
430                 if (drag_start == null)
431                         return;
432
433                 int dx = x - drag_start.x;
434                 int dy = y - drag_start.y;
435
436                 double distance = Math.hypot(dx, dy);
437
438                 if (distance > drag_far)
439                         dragged = true;
440
441                 if (transform == null)
442                         return;
443
444                 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
445                 centre(new_centre);
446                 drag_start = new AltosPointInt(x, y);
447         }
448
449         private void drag_start(int x, int y) {
450                 drag_start = new AltosPointInt(x, y);
451                 dragged = false;
452         }
453
454         private void drag_stop(int x, int y) {
455                 if (!dragged) {
456                         if (transform == null) {
457                                 return;
458                         }
459                         map_interface.select_object (transform.screen_lat_lon(new AltosPointInt(x,y)));
460                 }
461         }
462
463         private void line_start(int x, int y) {
464                 if (line != null && transform != null) {
465                         line.pressed(new AltosPointInt(x, y), transform);
466                         repaint();
467                 }
468         }
469
470         private void line(int x, int y) {
471                 if (line != null && transform != null) {
472                         line.dragged(new AltosPointInt(x, y), transform);
473                         repaint();
474                 }
475         }
476
477         public void touch_start(int x, int y, boolean is_drag) {
478                 notice_user_input();
479                 if (is_drag)
480                         drag_start(x, y);
481                 else
482                         line_start(x, y);
483         }
484
485         public void touch_continue(int x, int y, boolean is_drag) {
486                 notice_user_input();
487                 if (is_drag)
488                         drag(x, y);
489                 else
490                         line(x, y);
491         }
492
493         public void touch_stop(int x, int y, boolean is_drag) {
494                 notice_user_input();
495                 if (is_drag)
496                         drag_stop(x, y);
497         }
498
499         public AltosMapPathPoint nearest(int x, int y) {
500                 notice_user_input();
501                 if (path == null)
502                         return null;
503                 AltosLatLon     at = transform.screen_lat_lon(new  AltosPointInt(x, y));
504                 return path.nearest(at);
505         }
506
507         public AltosMap(AltosMapInterface map_interface, int scale) {
508                 this.map_interface = map_interface;
509                 this.scale = scale;
510                 cache = new AltosMapCache(map_interface);
511                 line = map_interface.new_line();
512                 path = map_interface.new_path();
513                 set_zoom_label();
514         }
515
516         public AltosMap(AltosMapInterface map_interface) {
517                 this(map_interface, 1);
518         }
519 }