altosdroid: Place icons on screen instead of drawing path
authorKeith Packard <keithp@keithp.com>
Wed, 27 May 2015 06:05:49 +0000 (23:05 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 27 May 2015 06:05:49 +0000 (23:05 -0700)
This makes drawing a bunch faster, and locating stuff on the screen easier.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/TabMapOffline.java
altoslib/AltosMap.java

index ceabe7b..6742e60 100644 (file)
@@ -38,9 +38,18 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
        AltosMap map;
 
+       AltosLatLon     here;
+
        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;
@@ -54,11 +63,31 @@ 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(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);
@@ -66,6 +95,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                        paint.setStrokeCap(Paint.Cap.ROUND);
                        paint.setStrokeJoin(Paint.Join.ROUND);
                        map.paint();
+                       draw_positions();
                        canvas = null;
                }
 
@@ -205,7 +235,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        }
 
        public AltosMapPath new_path() {
-               return new MapPath();
+               return null;
        }
 
        class MapLine extends AltosMapLine {
@@ -217,7 +247,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        }
 
        public AltosMapLine new_line() {
-               return new MapLine();
+               return null;
        }
 
        class MapImage implements AltosImage {
@@ -241,6 +271,8 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
        class MapMark extends AltosMapMark {
                public void paint(AltosMapTransform t) {
+                       if (state == AltosLib.ao_flight_boost)
+                               draw_bitmap(lat_lon, pad_bitmap, pad_off_x, pad_off_y);
                }
 
                MapMark(double lat, double lon, int state) {
@@ -357,6 +389,20 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                super.onAttach(activity);
                mAltosDroid = (AltosDroid) activity;
                mAltosDroid.registerTab(this);
+               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
@@ -400,42 +446,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;
                }
        }
@@ -461,13 +477,14 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                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);
                }
 
        }
index bdb60f0..762054a 100644 (file)
@@ -58,8 +58,9 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
 
        LinkedList<AltosMapMark> marks = new LinkedList<AltosMapMark>();
 
-       AltosMapPath    path;
-       AltosMapLine    line;
+       AltosMapPath            path;
+       AltosMapLine            line;
+       public AltosLatLon      last_position;
 
        boolean         have_boost = false;
        boolean         have_landed = false;
@@ -195,8 +196,6 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                if (!gps.locked && gps.nsat < 4)
                        return;
 
-               AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state);
-
                switch (state.state) {
                case AltosLib.ao_flight_boost:
                        if (!have_boost) {
@@ -212,8 +211,15 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                        break;
                }
 
-               if (damage != null)
-                       repaint(damage, AltosMapPath.stroke_width);
+               if (path != null) {
+                       AltosMapRectangle       damage = path.add(gps.lat, gps.lon, state.state);
+
+                       if (damage != null)
+                               repaint(damage, AltosMapPath.stroke_width);
+               }
+
+               last_position = new AltosLatLon(gps.lat, gps.lon);
+
                maybe_centre(gps.lat, gps.lon);
        }
 
@@ -323,9 +329,11 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                                mark.paint(transform);
                }
 
-               path.paint(transform);
+               if (path != null)
+                       path.paint(transform);
 
-               line.paint(transform);
+               if (line != null)
+                       line.paint(transform);
        }
 
        /* AltosMapTileListener methods */
@@ -368,13 +376,17 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
        }
 
        private void line_start(int x, int y) {
-               line.pressed(new AltosPointInt(x, y), transform);
-               repaint();
+               if (line != null) {
+                       line.pressed(new AltosPointInt(x, y), transform);
+                       repaint();
+               }
        }
 
        private void line(int x, int y) {
-               line.dragged(new AltosPointInt(x, y), transform);
-               repaint();
+               if (line != null) {
+                       line.dragged(new AltosPointInt(x, y), transform);
+                       repaint();
+               }
        }
 
        public void touch_start(int x, int y, boolean is_drag) {