2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
18 package org.altusmetrum.altoslib_11;
23 import java.util.concurrent.*;
25 public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
27 public static final int px_size = 512;
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;
35 public static final int default_zoom = 15;
36 public static final int min_zoom = 3;
37 public static final int max_zoom = 21;
39 public static final String[] maptype_names = {
46 public static final String[] maptype_labels = {
53 AltosMapInterface map_interface;
58 public AltosMapCache cache() { return cache; }
60 LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
64 public AltosLatLon last_position;
66 boolean have_boost = false;
67 boolean have_landed = false;
69 ConcurrentHashMap<AltosPointInt,AltosMapTile> tiles = new ConcurrentHashMap<AltosPointInt,AltosMapTile>();
71 AltosLatLon load_centre = null;
72 AltosMapTileListener load_listener;
74 int zoom = AltosMap.default_zoom;
75 int maptype = AltosMap.maptype_default;
79 /* Milliseconds to wait after user action before auto-scrolling
81 static final long auto_scroll_delay = 20 * 1000;
83 public AltosMapTransform transform;
90 /* MapInterface wrapping functions */
92 public void repaint(int x, int y, int w, int h) {
93 map_interface.repaint(new AltosRectangle(x, y, w, h));
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);
101 public void repaint() {
102 map_interface.repaint();
106 return map_interface.width();
109 public int height() {
110 return map_interface.height();
113 public void debug(String format, Object ... arguments) {
114 map_interface.debug(format, arguments);
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);
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);
127 public void notice_user_input() {
128 user_input_time = System.currentTimeMillis();
131 public boolean recent_user_input() {
132 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
135 public boolean has_centre() {
136 return centre != null;
139 public boolean far_from_centre(AltosLatLon lat_lon) {
141 if (centre == null || transform == null)
144 AltosPointDouble screen = transform.screen(lat_lon);
147 int dx = Math.abs ((int) (double) screen.x - width/2);
152 int height = height();
153 int dy = Math.abs ((int) (double) screen.y - height/2);
161 public void set_transform() {
162 if (centre != null) {
163 transform = new AltosMapTransform(width(), height(), zoom, centre);
168 private void set_zoom_label() {
169 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
173 public boolean set_zoom(int zoom) {
175 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
185 public boolean set_zoom_centre(int zoom, AltosPointInt centre) {
186 AltosLatLon mouse_lat_lon = null;
189 if (transform != null)
190 mouse_lat_lon = transform.screen_lat_lon(centre);
192 ret = set_zoom(zoom);
194 if (ret && mouse_lat_lon != null) {
195 AltosPointDouble new_mouse = transform.screen(mouse_lat_lon);
197 double dx = width()/2.0 - centre.x;
198 double dy = height()/2.0 - centre.y;
200 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointDouble(new_mouse.x + dx, new_mouse.y + dy));
208 public int get_zoom() {
212 public boolean set_maptype(int maptype) {
213 if (maptype != this.maptype) {
214 this.maptype = maptype;
222 public void show(AltosState state, AltosListenerState listener_state) {
224 /* If insufficient gps data, nothing to update
226 AltosGPS gps = state.gps;
231 if (!gps.locked && gps.nsat < 4)
234 switch (state.state()) {
235 case AltosLib.ao_flight_boost:
237 add_mark(gps.lat, gps.lon, state.state());
241 case AltosLib.ao_flight_landed:
243 add_mark(gps.lat, gps.lon, state.state());
250 AltosMapRectangle damage = path.add(gps.lat, gps.lon, state.state());
253 repaint(damage, AltosMapPath.stroke_width);
256 last_position = new AltosLatLon(gps.lat, gps.lon);
258 maybe_centre(gps.lat, gps.lon);
261 public void centre(AltosLatLon lat_lon) {
266 public void centre(double lat, double lon) {
267 centre(new AltosLatLon(lat, lon));
270 public void centre(AltosState state) {
271 if (!state.gps.locked && state.gps.nsat < 4)
273 centre(state.gps.lat, state.gps.lon);
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)))
282 public void add_mark(double lat, double lon, int state) {
283 synchronized(marks) {
284 AltosMapMark mark = map_interface.new_mark(lat, lon, state);
291 public void clear_marks() {
292 synchronized(marks) {
297 private void make_tiles() {
298 AltosPointInt upper_left;
299 AltosPointInt lower_right;
301 if (load_centre != null) {
302 AltosPointInt centre = floor(transform.point(load_centre));
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);
309 upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
310 lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
313 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
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) {
323 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
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);
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);
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)
345 load_centre = new AltosLatLon(lat, lon);
346 load_radius = radius;
347 load_listener = listener;
354 public String getName() {
358 public void paint() {
362 if (transform == null)
365 for (AltosMapTile tile : tiles.values())
366 tile.paint(transform);
368 synchronized(marks) {
369 for (AltosMapMark mark : marks)
370 mark.paint(transform);
374 path.paint(transform);
377 line.paint(transform);
380 /* AltosMapTileListener methods */
381 public synchronized void notify_tile(AltosMapTile tile, int status) {
382 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
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);
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);
404 AltosPointInt drag_start;
408 static final double drag_far = 20;
410 private void drag(int x, int y) {
411 if (drag_start == null)
414 int dx = x - drag_start.x;
415 int dy = y - drag_start.y;
417 double distance = Math.hypot(dx, dy);
419 if (distance > drag_far)
422 if (transform == null)
425 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
427 drag_start = new AltosPointInt(x, y);
430 private void drag_start(int x, int y) {
431 drag_start = new AltosPointInt(x, y);
435 private void drag_stop(int x, int y) {
437 if (transform == null) {
440 map_interface.select_object (transform.screen_lat_lon(new AltosPointInt(x,y)));
444 private void line_start(int x, int y) {
445 if (line != null && transform != null) {
446 line.pressed(new AltosPointInt(x, y), transform);
451 private void line(int x, int y) {
452 if (line != null && transform != null) {
453 line.dragged(new AltosPointInt(x, y), transform);
458 public void touch_start(int x, int y, boolean is_drag) {
466 public void touch_continue(int x, int y, boolean is_drag) {
474 public void touch_stop(int x, int y, boolean is_drag) {
480 public AltosMap(AltosMapInterface map_interface, int scale) {
481 this.map_interface = map_interface;
483 cache = new AltosMapCache(map_interface);
484 line = map_interface.new_line();
485 path = map_interface.new_path();
489 public AltosMap(AltosMapInterface map_interface) {
490 this(map_interface, 1);