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_6;
23 import java.util.concurrent.*;
25 public class AltosMap implements AltosFlightDisplay, AltosMapTileListener, AltosMapStoreListener {
27 static final int px_size = 512;
29 static final int maptype_hybrid = 0;
30 static final int maptype_roadmap = 1;
31 static final int maptype_satellite = 2;
32 static final int maptype_terrain = 3;
33 static final int maptype_default = maptype_hybrid;
35 static final int default_zoom = 15;
36 static final int min_zoom = 3;
37 static final int max_zoom = 21;
39 static final String[] maptype_names = {
46 public static final String[] maptype_labels = {
53 AltosMapInterface map_interface;
57 LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
59 LinkedList<AltosMapZoomListener> zoom_listeners = new LinkedList<AltosMapZoomListener>();
61 public void add_zoom_listener(AltosMapZoomListener listener) {
62 if (!zoom_listeners.contains(listener))
63 zoom_listeners.add(listener);
66 public void remove_zoom_listener(AltosMapZoomListener listener) {
67 zoom_listeners.remove(listener);
70 boolean have_boost = false;
71 boolean have_landed = false;
73 ConcurrentHashMap<AltosPointInt,AltosMapTile> tiles = new ConcurrentHashMap<AltosPointInt,AltosMapTile>();
75 AltosLatLon load_centre = null;
76 AltosMapTileListener load_listener;
78 int zoom = AltosMap.default_zoom;
79 int maptype = AltosMap.maptype_default;
83 /* Milliseconds to wait after user action before auto-scrolling
85 static final long auto_scroll_delay = 20 * 1000;
87 AltosMapTransform transform;
94 /* MapInterface wrapping functions */
95 public void set_units() {
96 map_interface.set_units();
99 public void repaint(AltosMapRectangle damage, int pad) {
100 map_interface.repaint(damage, pad);
103 public void repaint(double x, double y, double w, double h) {
104 map_interface.repaint(x, y, w, h);
107 public void repaint() {
108 map_interface.repaint();
112 return map_interface.width();
115 public int height() {
116 return map_interface.height();
119 public AltosPointInt floor(AltosPointDouble point) {
120 return new AltosPointInt ((int) Math.floor(point.x / AltosMap.px_size) * AltosMap.px_size,
121 (int) Math.floor(point.y / AltosMap.px_size) * AltosMap.px_size);
124 public AltosPointInt ceil(AltosPointDouble point) {
125 return new AltosPointInt ((int) Math.ceil(point.x / AltosMap.px_size) * AltosMap.px_size,
126 (int) Math.ceil(point.y / AltosMap.px_size) * AltosMap.px_size);
129 public void notice_user_input() {
130 user_input_time = System.currentTimeMillis();
133 public boolean recent_user_input() {
134 return (System.currentTimeMillis() - user_input_time) < auto_scroll_delay;
137 public boolean far_from_centre(AltosLatLon lat_lon) {
139 if (centre == null || transform == null)
142 AltosPointDouble screen = transform.screen(lat_lon);
145 int dx = Math.abs ((int) (double) screen.x - width/2);
150 int height = height();
151 int dy = Math.abs ((int) (double) screen.y - height/2);
159 public void font_size_changed(int font_size) {
160 map_interface.line.font_size_changed(font_size);
161 for (AltosMapTile tile : tiles.values())
162 tile.font_size_changed(font_size);
166 public void units_changed(boolean imperial_units) {
169 private void set_transform() {
170 transform = new AltosMapTransform(width(), height(), zoom, centre);
174 public boolean set_zoom(int zoom) {
175 if (AltosMap.min_zoom <= zoom && zoom <= AltosMap.max_zoom && zoom != this.zoom) {
180 for (AltosMapZoomListener listener : zoom_listeners)
181 listener.zoom_changed(this.zoom);
188 public int get_zoom() {
192 public boolean set_maptype(int maptype) {
193 if (maptype != this.maptype) {
194 this.maptype = maptype;
202 public void show(AltosState state, AltosListenerState listener_state) {
204 /* If insufficient gps data, nothing to update
206 AltosGPS gps = state.gps;
211 if (!gps.locked && gps.nsat < 4)
214 AltosMapRectangle damage = map_interface.path.add(gps.lat, gps.lon, state.state);
216 switch (state.state) {
217 case AltosLib.ao_flight_boost:
219 add_mark(gps.lat, gps.lon, state.state);
223 case AltosLib.ao_flight_landed:
225 add_mark(gps.lat, gps.lon, state.state);
232 repaint(damage, AltosMapPath.stroke_width);
233 maybe_centre(gps.lat, gps.lon);
236 public void centre(AltosLatLon lat_lon) {
241 public void centre(double lat, double lon) {
242 centre(new AltosLatLon(lat, lon));
245 public void centre(AltosState state) {
246 if (!state.gps.locked && state.gps.nsat < 4)
248 centre(state.gps.lat, state.gps.lon);
251 public void maybe_centre(double lat, double lon) {
252 AltosLatLon lat_lon = new AltosLatLon(lat, lon);
253 if (centre == null || (!recent_user_input() && far_from_centre(lat_lon)))
257 public void add_mark(double lat, double lon, int state) {
258 synchronized(marks) {
259 marks.add(map_interface.new_mark(lat, lon, state));
264 public void clear_marks() {
265 synchronized(marks) {
270 private void make_tiles() {
271 AltosPointInt upper_left;
272 AltosPointInt lower_right;
274 if (load_centre != null) {
275 AltosPointInt centre = floor(transform.point(load_centre));
277 upper_left = new AltosPointInt(centre.x - load_radius * AltosMap.px_size,
278 centre.y - load_radius * AltosMap.px_size);
279 lower_right = new AltosPointInt(centre.x + load_radius * AltosMap.px_size,
280 centre.y + load_radius * AltosMap.px_size);
282 upper_left = floor(transform.screen_point(new AltosPointDouble(0.0, 0.0)));
283 lower_right = floor(transform.screen_point(new AltosPointDouble(width(), height())));
285 LinkedList<AltosPointInt> to_remove = new LinkedList<AltosPointInt>();
287 for (AltosPointInt point : tiles.keySet()) {
288 if (point.x < upper_left.x || lower_right.x < point.x ||
289 point.y < upper_left.y || lower_right.y < point.y) {
290 to_remove.add(point);
294 for (AltosPointInt point : to_remove)
297 cache.set_cache_size((width() / AltosMap.px_size + 2) * (height() / AltosMap.px_size + 2));
298 for (int y = (int) upper_left.y; y <= lower_right.y; y += AltosMap.px_size) {
299 for (int x = (int) upper_left.x; x <= lower_right.x; x += AltosMap.px_size) {
300 AltosPointInt point = new AltosPointInt(x, y);
302 if (!tiles.containsKey(point)) {
303 AltosLatLon ul = transform.lat_lon(new AltosPointDouble(x, y));
304 AltosLatLon center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
305 AltosMapTile tile = new AltosMapTile(this, ul, center, zoom, maptype,
307 tiles.put(point, tile);
313 public void set_load_params(double lat, double lon, int radius, AltosMapTileListener listener) {
314 load_centre = new AltosLatLon(lat, lon);
315 load_radius = radius;
316 load_listener = listener;
319 for (AltosMapTile tile : tiles.values()) {
320 tile.add_store_listener(this);
321 if (tile.store_status() != AltosMapTile.loading)
322 listener.notify_tile(tile, tile.store_status());
327 public String getName() {
331 /* AltosMapTileListener methods */
332 public synchronized void notify_tile(AltosMapTile tile, int status) {
333 for (AltosPointInt point : tiles.keySet()) {
334 if (tile == tiles.get(point)) {
335 AltosPointInt screen = transform.screen(point);
336 repaint(screen.x, screen.y, AltosMap.px_size, AltosMap.px_size);
341 /* AltosMapStoreListener methods */
342 public synchronized void notify_store(AltosMapStore store, int status) {
343 if (load_listener != null) {
344 for (AltosMapTile tile : tiles.values())
345 if (store.equals(tile.store))
346 load_listener.notify_tile(tile, status);
350 public AltosMap(AltosMapInterface map_interface) {
351 this.map_interface = map_interface;
352 cache = new AltosMapCache(map_interface);