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