altosdroid: Save selected map type in AltosDroid object
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabMapOffline.java
index 42d80ad52c4e392437519f101c89475e49741113..cbf500187c1fbfbe0063a44898b57add7cdfff38 100644 (file)
@@ -31,20 +31,26 @@ import android.view.*;
 import android.widget.*;
 import android.location.Location;
 import android.content.*;
-import android.util.Log;
 
 public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
-       // Debugging
-       static final String TAG = "AltosDroid";
-       static final boolean D = true;
 
        AltosDroid mAltosDroid;
 
        AltosMap map;
 
+       AltosLatLon     here;
+       AltosLatLon     pad;
+
        Canvas  canvas;
        Paint   paint;
 
+       Bitmap  pad_bitmap;
+       int     pad_off_x, pad_off_y;
+       Bitmap  rocket_bitmap;
+       int     rocket_off_x, rocket_off_y;
+       Bitmap  here_bitmap;
+       int     here_off_x, here_off_y;
+
        private boolean pad_set;
 
        private TextView mDistanceView;
@@ -58,11 +64,32 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
        int     stroke_width = 20;
 
+       private void draw_bitmap(AltosLatLon lat_lon, Bitmap bitmap, int off_x, int off_y) {
+               if (lat_lon != null) {
+                       AltosPointInt pt = new AltosPointInt(map.transform.screen(lat_lon));
+
+                       canvas.drawBitmap(bitmap, pt.x - off_x, pt.y - off_y, paint);
+               }
+       }
+
        class MapView extends View implements ScaleGestureDetector.OnScaleGestureListener {
 
                ScaleGestureDetector    scale_detector;
                boolean                 scaling;
 
+               private void draw_positions() {
+                       if (map.last_position != null && here != null) {
+                               AltosPointDouble        rocket_screen = map.transform.screen(map.last_position);
+                               AltosPointDouble        here_screen = map.transform.screen(here);
+                               paint.setColor(0xff8080ff);
+                               canvas.drawLine((float) rocket_screen.x, (float) rocket_screen.y,
+                                               (float) here_screen.x, (float) here_screen.y, paint);
+                       }
+                       draw_bitmap(pad, pad_bitmap, pad_off_x, pad_off_y);
+                       draw_bitmap(map.last_position, rocket_bitmap, rocket_off_x, rocket_off_y);
+                       draw_bitmap(here, here_bitmap, here_off_x, here_off_y);
+               }
+
                protected void onDraw(Canvas view_canvas) {
                        canvas = view_canvas;
                        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
@@ -70,12 +97,13 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                        paint.setStrokeCap(Paint.Cap.ROUND);
                        paint.setStrokeJoin(Paint.Join.ROUND);
                        map.paint();
+                       draw_positions();
                        canvas = null;
                }
 
                public boolean onScale(ScaleGestureDetector detector) {
                        float   f = detector.getScaleFactor();
-                       if (D) Log.d(TAG, String.format("onScale %f\n", f));
+                       AltosDebug.debug("onScale %f\n", f);
                        if (f <= 0.8) {
                                map.set_zoom(map.get_zoom() - 1);
                                return true;
@@ -88,12 +116,12 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                }
 
                public boolean onScaleBegin(ScaleGestureDetector detector) {
-                       if (D) Log.d(TAG, String.format("onScaleBegin %f\n", detector.getScaleFactor()));
+                       AltosDebug.debug("onScaleBegin %f\n", detector.getScaleFactor());
                        return true;
                }
 
                public void onScaleEnd(ScaleGestureDetector detector) {
-                       if (D) Log.d(TAG, String.format("onScaleEnd %f\n", detector.getScaleFactor()));
+                       AltosDebug.debug("onScaleEnd %f\n", detector.getScaleFactor());
                }
 
                @Override
@@ -105,19 +133,19 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                        }
 
                        if (scaling) {
-                               if(D) Log.d(TAG, "scale in progress\n");
+                               if(AltosDebug.D) AltosDebug.debug("scale in progress\n");
                                if (event.getAction() == MotionEvent.ACTION_UP) {
-                                       if (D) Log.d(TAG, "scale finished\n");
+                                       AltosDebug.debug("scale finished\n");
                                        scaling = false;
                                }
                                return true;
                        }
 
                        if (event.getAction() == MotionEvent.ACTION_DOWN) {
-                               if(D) Log.d(TAG, String.format("down event %g %g\n", event.getX(), event.getY()));
+                               AltosDebug.debug("down event %g %g\n", event.getX(), event.getY());
                                map.touch_start((int) event.getX(), (int) event.getY(), true);
                        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
-                               if(D) Log.d(TAG, String.format("continue event %g %g\n", event.getX(), event.getY()));
+                               AltosDebug.debug("continue event %g %g\n", event.getX(), event.getY());
                                map.touch_continue((int) event.getX(), (int) event.getY(), true);
                        }
                        return true;
@@ -209,7 +237,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        }
 
        public AltosMapPath new_path() {
-               return new MapPath();
+               return null;
        }
 
        class MapLine extends AltosMapLine {
@@ -221,7 +249,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        }
 
        public AltosMapLine new_line() {
-               return new MapLine();
+               return null;
        }
 
        class MapImage implements AltosImage {
@@ -361,6 +389,24 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                super.onAttach(activity);
                mAltosDroid = (AltosDroid) activity;
                mAltosDroid.registerTab(this);
+
+               map = new AltosMap(this);
+               map.set_maptype(mAltosDroid.map_type);
+
+               pad_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pad);
+               /* arrow at the bottom of the launchpad image */
+               pad_off_x = pad_bitmap.getWidth() / 2;
+               pad_off_y = pad_bitmap.getHeight();
+
+               rocket_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
+               /* arrow at the bottom of the rocket image */
+               rocket_off_x = rocket_bitmap.getWidth() / 2;
+               rocket_off_y = rocket_bitmap.getHeight();
+
+               here_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position);
+               /* Center of the dot */
+               here_off_x = here_bitmap.getWidth() / 2;
+               here_off_y = here_bitmap.getHeight() / 2;
        }
 
        @Override
