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