d39c330201a0d01446388f64f3ec623ed0ea871e
[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 AltosPointInt floor(AltosPointDouble point) {
113                 return new AltosPointInt ((int) Math.floor(point.x / AltosMap.px_size) * AltosMap.px_size,
114                                               (int) Math.floor(point.y / AltosMap.px_size) * AltosMap.px_size);
115         }
116
117         public AltosPointInt ceil(AltosPointDouble point) {
118                 return new AltosPointInt ((int) Math.ceil(point.x / AltosMap.px_size) * AltosMap.px_size,
119                                               (int) Math.ceil(point.y / AltosMap.px_size) * AltosMap.px_size);
120         }
121
122         public void notice_user_input() {
123                 user_input_time = System.currentTimeMillis();
124         }
125
126         public boolean recent_user_input() {
127                 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
128         }
129
130         public boolean far_from_centre(AltosLatLon lat_lon) {
131
132                 if (centre == null || transform == null)
133                         return true;
134
135                 AltosPointDouble        screen = transform.screen(lat_lon);
136
137                 int             width = width();
138                 int             dx = Math.abs ((int) (double) screen.x - width/2);
139
140                 if (dx > width / 4)
141                         return true;
142
143                 int             height = height();
144                 int             dy = Math.abs ((int) (double) screen.y - height/2);
145
146                 if (dy > height / 4)
147                         return true;
148
149                 return false;
150         }
151
152         public void set_transform() {
153                 if (centre != null) {
154                         transform = new AltosMapTransform(width(), height(), zoom, centre);
155                         repaint();
156                 }
157         }
158
159         private void set_zoom_label() {
160                 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
161         }
162
163
164         public boolean set_zoom(int zoom) {
165                 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
166                         this.zoom = zoom;
167                         tiles.clear();
168                         set_transform();
169                         set_zoom_label();
170                         return true;
171                 }
172                 return false;
173         }
174
175         public int get_zoom() {
176                 return zoom;
177         }
178
179         public boolean set_maptype(int maptype) {
180                 if (maptype != this.maptype) {
181                         this.maptype = maptype;
182                         tiles.clear();
183                         repaint();
184                         return true;
185                 }
186                 return false;
187         }
188
189         public void show(AltosState state, AltosListenerState listener_state) {
190
191                 /* If insufficient gps data, nothing to update
192                  */
193                 AltosGPS        gps = state.gps;
194
195                 if (gps == null)
196                         return;
197
198                 if (!gps.locked && gps.nsat < 4)
199                         return;
200
201                 switch (state.state) {
202                 case AltosLib.ao_flight_boost:
203                         if (!have_boost) {
204                                 add_mark(gps.lat, gps.lon, state.state);
205                                 have_boost = true;
206                         }
207                         break;
208                 case AltosLib.ao_flight_landed:
209                         if (!have_landed) {
210                                 add_mark(gps.lat, gps.lon, state.state);
211                                 have_landed = true;
212                         }
213                         break;
214                 }
215
216                 if (path != null) {
217                         AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state);
218
219                         if (damage != null)
220                                 repaint(damage, AltosMapPath.stroke_width);
221                 }
222
223                 last_position = new AltosLatLon(gps.lat, gps.lon);
224
225                 maybe_centre(gps.lat, gps.lon);
226         }
227
228         public void centre(AltosLatLon lat_lon) {
229                 centre = lat_lon;
230                 set_transform();
231         }
232
233         public void centre(double lat, double lon) {
234                 centre(new AltosLatLon(lat, lon));
235         }
236
237         public void centre(AltosState state) {
238                 if (!state.gps.locked && state.gps.nsat < 4)
239                         return;
240                 centre(state.gps.lat, state.gps.lon);
241         }
242
243         public void maybe_centre(double lat, double lon) {
244                 AltosLatLon     lat_lon = new AltosLatLon(lat, lon);
245                 if (centre == null || (!recent_user_input() && far_from_centre(lat_lon)))
246                         centre(lat_lon);
247         }
248
249         public void add_mark(double lat, double lon, int state) {
250                 synchronized(marks) {
251                         marks.add(map_interface.new_mark(lat, lon, state));
252                 }
253                 repaint();
254         }
255
256         public void clear_marks() {
257                 synchronized(marks) {
258                         marks.clear();
259                 }
260         }
261
262         private void make_tiles() {
263                 AltosPointInt   upper_left;
264                 AltosPointInt   lower_right;
265
266                 if (load_centre != null) {
267                         AltosPointInt centre = floor(transform.point(load_centre));
268
269                         upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
270                                                                centre.y - load_radius * AltosMap.px_size);
271                         lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
272                                                                 centre.y + load_radius * AltosMap.px_size);
273                 } else {
274                         upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
275                         lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
276                 }
277                 LinkedList<AltosPointInt> to_remove = new LinkedList<AltosPointInt>();
278
279                 for (AltosPointInt point : tiles.keySet()) {
280                         if (point.x < upper_left.x || lower_right.x < point.x ||
281                             point.y < upper_left.y || lower_right.y < point.y) {
282                                 to_remove.add(point);
283                         }
284                 }
285
286                 for (AltosPointInt point : to_remove)
287                         tiles.remove(point);
288
289                 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
290
291                 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
292                         for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
293                                 AltosPointInt point = new AltosPointInt(x, y);
294
295                                 if (!tiles.containsKey(point)) {
296                                         AltosLatLon     ul = transform.lat_lon(new AltosPointDouble(x, y));
297                                         AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
298                                         AltosMapTile tile = map_interface.new_tile(this, ul, center, zoom, maptype,
299                                                                                    px_size);
300                                         tiles.put(point, tile);
301                                 }
302                         }
303                 }
304         }
305
306         public void set_load_params(double lat, double lon, int radius, AltosMapTileListener listener) {
307                 load_centre = new AltosLatLon(lat, lon);
308                 load_radius = radius;
309                 load_listener = listener;
310                 centre(lat, lon);
311                 make_tiles();
312                 for (AltosMapTile tile : tiles.values()) {
313                         tile.add_store_listener(this);
314                         if (tile.store_status() != AltosMapTile.loading)
315                                 listener.notify_tile(tile, tile.store_status());
316                 }
317                 repaint();
318         }
319
320         public String getName() {
321                 return "Map";
322         }
323
324         public void paint() {
325                 if (centre != null)
326                         make_tiles();
327
328                 for (AltosMapTile tile : tiles.values())
329                         tile.paint(transform);
330
331                 synchronized(marks) {
332                         for (AltosMapMark mark : marks)
333                                 mark.paint(transform);
334                 }
335
336                 if (path != null)
337                         path.paint(transform);
338
339                 if (line != null)
340                         line.paint(transform);
341         }
342
343         /* AltosMapTileListener methods */
344         public synchronized void notify_tile(AltosMapTile tile, int status) {
345                 for (AltosPointInt point : tiles.keySet()) {
346                         if (tile == tiles.get(point)) {
347                                 AltosPointInt   screen = transform.screen(point);
348                                 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
349                         }
350                 }
351         }
352
353         /* AltosMapStoreListener methods */
354         public synchronized void notify_store(AltosMapStore store, int status) {
355                 if (load_listener != null) {
356                         for (AltosMapTile tile : tiles.values())
357                                 if (store.equals(tile.store))
358                                         load_listener.notify_tile(tile, status);
359                 }
360         }
361
362         /* UI elements */
363
364         AltosPointInt   drag_start;
365
366         private void drag(int x, int y) {
367                 if (drag_start == null)
368                         return;
369
370                 int dx = x - drag_start.x;
371                 int dy = y - drag_start.y;
372
373                 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
374                 centre(new_centre);
375                 drag_start = new AltosPointInt(x, y);
376         }
377
378         private void drag_start(int x, int y) {
379                 drag_start = new AltosPointInt(x, y);
380         }
381
382         private void line_start(int x, int y) {
383                 if (line != null) {
384                         line.pressed(new AltosPointInt(x, y), transform);
385                         repaint();
386                 }
387         }
388
389         private void line(int x, int y) {
390                 if (line != null) {
391                         line.dragged(new AltosPointInt(x, y), transform);
392                         repaint();
393                 }
394         }
395
396         public void touch_start(int x, int y, boolean is_drag) {
397                 notice_user_input();
398                 if (is_drag)
399                         drag_start(x, y);
400                 else
401                         line_start(x, y);
402         }
403
404         public void touch_continue(int x, int y, boolean is_drag) {
405                 notice_user_input();
406                 if (is_drag)
407                         drag(x, y);
408                 else
409                         line(x, y);
410         }
411
412         public AltosMap(AltosMapInterface map_interface) {
413                 this.map_interface = map_interface;
414                 cache = new AltosMapCache(map_interface);
415                 line = map_interface.new_line();
416                 path = map_interface.new_path();
417                 set_zoom_label();
418         }
419 }