Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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         static 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         static 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 has_centre() {
135                 return centre != null;
136         }
137
138         public boolean far_from_centre(AltosLatLon lat_lon) {
139
140                 if (centre == null || transform == null)
141                         return true;
142
143                 AltosPointDouble        screen = transform.screen(lat_lon);
144
145                 int             width = width();
146                 int             dx = Math.abs ((int) (double) screen.x - width/2);
147
148                 if (dx > width / 4)
149                         return true;
150
151                 int             height = height();
152                 int             dy = Math.abs ((int) (double) screen.y - height/2);
153
154                 if (dy > height / 4)
155                         return true;
156
157                 return false;
158         }
159
160         public void set_transform() {
161                 if (centre != null) {
162                         transform = new AltosMapTransform(width(), height(), zoom, centre);
163                         repaint();
164                 }
165         }
166
167         private void set_zoom_label() {
168                 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
169         }
170
171
172         public boolean set_zoom(int zoom) {
173                 notice_user_input();
174                 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
175                         this.zoom = zoom;
176                         tiles.clear();
177                         set_transform();
178                         set_zoom_label();
179                         return true;
180                 }
181                 return false;
182         }
183
184         public boolean set_zoom_centre(int zoom, AltosPointInt centre) {
185                 AltosLatLon     mouse_lat_lon = null;
186                 boolean         ret;
187
188                 if (transform != null)
189                         mouse_lat_lon = transform.screen_lat_lon(centre);
190
191                 ret = set_zoom(zoom);
192
193                 if (ret && mouse_lat_lon != null) {
194                         AltosPointDouble        new_mouse = transform.screen(mouse_lat_lon);
195
196                         double  dx = width()/2.0 - centre.x;
197                         double  dy = height()/2.0 - centre.y;
198
199                         AltosLatLon     new_centre = transform.screen_lat_lon(new AltosPointDouble(new_mouse.x + dx, new_mouse.y + dy));
200
201                         centre(new_centre);
202                 }
203
204                 return ret;
205         }
206
207         public int get_zoom() {
208                 return zoom;
209         }
210
211         public boolean set_maptype(int maptype) {
212                 if (maptype != this.maptype) {
213                         this.maptype = maptype;
214                         tiles.clear();
215                         repaint();
216                         return true;
217                 }
218                 return false;
219         }
220
221         public void show(AltosState state, AltosListenerState listener_state) {
222
223                 /* If insufficient gps data, nothing to update
224                  */
225                 AltosGPS        gps = state.gps;
226
227                 if (gps == null)
228                         return;
229
230                 if (!gps.locked && gps.nsat < 4)
231                         return;
232
233                 switch (state.state) {
234                 case AltosLib.ao_flight_boost:
235                         if (!have_boost) {
236                                 add_mark(gps.lat, gps.lon, state.state);
237                                 have_boost = true;
238                         }
239                         break;
240                 case AltosLib.ao_flight_landed:
241                         if (!have_landed) {
242                                 add_mark(gps.lat, gps.lon, state.state);
243                                 have_landed = true;
244                         }
245                         break;
246                 }
247
248                 if (path != null) {
249                         AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state);
250
251                         if (damage != null)
252                                 repaint(damage, AltosMapPath.stroke_width);
253                 }
254
255                 last_position = new AltosLatLon(gps.lat, gps.lon);
256
257                 maybe_centre(gps.lat, gps.lon);
258         }
259
260         public void centre(AltosLatLon lat_lon) {
261                 centre = lat_lon;
262                 set_transform();
263         }
264
265         public void centre(double lat, double lon) {
266                 centre(new AltosLatLon(lat, lon));
267         }
268
269         public void centre(AltosState state) {
270                 if (!state.gps.locked && state.gps.nsat < 4)
271                         return;
272                 centre(state.gps.lat, state.gps.lon);
273         }
274
275         public void maybe_centre(double lat, double lon) {
276                 AltosLatLon     lat_lon = new AltosLatLon(lat, lon);
277                 if (centre == null || (!recent_user_input() && far_from_centre(lat_lon)))
278                         centre(lat_lon);
279         }
280
281         public void add_mark(double lat, double lon, int state) {
282                 synchronized(marks) {
283                         AltosMapMark mark = map_interface.new_mark(lat, lon, state);
284                         if (mark != null)
285                                 marks.add(mark);
286                 }
287                 repaint();
288         }
289
290         public void clear_marks() {
291                 synchronized(marks) {
292                         marks.clear();
293                 }
294         }
295
296         private void make_tiles() {
297                 AltosPointInt   upper_left;
298                 AltosPointInt   lower_right;
299
300                 if (load_centre != null) {
301                         AltosPointInt centre = floor(transform.point(load_centre));
302
303                         upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
304                                                                centre.y - load_radius * AltosMap.px_size);
305                         lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
306                                                                 centre.y + load_radius * AltosMap.px_size);
307                 } else {
308                         upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
309                         lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
310                 }
311                 for (AltosPointInt point : tiles.keySet()) {
312                         if (point.x < upper_left.x || lower_right.x < point.x ||
313                             point.y < upper_left.y || lower_right.y < point.y) {
314                                 tiles.remove(point);
315                         }
316                 }
317
318                 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
319
320                 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
321                         for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
322                                 AltosPointInt   point = new AltosPointInt(x, y);
323
324                                 if (!tiles.containsKey(point)) {
325                                         AltosLatLon     ul = transform.lat_lon(point);
326                                         AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
327                                         AltosMapTile tile = map_interface.new_tile(this, ul, center, zoom, maptype, px_size);
328                                         tiles.put(point, tile);
329                                 }
330                         }
331                 }
332         }
333
334         public void set_load_params(int new_zoom, int new_type, double lat, double lon, int radius, AltosMapTileListener listener) {
335                 if (AltosMap.min_zoom <= new_zoom && new_zoom <= AltosMap.max_zoom)
336                         zoom = new_zoom;
337                 maptype = new_type;
338                 load_centre = new AltosLatLon(lat, lon);
339                 load_radius = radius;
340                 load_listener = listener;
341                 centre(lat, lon);
342                 tiles.clear();
343                 make_tiles();
344                 for (AltosMapTile tile : tiles.values()) {
345                         tile.add_store_listener(this);
346                         if (tile.store_status() != AltosMapTile.loading)
347                                 listener.notify_tile(tile, tile.store_status());
348                 }
349                 repaint();
350         }
351
352         public String getName() {
353                 return "Map";
354         }
355
356         public void paint() {
357                 if (centre != null)
358                         make_tiles();
359
360                 if (transform == null)
361                         return;
362
363                 for (AltosMapTile tile : tiles.values())
364                         tile.paint(transform);
365
366                 synchronized(marks) {
367                         for (AltosMapMark mark : marks)
368                                 mark.paint(transform);
369                 }
370
371                 if (path != null)
372                         path.paint(transform);
373
374                 if (line != null)
375                         line.paint(transform);
376         }
377
378         /* AltosMapTileListener methods */
379         public synchronized void notify_tile(AltosMapTile tile, int status) {
380                 for (AltosPointInt point : tiles.keySet()) {
381                         if (tile == tiles.get(point)) {
382                                 AltosPointInt   screen = transform.screen(point);
383                                 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
384                         }
385                 }
386         }
387
388         /* AltosMapStoreListener methods */
389         public synchronized void notify_store(AltosMapStore store, int status) {
390                 if (load_listener != null) {
391                         for (AltosMapTile tile : tiles.values())
392                                 if (store.equals(tile.store))
393                                         load_listener.notify_tile(tile, status);
394                 }
395         }
396
397         /* UI elements */
398
399         AltosPointInt   drag_start;
400
401         boolean         dragged;
402
403         static final double drag_far = 20;
404
405         private void drag(int x, int y) {
406                 if (drag_start == null)
407                         return;
408
409                 int dx = x - drag_start.x;
410                 int dy = y - drag_start.y;
411
412                 double distance = Math.hypot(dx, dy);
413
414                 if (distance > drag_far)
415                         dragged = true;
416
417                 if (transform == null) {
418                         debug("Transform not set in drag\n");
419                         return;
420                 }
421
422                 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
423                 centre(new_centre);
424                 drag_start = new AltosPointInt(x, y);
425         }
426
427         private void drag_start(int x, int y) {
428                 drag_start = new AltosPointInt(x, y);
429                 dragged = false;
430         }
431
432         private void drag_stop(int x, int y) {
433                 if (!dragged) {
434                         if (transform == null) {
435                                 debug("Transform not set in stop\n");
436                                 return;
437                         }
438                         map_interface.select_object (transform.screen_lat_lon(new AltosPointInt(x,y)));
439                 }
440         }
441
442         private void line_start(int x, int y) {
443                 if (line != null) {
444                         line.pressed(new AltosPointInt(x, y), transform);
445                         repaint();
446                 }
447         }
448
449         private void line(int x, int y) {
450                 if (line != null) {
451                         line.dragged(new AltosPointInt(x, y), transform);
452                         repaint();
453                 }
454         }
455
456         public void touch_start(int x, int y, boolean is_drag) {
457                 notice_user_input();
458                 if (is_drag)
459                         drag_start(x, y);
460                 else
461                         line_start(x, y);
462         }
463
464         public void touch_continue(int x, int y, boolean is_drag) {
465                 notice_user_input();
466                 if (is_drag)
467                         drag(x, y);
468                 else
469                         line(x, y);
470         }
471
472         public void touch_stop(int x, int y, boolean is_drag) {
473                 notice_user_input();
474                 if (is_drag)
475                         drag_stop(x, y);
476         }
477
478         public AltosMap(AltosMapInterface map_interface) {
479                 this.map_interface = map_interface;
480                 cache = new AltosMapCache(map_interface);
481                 line = map_interface.new_line();
482                 path = map_interface.new_path();
483                 set_zoom_label();
484         }
485 }