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