/[aagtl_public1]/src/com/zoffcc/applications/aagtl/Rose.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/Rose.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sun Aug 5 14:00:28 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 50084 byte(s)
license text correction
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.io.FileNotFoundException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import android.content.Context;
28 import android.graphics.Bitmap;
29 import android.graphics.Canvas;
30 import android.graphics.Color;
31 import android.graphics.Paint;
32 import android.graphics.PointF;
33 import android.graphics.RectF;
34 import android.util.FloatMath;
35 import android.util.Log;
36 import android.view.MotionEvent;
37 import android.view.SurfaceHolder;
38 import android.view.SurfaceView;
39 import android.view.View;
40 import android.view.View.OnTouchListener;
41
42 public class Rose extends SurfaceView implements OnTouchListener, SurfaceHolder.Callback
43 {
44
45 ImageManager im;
46 int newx = 0, newy = 0;
47 int deltax = 0, deltay = 0;
48 int prev_x = 0, prev_y = 0;
49 int direction = 0;
50 boolean start_move = false;
51 static final int more_for_speed_x = 0;
52 static final int more_for_speed_y = 0;
53 static final int TOUCH_CACHE_RADIUS = 35;
54
55 static final int NONE = 0;
56 static final int DRAG = 1;
57 static final int ZOOM = 2;
58 static final int PRESS = 3;
59 int touch_mode = NONE;
60 float oldDist = 0;
61
62 PointF touch_now = new PointF(0, 0);
63 PointF touch_start = new PointF(0, 0);
64 PointF touch_prev = new PointF(0, 0);
65 PointF touch_last_load_tiles = new PointF(0, 0);
66 double map_center_x_before_move = 0;
67 double map_center_y_before_move = 0;
68 double map_heading_prev = 0;
69 double map_heading_start = 0;
70 double map_heading_last_load_tiles = 0;
71
72 //
73 //
74 //
75 static boolean use_parent_tiles = true;
76 //
77 //
78 //
79
80 public Boolean redraw_notify = false;
81
82 static final int larger_offset_x = 100;
83 static final int larger_offset_y = 100;
84
85 int tile_size_x = 256;
86 int tile_size_y = 256;
87 int num_tiles_x = 0;
88 int num_tiles_y = 0;
89 double map_center_x = 0.0;
90 double map_center_y = 0.0;
91 int zoom = 0;
92 Coordinate center_coord = null;
93 GeocacheCoordinate current_target = null;
94
95 public int mCanvasHeight = 1;
96 public int mCanvasWidth = 1;
97
98 Bitmap bitmap_main = null;
99 Canvas image_main = null;
100 Paint mainpaint = null;
101 Paint rectpaint = null;
102
103 Bitmap bitmap1 = null;
104 Bitmap bitmap2 = null;
105 Bitmap bitmap3 = null;
106 Bitmap bitmap4 = null;
107 Bitmap bitmap5 = null;
108 Bitmap bitmap6 = null;
109
110 Bitmap[][] map_tiles_onscreen = null;
111 String[][] map_tile_filename = null;
112 int[][][] map_tile_num_onscreen = null;
113 Bitmap[][] copy_bitmaps = null;
114 int[][][] copy_tiles = null;
115 Bitmap[] map_tiles_cache = null;
116 Bitmap[] map_tiles_parent_cache = null;
117 int[][] map_tiles_caches_values = null;
118 int[][] map_tiles_parent_caches_values = null;
119 int map_tiles_cache_oldest_counter = 0;
120 int map_tiles_parent_cache_oldest_counter = 0;
121 static final int map_tiles_cache_size = 15 * 3;
122 static final int map_tiles_parent_cache_size = 15 * 4;
123
124 List<Integer> yit = new ArrayList<Integer>();
125 List<Integer> xit = new ArrayList<Integer>();
126
127 aagtl main_object;
128
129 /** Handle to the surface manager object we interact with */
130 private SurfaceHolder mSurfaceHolder;
131
132 public static final int STATE_LOSE = 1;
133 public static final int STATE_PAUSE = 2;
134 public static final int STATE_READY = 3;
135 public static final int STATE_RUNNING = 4;
136 public static final int STATE_WIN = 5;
137
138 public static final int TILE_NULL = -1;
139 public static final int TILE_LOADING = 0;
140 public static final int TILE_LOADED_NEW = 1;
141 public static final int TILE_LOADED_DRAWN = 2;
142
143 public double[] deg2num(Coordinate coord)
144 {
145 double[] f_ret = new double[2];
146 f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom);
147 f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom);
148 // return --> tile_x,tile_y
149 return f_ret;
150 }
151
152 public double[] deg2num_give_zoom(Coordinate coord, int zoom2)
153 {
154 double[] f_ret = new double[2];
155 f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom2);
156 f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom2);
157 // return --> tile_x,tile_y
158 return f_ret;
159 }
160
161 public Coordinate num2deg(double xtile, double ytile)
162 {
163 double n = (1 << this.zoom);
164 double lon_deg = xtile / n * 360.0 - 180.0;
165 double lat_rad = Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n)));
166 double lat_deg = lat_rad * 180.0 / Math.PI;
167
168 Coordinate ret = new Coordinate(lat_deg, lon_deg);
169 return ret;
170 }
171
172 public Coordinate num2deg_give_zoom(double xtile, double ytile, int zoom2)
173 {
174 double n = (1 << zoom2);
175 double lon_deg = xtile / n * 360.0 - 180.0;
176 double lat_rad = Math.atan(Math.sinh(Math.PI * (1 - 2 * ytile / n)));
177 double lat_deg = lat_rad * 180.0 / Math.PI;
178
179 Coordinate ret = new Coordinate(lat_deg, lon_deg);
180 return ret;
181 }
182
183 public void set_center(Coordinate coord)
184 {
185 double[] f_arr = new double[2];
186 f_arr = deg2num(coord);
187 this.map_center_x = f_arr[0];
188 this.map_center_y = f_arr[1];
189 // System.out.println("set_center: " + map_center_x);
190 // System.out.println("set_center: " + map_center_y);
191 // System.out.println("set_center: " + coord.lat);
192 // System.out.println("set_center: " + coord.lon);
193 this.main_object.set_center(coord.lat, coord.lon);
194 }
195
196 synchronized public void __calc_tiles_on_display()
197 {
198 int xi = (int) map_center_x;
199 int yi = (int) map_center_y;
200
201 // long a = android.os.SystemClock.elapsedRealtime();
202 // make copies
203 if ((map_tiles_onscreen != null) && (map_tile_num_onscreen != null))
204 {
205 // System.out.println(String.valueOf(map_tiles_onscreen.length));
206 // System.out.println(String.valueOf(map_tile_num_onscreen.length));
207 for (int y = 0; y < map_tiles_onscreen.length; y++)
208 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
209 {
210 // System.out.println(String.valueOf(map_tiles_onscreen[y][x]));
211 copy_bitmaps[y][x] = map_tiles_onscreen[y][x];
212 }
213
214 for (int y = 0; y < map_tile_num_onscreen.length; y++)
215 for (int x = 0; x < map_tile_num_onscreen[y].length; x++)
216 for (int z = 0; z < 3; z++)
217 {
218 // System.out.println(String.valueOf(map_tile_num_onscreen[y][x][z]));
219 copy_tiles[y][x][z] = map_tile_num_onscreen[y][x][z];
220 }
221 }
222
223 // System.out.println("1:" +
224 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
225 // a = android.os.SystemClock.elapsedRealtime();
226
227 int i;
228 int j;
229 int s_x = xit.size();
230 int s_y = yit.size();
231 for (int m = 0; m < s_x; m++)
232 // for (int m = s_x - 1; m > -1; m--)
233 {
234 if (m % 2 == 0)
235 {
236 // even
237 int n = m / 2;
238 i = ((Integer) xit.get(n)).intValue();
239 }
240 else
241 {
242 // odd
243 int n = m / 2;
244 n = (s_x - 1) - n;
245 i = ((Integer) xit.get(n)).intValue();
246 }
247
248 for (int k = 0; k < s_y; k++)
249 // for (int k = s_y - 1; k > -1; k--)
250 {
251 if (k % 2 == 0)
252 {
253 // even
254 int n2 = k / 2;
255 j = ((Integer) xit.get(n2)).intValue();
256 }
257 else
258 {
259 // odd
260 int n2 = k / 2;
261 n2 = (s_y - 1) - n2;
262 j = ((Integer) yit.get(n2)).intValue();
263 }
264
265 int ww_2 = ((int) (num_tiles_x / 2)) + 1;
266 int hh_2 = ((int) (num_tiles_y / 2)) + 1;
267 int xx = xi + i - (ww_2);
268 int yy = yi + j - (hh_2);
269
270 // System.out.println(String.valueOf(i));
271 // System.out.println(String.valueOf(j));
272 // System.out.println(String.valueOf(ww_2));
273 // System.out.println(String.valueOf(hh_2));
274 // System.out.println(String.valueOf(xx));
275 // System.out.println(String.valueOf(yy));
276 // System.out.println(String.valueOf(xi));
277 // System.out.println(String.valueOf(yi));
278
279 // long b = android.os.SystemClock.elapsedRealtime();
280
281 check_tile(main_object.global_settings.map_type, zoom, xx, yy, i, j);
282
283 // System.out.println("b:" +
284 // String.valueOf(android.os.SystemClock.elapsedRealtime() -
285 // b));
286
287 }
288 }
289
290 // System.out.println("2:" +
291 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
292
293 }
294
295 public void change_map_type(int new_map_type)
296 {
297 main_object.global_settings.map_type = new_map_type;
298 // this.main_object.gcview.clear_stuff(); // not need
299 this.clear_stuff();
300 this.clear_stuff_2();
301 this.main_object.mdl.clear_stuff();
302 __calc_tiles_on_display();
303 draw_me();
304 // this.main_object.gcview.invalidate(); // not need
305 }
306
307 synchronized public void check_tile(int map_type, int zoom, int xx, int yy, int i, int j)
308 {
309 if (map_tile_num_onscreen == null)
310 {
311 return;
312 }
313
314 try
315 {
316 // System.out.println("" + (i - 1) + " " + (j - 1));
317 map_tile_num_onscreen[i - 1][j - 1][0] = xx;
318 map_tile_num_onscreen[i - 1][j - 1][1] = yy;
319 map_tile_filename[i - 1][j - 1] = this.main_object.mdl.get_local_url(map_type, zoom, xx, yy);
320
321 Boolean showing_parent = false;
322
323 Boolean found_it = false;
324 // decide where to get the tile from:
325 // 1.1 Memory ~10ms
326 // 1.2 Memory Cache ~10ms
327 // 2. File ~40ms
328 // 3. Internet background operation
329 // 3.x Parent tile on disk (optional) -> use_parent_tiles = t/f
330
331 for (int a = 0; a < copy_tiles.length; a++)
332 for (int b = 0; b < copy_tiles[a].length; b++)
333 {
334 if ((copy_tiles[a][b][0] == xx) && (copy_tiles[a][b][1] == yy))
335 {
336 // ok found it in memory
337 if (copy_bitmaps[a][b] != null)
338 {
339 if (CacheDownloader.DEBUG_) System.out.println("found it in memory");
340 map_tiles_onscreen[i - 1][j - 1] = copy_bitmaps[a][b];
341 // set to loaded
342 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_DRAWN;
343 found_it = true;
344 break;
345 }
346 }
347 }
348
349 if (!found_it)
350 {
351 Bitmap dummy_bitmap = get_tile_from_map_tile_cache(xx, yy);
352 if (dummy_bitmap != null)
353 {
354 // ok found it in big Bitmap cache
355 if (CacheDownloader.DEBUG_) System.out.println("found it in bitmap cache");
356 found_it = true;
357 map_tiles_onscreen[i - 1][j - 1] = dummy_bitmap;
358 // set to loaded
359 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_DRAWN;
360 }
361 }
362
363 if (!found_it)
364 {
365 String l_url = this.main_object.mdl.get_local_url(map_type, zoom, xx, yy);
366 Boolean is_dled = this.main_object.mdl.is_downloaded(l_url);
367 if (is_dled)
368 {
369 try
370 {
371 if (CacheDownloader.DEBUG_) System.out.println("found it on disk");
372 map_tiles_onscreen[i - 1][j - 1] = this.main_object.mdl.get_bitmap_from_local_url(l_url);
373 // add to big Bitmap cache, only if NOT in
374 add_to_map_tile_cache(map_tiles_onscreen[i - 1][j - 1], xx, yy);
375 // System.out.println(String.valueOf(map_tiles_onscreen[i
376 // - 1][j - 1]));
377 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADED_NEW;
378 }
379 catch (Exception e)
380 {
381
382 }
383 }
384 else
385 {
386 if (CacheDownloader.DEBUG_) System.out.println("11 start downloading tile in background thread");
387 //
388 //
389 // meanwhile see if we have parent tile on disk
390 if ((this.zoom > 2) && (use_parent_tiles))
391 {
392 if (CacheDownloader.DEBUG_) System.out.println("loading parent tile");
393 int less_zoom = zoom - 1;
394 Coordinate coord1 = this.num2deg(xx, yy);
395 double[] this_tile2 = new double[2];
396 this_tile2 = this.deg2num_give_zoom(coord1, less_zoom);
397 int this_tile3_x = (int) (this_tile2[0] + 0.001); // rounding fix!
398 int this_tile3_y = (int) (this_tile2[1] + 0.001); // rounding fix!
399
400 String[] new_file_names = this.main_object.mdl.get_remote_url(map_type, less_zoom, this_tile3_x, this_tile3_y);
401 String l_low = new_file_names[1];
402
403 // first look in parent bitmap cache for parent tile
404 Boolean is_in_parent_bitmap_cache = false;
405 Bitmap parent_tile = get_tile_from_map_tile_parent_cache(xx, yy);
406 if (parent_tile != null)
407 {
408 is_in_parent_bitmap_cache = true;
409 if (CacheDownloader.DEBUG_) System.out.println("found parent tile in bitmap cache");
410 }
411 else
412 {
413 Boolean is_dled_parent = this.main_object.mdl.is_downloaded(l_low);
414 if (CacheDownloader.DEBUG_) System.out.println("found parent tile on disk");
415 if (is_dled_parent)
416 {
417 if (CacheDownloader.DEBUG_) System.out.println("loading parent tile from disk");
418 try
419 {
420 parent_tile = this.main_object.mdl.get_bitmap_from_local_url(l_low);
421 }
422 catch (FileNotFoundException e)
423 {
424 if (CacheDownloader.DEBUG_) System.out.println("++parent tile, file not found++");
425 }
426 }
427 else
428 {
429 if (CacheDownloader.DEBUG_) System.out.println("**parent tile not here**");
430 }
431 }
432
433 if (parent_tile != null)
434 {
435 Bitmap quarter_tile = null;
436 if (!is_in_parent_bitmap_cache)
437 {
438 // resize tile
439 int new_size_x = this.tile_size_x * 2;
440 int new_size_y = this.tile_size_y * 2;
441 Bitmap parent_tile_scaled = Bitmap.createScaledBitmap(parent_tile, new_size_x, new_size_y, false);
442 //
443 int x_diff = 0;
444 int y_diff = 0;
445 if ((this_tile2[0] - this_tile3_x) > 0.1)
446 {
447 x_diff = this.tile_size_x;
448 }
449
450 else
451 {
452 x_diff = 0;
453 }
454 if ((this_tile2[1] - this_tile3_y) > 0.1)
455 {
456 y_diff = this.tile_size_y;
457 }
458 else
459 {
460 y_diff = 0;
461 }
462
463 // System.out.println("x_diff=" + x_diff);
464 // System.out.println("y_diff=" + y_diff);
465 // System.out.println("this_tile3_x=" +
466 // this_tile3_x);
467 // System.out.println("this_tile3_y=" +
468 // this_tile3_y);
469 // System.out.println("this_tile2[0]=" +
470 // this_tile2[0]);
471 // System.out.println("this_tile2[1]=" +
472 // this_tile2[1]);
473
474 quarter_tile = Bitmap.createBitmap(parent_tile_scaled, x_diff, y_diff, this.tile_size_x, this.tile_size_y);
475
476 add_to_map_tile_parent_cache(quarter_tile, xx, yy);
477 }
478 else
479 {
480 // parent_tile from bitmap cache is already the quarter tile!!
481 quarter_tile = parent_tile;
482 }
483
484 add_to_map_tile_cache(null, xx, yy);
485 map_tiles_onscreen[i - 1][j - 1] = quarter_tile;
486 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADING;
487
488 showing_parent = true;
489 }
490 }
491 if (CacheDownloader.DEBUG_) System.out.println("22 start downloading tile in background thread");
492 //
493 //
494 //
495 if (showing_parent)
496 {
497 if (CacheDownloader.DEBUG_) System.out.println("-- showing parent");
498 }
499 else
500 {
501 if (CacheDownloader.DEBUG_) System.out.println("-- NOT showing parent");
502 // System.out.println("bb1" + map_type + " " + zoom + " " + xx + " "
503 // + yy);
504 // Reset the Bitmap to null value
505 map_tiles_onscreen[i - 1][j - 1] = null;
506 map_tile_num_onscreen[i - 1][j - 1][2] = TILE_LOADING;
507 }
508 main_object.mdl.append_tile_to_list(map_type, zoom, xx, yy);
509 }
510 }
511 }
512 catch (Exception e)
513 {
514 if (CacheDownloader.DEBUG_) System.out.println("check_tile: Exception1");
515 }
516
517 }
518
519 synchronized public void add_to_map_tile_cache(Bitmap b, int tile_x, int tile_y)
520 {
521 int dummy2 = map_tiles_cache_oldest_counter;
522 int dummy3 = dummy2;
523 dummy2++;
524 if (dummy2 > map_tiles_cache_size - 1)
525 {
526 dummy2 = 0;
527 }
528 map_tiles_cache_oldest_counter = dummy2;
529 map_tiles_cache[dummy3] = b;
530 map_tiles_caches_values[dummy3][0] = tile_x;
531 map_tiles_caches_values[dummy3][1] = tile_y;
532
533 }
534
535 synchronized public void add_to_map_tile_parent_cache(Bitmap b, int tile_x, int tile_y)
536 {
537 int dummy2 = map_tiles_parent_cache_oldest_counter;
538 int dummy3 = dummy2;
539 dummy2++;
540 if (dummy2 > map_tiles_parent_cache_size - 1)
541 {
542 dummy2 = 0;
543 }
544 map_tiles_parent_cache_oldest_counter = dummy2;
545 map_tiles_parent_cache[dummy3] = b;
546 map_tiles_parent_caches_values[dummy3][0] = tile_x;
547 map_tiles_parent_caches_values[dummy3][1] = tile_y;
548
549 }
550
551 public Bitmap get_tile_from_map_tile_parent_cache(int tile_x, int tile_y)
552 {
553 Bitmap ret = null;
554 for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
555 {
556 if ((map_tiles_parent_caches_values[dummy][0] == tile_x) && (map_tiles_parent_caches_values[dummy][1] == tile_y))
557 {
558 ret = map_tiles_parent_cache[dummy];
559 break;
560 }
561 }
562 return ret;
563 }
564
565 public Bitmap get_tile_from_map_tile_cache(int tile_x, int tile_y)
566 {
567 Bitmap ret = null;
568 for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
569 {
570 if ((map_tiles_caches_values[dummy][0] == tile_x) && (map_tiles_caches_values[dummy][1] == tile_y))
571 {
572 ret = map_tiles_cache[dummy];
573 break;
574 }
575 }
576 return ret;
577 }
578
579 public void notify_tile_loaded_new(String local_filename)
580 {
581 // System.out.println("lf: " + local_filename);
582 Boolean must_redraw = false;
583 // synchronized (map_tile_num_onscreen)
584 // {
585 for (int a = 0; a < map_tile_num_onscreen.length; a++)
586 for (int b = 0; b < map_tile_num_onscreen[a].length; b++)
587 {
588 // System.out.println("lf search: " + map_tile_filename[a][b]);
589 if (map_tile_filename[a][b].equals(local_filename))
590 {
591 if (this.main_object.mdl.is_downloaded(local_filename))
592 {
593 try
594 {
595 map_tile_num_onscreen[a][b][2] = TILE_LOADED_NEW;
596 // refresh it from disk!
597 map_tiles_onscreen[a][b] = this.main_object.mdl.get_bitmap_from_local_url(local_filename);
598 must_redraw = true;
599 // System.out.println("tile loaded!!");
600 }
601 catch (FileNotFoundException e)
602 {
603
604 }
605 }
606 }
607 }
608
609 if (must_redraw)
610 {
611 // only draw if we are NOT in drag mode right now!
612 if (touch_mode != DRAG)
613 {
614 draw_me();
615 }
616 else
617 {
618 redraw_notify = true;
619 // System.out.println("redraw_notify ON 1");
620 }
621 }
622 // }
623
624 }
625
626 public void clear_stuff()
627 {
628 map_tiles_cache = new Bitmap[map_tiles_cache_size];
629 map_tiles_parent_cache = new Bitmap[map_tiles_parent_cache_size];
630 map_tiles_caches_values = new int[map_tiles_cache_size][2];
631 map_tiles_parent_caches_values = new int[map_tiles_parent_cache_size][2];
632 for (int dummy = 0; dummy < map_tiles_cache_size; dummy++)
633 {
634 map_tiles_cache[dummy] = null;
635 map_tiles_caches_values[dummy][0] = -1;
636 map_tiles_caches_values[dummy][1] = -1;
637 }
638 for (int dummy = 0; dummy < map_tiles_parent_cache_size; dummy++)
639 {
640 map_tiles_parent_cache[dummy] = null;
641 map_tiles_parent_caches_values[dummy][0] = -1;
642 map_tiles_parent_caches_values[dummy][1] = -1;
643 }
644 }
645
646 public void clear_stuff_2()
647 {
648 map_tiles_onscreen = new Bitmap[num_tiles_x][num_tiles_y];
649 map_tile_filename = new String[num_tiles_x][num_tiles_y];
650 map_tile_num_onscreen = new int[num_tiles_x][num_tiles_y][3];
651
652 copy_bitmaps = new Bitmap[num_tiles_x][num_tiles_y];
653 copy_tiles = new int[num_tiles_x][num_tiles_y][3];
654
655 for (int y = 0; y < map_tiles_onscreen.length; y++)
656 for (int x = 0; x < map_tiles_onscreen[y].length; x++)
657 {
658 // System.out.println("xxxxxxx:" + y + " " + x + " " +
659 // String.valueOf(map_tiles_onscreen[y][x]));
660 map_tiles_onscreen[y][x] = null;
661 }
662
663 for (int y = 0; y < map_tile_num_onscreen.length; y++)
664 for (int x = 0; x < map_tile_num_onscreen[y].length; x++)
665 {
666 for (int z = 0; z < 2; z++)
667 {
668 map_tile_num_onscreen[y][x][z] = -1;
669 // System.out.println("yyyyyyy:" + y + " " + x + " " + z +
670 // " " + String.valueOf(map_tile_num_onscreen[y][x][z]));
671 }
672 map_tile_num_onscreen[y][x][2] = TILE_NULL;
673 map_tile_filename[y][x] = "";
674 }
675 }
676
677 public void init_me()
678 {
679 mainpaint = new Paint(0);
680 mainpaint.setAntiAlias(false);
681 mainpaint.setDither(false);
682
683 rectpaint = new Paint(0);
684 rectpaint.setAntiAlias(false);
685 rectpaint.setDither(false);
686
687 bitmap_main = Bitmap.createBitmap(9, 9, Bitmap.Config.ARGB_8888);
688 image_main = new Canvas(bitmap_main);
689
690 this.clear_stuff();
691
692 int width = this.getWidth();
693 int height = this.getHeight();
694 // how many tiles on screen?
695 num_tiles_x = (width / tile_size_x) + 2;
696 num_tiles_y = (height / tile_size_y) + 2;
697
698 // if modulo 2, then add +1 (this always makes it an odd number!)
699 num_tiles_x = num_tiles_x + Math.abs((num_tiles_x % 2) - 1);
700 num_tiles_y = num_tiles_y + Math.abs((num_tiles_y % 2) - 1);
701
702 // System.out.println("num t x 1:" + String.valueOf(num_tiles_x));
703 // System.out.println("num t y 1:" + String.valueOf(num_tiles_y));
704
705 zoom = main_object.global_settings.map_zoom;
706 // System.out.println("rose:zoom3=" + zoom);
707
708 center_coord = new Coordinate(main_object.global_settings.map_position_lat, main_object.global_settings.map_position_lon);
709 // System.out.println(String.valueOf(main_object.global_settings.map_position_lat));
710 set_center(center_coord);
711 // System.out.println("Center coord x:" + String.valueOf(map_center_x));
712 // System.out.println("Center coord y:" + String.valueOf(map_center_y));
713 __calc_tiles_on_display();
714 }
715
716 public void zoom_in()
717 {
718 if (this.zoom < 19)
719 {
720 this.set_zoom(this.zoom + 1);
721 }
722 }
723
724 public void zoom_out()
725 {
726 if (this.zoom > 1)
727 {
728 this.set_zoom(this.zoom - 1);
729 }
730 }
731
732 public void set_zoom(int new_zoom)
733 {
734 int temp = new_zoom;
735 this.main_object.gcview.clear_stuff();
736 this.clear_stuff();
737 this.clear_stuff_2();
738 this.main_object.mdl.clear_stuff();
739 this.zoom = temp;
740 main_object.global_settings.map_zoom = this.zoom;
741 this.set_center(new Coordinate(this.main_object.global_settings.map_position_lat, this.main_object.global_settings.map_position_lon));
742 __calc_tiles_on_display();
743 draw_me();
744 this.load_caches_from_db();
745 this.main_object.gcview.invalidate();
746
747 }
748
749 public int[] __num2point(int xtile, int ytile)
750 {
751 int[] ret = new int[2];
752 // tiles to pixels on screen
753 // System.out.println("xt:" + xtile + " yt:" + ytile);
754 // System.out.println("mcx:" + map_center_x + " mcy:" + map_center_y);
755 // double d1 = xtile * tile_size_x;
756 // double d2 = map_center_x * tile_size_x;
757 // double d3 = mCanvasWidth / 2;
758 // System.out.println("DDD: " + d1 + " " + d2 + " " + d3);
759 ret[0] = (int) ((xtile * tile_size_x) - (map_center_x * tile_size_x) + (mCanvasWidth / 2));
760 ret[1] = (int) ((ytile * tile_size_y) - (map_center_y * tile_size_y) + (mCanvasHeight / 2));
761 return ret;
762 }
763
764 synchronized public void draw_me()
765 {
766 // System.out.println(String.valueOf(c.getHeight()));
767 // System.out.println(String.valueOf(c.getWidth()));
768 // c.setDensity(480);
769 // c.setDensity(160);
770
771 // long a = android.os.SystemClock.elapsedRealtime();
772
773 int[] dummy = new int[2];
774 for (int xtemp = 0; xtemp < num_tiles_x; xtemp++)
775 {
776 for (int ytemp = 0; ytemp < num_tiles_y; ytemp++)
777 {
778 if (map_tile_num_onscreen == null)
779 {
780 return;
781 }
782 if (map_tiles_onscreen == null)
783 {
784 return;
785 }
786
787 // System.out.println("x,y: " + xtemp + " " + ytemp);
788 dummy = __num2point(map_tile_num_onscreen[xtemp][ytemp][0], map_tile_num_onscreen[xtemp][ytemp][1]);
789 // System.out.println("dummy: " + dummy[0] + " " + dummy[1]);
790 // if (false)
791 if (map_tiles_onscreen[xtemp][ytemp] != null)
792 {
793 image_main.drawBitmap(map_tiles_onscreen[xtemp][ytemp], dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, mainpaint);
794 }
795 else
796 {
797 rectpaint.setColor(Color.BLACK);
798 rectpaint.setStyle(Paint.Style.FILL);
799 RectF rectf = new RectF(dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, dummy[0] + tile_size_x + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y + tile_size_y);
800 image_main.drawRect(rectf, rectpaint);
801
802 rectpaint.setColor(Color.RED);
803 rectpaint.setStyle(Paint.Style.STROKE);
804 rectf = new RectF(dummy[0] + Rose.larger_offset_x, dummy[1] + Rose.larger_offset_y, dummy[0] + tile_size_x + Rose.larger_offset_x, dummy[1] + tile_size_y + Rose.larger_offset_y);
805 image_main.drawRect(rectf, rectpaint);
806 }
807 }
808 }
809
810 // System.out.println("dm1:" +
811 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
812 // a = android.os.SystemClock.elapsedRealtime();
813
814 Canvas c = null;
815 try
816 {
817 c = mSurfaceHolder.lockCanvas(null);
818 synchronized (mSurfaceHolder)
819 {
820 // System.out.println(String.valueOf(c));
821 doDraw(c);
822 }
823 }
824 finally
825 {
826 if (c != null)
827 {
828 mSurfaceHolder.unlockCanvasAndPost(c);
829 }
830 }
831
832 // System.out.println("dm2:" +
833 // String.valueOf(android.os.SystemClock.elapsedRealtime() - a));
834
835 // ok , reset global redraw notifier
836 // System.out.println("redraw_notify *OFF* 1");
837 redraw_notify = false;
838 }
839
840 /* Callback invoked when the surface dimensions change. */
841 public void setSurfaceSize(int width, int height)
842 {
843 // synchronized to make sure these all change atomically
844 synchronized (mSurfaceHolder)
845 {
846 // System.out.println("x2:" + String.valueOf(width));
847 // System.out.println("y2:" + String.valueOf(height));
848
849 // mCanvasWidth = width;
850 // mCanvasHeight = height;
851 }
852 }
853
854 public int[] __coord2point(Coordinate c)
855 {
856 int[] ret = new int[2];
857 double[] point = new double[2];
858
859 point = this.deg2num(c);
860 ret[0] = (int) (point[0] * this.tile_size_x - this.map_center_x * this.tile_size_x + this.mCanvasWidth / 2);
861 ret[1] = (int) (point[1] * this.tile_size_y - this.map_center_y * this.tile_size_y + this.mCanvasHeight / 2);
862
863 return ret;
864 }
865
866 public GeocacheCoordinate check_for_cache(int touch_x, int touch_y)
867 {
868 GeocacheCoordinate ret = null;
869 GeocacheCoordinate tmp = null;
870
871 for (int i = 0; i < this.main_object.gcview.caches_loaded.size(); i++)
872 {
873 int[] p = new int[2];
874 try
875 {
876 tmp = this.main_object.gcview.caches_loaded.get(i);
877 // p = this.__coord2point(new Coordinate(tmp.lat, tmp.lon));
878 p = this.__coord2point((Coordinate) tmp);
879 if ((Math.abs(p[0] - touch_x) < TOUCH_CACHE_RADIUS) && (Math.abs(p[1] - touch_y) < TOUCH_CACHE_RADIUS))
880 {
881 ret = tmp;
882 return ret;
883 }
884 }
885 catch (IndexOutOfBoundsException e)
886 {
887
888 }
889 }
890
891 return ret;
892 }
893
894 /** Show an event in the LogCat view, for debugging */
895 private void dumpEvent(MotionEvent event)
896 {
897 String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?", "8?", "9?" };
898 StringBuilder sb = new StringBuilder();
899 int action = event.getAction();
900 int actionCode = action & MotionEvent.ACTION_MASK;
901 sb.append("event ACTION_").append(names[actionCode]);
902 if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP)
903 {
904 sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
905 sb.append(")");
906 }
907 sb.append("[");
908 for (int i = 0; i < event.getPointerCount(); i++)
909 {
910 sb.append("#").append(i);
911 sb.append("(pid ").append(event.getPointerId(i));
912 sb.append(")=").append((int) event.getX(i));
913 sb.append(",").append((int) event.getY(i));
914 if (i + 1 < event.getPointerCount()) sb.append(";");
915 }
916 sb.append("]");
917 Log.d("dump event", sb.toString());
918 }
919
920 private float spacing(MotionEvent event)
921 {
922 float x = event.getX(0) - event.getX(1);
923 float y = event.getY(0) - event.getY(1);
924 return FloatMath.sqrt(x * x + y * y);
925 }
926
927 private float spacing(PointF a, PointF b)
928 {
929 float x = a.x - b.x;
930 float y = a.y - b.y;
931 return FloatMath.sqrt(x * x + y * y);
932 }
933
934 public PointF turn_point(PointF point, double angle)
935 {
936 float x = point.x - (this.getWidth() / 2);
937 float y = point.y - (this.getHeight() / 2);
938 float x2 = 0;
939 float y2 = 0;
940 x2 = (this.getWidth() / 2) + FloatMath.cos((float) Math.toRadians(angle)) * x - FloatMath.sin((float) Math.toRadians(angle)) * y;
941 y2 = (this.getHeight() / 2) + FloatMath.sin((float) Math.toRadians(angle)) * x + FloatMath.cos((float) Math.toRadians(angle)) * y;
942 PointF point2 = new PointF(x2, y2);
943 return point2;
944 }
945
946 private void midPoint(PointF point, MotionEvent event)
947 {
948 float x = event.getX(0) + event.getX(1);
949 float y = event.getY(0) + event.getY(1);
950 point.set(x / 2, y / 2);
951 }
952
953 public boolean onTouch(View view, MotionEvent event)
954 {
955 return onTouch_aagtl(view, event);
956 }
957
958 public boolean onTouch_aagtl(View view, MotionEvent event)
959 {
960 String TAG = "onTouch event";
961
962 // dumpEvent(event);
963
964 PointF touch_now2 = null;
965 PointF touch_start2 = null;
966 PointF touch_prev2 = null;
967 PointF touch_last_load_tiles2 = null;
968
969 // touch_now2 = turn_point(this.touch_now,
970 // this.main_object.cross.get_gps_heading());
971 // touch_start2 = turn_point(this.touch_start,
972 // this.main_object.cross.get_gps_heading());
973 // touch_prev2 = turn_point(this.touch_prev,
974 // this.main_object.cross.get_gps_heading());
975 // touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles,
976 // this.main_object.cross
977 // .get_gps_heading());
978
979 switch (event.getAction() & MotionEvent.ACTION_MASK)
980 {
981 case MotionEvent.ACTION_DOWN:
982 this.touch_now.set(event.getX(), event.getY());
983 this.touch_start.set(event.getX(), event.getY());
984 map_heading_start = this.main_object.cross.get_gps_heading();
985 this.touch_prev.set(event.getX(), event.getY());
986 map_heading_prev = this.main_object.cross.get_gps_heading();
987 this.touch_last_load_tiles.set(event.getX(), event.getY());
988 map_heading_last_load_tiles = this.main_object.cross.get_gps_heading();
989 map_center_x_before_move = this.map_center_x;
990 map_center_y_before_move = this.map_center_y;
991 touch_mode = DRAG;
992
993 // Log.d(TAG, "touch_mode=(START!!)");
994 // Log.d(TAG, "start=" + this.touch_start.x + "," +
995 // this.touch_start.y);
996 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
997 // this.touch_prev.y);
998 // Log.d(TAG, "now=" + this.touch_now.x + "," + this.touch_now.y);
999
1000 break;
1001 case MotionEvent.ACTION_UP:
1002 case MotionEvent.ACTION_POINTER_UP:
1003 this.touch_now.set(event.getX(), event.getY());
1004 if (this.main_object.global_settings.options_turn_map_on_heading)
1005 {
1006 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1007 touch_start2 = turn_point(this.touch_start, map_heading_start);
1008 }
1009 else
1010 {
1011 touch_now2 = touch_now;
1012 touch_start2 = touch_start;
1013 }
1014 // Log.d(TAG, "hd1=" + this.main_object.cross.get_gps_heading());
1015 // Log.d(TAG, "hd2=" + map_heading_start);
1016
1017 // Log.d(TAG, "spacing from start=" + spacing(touch_start2,
1018 // touch_now2));
1019
1020 if ((touch_mode == DRAG) && (spacing(touch_start2, touch_now2) < 8f))
1021 {
1022 // just a single press down
1023 touch_mode = PRESS;
1024
1025 // check if OSD-button was touched
1026 if (this.main_object.follow_button_rect.contains(touch_start2.x, touch_start2.y))
1027 {
1028 // toggle follow mode
1029 if (this.main_object.follow_mode)
1030 {
1031 this.main_object.turn_off_follow_mode();
1032 this.main_object.cross.invalidate();
1033 }
1034 else
1035 {
1036 this.main_object.turn_on_follow_mode();
1037 this.main_object.cross.invalidate();
1038 }
1039 }
1040 else if (this.main_object.arrow_button_rect.contains(touch_start2.x, touch_start2.y))
1041 {
1042 // arrow OSD button pressed
1043 this.main_object.set_display_screen(aagtl.DISPLAY_VIEW_ARROW);
1044 return true;
1045 }
1046 // check if a cache was touched
1047 else if (this.main_object.gcview.caches_loaded != null)
1048 {
1049 // only allow to touch caches if a certain zoom level is reached
1050 if (this.zoom >= aagtl.TOUCH_CACHES_AFTER_THIS_ZOOM_LEVEL)
1051 {
1052 System.out.println("XXXXXXXX zoom=" + this.zoom);
1053 if (this.main_object.gcview.caches_loaded.size() <= this.main_object.gcview.MAX_DRAW_POINTS)
1054 {
1055 GeocacheCoordinate cache_touched = this.check_for_cache((int) touch_start2.x, (int) touch_start2.y);
1056 if (cache_touched != null)
1057 {
1058 this.main_object.cacheview.set_cache(cache_touched);
1059 this.main_object.set_display_screen(aagtl.DISPLAY_VIEW_GC);
1060 return true;
1061 }
1062 }
1063 }
1064 }
1065
1066 if (redraw_notify)
1067 {
1068 // System.out.println("redraw_notify 1");
1069 // __calc_tiles_on_display();
1070 draw_me();
1071 }
1072 // check if a cache was touched
1073
1074 // Log.d(TAG, "touch_mode=PRESS");
1075 // Log.d(TAG, "start=" + touch_start.x + "," + touch_start.y);
1076 // Log.d(TAG, "start2=" + touch_start2.x + "," +
1077 // touch_start2.y);
1078 // Log.d(TAG, "prev=" +touch_prev.x + "," + touch_prev.y);
1079 // Log.d(TAG, "now=" + touch_now.x + "," + touch_now.y);
1080 // Log.d(TAG, "now2=" + touch_now2.x + "," + touch_now2.y);
1081 touch_mode = NONE;
1082 }
1083 else
1084 {
1085 if (touch_mode == DRAG)
1086 {
1087 if (this.main_object.global_settings.options_turn_map_on_heading)
1088 {
1089 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1090 touch_start2 = turn_point(this.touch_start, map_heading_start);
1091 }
1092 else
1093 {
1094 touch_now2 = touch_now;
1095 touch_start2 = touch_start;
1096 }
1097
1098 // end of "drag" move
1099 // Log.d(TAG, "spacing from start=" +
1100 // spacing(this.touch_start, this.touch_now));
1101
1102 // ------------ OLD routine --------------
1103 // ------------ OLD routine --------------
1104 // ------------ OLD routine --------------
1105 // PointF now_turned = turn_point(this.touch_now,
1106 // this.main_object.cross
1107 // .get_gps_heading());
1108 // PointF start_turned = turn_point(this.touch_start,
1109 // this.main_object.cross
1110 // .get_gps_heading());
1111 double map_center_x_t = map_center_x_before_move - ((float) (touch_now2.x - touch_start2.x) / (float) tile_size_x);
1112 double map_center_y_t = map_center_y_before_move - ((float) (touch_now2.y - touch_start2.y) / (float) tile_size_y);
1113 Coordinate temp_c = this.num2deg(map_center_x_t, map_center_y_t);
1114 set_center(temp_c);
1115 __calc_tiles_on_display();
1116 draw_me();
1117 // draw caches
1118 // this.main_object.gcview.invalidate();
1119 // load caches fresh from DB
1120 this.load_caches_from_db();
1121
1122 this.main_object.change_status_text(String.format("lat %.5f", temp_c.lat) + " " + String.format("lon %.5f", temp_c.lon));
1123 // this.main_object.status_text.append(" " +
1124 // String.format("%.3f", this.map_center_x) + " "
1125 // + String.format("%.3f", this.map_center_y));
1126 // ------------ OLD routine --------------
1127 // ------------ OLD routine --------------
1128 // ------------ OLD routine --------------
1129
1130 // Log.d(TAG, "touch_mode=NONE (END of DRAG) (" +
1131 // (event.getX() - this.touch_prev.x)
1132 // + "," + (event.getY() - this.touch_prev.y) + ")");
1133 // Log.d(TAG, "touch_mode=NONE (END of DRAG) ("
1134 // + (event.getX() - this.touch_start.x) + ","
1135 // + (event.getY() - this.touch_start.y) + ")");
1136
1137 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1138 // this.touch_start.y);
1139 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1140 // this.touch_prev.y);
1141 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1142 // this.touch_now.y);
1143 touch_mode = NONE;
1144 }
1145 else
1146 {
1147 if (touch_mode == ZOOM)
1148 {
1149 // end of "pinch zoom" move
1150 float newDist = spacing(event);
1151 float scale = 0;
1152 if (newDist > 10f)
1153 {
1154 scale = newDist / oldDist;
1155 }
1156
1157 if (scale > 1.3)
1158 {
1159 // zoom in
1160 this.zoom_in();
1161 }
1162 else if (scale < 0.8)
1163 {
1164 // zoom out
1165 this.zoom_out();
1166 }
1167
1168 // Log.d(TAG, "touch_mode=NONE (END of ZOOM part 1) (" +
1169 // scale + ")");
1170 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1171 // this.touch_start.y);
1172 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1173 // this.touch_prev.y);
1174 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1175 // this.touch_now.y);
1176 touch_mode = NONE;
1177 }
1178 else
1179 {
1180 // Log.d(TAG, "touch_mode=NONE (END of ZOOM part 2)");
1181 touch_mode = NONE;
1182 }
1183 }
1184 }
1185 break;
1186 case MotionEvent.ACTION_MOVE:
1187 if (touch_mode == DRAG)
1188 {
1189 this.touch_now.set(event.getX(), event.getY());
1190 if (this.main_object.global_settings.options_turn_map_on_heading)
1191 {
1192 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1193 touch_start2 = turn_point(this.touch_start, map_heading_start);
1194 touch_prev2 = turn_point(this.touch_prev, map_heading_prev);
1195 touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles, map_heading_last_load_tiles);
1196 }
1197 else
1198 {
1199 touch_now2 = touch_now;
1200 touch_start2 = touch_start;
1201 touch_prev2 = touch_prev;
1202 touch_last_load_tiles2 = touch_last_load_tiles;
1203 }
1204
1205 // Log.d(TAG, "spacing from start=" + spacing(touch_start2,
1206 // touch_now2));
1207
1208 if ((Math.abs(touch_now2.x - touch_last_load_tiles2.x) < tile_size_x * 0.4) && (Math.abs(touch_now2.y - touch_last_load_tiles2.y) < tile_size_y * 0.4))
1209 {
1210 // only small move -> smear image
1211 // System.out.println("DRAG -> near");
1212
1213 // ------------ OLD routine ------------
1214 // ------------ OLD routine ------------
1215 // ------------ OLD routine ------------
1216 // System.out.println("Ms d:" + deltax + " " + deltay +
1217 // " p:" + prev_x + " " + prev_y + "mc:"
1218 // + map_center_x + " " + map_center_y + " C" + X + " " +
1219 // Y);
1220
1221 double tempxxx = ((float) (touch_now2.x - touch_prev2.x) / (float) tile_size_x);
1222 double tempyyy = ((float) (touch_now2.y - touch_prev2.y) / (float) tile_size_y);
1223 // System.out.println("tx:" + tempxxx + " ty:" + tempyyy);
1224 double map_center_x_t8 = map_center_x - tempxxx;
1225 double map_center_y_t8 = map_center_y - tempyyy;
1226 // System.out.println("tx:" + tile_size_x + " ty:" +
1227 // tile_size_y);
1228 // System.out.println("mxn:" + map_center_x_t8 + " myn:" +
1229 // map_center_y_t8);
1230 Coordinate temp_c8 = this.num2deg(map_center_x_t8, map_center_y_t8);
1231 set_center(temp_c8);
1232
1233 Canvas c = null;
1234 try
1235 {
1236 c = mSurfaceHolder.lockCanvas(null);
1237 synchronized (mSurfaceHolder)
1238 {
1239 if ((c != null) && (bitmap_main != null))
1240 {
1241 if (this.main_object.global_settings.options_turn_map_on_heading)
1242 {
1243 c.rotate((int) -this.main_object.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
1244 }
1245 c.drawBitmap(bitmap_main, touch_now2.x - touch_last_load_tiles2.x - larger_offset_x, touch_now2.y - touch_last_load_tiles2.y - larger_offset_y, null);
1246 // draw caches
1247 // System.out.println("iigc2");
1248 this.main_object.gcview.invalidate();
1249 }
1250 }
1251 }
1252 finally
1253 {
1254 if (c != null)
1255 {
1256 mSurfaceHolder.unlockCanvasAndPost(c);
1257 }
1258 }
1259 this.main_object.change_status_text(String.format("lat %.5f", temp_c8.lat) + " " + String.format("lon %.5f", temp_c8.lon));
1260 // this.main_object.status_text.append(" " +
1261 // String.format("%.3f", this.map_center_x) + " "
1262 // + String.format("%.3f", this.map_center_y));
1263 // ------------ OLD routine ------------
1264 // ------------ OLD routine ------------
1265 // ------------ OLD routine ------------
1266 }
1267 else
1268 {
1269 // bigger move -> load tiles new, draw new
1270 // System.out.println("DRAG -> far");
1271
1272 // ------------ OLD routine ------------
1273 // ------------ OLD routine ------------
1274 // ------------ OLD routine ------------
1275
1276 // System.out.println("Mb d:" + deltax + " " + deltay +
1277 // " p:" + prev_x + " " + prev_y + "mc:"
1278 // + map_center_x + " " + map_center_y + " C" + X + " " +
1279 // Y);
1280 if (this.main_object.global_settings.options_turn_map_on_heading)
1281 {
1282 touch_now2 = turn_point(this.touch_now, this.main_object.cross.get_gps_heading());
1283 touch_start2 = turn_point(this.touch_start, map_heading_start);
1284 touch_prev2 = turn_point(this.touch_prev, map_heading_prev);
1285 touch_last_load_tiles2 = turn_point(this.touch_last_load_tiles, map_heading_last_load_tiles);
1286 }
1287 else
1288 {
1289 touch_now2 = touch_now;
1290 touch_start2 = touch_start;
1291 touch_prev2 = touch_prev;
1292 touch_last_load_tiles2 = touch_last_load_tiles;
1293 }
1294
1295 double tempxxx = ((float) (touch_now2.x - touch_prev2.x) / (float) tile_size_x);
1296 double tempyyy = ((float) (touch_now2.y - touch_prev2.y) / (float) tile_size_y);
1297 // System.out.println("tx:" + tempxxx + " ty:" + tempyyy);
1298 double map_center_x_t8 = map_center_x - tempxxx;
1299 double map_center_y_t8 = map_center_y - tempyyy;
1300 // System.out.println("tx:" + tile_size_x + " ty:" +
1301 // tile_size_y);
1302 // System.out.println("mxn:" + map_center_x_t8 + " myn:" +
1303 // map_center_y_t8);
1304 Coordinate temp_c8 = this.num2deg(map_center_x_t8, map_center_y_t8);
1305 set_center(temp_c8);
1306
1307 // double map_center_x_t22 = map_center_x
1308 // - ((float) (this.touch_now.x - this.touch_prev.x) /
1309 // (float) tile_size_x);
1310 // double map_center_y_t22 = map_center_y
1311 // - ((float) (this.touch_now.x - this.touch_prev.y) /
1312 // (float) tile_size_y);
1313 // Coordinate temp_c22 = this.num2deg(map_center_x_t22,
1314 // map_center_y_t22);
1315 // set_center(temp_c22);
1316
1317 __calc_tiles_on_display();
1318 draw_me();
1319 // draw caches
1320 // this.main_object.gcview.invalidate();
1321 // load caches fresh from DB
1322 this.load_caches_from_db();
1323
1324 this.main_object.change_status_text(String.format("lat %.5f", temp_c8.lat) + " " + String.format("lon %.5f", temp_c8.lon));
1325 // this.main_object.status_text.append(" " +
1326 // String.format("%.3f", this.map_center_x) + " "
1327 // + String.format("%.3f", this.map_center_y));
1328
1329 // ------------ OLD routine ------------
1330 // ------------ OLD routine ------------
1331 // ------------ OLD routine ------------
1332
1333 // set "last load tiles" point
1334 this.touch_last_load_tiles.set(this.touch_now.x, this.touch_now.y);
1335 map_heading_last_load_tiles = this.main_object.cross.get_gps_heading();
1336 }
1337
1338 this.touch_prev.set(event.getX(), event.getY());
1339 map_heading_prev = this.main_object.cross.get_gps_heading();
1340 }
1341 else if (touch_mode == ZOOM)
1342 {
1343 this.touch_now.set(event.getX(), event.getY());
1344 // float newDist = spacing(event);
1345 // Log.d(TAG, "newDist=" + newDist);
1346 // if (newDist > 10f)
1347 // {
1348 // matrix.set(savedMatrix);
1349 // float scale = newDist / oldDist;
1350
1351 // Log.d(TAG, "touch_mode=ZOOM (" + scale + ")");
1352 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1353 // this.touch_start.y);
1354 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1355 // this.touch_prev.y);
1356 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1357 // this.touch_now.y);
1358 // matrix.postScale(scale, scale, mid.x, mid.y);
1359 // }
1360
1361 this.touch_prev.set(event.getX(), event.getY());
1362 map_heading_prev = this.main_object.cross.get_gps_heading();
1363 }
1364 break;
1365 case MotionEvent.ACTION_POINTER_DOWN:
1366 oldDist = spacing(event);
1367 // Log.d(TAG, "oldDist=" + oldDist);
1368 if (oldDist > 10f)
1369 {
1370 // savedMatrix.set(matrix);
1371 // midPoint(mid, event);
1372 touch_mode = ZOOM;
1373
1374 // Log.d(TAG, "touch_mode=ZOOM");
1375 // Log.d(TAG, "start=" + this.touch_start.x + "," +
1376 // this.touch_start.y);
1377 // Log.d(TAG, "prev=" + this.touch_prev.x + "," +
1378 // this.touch_prev.y);
1379 // Log.d(TAG, "now=" + this.touch_now.x + "," +
1380 // this.touch_now.y);
1381 }
1382 break;
1383 }
1384 return true;
1385 }
1386
1387 public void load_caches_from_db()
1388 {
1389 List<GeocacheCoordinate> caches_read = new ArrayList<GeocacheCoordinate>();
1390 try
1391 {
1392 caches_read = this.main_object.pv.get_points_filter(this.main_object.get_visible_area_large(), this.main_object.global_settings.options_hide_found, 50);
1393 }
1394 catch (Exception e)
1395 {
1396 e.printStackTrace();
1397 }
1398 this.main_object.gcview.set_loaded_caches(caches_read);
1399 }
1400
1401 public void draw_view()
1402 {
1403 Canvas c = null;
1404 try
1405 {
1406 c = mSurfaceHolder.lockCanvas(null);
1407 synchronized (mSurfaceHolder)
1408 {
1409 if ((c != null) && (bitmap_main != null))
1410 {
1411 this.doDraw(c);
1412 }
1413 }
1414 }
1415 finally
1416 {
1417 if (c != null)
1418 {
1419 mSurfaceHolder.unlockCanvasAndPost(c);
1420 }
1421 }
1422 }
1423
1424 private void doDraw(Canvas canvas)
1425 {
1426 // System.out.println("Rose: doDraw");
1427 if ((canvas != null) && (bitmap_main != null))
1428 {
1429 // System.out.println("mcx:" + map_center_x);
1430 // System.out.println("mcy:" + map_center_y);
1431 if (this.main_object.global_settings.options_turn_map_on_heading)
1432 {
1433 canvas.rotate((int) -this.main_object.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
1434 }
1435 canvas.drawBitmap(bitmap_main, -Rose.larger_offset_x, -Rose.larger_offset_y, null);
1436 }
1437 }
1438
1439 public Rose(Context context, aagtl m)
1440 {
1441 super(context);
1442
1443 // register our interest in hearing about changes to our surface
1444 SurfaceHolder holder = getHolder();
1445 mSurfaceHolder = holder;
1446 main_object = m;
1447 holder.addCallback(this);
1448
1449 init_me();
1450
1451 // System.out.println("x " + String.valueOf(this.getWidth()));
1452 // System.out.println("y " + String.valueOf(this.getHeight()));
1453
1454 setFocusable(true); // make sure we get key events
1455 this.setOnTouchListener(this); // get touchscreen events
1456 }
1457
1458 @Override
1459 public void onWindowFocusChanged(boolean hasWindowFocus)
1460 {
1461 // System.out.println("rose:zoom2=" + zoom);
1462 }
1463
1464 /* Callback invoked when the surface dimensions change. */
1465 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
1466 {
1467 // System.out.println("surfaceChanged:" + String.valueOf(width));
1468 // System.out.println("surfaceChanged:" + String.valueOf(height));
1469
1470 this.mCanvasWidth = width;
1471 this.mCanvasHeight = height;
1472
1473 // -----------------------------------------
1474 // -----------------------------------------
1475 // OSD button positions
1476 this.main_object.follow_button_rect = new RectF(10, this.mCanvasHeight - this.main_object.follow_current.getHeight() - 10, 10 + this.main_object.follow_current.getWidth(), this.mCanvasHeight - 10);
1477
1478 this.main_object.arrow_button_rect = new RectF(this.mCanvasWidth - 10 - this.main_object.arrow_button.getWidth(), this.mCanvasHeight - this.main_object.arrow_button.getHeight() - 10, this.mCanvasWidth - 10, this.mCanvasHeight - 10);
1479 // OSD button positions
1480 // -----------------------------------------
1481 // -----------------------------------------
1482
1483 if (bitmap_main != null)
1484 {
1485 // try to free the memory of bitmap
1486 bitmap_main.recycle();
1487 }
1488 bitmap_main = Bitmap.createBitmap((this.getWidth() + Rose.larger_offset_x * 2), (this.getHeight() + Rose.larger_offset_x * 2), Bitmap.Config.ARGB_8888);
1489 image_main = new Canvas(bitmap_main);
1490
1491 // how many tiles on screen?
1492 num_tiles_x = (width / tile_size_x) + 2 + more_for_speed_x;
1493 num_tiles_y = (height / tile_size_y) + 2 + more_for_speed_y;
1494
1495 // if modulo 2, then add +1 (this always makes it an odd number!)
1496 num_tiles_x = num_tiles_x + Math.abs((num_tiles_x % 2) - 1);
1497 num_tiles_y = num_tiles_y + Math.abs((num_tiles_y % 2) - 1);
1498
1499 // System.out.println("num t x:" + String.valueOf(num_tiles_x));
1500 // System.out.println("num t y:" + String.valueOf(num_tiles_y));
1501
1502 this.clear_stuff_2();
1503
1504 for (int xtemp = 1; xtemp < num_tiles_x + 1; xtemp++)
1505 {
1506 xit.add(new Integer(xtemp));
1507 }
1508
1509 for (int ytemp = 1; ytemp < num_tiles_y + 1; ytemp++)
1510 {
1511 yit.add(new Integer(ytemp));
1512 }
1513
1514 // zoom = main_object.global_settings.map_zoom;
1515 //
1516 // System.out.println("rose:zoom1=" + zoom);
1517 //
1518 // center_coord = new
1519 // Coordinate(main_object.global_settings.map_position_lat,
1520 // main_object.global_settings.map_position_lon);
1521 // set_center(center_coord);
1522
1523 __calc_tiles_on_display();
1524 draw_me();
1525 this.load_caches_from_db();
1526 // System.out.println("iigc4");
1527 this.main_object.gcview.invalidate();
1528 }
1529
1530 /*
1531 * Callback invoked when the Surface has been created and is ready to be
1532 * used.
1533 */
1534 public void surfaceCreated(SurfaceHolder holder)
1535 {
1536 // start the thread here so that we don't busy-wait in run()
1537 // waiting for the surface to be created
1538 // System.out.println("Rose: surfaceCreated");
1539 }
1540
1541 /*
1542 * Callback invoked when the Surface has been destroyed and must no longer
1543 * be touched. WARNING: after this method returns, the Surface/Canvas must
1544 * never be touched again!
1545 */
1546 public void surfaceDestroyed(SurfaceHolder holder)
1547 {
1548 // we have to tell thread to shut down & wait for it to finish, or else
1549 // it might touch the Surface after we return and explode
1550 }
1551
1552 }

   
Visit the aagtl Website