Changeset 60


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

Fixes Issue #8

Files:
5 added
6 edited

Legend:

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

    r50 r60  
    11package org.bbtracker.mobile.gui; 
     2 
     3import java.util.TimerTask; 
    24 
    35import javax.microedition.lcdui.Canvas; 
     
    911import javax.microedition.rms.RecordStoreException; 
    1012 
    11 import org.bbtracker.Track; 
    1213import org.bbtracker.TrackPoint; 
    1314import org.bbtracker.mobile.BBTracker; 
     
    1617 
    1718public class MainCanvas extends Canvas implements TrackListener, CommandListener { 
     19        private static final int DEFAULT_STATUS_TIMEOUT = 5 * 1000; 
     20 
     21        private static final int MAX_TILES = 2; 
     22 
     23        private final Tile[] visibleTiles = new Tile[MAX_TILES]; 
     24 
    1825        private final TrackManager manager; 
    1926 
    20         private Track track; 
    21  
    22         private TrackTile trackTile; 
     27        private final Tile trackTile; 
     28 
     29        private final Tile elevationProfileTile; 
     30 
     31        private final Tile speedProfileTile; 
    2332 
    2433        private final StatusTile statusTile; 
    2534 
    26         private final Command newTrackCommand = new Command("New Track", Command.ITEM, 2); 
    27  
    28         private final Command tracksCommand = new Command("Tracks", Command.ITEM, 2); 
    29  
    30         private final Command optionsCommand = new Command("Options", Command.ITEM, 2); 
    31  
    32         private final Command aboutCommand = new Command("About", Command.ITEM, 4); 
    33  
    34         private final Command exitCommand = new Command("Exit", Command.CANCEL, 3); 
     35        private final Command newTrackCommand; 
     36 
     37        private final Command tracksCommand; 
     38 
     39        private final Command optionsCommand; 
     40 
     41        private final Command switchViewCommand; 
     42 
     43        private final Command aboutCommand; 
     44 
     45        private final Command exitCommand; 
     46 
     47        private String statusMessage = null; 
     48 
     49        private long statusMessageEndTime = 0; 
     50 
     51        private int tileConfiguration = 0; 
    3552 
    3653        public MainCanvas(final TrackManager manager) { 
    3754                this.manager = manager; 
    38                 track = manager.getTrack(); 
    39  
    40                 trackTile = new TrackTile(track); 
     55 
     56                trackTile = new TrackTile(manager); 
     57                elevationProfileTile = new ElevationPlotterTile(manager, DataProvider.TIME); 
     58                speedProfileTile = new SpeedPlotterTile(manager, DataProvider.TIME); 
    4159                statusTile = new StatusTile(manager); 
    4260 
     61                switchViewCommand = new Command("Switch View", Command.SCREEN, 0); 
     62                newTrackCommand = new Command("New Track", "Start a new Track", Command.SCREEN, 1); 
     63                tracksCommand = new Command("Tracks", "Open Track Manager", Command.SCREEN, 2); 
     64                optionsCommand = new Command("Options", Command.SCREEN, 3); 
     65                aboutCommand = new Command("About", Command.SCREEN, 5); 
     66                exitCommand = new Command("Exit", Command.EXIT, 6); 
     67 
     68                addCommand(switchViewCommand); 
    4369                addCommand(newTrackCommand); 
    4470                addCommand(tracksCommand); 
     
    4773                addCommand(exitCommand); 
    4874                setCommandListener(this); 
     75 
     76                setMainTile(trackTile, true); 
     77        } 
     78 
     79        protected void setMainTile(final Tile mainTile, final boolean withStatus) { 
     80                visibleTiles[0] = mainTile; 
     81                if (withStatus) { 
     82                        visibleTiles[1] = statusTile; 
     83                } else { 
     84                        visibleTiles[1] = null; 
     85                } 
     86                updateTileSize(); 
     87        } 
     88 
     89        protected void updateTileSize() { 
     90                final int w = getWidth(); 
     91                final int h = getHeight(); 
     92                if (visibleTiles[1] == null) { 
     93                        visibleTiles[0].resize(0, 0, w, h); 
     94                } else { 
     95                        final int statusTileHeight = statusTile.getPreferredHeight(w); 
     96                        visibleTiles[0].resize(0, 0, w, h - statusTileHeight); 
     97                        visibleTiles[1].resize(0, h - statusTileHeight, w, statusTileHeight); 
     98                } 
     99        } 
     100 
     101        protected void setStatusMessage(final String statusMessage) { 
     102                setStatusMessage(statusMessage, DEFAULT_STATUS_TIMEOUT); 
     103        } 
     104 
     105        protected void setStatusMessage(final String statusMessage, final int duration) { 
     106                this.statusMessage = statusMessage; 
     107                statusMessageEndTime = System.currentTimeMillis() + duration; 
     108                BBTracker.getTimer().schedule(new RepaintTask(), duration + 10); 
     109                repaint(); 
    49110        } 
    50111 
     
    52113                super.sizeChanged(w, h); 
    53114 
    54                 final int statusHeight = statusTile.getPreferredHeight(w); 
    55                 trackTile.resize(0, 0, w, h - (statusHeight + 1)); 
    56                 statusTile.resize(0, h - statusHeight, w, statusHeight); 
     115                updateTileSize(); 
    57116        } 
    58117 
    59118        protected void paint(final Graphics g) { 
    60                 if (trackTile.width == 0) { 
     119                if (visibleTiles[0].width == 0) { 
    61120                        // BlackBerry (at least 8800) seems not to call sizeChanged before initial paint() 
    62121                        sizeChanged(getWidth(), getHeight()); 
    63122                } 
    64                 trackTile.paint(g); 
    65                 statusTile.paint(g); 
    66  
    67                 switch (manager.getState()) { 
    68                 case TrackManager.STATE_TRACKING: 
    69                         return; 
    70                 default: 
    71                         final String state = manager.getStateString(); 
     123 
     124                for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 
     125                        visibleTiles[i].paint(g); 
     126                } 
     127 
     128                if (statusMessageEndTime > System.currentTimeMillis()) { 
    72129                        final Font font = Font.getDefaultFont(); 
    73                         final int stringWidth = font.stringWidth(state); 
     130                        final int stringWidth = font.stringWidth(statusMessage); 
    74131                        final int stringHeight = g.getFont().getHeight(); 
    75132 
     
    79136                        g.setColor(0x00000000); 
    80137                        g.drawRect(2, 2, stringWidth + 4, stringHeight + 4); 
    81                         g.drawString(state, 4, 4, Graphics.TOP | Graphics.LEFT); 
     138                        g.drawString(statusMessage, 4, 4, Graphics.TOP | Graphics.LEFT); 
    82139                } 
    83140        } 
    84141 
    85142        public void newPoint(final TrackPoint newPoint, final boolean boundsChanged, final boolean newSegment) { 
    86                 if (boundsChanged) { 
    87                         trackTile.onResize(); // XXX Make that nicer 
     143                for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 
     144                        visibleTiles[i].newPoint(newPoint, boundsChanged, newSegment); 
     145                } 
     146        } 
     147 
     148        public void currentPointChanged(final TrackPoint newPoint, final int newIndex) { 
     149                for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 
     150                        visibleTiles[i].currentPointChanged(newPoint, newIndex); 
    88151                } 
    89152                repaint(); 
    90153        } 
    91154 
    92         public void currentPointChanged(final TrackPoint newPoint, final int newIndex) { 
    93                 trackTile.setCurrentPoint(newPoint); 
     155        public void stateChanged(final int newState) { 
     156                updateStatusText(newState); 
     157                for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 
     158                        visibleTiles[i].stateChanged(newState); 
     159                } 
    94160                repaint(); 
    95161        } 
    96162 
    97         public void stateChanged(final int newState) { 
    98                 repaint(); 
    99         } 
    100  
    101         public void setTrack(final Track track) { 
    102                 this.track = track; 
    103                 trackTile = new TrackTile(track); 
    104                 repaint(); 
     163        protected void updateStatusText(final int newState) { 
     164                switch (newState) { 
     165                case TrackManager.STATE_STATIC: 
     166                        setStatusMessage("Static Track"); 
     167                        break; 
     168                case TrackManager.STATE_TRACKING: 
     169                        setStatusMessage("Tracking"); 
     170                        break; 
     171                } 
     172        } 
     173 
     174        private void nextTileConfiguration() { 
     175                tileConfiguration = (tileConfiguration + 1) % 3; 
     176                switch (tileConfiguration) { 
     177                case 0: 
     178                        setMainTile(trackTile, true); 
     179                        setStatusMessage("Track view"); 
     180                        break; 
     181                case 1: 
     182                        setMainTile(elevationProfileTile, true); 
     183                        setStatusMessage("Elevation over time"); 
     184                        break; 
     185                case 2: 
     186                        setMainTile(speedProfileTile, true); 
     187                        setStatusMessage("Speed over time"); 
     188                        break; 
     189                } 
    105190        } 
    106191 
     
    112197        protected void showNotify() { 
    113198                super.showNotify(); 
    114                 final Track newTrack = manager.getTrack(); 
    115                 if (newTrack != track) { 
    116                         setTrack(newTrack); 
    117                 } 
    118                 trackTile.onResize(); 
    119199                manager.addPointListener(this); 
     200                for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 
     201                        visibleTiles[i].showNotify(); 
     202                } 
     203                updateStatusText(manager.getState()); 
    120204        } 
    121205 
    122206        public void commandAction(final Command command, final Displayable displayable) { 
    123207                Displayable nextDisplayable = null; 
    124                 if (command == aboutCommand) { 
    125                         nextDisplayable = new AboutForm(); 
    126                 } else if (command == optionsCommand) { 
    127                         nextDisplayable = new OptionsForm(manager); 
    128                 } else if (command == newTrackCommand) { 
    129                         nextDisplayable = new NewTrackForm(manager); 
    130                 } else if (command == tracksCommand) { 
    131                         try { 
    132                                 nextDisplayable = new TracksForm(manager); 
    133                         } catch (final RecordStoreException e) { 
    134                                 BBTracker.nonFatal(e, "getting list of stored tracks", this); 
     208                if (command == exitCommand) { 
     209                        BBTracker.getInstance().shutdown(true); 
     210                } else if (command == switchViewCommand) { 
     211                        nextTileConfiguration(); 
     212                } else { 
     213                        if (command == aboutCommand) { 
     214                                nextDisplayable = new AboutForm(); 
     215                        } else if (command == optionsCommand) { 
     216                                nextDisplayable = new OptionsForm(manager); 
     217                        } else if (command == newTrackCommand) { 
     218                                nextDisplayable = new NewTrackForm(manager); 
     219                        } else if (command == tracksCommand) { 
     220                                try { 
     221                                        nextDisplayable = new TracksForm(manager); 
     222                                } catch (final RecordStoreException e) { 
     223                                        BBTracker.nonFatal(e, "getting list of stored tracks", this); 
     224                                } 
    135225                        } 
    136                 } else if (command == exitCommand) { 
    137                         BBTracker.getInstance().shutdown(true); 
    138                         return; 
    139                 } 
    140                 BBTracker.getDisplay().setCurrent(nextDisplayable); 
     226                        BBTracker.getDisplay().setCurrent(nextDisplayable); 
     227                } 
    141228        } 
    142229 
    143230        protected void keyReleased(final int keyCode) { 
    144231                final int gameAction = getGameAction(keyCode); 
    145                 if (gameAction == LEFT) { 
     232                switch (gameAction) { 
     233                case LEFT: 
    146234                        manager.changeCurrentPoint(-1); 
    147                 } else if (gameAction == RIGHT) { 
     235                        break; 
     236                case RIGHT: 
    148237                        manager.changeCurrentPoint(+1); 
    149                 } else if (gameAction == DOWN) { 
     238                        break; 
     239                case DOWN: 
    150240                        manager.changeCurrentPoint(-10); 
    151                 } else if (gameAction == UP) { 
     241                        break; 
     242                case UP: 
    152243                        manager.changeCurrentPoint(+10); 
     244                        break; 
     245                } 
     246        } 
     247 
     248        private class RepaintTask extends TimerTask { 
     249                public void run() { 
     250                        MainCanvas.this.repaint(); 
    153251                } 
    154252        } 
  • bbtracker/trunk/src/org/bbtracker/mobile/gui/Tile.java

    r4 r60  
    33import javax.microedition.lcdui.Graphics; 
    44 
    5 public abstract class Tile { 
     5import org.bbtracker.TrackPoint; 
     6import org.bbtracker.mobile.TrackListener; 
     7 
     8/** 
     9 * A tile represents a Square area on the Screen that draws itself. 
     10 *  
     11 * Each Tile will automagically receive all events received by a {@link TrackListener}, as long as it is visible. When 
     12 * it is not shown (i.e. it has been hidden or the Canvas containing it was not shown, then some of those events may not 
     13 * be received. In this case {@link #showNotify()} will be called before the next time {@link #doPaint(Graphics)} is 
     14 * called. 
     15 */ 
     16public abstract class Tile implements TrackListener { 
    617        protected int xOffset; 
    718 
     
    3445 
    3546        protected abstract void doPaint(final Graphics g); 
     47 
     48        public void currentPointChanged(final TrackPoint newPoint, final int newIndex) { 
     49                // nothing 
     50        } 
     51 
     52        public void newPoint(final TrackPoint newPoint, final boolean boundsChanged, final boolean newSegment) { 
     53                // nothing 
     54        } 
     55 
     56        public void stateChanged(final int newState) { 
     57                // nothing 
     58        } 
     59 
     60        public abstract void showNotify(); 
    3661} 
  • 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} 
  • bbtracker_common/trunk/src/org/bbtracker/ImperialUnitConverter.java

    r52 r60  
    1515                } 
    1616                final float mph = speed * MS_TO_MPH_FACTOR; 
    17                 return String.valueOf(((int) (mph * 10)) / 10f) + "mph"; 
     17                return Utils.floatToString(mph, false) + "mph"; 
    1818        } 
    1919 
     
    3535                        return feet + "ft"; 
    3636                } else { 
    37                         return String.valueOf(((int) (miles * 10)) / 10f) + "mi"; 
     37                        return Utils.doubleToString(miles, false) + "mi"; 
    3838                } 
    3939        } 
    4040 
    41         public ScaleConfiguration getScaleConfiguration(final double lengthInMeter) { 
     41        public ScaleConfiguration getScaleDistance(final double lengthInMeter) { 
    4242                final double lengthInFoot = lengthInMeter * METER_TO_FOOT_FACTOR; 
    4343                double factor = METER_TO_FOOT_FACTOR; 
     
    5555                } 
    5656 
    57                 conf.lengthInUnits = getRoundScaleSize(available); 
    58                 conf.lengthInMeter = conf.lengthInUnits / factor; 
     57                final int lengthInUnits = getRoundScaleSize(available); 
     58                conf.lengthInSourceUnits = lengthInUnits / factor; 
     59                conf.labelLocation = new float[] { 0.0f, 0.5f, 1.0f }; 
     60                conf.labelValue = new float[] { 0f, lengthInUnits / 2, lengthInUnits }; 
    5961                return conf; 
    6062        } 
     63 
     64        public ScaleConfiguration getScaleElevation(final int min, final int max) { 
     65                return getScaleConfiguration("ft", min * METER_TO_FOOT_FACTOR, max * METER_TO_FOOT_FACTOR); 
     66        } 
     67 
     68        public ScaleConfiguration getScaleSpeed(final double maxSpeed) { 
     69                return getScaleConfiguration("mph", 0, (float) (maxSpeed * MS_TO_MPH_FACTOR)); 
     70        } 
    6171} 
  • bbtracker_common/trunk/src/org/bbtracker/MetricUnitConverter.java

    r52 r60  
    1212                } 
    1313                final float value = speed * MS_TO_KMH_FACTOR; 
    14                 return String.valueOf(((int) (value * 10)) / 10f) + "km/h"; 
     14                return Utils.floatToString(value, false) + "km/h"; 
    1515        } 
    1616 
     
    2828                        return ((int) length) + "m"; 
    2929                } else { 
    30                         return String.valueOf(((int) (length / 100)) / 10f) + "km"; 
     30                        return Utils.doubleToString(length / 1000, false) + "km"; 
    3131                } 
    3232        } 
    3333 
    34         public ScaleConfiguration getScaleConfiguration(final double lengthInMeter) { 
     34        public ScaleConfiguration getScaleDistance(final double lengthInMeter) { 
    3535                final int scaleSize = getRoundScaleSize((int) lengthInMeter); 
    3636                final ScaleConfiguration conf = new ScaleConfiguration(); 
     37                int lengthInUnits; 
    3738                if (scaleSize >= 1000) { 
    3839                        conf.unit = "km"; 
    39                         conf.lengthInUnits = scaleSize / 1000; 
     40                        lengthInUnits = scaleSize / 1000; 
    4041                } else { 
    4142                        conf.unit = "m"; 
    42                         conf.lengthInUnits = scaleSize; 
     43                        lengthInUnits = scaleSize; 
    4344                } 
    44                 conf.lengthInMeter = scaleSize; 
     45                conf.lengthInSourceUnits = scaleSize; 
     46                conf.labelLocation = new float[] { 0.0f, 0.5f, 1.0f }; 
     47                conf.labelValue = new float[] { 0f, lengthInUnits / 2f, lengthInUnits }; 
    4548                return conf; 
    4649        } 
     50 
     51        public ScaleConfiguration getScaleElevation(final int min, final int max) { 
     52                return getScaleConfiguration("m", min, max); 
     53        } 
     54 
     55        public ScaleConfiguration getScaleSpeed(final double maxSpeed) { 
     56                System.out.println(maxSpeed); 
     57                return getScaleConfiguration("km/h", 0f, (float) (maxSpeed * MS_TO_KMH_FACTOR)); 
     58        } 
    4759} 
  • bbtracker_common/trunk/src/org/bbtracker/UnitConverter.java

    r36 r60  
    2828        public abstract String distanceToString(final double distance); 
    2929 
    30         public abstract ScaleConfiguration getScaleConfiguration(final double availableMeter); 
     30        public abstract ScaleConfiguration getScaleDistance(final double availableMeter); 
     31 
     32        public abstract ScaleConfiguration getScaleElevation(final int min, final int max); 
     33 
     34        public abstract ScaleConfiguration getScaleSpeed(final double maxSpeed); 
    3135 
    3236        /** 
     
    5155        } 
    5256 
     57        protected static ScaleConfiguration getScaleConfiguration(final String unit, final double min, final double max) { 
     58                final double diff = max - min; 
     59                if (diff == 0) { 
     60                        final ScaleConfiguration conf = new ScaleConfiguration(); 
     61                        conf.unit = unit; 
     62                        conf.lengthInSourceUnits = diff; 
     63                        conf.labelLocation = new float[] { (float) min }; 
     64                        conf.labelValue = new float[] { 0.0f }; 
     65                        return conf; 
     66                } 
     67 
     68                final ScaleConfiguration conf = new ScaleConfiguration(); 
     69                conf.unit = unit; 
     70                conf.lengthInSourceUnits = diff; 
     71 
     72                // calculate approximate tick size (find correct power of ten) 
     73                double tickSize; 
     74                // I wanted to use log10 to find the correct magnitude, but that's not available in CLDC 1.1 
     75                tickSize = 1; 
     76                if (tickSize > diff) { 
     77                        while (tickSize > diff) { 
     78                                tickSize /= 10; 
     79                        } 
     80                } else { 
     81                        while (tickSize < diff / 10) { 
     82                                tickSize *= 10; 
     83                        } 
     84                } 
     85 
     86                // adjust to get a reasonable number of ticks (usually between 3 and 6) 
     87                int ticks = (int) (diff / tickSize); 
     88                if (ticks > 5) { 
     89                        tickSize *= 2; 
     90                } 
     91 
     92                // might be one to big, but that's better than one to small 
     93                ticks = (int) (diff / tickSize) + 1; 
     94 
     95                conf.labelLocation = new float[ticks]; 
     96                conf.labelValue = new float[ticks]; 
     97 
     98                // find first tick 
     99                double tMin = (int) (min / tickSize) * tickSize; 
     100                if (tMin < min) { 
     101                        tMin += tickSize; 
     102                } 
     103 
     104                double t = tMin; 
     105                int i = 0; 
     106                do { 
     107                        // adding a multiple results in smaller numerical errors than adding each iteration 
     108                        conf.labelValue[i] = (float) t; 
     109                        conf.labelLocation[i] = (float) ((t - min) / diff); 
     110                        t = tMin + (++i) * tickSize; 
     111                } while (t <= max); 
     112 
     113                if (i < ticks) { 
     114                        conf.labelLocation[i] = Float.NaN; 
     115                        conf.labelValue[i] = Float.NaN; 
     116                } 
     117                return conf; 
     118        } 
     119 
    53120        public static class ScaleConfiguration { 
     121                ScaleConfiguration() { 
     122                } 
     123 
    54124                public String unit; 
    55125 
    56                 public int lengthInUnits; 
     126                /** 
     127                 * The values for the labels. 
     128                 *  
     129                 * Any value might be {@link Float#NaN} to indicate an unused entry (this is used to avoid unnecessary 
     130                 * re-allocation of a smaller array). 
     131                 */ 
     132                public float[] labelValue; 
    57133 
    58                 public double lengthInMeter; 
     134                /** 
     135                 * The relative label positions ranging from 0 (minimum) to 1 (maximum). 
     136                 *  
     137                 * Any value might be {@link Float#NaN} just as in {@link #labelValue}. 
     138                 */ 
     139                public float[] labelLocation; 
     140 
     141                /** 
     142                 * The length in source units (meter for distance and elevation, m/s for speed) 
     143                 */ 
     144                public double lengthInSourceUnits; 
    59145        } 
    60146} 
Note: See TracChangeset for help on using the changeset viewer.