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/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        } 
Note: See TracChangeset for help on using the changeset viewer.