1 |
/**
|
2 |
* aagtl Advanced Geocaching Tool for Android
|
3 |
* loosely based on agtl by Daniel Fett <fett@danielfett.de>
|
4 |
* Copyright (C) 2010 - 2012 Zoff <aagtl@work.zoff.cc>
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* version 2 as published by the Free Software Foundation.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the
|
17 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
18 |
* Boston, MA 02110-1301, USA.
|
19 |
*/
|
20 |
|
21 |
package com.zoffcc.applications.aagtl;
|
22 |
|
23 |
import java.util.ArrayList;
|
24 |
import java.util.List;
|
25 |
|
26 |
import android.content.Context;
|
27 |
import android.graphics.Bitmap;
|
28 |
import android.graphics.Canvas;
|
29 |
import android.graphics.Color;
|
30 |
import android.graphics.Paint;
|
31 |
import android.graphics.RectF;
|
32 |
import android.graphics.Typeface;
|
33 |
import android.widget.ImageView;
|
34 |
|
35 |
public class GeocachesView extends ImageView
|
36 |
{
|
37 |
int gc_box_small_width = 8;
|
38 |
int gc_box_small_height = 8;
|
39 |
int gc_box_big_width = 15;
|
40 |
int gc_box_big_height = 15;
|
41 |
|
42 |
int gc_box_swidth = 4;
|
43 |
int gc_box_swidth_small = 3;
|
44 |
|
45 |
int TOO_MUCH_POINTS = 30;
|
46 |
int MAX_DRAW_POINTS = 250;
|
47 |
|
48 |
aagtl main_aagtl;
|
49 |
|
50 |
Bitmap bitmap_main = null;
|
51 |
Canvas image_main = null;
|
52 |
|
53 |
Boolean double_buffer = false;
|
54 |
|
55 |
int COLOR_FOUND = Color.parseColor("#bebebe");
|
56 |
int COLOR_REGULAR = Color.parseColor("#11a011");
|
57 |
int COLOR_MULTI = Color.parseColor("#b0a010");
|
58 |
int COLOR_WAYPOINTS = Color.parseColor("#b0a010");
|
59 |
int COLOR_DEFAULT = Color.parseColor("#1111ef");
|
60 |
int COLOR_CURRENT_CACHE = Color.parseColor("#fe0000");
|
61 |
int COLOR_CACHE_CENTER = Color.parseColor("#101010");
|
62 |
|
63 |
Paint box_paint = new Paint(0);
|
64 |
Paint text_paint = new Paint(0);
|
65 |
|
66 |
List<GeocacheCoordinate> caches_loaded = new ArrayList<GeocacheCoordinate>();
|
67 |
|
68 |
public GeocachesView(Context context, aagtl main_aagtl)
|
69 |
{
|
70 |
super(context);
|
71 |
|
72 |
text_paint.setColor(Color.BLACK);
|
73 |
//text_paint.setStyle(Paint.Style.FILL);
|
74 |
text_paint.setTextSize(21);
|
75 |
text_paint.setStrokeWidth(2);
|
76 |
text_paint.setTypeface(Typeface.DEFAULT_BOLD);
|
77 |
text_paint.setAntiAlias(true);
|
78 |
|
79 |
bitmap_main = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
|
80 |
image_main = new Canvas(bitmap_main);
|
81 |
|
82 |
this.main_aagtl = main_aagtl;
|
83 |
this.clear_stuff();
|
84 |
}
|
85 |
|
86 |
public void clear_stuff()
|
87 |
{
|
88 |
// clear caches loaded
|
89 |
if (caches_loaded != null)
|
90 |
{
|
91 |
caches_loaded.clear();
|
92 |
}
|
93 |
}
|
94 |
|
95 |
public void set_loaded_caches(List<GeocacheCoordinate> new_list)
|
96 |
{
|
97 |
this.caches_loaded = new_list;
|
98 |
//System.out.println("iigcXX");
|
99 |
this.invalidate();
|
100 |
}
|
101 |
|
102 |
public double[] deg2num_give_zoom(Coordinate coord, int zoom)
|
103 |
{
|
104 |
double[] f_ret = new double[2];
|
105 |
f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom);
|
106 |
f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom);
|
107 |
// return --> tile_x,tile_y
|
108 |
return f_ret;
|
109 |
}
|
110 |
|
111 |
public int[] __coord2point_give_zoom(Coordinate coord, int zoom)
|
112 |
{
|
113 |
double[] point = new double[2];
|
114 |
point = this.deg2num_give_zoom(coord, zoom);
|
115 |
int[] i_ret = new int[2];
|
116 |
i_ret[0] = (int) (point[0] * this.main_aagtl.rose.tile_size_x - this.main_aagtl.rose.map_center_x * this.main_aagtl.rose.tile_size_x + this.main_aagtl.rose.mCanvasWidth / 2);
|
117 |
i_ret[1] = (int) (point[1] * this.main_aagtl.rose.tile_size_y - this.main_aagtl.rose.map_center_y * this.main_aagtl.rose.tile_size_y + this.main_aagtl.rose.mCanvasHeight / 2);
|
118 |
return i_ret;
|
119 |
}
|
120 |
|
121 |
public void onDraw(Canvas c)
|
122 |
{
|
123 |
//System.out.println("GeocachesView: onDraw");
|
124 |
|
125 |
GeocacheCoordinate this_gc = null;
|
126 |
Coordinate this_c = null;
|
127 |
|
128 |
if (this.main_aagtl.global_settings.options_turn_map_on_heading)
|
129 |
{
|
130 |
double_buffer = true;
|
131 |
}
|
132 |
else
|
133 |
{
|
134 |
double_buffer = false;
|
135 |
}
|
136 |
|
137 |
Canvas draw_on_me = c;
|
138 |
if (double_buffer)
|
139 |
{
|
140 |
draw_on_me = this.image_main;
|
141 |
this.bitmap_main.eraseColor(Color.TRANSPARENT);
|
142 |
}
|
143 |
|
144 |
if (this.caches_loaded == null)
|
145 |
{
|
146 |
return;
|
147 |
}
|
148 |
|
149 |
// this.main_aagtl.status_text.setText(this.main_aagtl.status_text.getText() + " c:"
|
150 |
// + this.caches_loaded.size());
|
151 |
|
152 |
// too much caches to display
|
153 |
if (this.caches_loaded.size() > this.MAX_DRAW_POINTS)
|
154 |
{
|
155 |
return;
|
156 |
}
|
157 |
|
158 |
int box_width = this.gc_box_big_width;
|
159 |
int box_height = this.gc_box_big_height;
|
160 |
|
161 |
if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
|
162 |
{
|
163 |
box_width = this.gc_box_small_width;
|
164 |
box_height = this.gc_box_small_height;
|
165 |
}
|
166 |
|
167 |
Boolean target_drawn = false;
|
168 |
|
169 |
for (int _cache = 0; _cache < this.caches_loaded.size(); _cache++)
|
170 |
{
|
171 |
this_gc = this.caches_loaded.get(_cache);
|
172 |
|
173 |
//this_c = new Coordinate(this_gc.lat, this_gc.lon);
|
174 |
this_c = (Coordinate) this_gc;
|
175 |
|
176 |
//System.out.println("" + String.valueOf(this_c));
|
177 |
|
178 |
int[] p = new int[2];
|
179 |
p = this.__coord2point_give_zoom(this_c, this.main_aagtl.rose.zoom);
|
180 |
|
181 |
//
|
182 |
//
|
183 |
if ((this_gc.found) || (this_gc.aagtl_status == GeocacheCoordinate.AAGTL_STATUS_FOUND))
|
184 |
{
|
185 |
//System.out.println("cache found!");
|
186 |
box_paint.setColor(this.COLOR_FOUND);
|
187 |
}
|
188 |
else
|
189 |
{
|
190 |
if (this_gc.type == null)
|
191 |
{
|
192 |
box_paint.setColor(this.COLOR_DEFAULT);
|
193 |
}
|
194 |
else
|
195 |
{
|
196 |
if (this_gc.type.matches(GeocacheCoordinate.TYPE_REGULAR))
|
197 |
{
|
198 |
box_paint.setColor(this.COLOR_REGULAR);
|
199 |
}
|
200 |
else if (this_gc.type.matches(GeocacheCoordinate.TYPE_MULTI))
|
201 |
{
|
202 |
box_paint.setColor(this.COLOR_MULTI);
|
203 |
}
|
204 |
else
|
205 |
{
|
206 |
box_paint.setColor(this.COLOR_DEFAULT);
|
207 |
}
|
208 |
}
|
209 |
}
|
210 |
|
211 |
//
|
212 |
//
|
213 |
box_paint.setStrokeWidth(gc_box_swidth);
|
214 |
if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
|
215 |
{
|
216 |
box_paint.setStrokeWidth(gc_box_swidth_small);
|
217 |
}
|
218 |
|
219 |
box_paint.setStyle(Paint.Style.STROKE);
|
220 |
RectF gc_box = new RectF(p[0] - box_width, p[1] - box_height, p[0] + box_width, p[1] + box_height);
|
221 |
|
222 |
draw_on_me.drawRect(gc_box, box_paint);
|
223 |
// gc_box = new RectF(p[0] - box_width + 1, p[1] - box_height + 1, p[0] + box_width - 1, p[1]
|
224 |
// + box_height - 1);
|
225 |
// draw_on_me.drawRect(gc_box, box_paint);
|
226 |
|
227 |
//
|
228 |
//
|
229 |
box_paint.setStrokeWidth(2);
|
230 |
int inside = (int) (3f * aagtl.Global_dpi_factor);
|
231 |
int outside = (int) (2f * aagtl.Global_dpi_factor);
|
232 |
if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
|
233 |
{
|
234 |
box_paint.setStrokeWidth(1);
|
235 |
inside = (int) (1f * aagtl.Global_dpi_factor);
|
236 |
outside = (int) (2f * aagtl.Global_dpi_factor);
|
237 |
}
|
238 |
|
239 |
box_paint.setColor(Color.BLACK);
|
240 |
gc_box = new RectF(p[0] - box_width - outside, p[1] - box_height - outside, p[0] + box_width + outside, p[1] + box_height + outside);
|
241 |
draw_on_me.drawRect(gc_box, box_paint);
|
242 |
gc_box = new RectF(p[0] - box_width + inside, p[1] - box_height + inside, p[0] + box_width - inside, p[1] + box_height - inside);
|
243 |
draw_on_me.drawRect(gc_box, box_paint);
|
244 |
|
245 |
// if this cache is disabled
|
246 |
if (this_gc.status == GeocacheCoordinate.STATUS_DISABLED)
|
247 |
{
|
248 |
Paint temp_paint = new Paint();
|
249 |
temp_paint.setColor(Color.RED);
|
250 |
temp_paint.setAntiAlias(true);
|
251 |
temp_paint.setStrokeWidth(4);
|
252 |
draw_on_me.drawLine(p[0] + box_width, p[1] - box_height, p[0] - box_width - 1, p[1] + box_height - 1, temp_paint);
|
253 |
}
|
254 |
|
255 |
//
|
256 |
//
|
257 |
// draw GC-code
|
258 |
if (this.main_aagtl.rose.zoom > 15)
|
259 |
{
|
260 |
text_paint.setTextScaleX(1.0f);
|
261 |
text_paint.setTextSize(25 * aagtl.Global_dpi_factor);
|
262 |
if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
|
263 |
{
|
264 |
text_paint.setTextSize(17 * aagtl.Global_dpi_factor);
|
265 |
}
|
266 |
draw_on_me.drawText(this_gc.name, p[0] - box_width, p[1] - box_height - 6, text_paint);
|
267 |
}
|
268 |
//
|
269 |
// draw fullname (only first 20 letters!!)
|
270 |
if (this.main_aagtl.rose.zoom > 17)
|
271 |
{
|
272 |
text_paint.setTextScaleX(0.9f);
|
273 |
text_paint.setTextSize(19* aagtl.Global_dpi_factor);
|
274 |
if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
|
275 |
{
|
276 |
text_paint.setTextSize(15* aagtl.Global_dpi_factor);
|
277 |
}
|
278 |
draw_on_me.drawText(this_gc.title.substring(0, Math.min(20, this_gc.title.length())), p[0] - box_width, p[1] + box_height + 19, text_paint);
|
279 |
}
|
280 |
|
281 |
if (this.main_aagtl.rose.current_target != null)
|
282 |
{
|
283 |
if (this.main_aagtl.rose.current_target.name.compareTo(this_gc.name) == 0)
|
284 |
{
|
285 |
// ok this is the current target
|
286 |
box_paint.setColor(Color.BLUE);
|
287 |
box_paint.setStrokeWidth(3);
|
288 |
//this_c = new Coordinate(this_gc.lat, this_gc.lon);
|
289 |
this_c = (Coordinate) this_gc;
|
290 |
|
291 |
//System.out.println("draw target 001" + String.valueOf(this_c));
|
292 |
|
293 |
draw_on_me.drawLine(p[0], p[1], this.getWidth() / 2, this.getHeight() / 2, box_paint);
|
294 |
|
295 |
target_drawn = true;
|
296 |
}
|
297 |
else
|
298 |
{
|
299 |
// we have a manual target!!
|
300 |
}
|
301 |
}
|
302 |
|
303 |
//System.out.println("yyyy1");
|
304 |
|
305 |
}
|
306 |
|
307 |
//System.out.println("zzzzz1");
|
308 |
|
309 |
if (!target_drawn)
|
310 |
{
|
311 |
if (this.main_aagtl.rose.current_target != null)
|
312 |
{
|
313 |
// ok , manual target found!!
|
314 |
// draw it!
|
315 |
|
316 |
this_gc = this.main_aagtl.rose.current_target;
|
317 |
this_c = (Coordinate) this_gc;
|
318 |
int[] p = new int[2];
|
319 |
p = this.__coord2point_give_zoom(this_c, this.main_aagtl.rose.zoom);
|
320 |
|
321 |
// ok this is the current target
|
322 |
box_paint.setColor(Color.BLUE);
|
323 |
box_paint.setStrokeWidth(3);
|
324 |
|
325 |
//System.out.println("draw target 002=" + this_c.lat + "," + this_c.lon);
|
326 |
|
327 |
draw_on_me.drawLine(p[0], p[1], this.getWidth() / 2, this.getHeight() / 2, box_paint);
|
328 |
|
329 |
}
|
330 |
}
|
331 |
|
332 |
if ((bitmap_main != null) && (this.double_buffer))
|
333 |
{
|
334 |
//System.out.println("GCView: doDraw");
|
335 |
if (this.main_aagtl.global_settings.options_turn_map_on_heading)
|
336 |
{
|
337 |
c.save();
|
338 |
c.rotate((int) -this.main_aagtl.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
|
339 |
}
|
340 |
c.drawBitmap(bitmap_main, 0, 0, null);
|
341 |
if (this.main_aagtl.global_settings.options_turn_map_on_heading)
|
342 |
{
|
343 |
c.restore();
|
344 |
}
|
345 |
}
|
346 |
|
347 |
}
|
348 |
|
349 |
public void onSizeChanged(int width, int height, int old_w, int old_h)
|
350 |
{
|
351 |
//System.out.println("GCView: onSizeChanged");
|
352 |
if (bitmap_main != null)
|
353 |
{
|
354 |
bitmap_main.recycle();
|
355 |
}
|
356 |
bitmap_main = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
357 |
image_main = new Canvas(bitmap_main);
|
358 |
|
359 |
// DPI
|
360 |
gc_box_small_width = (int) ((float) 8 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
361 |
gc_box_small_height = (int) ((float) 8 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
362 |
gc_box_big_width = (int) ((float) 15 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
363 |
gc_box_big_height = (int) ((float) 15 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
364 |
|
365 |
gc_box_swidth = (int) ((float) 4 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
366 |
gc_box_swidth_small = (int) ((float) 3 * ((float) aagtl.Global_real_dpi / (float) aagtl.Global_want_dpi));
|
367 |
|
368 |
}
|
369 |
|
370 |
}
|