cf7169ba5dbc0f249d42d78f710b9db395dc74b6
[fw/altos] / altoslib / AltosMapLoader.java
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_7;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23 import java.lang.Math;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 public class AltosMapLoader implements AltosMapTileListener {
28         AltosMapLoaderListener  listener;
29
30         double  latitude, longitude;
31         int     min_z;
32         int     max_z;
33         int     cur_z;
34         int     all_types;
35         int     cur_type;
36         int     radius;
37
38         int     tiles_per_layer;
39         int     tiles_loaded;
40         int     layers_total;
41         int     layers_loaded;
42
43         AltosMap        map;
44
45         public void do_load() {
46                 map.set_load_params(cur_z + AltosMap.default_zoom, cur_type, latitude, longitude, radius, this);
47         }
48
49         public int next_type(int start) {
50                 int next_type;
51                 for (next_type = start;
52                      next_type <= AltosMap.maptype_terrain && (all_types & (1 << next_type)) == 0;
53                      next_type++)
54                         ;
55                 return next_type;
56         }
57
58         public void next_load() {
59                 int next_type = next_type(cur_type + 1);
60
61                 if (next_type > AltosMap.maptype_terrain) {
62                         if (cur_z == max_z) {
63                                 return;
64                         } else {
65                                 cur_z++;
66                         }
67                         next_type = next_type(0);
68                 }
69                 cur_type = next_type;
70                 do_load();
71         }
72
73         private void start_load() {
74
75                 cur_z = min_z;
76                 int ntype = 0;
77
78                 for (int t = AltosMap.maptype_hybrid; t <= AltosMap.maptype_terrain; t++)
79                         if ((all_types & (1 << t)) != 0)
80                                 ntype++;
81                 if (ntype == 0) {
82                         all_types = (1 << AltosMap.maptype_hybrid);
83                         ntype = 1;
84                 }
85
86                 cur_type = next_type(0);
87                 tiles_per_layer = (radius * 2 + 1) * (radius * 2 + 1);
88                 layers_total = (max_z - min_z + 1) * ntype;
89                 layers_loaded = 0;
90                 tiles_loaded = 0;
91
92                 listener.loader_start(layers_total * tiles_per_layer);
93                 do_load();
94         }
95
96         public void load(double latitude, double longitude, int min_z, int max_z, int radius, int all_types) {
97                 this.latitude = latitude;
98                 this.longitude = longitude;
99                 this.min_z = min_z;
100                 this.max_z = max_z;
101                 this.radius = radius;
102                 this.all_types = all_types;
103                 start_load();
104         }
105
106         public synchronized void notify_tile(AltosMapTile tile, int status) {
107                 boolean do_next = false;
108                 if (status == AltosMapTile.loading)
109                         return;
110
111                 if (layers_loaded >= layers_total)
112                         return;
113
114                 ++tiles_loaded;
115
116                 if (tiles_loaded == tiles_per_layer) {
117                         tiles_loaded = 0;
118                         ++layers_loaded;
119                         if (layers_loaded == layers_total) {
120                                 listener.loader_done(layers_total * tiles_per_layer);
121                                 return;
122                         } else {
123                                 do_next = true;
124                         }
125                 }
126                 listener.loader_notify(layers_loaded * tiles_per_layer + tiles_loaded,
127                                        layers_total * tiles_per_layer, tile.store.file.toString());
128                 if (do_next)
129                         next_load();
130         }
131
132         public AltosMapCache cache() { return map.cache(); }
133
134         public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener) {
135                 this.map = map;
136                 this.listener = listener;
137         }
138 }