summaryrefslogtreecommitdiff
path: root/altoslib/AltosMapLoader.java
blob: cf7169ba5dbc0f249d42d78f710b9db395dc74b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright © 2015 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_7;

import java.io.*;
import java.util.*;
import java.text.*;
import java.lang.Math;
import java.net.URL;
import java.net.URLConnection;

public class AltosMapLoader implements AltosMapTileListener {
	AltosMapLoaderListener	listener;

	double	latitude, longitude;
	int	min_z;
	int	max_z;
	int	cur_z;
	int	all_types;
	int	cur_type;
	int	radius;

	int	tiles_per_layer;
	int	tiles_loaded;
	int	layers_total;
	int	layers_loaded;

	AltosMap	map;

	public void do_load() {
		map.set_load_params(cur_z + AltosMap.default_zoom, cur_type, latitude, longitude, radius, this);
	}

	public int next_type(int start) {
		int next_type;
		for (next_type = start;
		     next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
		     next_type++)
			;
		return next_type;
	}

	public void next_load() {
		int next_type = next_type(cur_type + 1);

		if (next_type > AltosMap.maptype_terrain) {
			if (cur_z == max_z) {
				return;
			} else {
				cur_z++;
			}
			next_type = next_type(0);
		}
		cur_type = next_type;
		do_load();
	}

	private void start_load() {

		cur_z = min_z;
		int ntype = 0;

		for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
			if ((all_types & (1 << t)) != 0)
				ntype++;
		if (ntype == 0) {
			all_types = (1 << AltosMap.maptype_hybrid);
			ntype = 1;
		}

		cur_type = next_type(0);
		tiles_per_layer = (radius * 2 + 1) * (radius * 2 + 1);
		layers_total = (max_z - min_z + 1) * ntype;
		layers_loaded = 0;
		tiles_loaded = 0;

		listener.loader_start(layers_total * tiles_per_layer);
		do_load();
	}

	public void load(double latitude, double longitude, int min_z, int max_z, int radius, int all_types) {
		this.latitude = latitude;
		this.longitude = longitude;
		this.min_z = min_z;
		this.max_z = max_z;
		this.radius = radius;
		this.all_types = all_types;
		start_load();
	}

	public synchronized void notify_tile(AltosMapTile tile, int status) {
		boolean	do_next = false;
		if (status == AltosMapTile.loading)
			return;

		if (layers_loaded >= layers_total)
			return;

		++tiles_loaded;

		if (tiles_loaded == tiles_per_layer) {
			tiles_loaded = 0;
			++layers_loaded;
			if (layers_loaded == layers_total) {
				listener.loader_done(layers_total * tiles_per_layer);
				return;
			} else {
				do_next = true;
			}
		}
		listener.loader_notify(layers_loaded * tiles_per_layer + tiles_loaded,
				       layers_total * tiles_per_layer, tile.store.file.toString());
		if (do_next)
			next_load();
	}

	public AltosMapCache cache() { return map.cache(); }

	public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener) {
		this.map = map;
		this.listener = listener;
	}
}