Ignore:
Timestamp:
08/08/07 02:46:26 (5 years ago)
Author:
saua
Message:

Fixes Issue #8

File:
1 edited

Legend:

Unmodified
Added
Removed
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/TrackTile.java

    r36 r60  
    11package org.bbtracker.mobile.gui; 
    2  
    3 import java.util.Enumeration; 
    42 
    53import javax.microedition.lcdui.Font; 
    64import javax.microedition.lcdui.Graphics; 
    75 
    8 import org.bbtracker.Track; 
    9 import org.bbtracker.TrackPoint; 
    10 import org.bbtracker.TrackSegment; 
    116import org.bbtracker.Utils; 
    127import org.bbtracker.UnitConverter.ScaleConfiguration; 
    138import org.bbtracker.mobile.Preferences; 
     9import org.bbtracker.mobile.TrackManager; 
    1410 
    15 public class TrackTile extends Tile { 
    16         private static final int MARKED_POINT_COLOR = 0x00bb0000; 
    17  
    18         private static final int LAST_POINT_COLOR = 0x00555555; 
    19  
    20         private static final int LAST_MARKED_POINT_COLOR = 0x00550000; 
    21  
    22         private static final int LINK_COLOR = 0x00003300; 
    23  
    24         private static final int SEGMENT_LINK_COLOR = 0x00aaaaaa; 
    25  
    26         private static final int FONT_COLOR = 0x00000000; 
    27  
    28         private static final double SMALL_STEP = 1 / (60 * 4); 
    29  
    30         private static final double MARGIN = 0.025; 
    31  
     11public class TrackTile extends PlotterTile { 
    3212        private static final int SCALE_HEIGTH = 5; 
    33  
    34         private final Track track; 
    35  
    36         private double minimumLongitude, minimumLatitude; 
    37  
    38         private double scale; 
    39  
    40         private TrackPoint currentPoint; 
    4113 
    4214        private int scaleSizeInPixel; 
    4315 
    44         private String scaleLabelCenter; 
     16        private ScaleConfiguration scaleConfiguration; 
    4517 
    46         private String scaleLabelRight; 
    47  
    48         public TrackTile(final Track track) { 
    49                 this.track = track; 
    50  
    51                 minimumLongitude = minimumLatitude = Double.NaN; 
    52                 scale = Float.NaN; 
     18        public TrackTile(final TrackManager manager) { 
     19                super(manager, DataProvider.LONGITUDE, DataProvider.LATITUDE, true); 
    5320        } 
    5421 
    55         public void setCurrentPoint(final TrackPoint currentPoint) { 
    56                 this.currentPoint = currentPoint; 
    57         } 
    58  
    59         public TrackPoint getCurrentPoint() { 
    60                 return currentPoint; 
    61         } 
    62  
    63         protected void recalculateBounds() { 
    64                 final double marginFactor = (2 * MARGIN) + 1; 
    65  
    66                 final double maxLon = track.getMaxLongitude(); 
    67                 final double minLon = track.getMinLongitude(); 
    68                 final double maxLat = track.getMaxLatitude(); 
    69                 final double minLat = track.getMinLatitude(); 
    70  
    71                 double lonDiff = (maxLon - minLon) * marginFactor; 
    72                 double latDiff = (maxLat - minLat) * marginFactor; 
    73  
    74                 if (lonDiff == 0) { 
    75                         lonDiff = SMALL_STEP; 
    76                 } else if (latDiff == 0) { 
    77                         latDiff = SMALL_STEP; 
    78                 } 
    79  
    80                 final double xScale = lonDiff / width; 
    81                 final double yScale = latDiff / height; 
    82  
    83                 scale = Math.max(xScale, yScale); 
    84  
    85                 minimumLongitude = ((maxLon + minLon) - (width * scale)) / 2; 
    86                 minimumLatitude = ((maxLat + minLat) - (height * scale)) / 2; 
    87                 final double maximumLongitude = minimumLongitude + (width * scale); 
    88  
    89                 // calculate the size of the scale 
    90                 final double widthInMeter = Utils 
    91                                 .distance(minimumLatitude, minimumLongitude, minimumLatitude, maximumLongitude); 
     22        protected void onScaleChanged() { 
     23                super.onScaleChanged(); 
     24                final double widthInMeter = Utils.distance(getYAxis().minValue, getXAxis().maxValue, getYAxis().maxValue, 
     25                                getXAxis().maxValue); 
    9226                if (widthInMeter < 1) { 
    9327                        scaleSizeInPixel = 0; 
     
    9731                final double availableLengthInMeter = widthInMeter * 0.9; 
    9832 
    99                 final ScaleConfiguration conf = Preferences.getInstance().getUnitsConverter().getScaleConfiguration( 
    100                                 availableLengthInMeter); 
     33                scaleConfiguration = Preferences.getInstance().getUnitsConverter().getScaleDistance(availableLengthInMeter); 
    10134 
    102                 switch (conf.lengthInUnits) { 
    103                 case 1: 
    104                         scaleLabelCenter = "0.5"; 
    105                         break; 
    106                 case 5: 
    107                         scaleLabelCenter = "2.5"; 
    108                         break; 
    109                 default: 
    110                         scaleLabelCenter = String.valueOf(conf.lengthInUnits / 2); 
    111                 } 
    112                 scaleLabelRight = conf.lengthInUnits + " " + conf.unit; 
    113  
    114                 scaleSizeInPixel = (int) ((conf.lengthInMeter / widthInMeter) * width); 
     35                scaleSizeInPixel = (int) ((scaleConfiguration.lengthInSourceUnits / widthInMeter) * width); 
    11536        } 
    11637 
    117         protected void onResize() { 
    118                 minimumLongitude = Double.NaN; 
    119         } 
    120  
    121         protected void doPaint(final Graphics g) { 
    122                 g.setColor(0x00ffffff); 
    123                 g.fillRect(xOffset, yOffset, width, height); 
    124                 if (track == null) { 
    125                         g.drawString("No Track ...", 2, 2, Graphics.TOP | Graphics.RIGHT); 
    126                         return; 
    127                 } 
    128                 if (track.getPointCount() == 0) { 
    129                         return; 
    130                 } 
    131                 if (Double.isNaN(minimumLongitude)) { 
    132                         recalculateBounds(); 
    133                 } 
    134  
    135                 drawScale(g); 
    136                 drawTrack(g); 
    137         } 
    138  
    139         private void drawTrack(final Graphics g) { 
    140                 g.setColor(FONT_COLOR); 
    141                 g.setColor(LINK_COLOR); 
    142  
    143                 int prevX = -1; 
    144                 int prevY = -1; 
    145                 final boolean prevIsWaypoint = false; 
    146                 final Enumeration segments = track.getSegments(); 
    147                 while (segments.hasMoreElements()) { 
    148                         boolean newSegment = (prevX != -1); 
    149                         final TrackSegment segment = (TrackSegment) segments.nextElement(); 
    150                         final Enumeration points = segment.getPoints(); 
    151                         TrackPoint prev = null; 
    152                         while (points.hasMoreElements()) { 
    153                                 final TrackPoint point = (TrackPoint) points.nextElement(); 
    154                                 final int curX = (int) ((point.getLongitude() - minimumLongitude) / scale); 
    155                                 final int curY = height - (int) ((point.getLatitude() - minimumLatitude) / scale); 
    156                                 if (prevX != -1) { 
    157  
    158                                         g.drawLine(prevX, prevY, curX, curY); 
    159                                         drawPoint(g, prevX, prevY, prevIsWaypoint, prev == currentPoint); 
    160                                         if (newSegment) { 
    161                                                 g.setColor(SEGMENT_LINK_COLOR); 
    162                                                 newSegment = false; 
    163                                         } else { 
    164                                                 g.setColor(LINK_COLOR); 
    165                                         } 
    166                                 } 
    167                                 prevX = curX; 
    168                                 prevY = curY; 
    169                                 prev = point; 
    170                         } 
    171                         drawPoint(g, prevX, prevY, prevIsWaypoint, prev == currentPoint); 
    172                 } 
    173         } 
    174  
    175         private void drawPoint(final Graphics g, final int x, final int y, final boolean waypoint, final boolean current) { 
    176                 if (!(waypoint || current)) { 
    177                         return; 
    178                 } 
    179                 if (current) { 
    180                         g.setColor(waypoint ? LAST_MARKED_POINT_COLOR : LAST_POINT_COLOR); 
    181                         g.drawLine(x, y - 3, x + 3, y); 
    182                         g.drawLine(x + 3, y, x, y + 3); 
    183                         g.drawLine(x, y + 3, x - 3, y); 
    184                         g.drawLine(x - 3, y, x, y - 3); 
    185  
    186                 } else { 
    187                         g.setColor(MARKED_POINT_COLOR); 
    188                         g.drawLine(x - 2, y - 2, x + 2, y + 2); 
    189                         g.drawLine(x - 2, y + 2, x + 2, y - 2); 
    190                 } 
    191         } 
    192  
    193         private void drawScale(final Graphics g) { 
     38        protected void doPaintAxis(final Graphics g) { 
    19439                if (scaleSizeInPixel == 0) { 
    19540                        return; 
     
    19843                final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); 
    19944 
    200                 final int left = (font.stringWidth("0") / 2) + 2; 
     45                final int textBottom = height - 4 - SCALE_HEIGTH; 
     46                int left = 0; 
     47                for (int i = 0; i < scaleConfiguration.labelLocation.length; i++) { 
     48                        final float location = scaleConfiguration.labelLocation[i]; 
     49                        final float value = scaleConfiguration.labelValue[i]; 
     50                        String label = Utils.floatToString(value, true); 
     51                        if (location == 0) { 
     52                                left = (font.stringWidth(label) / 2) + 2; 
     53                        } else if (location == 1) { 
     54                                label += " " + scaleConfiguration.unit; 
     55                        } 
     56                        g.drawString(label, left + (int) (scaleSizeInPixel * location), textBottom, Graphics.BOTTOM | 
     57                                        Graphics.HCENTER); 
     58                } 
    20159                g.setFont(font); 
    20260                g.setColor(0x00000000); 
    20361                g.drawRect(left, height - 2 - SCALE_HEIGTH, scaleSizeInPixel, SCALE_HEIGTH); 
    20462                g.fillRect(left, height - 2 - SCALE_HEIGTH, scaleSizeInPixel / 2, SCALE_HEIGTH); 
    205  
    206                 final int textBottom = height - 4 - SCALE_HEIGTH; 
    207                 g.drawString("0", left, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    208                 g.drawString(scaleLabelCenter, left + (scaleSizeInPixel / 2), textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    209                 g.drawString(scaleLabelRight, left + scaleSizeInPixel, textBottom, Graphics.BOTTOM | Graphics.HCENTER); 
    21063        } 
    21164} 
Note: See TracChangeset for help on using the changeset viewer.