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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
24 import java.util.concurrent.*;
26 public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
28 public static final int px_size = 512;
30 public static final int maptype_hybrid = 0;
31 public static final int maptype_roadmap = 1;
32 public static final int maptype_satellite = 2;
33 public static final int maptype_terrain = 3;
34 public static final int maptype_default = maptype_hybrid;
36 public static final int default_zoom = 15;
37 public static final int min_zoom = 3;
38 public static final int max_zoom = 21;
40 public static final String[] maptype_names = {
47 public static final String[] maptype_labels = {
54 AltosMapInterface map_interface;
59 public AltosMapCache cache() { return cache; }
61 LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
65 public AltosLatLon last_position;
67 boolean have_boost = false;
68 boolean have_landed = false;
70 ConcurrentHashMap<AltosPointInt,AltosMapTile> tiles = new ConcurrentHashMap<AltosPointInt,AltosMapTile>();
72 AltosLatLon load_centre = null;
73 AltosMapTileListener load_listener;
75 int zoom = AltosMap.default_zoom;
76 int maptype = AltosMap.maptype_default;
80 /* Milliseconds to wait after user action before auto-scrolling
82 static final long auto_scroll_delay = 20 * 1000;
84 public AltosMapTransform transform;
91 /* MapInterface wrapping functions */
93 public void repaint(int x, int y, int w, int h) {
94 map_interface.repaint(new AltosRectangle(x, y, w, h));
97 public void repaint(AltosMapRectangle damage, int pad) {
98 AltosRectangle r = transform.screen(damage);
99 repaint(r.x - pad, r.y - pad, r.width + pad * 2, r.height + pad * 2);
102 public void repaint() {
103 map_interface.repaint();
107 return map_interface.width();
110 public int height() {
111 return map_interface.height();
114 public void debug(String format, Object ... arguments) {
115 map_interface.debug(format, arguments);
118 static public AltosPointInt floor(AltosPointDouble point) {
119 return new AltosPointInt ((int) Math.floor(point.x / AltosMap.px_size) * AltosMap.px_size,
120 (int) Math.floor(point.y / AltosMap.px_size) * AltosMap.px_size);
123 static public AltosPointInt ceil(AltosPointDouble point) {
124 return new AltosPointInt ((int) Math.ceil(point.x / AltosMap.px_size) * AltosMap.px_size,
125 (int) Math.ceil(point.y / AltosMap.px_size) * AltosMap.px_size);
128 public void notice_user_input() {
129 user_input_time = System.currentTimeMillis();
132 public boolean recent_user_input() {
133 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
136 public boolean has_centre() {
137 return centre != null;
140 public boolean far_from_centre(AltosLatLon lat_lon) {
142 if (centre == null || transform == null)
145 AltosPointDouble screen = transform.screen(lat_lon);
148 int dx = Math.abs ((int) (double) screen.x - width/2);
153 int height = height();
154 int dy = Math.abs ((int) (double) screen.y - height/2);
162 public void set_transform() {
163 if (centre != null) {
164 transform = new AltosMapTransform(width(), height(), zoom, centre);
169 private void set_zoom_label() {
170 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
174 public boolean set_zoom(int zoom) {
176 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
186 public boolean set_zoom_centre(int zoom, AltosPointInt centre) {
187 AltosLatLon mouse_lat_lon = null;
190 if (transform != null)
191 mouse_lat_lon = transform.screen_lat_lon(centre);
193 ret = set_zoom(zoom);
195 if (ret && mouse_lat_lon != null) {
196 AltosPointDouble new_mouse = transform.screen(mouse_lat_lon);
198 double dx = width()/2.0 - centre.x;
199 double dy = height()/2.0 - centre.y;
201 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointDouble(new_mouse.x + dx, new_mouse.y + dy));
209 public int get_zoom() {
213 public boolean set_maptype(int maptype) {
214 if (maptype != this.maptype) {
215 this.maptype = maptype;
223 public void show(AltosState state, AltosListenerState listener_state) {
225 /* If insufficient gps data, nothing to update
227 AltosGPS gps = state.gps;
232 if (!gps.locked && gps.nsat < 4)
235 switch (state.state()) {
236 case AltosLib.ao_flight_boost:
238 add_mark(gps.lat, gps.lon, state.state());
242 case AltosLib.ao_flight_landed:
244 add_mark(gps.lat, gps.lon, state.state());
251 AltosMapRectangle damage = path.add(gps.lat, gps.lon, state.state());
254 repaint(damage, AltosMapPath.stroke_width);
257 last_position = new AltosLatLon(gps.lat, gps.lon);
259 maybe_centre(gps.lat, gps.lon);
262 public void centre(AltosLatLon lat_lon) {
267 public void centre(double lat, double lon) {
268 centre(new AltosLatLon(lat, lon));
271 public void centre(AltosState state) {
272 if (!state.gps.locked && state.gps.nsat < 4)
274 centre(state.gps.lat, state.gps.lon);
277 public void maybe_centre(double lat, double lon) {
278 AltosLatLon lat_lon = new AltosLatLon(lat, lon);
279 if (centre == null || (!recent_user_input() && far_from_centre(lat_lon)))
283 public void add_mark(double lat, double lon, int state) {
284 synchronized(marks) {
285 AltosMapMark mark = map_interface.new_mark(lat, lon, state);
292 public void clear_marks() {
293 synchronized(marks) {
298 private void make_tiles() {
299 AltosPointInt upper_left;
300 AltosPointInt lower_right;
302 if (load_centre != null) {
303 AltosPointInt centre = floor(transform.point(load_centre));
305 upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
306 centre.y - load_radius * AltosMap.px_size);
307 lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
308 centre.y + load_radius * AltosMap.px_size);
310 upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
311 lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
314 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
316 while (keyEnumeration.hasMoreElements()) {
317 AltosPointInt point = keyEnumeration.nextElement();
318 if (point.x < upper_left.x || lower_right.x < point.x ||
319 point.y < upper_left.y || lower_right.y < point.y) {
324 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
326 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
327 for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
328 AltosPointInt point = new AltosPointInt(x, y);
330 if (!tiles.containsKey(point)) {
331 AltosLatLon ul = transform.lat_lon(point);
332 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
333 AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size, scale);
334 debug("show state %s url %s\n", AltosMapTile.status_name(tile.store.status()), tile.store.url);
335 tile.add_listener(this);
336 tiles.put(point, tile);
342 public void set_load_params(int new_zoom, int new_type, double lat, double lon, int radius, AltosMapTileListener listener) {
343 if (AltosMap.min_zoom <= new_zoom && new_zoom <= AltosMap.max_zoom)
346 load_centre = new AltosLatLon(lat, lon);
347 load_radius = radius;
348 load_listener = listener;
355 public String getName() {
359 public void paint() {
363 if (transform == null)
366 for (AltosMapTile tile : tiles.values())
367 tile.paint(transform);
369 synchronized(marks) {
370 for (AltosMapMark mark : marks)
371 mark.paint(transform);
375 path.paint(transform);
378 line.paint(transform);
381 /* AltosMapTileListener methods */
382 public synchronized void notify_tile(AltosMapTile tile, int status) {
383 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
385 while (keyEnumeration.hasMoreElements()) {
386 AltosPointInt point = keyEnumeration.nextElement();
387 if (tile == tiles.get(point)) {
388 AltosPointInt screen = transform.screen(point);
389 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
394 /* AltosMapStoreListener methods */
395 public synchronized void notify_store(AltosMapStore store, int status) {
396 if (load_listener != null) {
397 for (AltosMapTile tile : tiles.values())
398 if (store.equals(tile.store))
399 load_listener.notify_tile(tile, status);
405 AltosPointInt drag_start;
409 static final double drag_far = 20;
411 private void drag(int x, int y) {
412 if (drag_start == null)
415 int dx = x - drag_start.x;
416 int dy = y - drag_start.y;
418 double distance = Math.hypot(dx, dy);
420 if (distance > drag_far)
423 if (transform == null)
426 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
428 drag_start = new AltosPointInt(x, y);
431 private void drag_start(int x, int y) {
432 drag_start = new AltosPointInt(x, y);
436 private void drag_stop(int x, int y) {
438 if (transform == null) {
441 map_interface.select_object (transform.screen_lat_lon(new AltosPointInt(x,y)));
445 private void line_start(int x, int y) {
446 if (line != null && transform != null) {
447 line.pressed(new AltosPointInt(x, y), transform);
452 private void line(int x, int y) {
453 if (line != null && transform != null) {
454 line.dragged(new AltosPointInt(x, y), transform);
459 public void touch_start(int x, int y, boolean is_drag) {
467 public void touch_continue(int x, int y, boolean is_drag) {
475 public void touch_stop(int x, int y, boolean is_drag) {
481 public AltosMap(AltosMapInterface map_interface, int scale) {
482 this.map_interface = map_interface;
484 cache = new AltosMapCache(map_interface);
485 line = map_interface.new_line();
486 path = map_interface.new_path();
490 public AltosMap(AltosMapInterface map_interface) {
491 this(map_interface, 1);