Changeset 60
- Timestamp:
- 08/08/07 02:46:26 (4 years ago)
- Files:
-
- 5 added
- 6 edited
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/AxisPlotterTile.java (added)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/DataProvider.java (added)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/ElevationPlotterTile.java (added)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/MainCanvas.java (modified) (7 diffs)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/PlotterTile.java (added)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/SpeedPlotterTile.java (added)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/Tile.java (modified) (2 diffs)
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/TrackTile.java (modified) (3 diffs)
-
bbtracker_common/trunk/src/org/bbtracker/ImperialUnitConverter.java (modified) (3 diffs)
-
bbtracker_common/trunk/src/org/bbtracker/MetricUnitConverter.java (modified) (2 diffs)
-
bbtracker_common/trunk/src/org/bbtracker/UnitConverter.java (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
bbtracker/trunk/src/org/bbtracker/mobile/gui/MainCanvas.java
r50 r60 1 1 package org.bbtracker.mobile.gui; 2 3 import java.util.TimerTask; 2 4 3 5 import javax.microedition.lcdui.Canvas; … … 9 11 import javax.microedition.rms.RecordStoreException; 10 12 11 import org.bbtracker.Track;12 13 import org.bbtracker.TrackPoint; 13 14 import org.bbtracker.mobile.BBTracker; … … 16 17 17 18 public 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 18 25 private final TrackManager manager; 19 26 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; 23 32 24 33 private final StatusTile statusTile; 25 34 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; 35 52 36 53 public MainCanvas(final TrackManager manager) { 37 54 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); 41 59 statusTile = new StatusTile(manager); 42 60 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); 43 69 addCommand(newTrackCommand); 44 70 addCommand(tracksCommand); … … 47 73 addCommand(exitCommand); 48 74 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(); 49 110 } 50 111 … … 52 113 super.sizeChanged(w, h); 53 114 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(); 57 116 } 58 117 59 118 protected void paint(final Graphics g) { 60 if ( trackTile.width == 0) {119 if (visibleTiles[0].width == 0) { 61 120 // BlackBerry (at least 8800) seems not to call sizeChanged before initial paint() 62 121 sizeChanged(getWidth(), getHeight()); 63 122 } 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()) { 72 129 final Font font = Font.getDefaultFont(); 73 final int stringWidth = font.stringWidth(stat e);130 final int stringWidth = font.stringWidth(statusMessage); 74 131 final int stringHeight = g.getFont().getHeight(); 75 132 … … 79 136 g.setColor(0x00000000); 80 137 g.drawRect(2, 2, stringWidth + 4, stringHeight + 4); 81 g.drawString(stat e, 4, 4, Graphics.TOP | Graphics.LEFT);138 g.drawString(statusMessage, 4, 4, Graphics.TOP | Graphics.LEFT); 82 139 } 83 140 } 84 141 85 142 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); 88 151 } 89 152 repaint(); 90 153 } 91 154 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 } 94 160 repaint(); 95 161 } 96 162 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 } 105 190 } 106 191 … … 112 197 protected void showNotify() { 113 198 super.showNotify(); 114 final Track newTrack = manager.getTrack();115 if (newTrack != track) {116 setTrack(newTrack);117 }118 trackTile.onResize();119 199 manager.addPointListener(this); 200 for (int i = 0; i < visibleTiles.length && visibleTiles[i] != null; i++) { 201 visibleTiles[i].showNotify(); 202 } 203 updateStatusText(manager.getState()); 120 204 } 121 205 122 206 public void commandAction(final Command command, final Displayable displayable) { 123 207 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 } 135 225 } 136 } else if (command == exitCommand) { 137 BBTracker.getInstance().shutdown(true); 138 return; 139 } 140 BBTracker.getDisplay().setCurrent(nextDisplayable); 226 BBTracker.getDisplay().setCurrent(nextDisplayable); 227 } 141 228 } 142 229 143 230 protected void keyReleased(final int keyCode) { 144 231 final int gameAction = getGameAction(keyCode); 145 if (gameAction == LEFT) { 232 switch (gameAction) { 233 case LEFT: 146 234 manager.changeCurrentPoint(-1); 147 } else if (gameAction == RIGHT) { 235 break; 236 case RIGHT: 148 237 manager.changeCurrentPoint(+1); 149 } else if (gameAction == DOWN) { 238 break; 239 case DOWN: 150 240 manager.changeCurrentPoint(-10); 151 } else if (gameAction == UP) { 241 break; 242 case UP: 152 243 manager.changeCurrentPoint(+10); 244 break; 245 } 246 } 247 248 private class RepaintTask extends TimerTask { 249 public void run() { 250 MainCanvas.this.repaint(); 153 251 } 154 252 } -
bbtracker/trunk/src/org/bbtracker/mobile/gui/Tile.java
r4 r60 3 3 import javax.microedition.lcdui.Graphics; 4 4 5 public abstract class Tile { 5 import org.bbtracker.TrackPoint; 6 import 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 */ 16 public abstract class Tile implements TrackListener { 6 17 protected int xOffset; 7 18 … … 34 45 35 46 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(); 36 61 } -
bbtracker/trunk/src/org/bbtracker/mobile/gui/TrackTile.java
r36 r60 1 1 package org.bbtracker.mobile.gui; 2 3 import java.util.Enumeration;4 2 5 3 import javax.microedition.lcdui.Font; 6 4 import javax.microedition.lcdui.Graphics; 7 5 8 import org.bbtracker.Track;9 import org.bbtracker.TrackPoint;10 import org.bbtracker.TrackSegment;11 6 import org.bbtracker.Utils; 12 7 import org.bbtracker.UnitConverter.ScaleConfiguration; 13 8 import org.bbtracker.mobile.Preferences; 9 import org.bbtracker.mobile.TrackManager; 14 10 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 11 public class TrackTile extends PlotterTile { 32 12 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;41 13 42 14 private int scaleSizeInPixel; 43 15 44 private S tring scaleLabelCenter;16 private ScaleConfiguration scaleConfiguration; 45 17 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); 53 20 } 54 21 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); 92 26 if (widthInMeter < 1) { 93 27 scaleSizeInPixel = 0; … … 97 31 final double availableLengthInMeter = widthInMeter * 0.9; 98 32 99 final ScaleConfiguration conf = Preferences.getInstance().getUnitsConverter().getScaleConfiguration( 100 availableLengthInMeter); 33 scaleConfiguration = Preferences.getInstance().getUnitsConverter().getScaleDistance(availableLengthInMeter); 101 34 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); 115 36 } 116 37 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) { 194 39 if (scaleSizeInPixel == 0) { 195 40 return; … … 198 43 final Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); 199 44 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 } 201 59 g.setFont(font); 202 60 g.setColor(0x00000000); 203 61 g.drawRect(left, height - 2 - SCALE_HEIGTH, scaleSizeInPixel, SCALE_HEIGTH); 204 62 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);210 63 } 211 64 } -
bbtracker_common/trunk/src/org/bbtracker/ImperialUnitConverter.java
r52 r60 15 15 } 16 16 final float mph = speed * MS_TO_MPH_FACTOR; 17 return String.valueOf(((int) (mph * 10)) / 10f) + "mph";17 return Utils.floatToString(mph, false) + "mph"; 18 18 } 19 19 … … 35 35 return feet + "ft"; 36 36 } else { 37 return String.valueOf(((int) (miles * 10)) / 10f) + "mi";37 return Utils.doubleToString(miles, false) + "mi"; 38 38 } 39 39 } 40 40 41 public ScaleConfiguration getScale Configuration(final double lengthInMeter) {41 public ScaleConfiguration getScaleDistance(final double lengthInMeter) { 42 42 final double lengthInFoot = lengthInMeter * METER_TO_FOOT_FACTOR; 43 43 double factor = METER_TO_FOOT_FACTOR; … … 55 55 } 56 56 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 }; 59 61 return conf; 60 62 } 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 } 61 71 } -
bbtracker_common/trunk/src/org/bbtracker/MetricUnitConverter.java
r52 r60 12 12 } 13 13 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"; 15 15 } 16 16 … … 28 28 return ((int) length) + "m"; 29 29 } else { 30 return String.valueOf(((int) (length / 100)) / 10f) + "km";30 return Utils.doubleToString(length / 1000, false) + "km"; 31 31 } 32 32 } 33 33 34 public ScaleConfiguration getScale Configuration(final double lengthInMeter) {34 public ScaleConfiguration getScaleDistance(final double lengthInMeter) { 35 35 final int scaleSize = getRoundScaleSize((int) lengthInMeter); 36 36 final ScaleConfiguration conf = new ScaleConfiguration(); 37 int lengthInUnits; 37 38 if (scaleSize >= 1000) { 38 39 conf.unit = "km"; 39 conf.lengthInUnits = scaleSize / 1000;40 lengthInUnits = scaleSize / 1000; 40 41 } else { 41 42 conf.unit = "m"; 42 conf.lengthInUnits = scaleSize;43 lengthInUnits = scaleSize; 43 44 } 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 }; 45 48 return conf; 46 49 } 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 } 47 59 } -
bbtracker_common/trunk/src/org/bbtracker/UnitConverter.java
r36 r60 28 28 public abstract String distanceToString(final double distance); 29 29 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); 31 35 32 36 /** … … 51 55 } 52 56 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 53 120 public static class ScaleConfiguration { 121 ScaleConfiguration() { 122 } 123 54 124 public String unit; 55 125 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; 57 133 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; 59 145 } 60 146 }
Note: See TracChangeset
for help on using the changeset viewer.