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_10;
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;
57 public AltosMapCache cache() { return cache; }
59 LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
63 public AltosLatLon last_position;
65 boolean have_boost = false;
66 boolean have_landed = false;
68 ConcurrentHashMap<AltosPointInt,AltosMapTile> tiles = new ConcurrentHashMap<AltosPointInt,AltosMapTile>();
70 AltosLatLon load_centre = null;
71 AltosMapTileListener load_listener;
73 int zoom = AltosMap.default_zoom;
74 int maptype = AltosMap.maptype_default;
78 /* Milliseconds to wait after user action before auto-scrolling
80 static final long auto_scroll_delay = 20 * 1000;
82 public AltosMapTransform transform;
89 /* MapInterface wrapping functions */
91 public void repaint(int x, int y, int w, int h) {
92 map_interface.repaint(new AltosRectangle(x, y, w, h));
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);
100 public void repaint() {
101 map_interface.repaint();
105 return map_interface.width();
108 public int height() {
109 return map_interface.height();
112 public void debug(String format, Object ... arguments) {
113 map_interface.debug(format, arguments);
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);
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);
126 public void notice_user_input() {
127 user_input_time = System.currentTimeMillis();
130 public boolean recent_user_input() {
131 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
134 public boolean has_centre() {
135 return centre != null;
138 public boolean far_from_centre(AltosLatLon lat_lon) {
140 if (centre == null || transform == null)
143 AltosPointDouble screen = transform.screen(lat_lon);
146 int dx = Math.abs ((int) (double) screen.x - width/2);
151 int height = height();
152 int dy = Math.abs ((int) (double) screen.y - height/2);
160 public void set_transform() {
161 if (centre != null) {
162 transform = new AltosMapTransform(width(), height(), zoom, centre);
167 private void set_zoom_label() {
168 map_interface.set_zoom_label(String.format("Zoom %d", get_zoom() - default_zoom));
172 public boolean set_zoom(int zoom) {
174 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
184 public boolean set_zoom_centre(int zoom, AltosPointInt centre) {
185 AltosLatLon mouse_lat_lon = null;
188 if (transform != null)
189 mouse_lat_lon = transform.screen_lat_lon(centre);
191 ret = set_zoom(zoom);
193 if (ret && mouse_lat_lon != null) {
194 AltosPointDouble new_mouse = transform.screen(mouse_lat_lon);
196 double dx = width()/2.0 - centre.x;
197 double dy = height()/2.0 - centre.y;
199 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointDouble(new_mouse.x + dx, new_mouse.y + dy));
207 public int get_zoom() {
211 public boolean set_maptype(int maptype) {
212 if (maptype != this.maptype) {
213 this.maptype = maptype;
221 public void show(AltosState state, AltosListenerState listener_state) {
223 /* If insufficient gps data, nothing to update
225 AltosGPS gps = state.gps;
230 if (!gps.locked && gps.nsat < 4)
233 switch (state.state()) {
234 case AltosLib.ao_flight_boost:
236 add_mark(gps.lat, gps.lon, state.state());
240 case AltosLib.ao_flight_landed:
242 add_mark(gps.lat, gps.lon, state.state());
249 AltosMapRectangle damage = path.add(gps.lat, gps.lon, state.state());
252 repaint(damage, AltosMapPath.stroke_width);
255 last_position = new AltosLatLon(gps.lat, gps.lon);
257 maybe_centre(gps.lat, gps.lon);
260 public void centre(AltosLatLon lat_lon) {
265 public void centre(double lat, double lon) {
266 centre(new AltosLatLon(lat, lon));
269 public void centre(AltosState state) {
270 if (!state.gps.locked && state.gps.nsat < 4)
272 centre(state.gps.lat, state.gps.lon);
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)))
281 public void add_mark(double lat, double lon, int state) {
282 synchronized(marks) {
283 AltosMapMark mark = map_interface.new_mark(lat, lon, state);
290 public void clear_marks() {
291 synchronized(marks) {
296 private void make_tiles() {
297 AltosPointInt upper_left;
298 AltosPointInt lower_right;
300 if (load_centre != null) {
301 AltosPointInt centre = floor(transform.point(load_centre));
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);
308 upper_left = floor(transform.screen_point(new AltosPointInt(0, 0)));
309 lower_right = floor(transform.screen_point(new AltosPointInt(width(), height())));
312 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
314 while (keyEnumeration.hasMoreElements()) {
315 AltosPointInt point = keyEnumeration.nextElement();
316 if (point.x < upper_left.x || lower_right.x < point.x ||
317 point.y < upper_left.y || lower_right.y < point.y) {
322 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
324 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
325 for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
326 AltosPointInt point = new AltosPointInt(x, y);
328 if (!tiles.containsKey(point)) {
329 AltosLatLon ul = transform.lat_lon(point);
330 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
331 AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size);
332 tile.add_listener(this);
333 tiles.put(point, tile);
339 public void set_load_params(int new_zoom, int new_type, double lat, double lon, int radius, AltosMapTileListener listener) {
340 if (AltosMap.min_zoom <= new_zoom && new_zoom <= AltosMap.max_zoom)
343 load_centre = new AltosLatLon(lat, lon);
344 load_radius = radius;
345 load_listener = listener;
352 public String getName() {
356 public void paint() {
360 if (transform == null)
363 for (AltosMapTile tile : tiles.values())
364 tile.paint(transform);
366 synchronized(marks) {
367 for (AltosMapMark mark : marks)
368 mark.paint(transform);
372 path.paint(transform);
375 line.paint(transform);
378 /* AltosMapTileListener methods */
379 public synchronized void notify_tile(AltosMapTile tile, int status) {
380 Enumeration<AltosPointInt> keyEnumeration = tiles.keys();
382 while (keyEnumeration.hasMoreElements()) {
383 AltosPointInt point = keyEnumeration.nextElement();
384 if (tile == tiles.get(point)) {
385 AltosPointInt screen = transform.screen(point);
386 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
391 /* AltosMapStoreListener methods */
392 public synchronized void notify_store(AltosMapStore store, int status) {
393 if (load_listener != null) {
394 for (AltosMapTile tile : tiles.values())
395 if (store.equals(tile.store))
396 load_listener.notify_tile(tile, status);
402 AltosPointInt drag_start;
406 static final double drag_far = 20;
408 private void drag(int x, int y) {
409 if (drag_start == null)
412 int dx = x - drag_start.x;
413 int dy = y - drag_start.y;
415 double distance = Math.hypot(dx, dy);
417 if (distance > drag_far)
420 if (transform == null)
423 AltosLatLon new_centre = transform.screen_lat_lon(new AltosPointInt(width() / 2 - dx, height() / 2 - dy));
425 drag_start = new AltosPointInt(x, y);
428 private void drag_start(int x, int y) {
429 drag_start = new AltosPointInt(x, y);
433 private void drag_stop(int x, int y) {
435 if (transform == null) {
438 map_interface.select_object (transform.screen_lat_lon(new AltosPointInt(x,y)));
442 private void line_start(int x, int y) {
443 if (line != null && transform != null) {
444 line.pressed(new AltosPointInt(x, y), transform);
449 private void line(int x, int y) {
450 if (line != null && transform != null) {
451 line.dragged(new AltosPointInt(x, y), transform);
456 public void touch_start(int x, int y, boolean is_drag) {
464 public void touch_continue(int x, int y, boolean is_drag) {
472 public void touch_stop(int x, int y, boolean is_drag) {
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();