- Timestamp:
- 08/08/07 02:46:26 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.