@@ -404,42 +450,12 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        }
 
        private void setupMap() {
-/*
-               mMap = mMapFragment.getMap();
-               if (mMap != null) {
-                       mMap.setMyLocationEnabled(true);
-                       mMap.getUiSettings().setTiltGesturesEnabled(false);
-                       mMap.getUiSettings().setZoomControlsEnabled(false);
-
-                       mRocketMarker = mMap.addMarker(
-                                       // From: http://mapicons.nicolasmollet.com/markers/industry/military/missile-2/
-                                       new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.rocket))
-                                                          .position(new LatLng(0,0))
-                                                          .visible(false)
-                                       );
-
-                       mPadMarker = mMap.addMarker(
-                                       new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.pad))
-                                                          .position(new LatLng(0,0))
-                                                          .visible(false)
-                                       );
-
-                       mPolyline = mMap.addPolyline(
-                                       new PolylineOptions().add(new LatLng(0,0), new LatLng(0,0))
-                                                            .width(3)
-                                                            .color(Color.BLUE)
-                                                            .visible(false)
-                                       );
-
-                       mapLoaded = true;
-               }
-*/
        }
 
        private void center(double lat, double lon, double accuracy) {
                if (mapAccuracy < 0 || accuracy < mapAccuracy/10) {
                        if (map != null)
-                               map.centre(lat, lon);
+                               map.maybe_centre(lat, lon);
                        mapAccuracy = accuracy;
                }
        }
@@ -460,22 +476,30 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                                if (state.gps.locked && state.gps.nsat >= 4)
                                        center (state.gps.lat, state.gps.lon, 10);
                        }
+                       if (state.pad_lat != AltosLib.MISSING && pad == null)
+                               pad = new AltosLatLon(state.pad_lat, state.pad_lon);
                }
 
                if (receiver != null) {
                        double accuracy;
 
+                       here = new AltosLatLon(receiver.getLatitude(), receiver.getLongitude());
                        if (receiver.hasAccuracy())
                                accuracy = receiver.getAccuracy();
                        else
                                accuracy = 1000;
-                       mReceiverLatitudeView.setText(AltosDroid.pos(receiver.getLatitude(), "N", "S"));
-                       mReceiverLongitudeView.setText(AltosDroid.pos(receiver.getLongitude(), "E", "W"));
-                       center (receiver.getLatitude(), receiver.getLongitude(), accuracy);
+                       mReceiverLatitudeView.setText(AltosDroid.pos(here.lat, "N", "S"));
+                       mReceiverLongitudeView.setText(AltosDroid.pos(here.lon, "E", "W"));
+                       center (here.lat, here.lon, accuracy);
                }
 
        }
 
+       public void set_map_type(int map_type) {
+               if (map != null)
+                       map.set_maptype(map_type);
+       }
+
        public TabMapOffline() {
        }
 }