- Timestamp:
- 02/05/09 21:41:56 (13 years ago)
- Location:
- chess/src/chess
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
chess/src/chess/MSG.properties
r22 r25 9 9 command.help=\u0421\u043f\u0440\u0430\u0432\u043a\u0430 10 10 11 start-menu.title= \u041 c\u044b \u0432\u0430\u0441 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u043c!12 start-menu.new-game= \u04 1d\u043e\u0432\u0430\u044f \u0438\u0433\u0440\u043013 start-menu.join-game= \u041 f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f11 start-menu.title= \u041a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e! 12 start-menu.new-game= \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u0433\u0440\u0443 13 start-menu.join-game= \u041d\u0430\u0439\u0442\u0438 \u0438\u0433\u0440\u0443 14 14 start-menu.help= \u0421\u043f\u0440\u0430\u0432\u043a\u0430 15 15 start-menu.exit= \u0412\u044b\u0439\u0442\u0438 16 16 17 join-game.title=\u041 2\u044b\u0431\u0435\u0440\u0438\u0442\u043517 join-game.title=\u0414\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0438\u0433\u0440\u044b: 18 18 join-game.no-server-found.title =\u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e :( 19 19 join-game.no-server-found.message= \u041f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0438\u0433\u0440 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e … … 34 34 35 35 help.title = \u0421\u043f\u0440\u0430\u0432\u043a\u0430 36 help.content = \u0421\u0434\u0435\u043b\u0430\u0442\u044c: \u041e\u0431\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0438\u0433\u0440\u043536 help.content = todo: \u041e\u0431\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u0431 \u0438\u0433\u0440\u0435 37 37 38 38 player.white = \u0411\u0435\u043b\u044b\u0435 -
chess/src/chess/control/BaseController.java
r22 r25 1 1 package chess.control; 2 2 3 import java.util.Vector;4 3 import java.io.IOException; 4 import java.util.*; 5 import javax.microedition.lcdui.Alert; 5 6 import javax.microedition.lcdui.Command; 6 7 import javax.microedition.lcdui.Displayable; 7 import javax.microedition.lcdui.Alert;8 8 9 9 import chess.game.*; 10 import chess.remote.*; 10 import chess.remote.IConnectionListener; 11 import chess.remote.IDatagram; 12 import chess.remote.IRemoteNode; 13 import chess.ui.ChessMIDlet; 11 14 import chess.ui.GameCanvas; 12 15 import chess.ui.IUIListener; 13 import chess.ui.ChessMIDlet;14 16 15 17 … … 21 23 */ 22 24 public class BaseController implements IConnectionListener, IUIListener { 23 public static final int WAITING_FOR_LOCAL = 0; 24 public static final int WAITING_FOR_NETWORK = 1; 25 public static final int WAITING_FOR_OPPONENT = 2; 26 public static final int WAITING_CONNECT = 3; 27 28 private final App app; 29 private final GameCanvas ui; 30 private final IGame game; 31 private final IRemoteNode node; 32 private Player player; 33 34 private final boolean master; 35 36 private int status; 37 38 private long lastTimeStamp = System.currentTimeMillis(); 39 40 private BaseController( final App app, 41 final GameCanvas ui, 42 final IGame game, 43 final Player player, 44 final IRemoteNode node ) throws IOException { 45 this.app = app; 46 this.ui = ui; 47 this.game = game; 48 this.player = player; 49 this.node = node; 50 game.setOwner( player ); 51 52 master = ( player != null ); 53 54 node.setListener( this ); 55 node.start(); 56 57 ui.setUIListener( this ); 58 setStatus( WAITING_CONNECT ); 59 if ( master ) { 60 sendLog( game, player, node ); 61 } else { 62 //wait for incoming actions log 63 //synchronized ( this ) { 64 // while ( this.player == null ) { 65 // try { 66 // //ñòðàõóåìñÿ íà ñëó÷àé, åñëè ñîáûòèÿ óæå óñïåëè ïðèéòè 67 // wait( 150 ); 68 // } catch ( InterruptedException e ) { 69 // e.printStackTrace(); 70 // } 71 // } 72 //} 73 } 74 lastTimeStamp = System.currentTimeMillis(); 75 } 76 77 private void sendLog( final IGame game, 78 final Player player, 79 final IRemoteNode node ) { 80 //ïðè ñîåäèíåíèè â ïåðâóþ î÷åðåäü ïåðåäàåì ïàðòíåðó ñîñòîÿíèå èãðû 81 final Vector log = game.getActionsLog(); 82 for ( int i = 0; i < log.size(); i++ ) { 83 final Action a = ( Action )log.elementAt( i ); 84 node.send( a ); 85 } 86 //çàòåì êåì îí èãðàåò 87 node.send( new SystemAction( SystemAction.SET_PLAYER, player.opponent() ) ); 88 89 /*ðåøàåì, êòî õîäèò*/ 90 if ( log.isEmpty() ) {//íà÷àëî èãðû - õîäÿò áåëûå 91 if ( player.isWhite() ) { 92 ui.setStatus( GameCanvas.ACTIVE ); 93 } else { 94 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 95 } 96 } else { 97 final Action lastAction = ( Action )log.elementAt( log.size() - 1 ); 98 if ( lastAction.getPlayer() == player ) { 99 ui.setStatus( GameCanvas.ACTIVE ); 100 } else { 101 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 102 } 103 } 104 } 105 106 public void connected() { 107 //todo 108 setStatus( WAITING_FOR_OPPONENT ); 109 } 110 111 public synchronized void received( final IDatagram data ) { 112 if ( data instanceof Action ) { 113 processAction( ( Action )data ); 114 } else if ( data instanceof SystemAction ) { 115 final SystemAction sa = ( SystemAction )data; 116 processSystemAction( sa ); 117 } else { 118 throw new IllegalArgumentException( "Datagram " + data + " is in unknown format" ); 119 } 120 } 121 122 protected void processAction( final Action a ) { 123 game.doAction( a ); 124 } 125 126 protected synchronized void processSystemAction( final SystemAction sa ) { 127 if ( sa.type() == SystemAction.SET_PLAYER ) { 128 player = sa.getPlayer(); 129 game.setOwner( player ); 130 if ( isMaster() ) { 131 throw new IllegalStateException( "Master should not got SET_PLAYER commands" ); 132 } 133 setStatus( WAITING_FOR_OPPONENT ); 134 } else if ( sa.type() == SystemAction.YOUR_TURN ) { 135 if ( player == null ) { 136 throw new IllegalStateException( "Player == null" ); 137 } 138 //if ( !master ) { 139 // synchronized ( this ) { 140 // notifyAll(); 141 // } 142 //} 143 lastTimeStamp = System.currentTimeMillis(); 144 setStatus( WAITING_FOR_LOCAL ); 145 } else if ( sa.type() == SystemAction.SURRENDER ) { 146 final ChessMIDlet midlet = app.getMidlet(); 147 setStatus( WAITING_FOR_LOCAL ); 148 midlet.display.setCurrent( 149 new Alert( "Opponent surrenders. You win." ), 150 midlet.startForm 151 ); 152 } else if ( sa.type() == SystemAction.ASK_FOR_DRAW ) { 153 //todo implement 154 } 155 } 156 157 public synchronized void wasSent( final IDatagram data ) { 158 if ( status == WAITING_FOR_NETWORK ) { 159 setStatus( WAITING_FOR_OPPONENT ); 160 } 161 } 162 163 public void closed() { 164 setStatus( WAITING_CONNECT ); 165 } 166 167 public void post( final IDatagram data ) { 168 setStatus( WAITING_FOR_NETWORK ); 169 node.send( data ); 170 } 171 172 public synchronized void setStatus( final int status ) { 173 //todo 174 this.status = status; 175 switch ( status ) { 176 case WAITING_FOR_LOCAL: 177 ui.setStatus( GameCanvas.ACTIVE ); 178 break; 179 case WAITING_FOR_NETWORK: 180 ui.setStatus( GameCanvas.WAITING_FOR_NETWORK ); 181 break; 182 case WAITING_FOR_OPPONENT: 183 ui.setStatus( GameCanvas.WAITING_FOR_OPPONENT ); 184 break; 185 case WAITING_CONNECT: 186 ui.setStatus( GameCanvas.DISCONNECTED ); 187 break; 188 default: 189 throw new IllegalStateException( "Unknown status:" + status ); 190 } 191 } 192 193 public boolean isMaster() { 194 return master; 195 } 196 197 public void keyPressed( final int code ) { 198 ui.clearHighlightedCells(); 199 final Location prevFC = ui.getFocusedCell(); 200 int x, y; 201 if ( prevFC == null ) { 202 x = y = 0; 203 } else { 204 x = prevFC.x; 205 y = prevFC.y; 206 } 207 if ( code == ui.leftKey ) { 208 x -= 1; 209 if ( x < 0 ) { 210 x = game.getWidth() - 1; 211 } 212 } else if ( code == ui.rightKey ) { 213 x += 1; 214 if ( x >= game.getWidth() ) { 215 x = 0; 216 } 217 } else if ( code == ui.downKey ) { 218 y -= 1; 219 if ( y < 0 ) { 220 y = game.getHeight() - 1; 221 } 222 223 } else if ( code == ui.upKey ) { 224 y += 1; 225 if ( y >= game.getHeight() ) { 226 y = 0; 227 } 228 } 229 230 final Location selectedCell = ui.getSelectedCell(); 231 if ( selectedCell == null ) { 232 final Location focused = new Location( x, y ); 233 ui.setFocusedCell( focused ); 234 final IUnit unit = game.getUnit( focused ); 235 if ( unit != null ) { 236 highlightDomain( unit ); 237 } 238 239 if ( code == ui.fireKey 240 && unit != null 241 && unit.getOwner() == player ) { 242 ui.setSelectedCell( focused ); 243 ui.setFocusedCell( focused ); 244 245 ui.addCommand( app.getMidlet().backCommand ); 246 } 247 } else { 248 final IUnit unit = game.getUnit( selectedCell ); 249 if ( unit != null ) { 250 final Location[] domain = highlightDomain( unit ); 251 //final int index = findNearest( domain, x, y ); 252 //if ( index >= 0 ) { 253 // ui.setFocusedCell( domain[index] ); 254 //} 255 final Location _fc = new Location( x, y ); 256 ui.setFocusedCell( _fc ); 257 if ( code == ui.fireKey ) { 258 if ( selectedCell.equals( _fc ) ) { 259 ui.setSelectedCell( null ); 260 ui.removeCommand( app.getMidlet().backCommand ); 261 } else { 262 boolean inDomain = false; 263 for ( int i = 0; i < domain.length; i++ ) { 264 final Location ld = domain[i]; 265 if ( ld.equals( _fc ) ) { 266 inDomain = true; 267 break; 268 } 269 } 270 if ( inDomain ) { 271 final Location focusedCell = ui.getFocusedCell(); 272 273 ui.setFocusedCell( null ); 274 ui.setSelectedCell( null ); 275 ui.clearHighlightedCells(); 276 277 ui.removeCommand( app.getMidlet().backCommand ); 278 279 280 postAction( selectedCell, focusedCell ); 281 } 282 } 283 } 284 } else { 285 ui.setSelectedCell( null ); 286 } 287 288 } 289 } 290 291 private void postAction( final Location from, 292 final Location to ) { 293 final Action action = new Action( 294 player, 295 System.currentTimeMillis() - lastTimeStamp, 296 from, 297 to 298 ); 299 game.doAction( action ); 300 postAction( action ); 301 } 302 303 private void postAction( final Action action ) { 304 post( action ); 305 post( new SystemAction( SystemAction.YOUR_TURN, player ) ); 306 } 307 308 private static int findNearest( final Location[] ls, 309 final int x, 310 final int y ) { 311 int d = Integer.MAX_VALUE; 312 int index = -1; 313 for ( int i = 0; i < ls.length; i++ ) { 314 final Location l = ls[i]; 315 final int _d = l.ldistance( x, y ); 316 if ( _d < d ) { 317 d = _d; 318 index = i; 319 } 320 } 321 322 return index; 323 } 324 325 public void commandAction( final Command command, 326 final Displayable displayable ) { 327 if ( command == app.getMidlet().backCommand ) { 328 ui.removeCommand( command ); 329 ui.setSelectedCell( null ); 330 ui.clearHighlightedCells(); 331 } 332 } 333 334 private Location[] highlightDomain( final IUnit unit ) { 335 final Location[] available = unit.canMoveTo(); 336 for ( int i = 0; i < available.length; i++ ) { 337 final Location l = available[i]; 338 final IUnit u = game.getUnit( l ); 339 final boolean attack = u != null && unit.isOpponent( u ); 340 ui.addHighlightedCell( l, attack ); 341 } 342 return available; 343 } 344 345 public static BaseController createClient( final App app, 346 final GameCanvas ui, 347 final IGame game, 348 final IRemoteNode node ) throws IOException { 349 return new BaseController( app, ui, game, null, node ); 350 } 351 352 public static BaseController createMaster( final App app, 353 final GameCanvas ui, 354 final IGame game, 355 final Player player, 356 final IRemoteNode node ) throws IOException { 357 return new BaseController( app, ui, game, player, node ); 358 } 25 public static final int WAITING_FOR_LOCAL = 0; 26 public static final int WAITING_FOR_NETWORK = 1; 27 public static final int WAITING_FOR_OPPONENT = 2; 28 public static final int WAITING_CONNECT = 3; 29 30 private final App app; 31 private final GameCanvas ui; 32 private final IGame game; 33 private final IRemoteNode node; 34 private Player player; 35 36 private final boolean master; 37 38 private int status; 39 40 private long lastTimeStamp = System.currentTimeMillis(); 41 42 private BaseController( final App app, 43 final GameCanvas ui, 44 final IGame game, 45 final Player player, 46 final IRemoteNode node ) throws IOException { 47 this.app = app; 48 this.ui = ui; 49 this.game = game; 50 this.player = player; 51 this.node = node; 52 game.setOwner( player ); 53 54 master = ( player != null ); 55 56 node.setListener( this ); 57 node.start(); 58 setStatus( WAITING_CONNECT ); 59 60 ui.setUIListener( this ); 61 if( master ) { 62 sendLog( game, player, node ); 63 } 64 lastTimeStamp = System.currentTimeMillis(); 65 } 66 67 private void sendLog( final IGame game, 68 final Player player, 69 final IRemoteNode node ) { 70 //ïðè ñîåäèíåíèè â ïåðâóþ î÷åðåäü ïåðåäàåì ïàðòíåðó ñîñòîÿíèå èãðû 71 final Vector log = game.getActionsLog(); 72 for( int i = 0; i < log.size(); i++ ) { 73 final Action a = ( Action ) log.elementAt( i ); 74 node.send( a ); 75 } 76 //çàòåì êåì îí èãðàåò 77 node.send( new SystemAction( SystemAction.SET_PLAYER, player.opponent() ) ); 78 79 /*ðåøàåì, êòî õîäèò*/ 80 if( log.isEmpty() ) {//íà÷àëî èãðû - õîäÿò áåëûå 81 if( player.isWhite() ) { 82 ui.setStatus( GameCanvas.ACTIVE ); 83 } else { 84 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 85 } 86 } else { 87 final Action lastAction = ( Action ) log.elementAt( log.size() - 1 ); 88 if( lastAction.getPlayer() == player ) { 89 ui.setStatus( GameCanvas.ACTIVE ); 90 } else { 91 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 92 } 93 } 94 } 95 96 public void connected() { 97 //todo 98 setStatus( WAITING_FOR_OPPONENT ); 99 } 100 101 public synchronized void received( final IDatagram data ) { 102 if( data instanceof Action ) { 103 processAction( ( Action ) data ); 104 } else if( data instanceof SystemAction ) { 105 final SystemAction sa = ( SystemAction ) data; 106 processSystemAction( sa ); 107 } else { 108 throw new IllegalArgumentException( "Datagram " + data + " is in unknown format" ); 109 } 110 } 111 112 protected void processAction( final Action a ) { 113 game.doAction( a ); 114 } 115 116 protected synchronized void processSystemAction( final SystemAction sa ) { 117 if( sa.type() == SystemAction.SET_PLAYER ) { 118 player = sa.getPlayer(); 119 game.setOwner( player ); 120 if( isMaster() ) { 121 throw new IllegalStateException( "Master should not got SET_PLAYER commands" ); 122 } 123 setStatus( WAITING_FOR_OPPONENT ); 124 } else if( sa.type() == SystemAction.YOUR_TURN ) { 125 if( player == null ) { 126 throw new IllegalStateException( "Player == null" ); 127 } 128 //if ( !master ) { 129 // synchronized ( this ) { 130 // notifyAll(); 131 // } 132 //} 133 lastTimeStamp = System.currentTimeMillis(); 134 setStatus( WAITING_FOR_LOCAL ); 135 } else if( sa.type() == SystemAction.SURRENDER ) { 136 final ChessMIDlet midlet = app.getMidlet(); 137 setStatus( WAITING_FOR_LOCAL ); 138 midlet.display.setCurrent( 139 new Alert( "Opponent surrenders. You win." ), 140 midlet.startForm 141 ); 142 } else if( sa.type() == SystemAction.ASK_FOR_DRAW ) { 143 //todo implement 144 } 145 } 146 147 public synchronized void wasSent( final IDatagram data ) { 148 if( status == WAITING_FOR_NETWORK ) { 149 setStatus( WAITING_FOR_OPPONENT ); 150 } 151 } 152 153 public void closed() { 154 setStatus( WAITING_CONNECT ); 155 } 156 157 public void post( final IDatagram data ) { 158 setStatus( WAITING_FOR_NETWORK ); 159 node.send( data ); 160 } 161 162 public synchronized void setStatus( final int status ) { 163 //todo 164 this.status = status; 165 switch( status ) { 166 case WAITING_FOR_LOCAL: 167 ui.setStatus( GameCanvas.ACTIVE ); 168 break; 169 case WAITING_FOR_NETWORK: 170 ui.setStatus( GameCanvas.WAITING_FOR_NETWORK ); 171 break; 172 case WAITING_FOR_OPPONENT: 173 ui.setStatus( GameCanvas.WAITING_FOR_OPPONENT ); 174 break; 175 case WAITING_CONNECT: 176 ui.setStatus( GameCanvas.NOT_CONNECTED ); 177 break; 178 default: 179 throw new IllegalStateException( "Unknown status:" + status ); 180 } 181 } 182 183 public boolean isMaster() { 184 return master; 185 } 186 187 public void keyPressed( final int code ) { 188 ui.clearHighlightedCells(); 189 final Location prevFC = ui.getFocusedCell(); 190 int x, y; 191 if( prevFC == null ) { 192 x = y = 0; 193 } else { 194 x = prevFC.x; 195 y = prevFC.y; 196 } 197 if( code == ui.leftKey ) { 198 x -= 1; 199 if( x < 0 ) { 200 x = game.getWidth() - 1; 201 } 202 } else if( code == ui.rightKey ) { 203 x += 1; 204 if( x >= game.getWidth() ) { 205 x = 0; 206 } 207 } else if( code == ui.downKey ) { 208 y -= 1; 209 if( y < 0 ) { 210 y = game.getHeight() - 1; 211 } 212 213 } else if( code == ui.upKey ) { 214 y += 1; 215 if( y >= game.getHeight() ) { 216 y = 0; 217 } 218 } 219 220 final Location selectedCell = ui.getSelectedCell(); 221 if( selectedCell == null ) { 222 final Location focused = new Location( x, y ); 223 ui.setFocusedCell( focused ); 224 final IUnit unit = game.getUnit( focused ); 225 if( unit != null ) { 226 highlightDomain( unit ); 227 } 228 229 if( code == ui.fireKey 230 && unit != null 231 && unit.getOwner() == player ) { 232 ui.setSelectedCell( focused ); 233 ui.setFocusedCell( focused ); 234 235 ui.addCommand( app.getMidlet().backCommand ); 236 } 237 } else { 238 final IUnit unit = game.getUnit( selectedCell ); 239 if( unit != null ) { 240 final Location[] domain = highlightDomain( unit ); 241 //final int index = findNearest( domain, x, y ); 242 //if ( index >= 0 ) { 243 // ui.setFocusedCell( domain[index] ); 244 //} 245 final Location _fc = new Location( x, y ); 246 ui.setFocusedCell( _fc ); 247 if( code == ui.fireKey ) { 248 if( selectedCell.equals( _fc ) ) { 249 ui.setSelectedCell( null ); 250 ui.removeCommand( app.getMidlet().backCommand ); 251 } else { 252 boolean inDomain = false; 253 for( int i = 0; i < domain.length; i++ ) { 254 final Location ld = domain[i]; 255 if( ld.equals( _fc ) ) { 256 inDomain = true; 257 break; 258 } 259 } 260 if( inDomain ) { 261 final Location focusedCell = ui.getFocusedCell(); 262 263 ui.setFocusedCell( null ); 264 ui.setSelectedCell( null ); 265 ui.clearHighlightedCells(); 266 267 ui.removeCommand( app.getMidlet().backCommand ); 268 269 270 postAction( selectedCell, focusedCell ); 271 } 272 } 273 } 274 } else { 275 ui.setSelectedCell( null ); 276 } 277 278 } 279 } 280 281 private void postAction( final Location from, 282 final Location to ) { 283 final Action action = new Action( 284 player, 285 System.currentTimeMillis() - lastTimeStamp, 286 from, 287 to 288 ); 289 game.doAction( action ); 290 postAction( action ); 291 } 292 293 private void postAction( final Action action ) { 294 post( action ); 295 post( new SystemAction( SystemAction.YOUR_TURN, player ) ); 296 } 297 298 public void commandAction( final Command c, 299 final Displayable d ) { 300 final ChessMIDlet midlet = app.getMidlet(); 301 if( c == midlet.backCommand ) { 302 ui.removeCommand( c ); 303 ui.setSelectedCell( null ); 304 ui.clearHighlightedCells(); 305 } else if( c == midlet.cancelCommand && d == ui.waitScreen ) { 306 if( status == WAITING_CONNECT ) { 307 //fixme: òóò ïîäñòàâà -- ïîõîæå, deadlock 308 node.close(); 309 midlet.searchGame(); 310 } 311 } 312 } 313 314 private Location[] highlightDomain( final IUnit unit ) { 315 final Location[] available = unit.canMoveTo(); 316 for( int i = 0; i < available.length; i++ ) { 317 final Location l = available[i]; 318 final IUnit u = game.getUnit( l ); 319 final boolean attack = u != null && unit.isOpponent( u ); 320 ui.addHighlightedCell( l, attack ); 321 } 322 return available; 323 } 324 325 public static BaseController createClient( final App app, 326 final GameCanvas ui, 327 final IGame game, 328 final IRemoteNode node ) throws IOException { 329 return new BaseController( app, ui, game, null, node ); 330 } 331 332 public static BaseController createMaster( final App app, 333 final GameCanvas ui, 334 final IGame game, 335 final Player player, 336 final IRemoteNode node ) throws IOException { 337 return new BaseController( app, ui, game, player, node ); 338 } 359 339 } -
chess/src/chess/remote/impl/BTNode.java
r23 r25 23 23 /** 24 24 * êàíàë BT, èñïîëüçóåìûé íàøèì ïðèëîæåíèåì 25 * todo ïîíÿòü, êàêèå êàíàëû ñóùåñòâóþò 25 26 */ 26 27 private static final String BT_STREAM = "1"; … … 268 269 if ( connection == null ) { 269 270 final String addr = getBluetoothURL( device ); 270 connection = ( StreamConnection )Connector.open( addr );271 connection = ( StreamConnection )Connector.open( addr, Connector.READ_WRITE, true ); 271 272 } 272 273 if ( in == null ) { -
chess/src/chess/remote/impl/BTRemote.java
r24 r25 228 228 229 229 public void inquiryCompleted( final int discType ) { 230 if( discType == INQUIRY_COMPLETED ) { 231 remote.refreshCache(); 232 status = SUCCEEDED; 233 l.finished( remote.getNodes() ); 234 } else { 235 status = FAILED; 236 l.finished( null ); 237 } 230 switch( discType ) { 231 case INQUIRY_COMPLETED: 232 remote.refreshCache(); 233 status = SUCCEEDED; 234 l.finished( remote.getNodes() ); 235 break; 236 case INQUIRY_TERMINATED: 237 Util.log.println( "query interrupted" ); 238 case INQUIRY_ERROR: 239 status = FAILED; 240 l.finished( null ); 241 } 238 242 } 239 243 } -
chess/src/chess/ui/ChessMIDlet.java
r22 r25 195 195 } 196 196 197 p rotectedvoid searchGame() {197 public void searchGame() { 198 198 display.setCurrent( searchForm ); 199 199 searchForm.refresh( false ); … … 201 201 } 202 202 203 p rotectedvoid showHelp() {203 public void showHelp() { 204 204 display.setCurrent( helpScreen ); 205 205 Util.log.println( "show help" ); -
chess/src/chess/ui/GameCanvas.java
r23 r25 1 1 package chess.ui; 2 2 3 import java.util. Vector;3 import java.util.*; 4 4 import javax.microedition.lcdui.*; 5 5 … … 17 17 */ 18 18 public class GameCanvas extends Canvas implements CommandListener, IGameListener { 19 public static final int ACTIVE = 0; 20 public static final int WAITING_FOR_NETWORK = 1; 21 public static final int WAITING_FOR_OPPONENT = 2; 22 public static final int DISCONNECTED = 3; 23 24 private static final char[] COLUMNS = new char[]{ 25 'A', 'B', 'C', 'D', 'E', 'F', 'J', 'H' 26 }; 27 private static final char[] ROWS = new char[]{ 28 '1', '2', '3', '4', '5', '6', '7', '8' 29 }; 30 31 protected final ChessMIDlet midlet; 32 33 public final int fireKey; 34 public final int leftKey; 35 public final int rightKey; 36 public final int upKey; 37 public final int downKey; 38 39 //protected Player player; 40 protected IGame game; 41 42 private int status = ACTIVE; 43 44 private Location focusedCell; 45 private Location selectedCell; 46 47 //bitwise masks -- 1 bit per cell 48 private final BitSet64 highlightedCells = new BitSet64(); 49 private final BitSet64 attackedCells = new BitSet64(); 50 51 private IUIListener listener = null; 52 private transient int vMargin; 53 private transient int hMargin; 54 private transient int cellSize; 55 56 57 private static final int MARGIN = 1; 58 //colors 59 private static final int BG_COLOR = 0xFFFFFF; 60 private static final int LABELS_COLOR = 0; 61 62 private final WaitScreen waitScreen; 63 64 private final Command surrenderCommand = new Command( MSG.getMessage( "command.surrender" ), Command.CANCEL, 1 ); 65 private final Command askForDrawCommand = new Command( MSG.getMessage( "command.ask-for-draw" ), Command.SCREEN, 1 ); 66 67 public GameCanvas( final ChessMIDlet midlet ) { 68 this.midlet = midlet; 69 fireKey = getKeyCode( FIRE ); 70 leftKey = getKeyCode( LEFT ); 71 rightKey = getKeyCode( RIGHT ); 72 upKey = getKeyCode( UP ); 73 downKey = getKeyCode( DOWN ); 74 75 waitScreen = new WaitScreen( 76 midlet, 77 MSG.getMessage( "wait.title" ), 78 this 79 ); 80 81 addCommand( midlet.closeCommand ); 82 addCommand( surrenderCommand ); 83 addCommand( askForDrawCommand ); 84 addCommand( midlet.helpCommand ); 85 setCommandListener( this ); 86 } 87 88 public void init( final IGame game ) { 89 this.game = game; 90 game.setListener( this ); 91 } 92 93 public void setUIListener( final IUIListener listener ) { 94 this.listener = listener; 95 } 96 97 public Location getFocusedCell() { 98 return focusedCell; 99 } 100 101 public void setFocusedCell( final Location l ) { 102 if ( focusedCell == null && l == null ) { 103 //nothing 104 } else if ( focusedCell == null ) { 105 focusedCell = l; 106 repaintCell( l.x, l.y ); 107 } else if ( l == null ) { 108 repaintCell( focusedCell.x, focusedCell.y ); 109 focusedCell = l; 110 } else { 111 final Location old = focusedCell; 112 focusedCell = l; 113 repaintCell( old.x, old.y ); 114 repaintCell( l.x, l.y ); 115 } 116 } 117 118 public Location getSelectedCell() { 119 return selectedCell; 120 } 121 122 public void setSelectedCell( final Location l ) { 123 if ( selectedCell == null && l == null ) { 124 //nothing 125 } else if ( selectedCell == null ) { 126 selectedCell = l; 127 repaintCell( l.x, l.y ); 128 } else if ( l == null ) { 129 repaintCell( selectedCell.x, selectedCell.y ); 130 selectedCell = l; 131 } else { 132 final Location old = selectedCell; 133 selectedCell = l; 134 repaintCell( old.x, old.y ); 135 repaintCell( l.x, l.y ); 136 } 137 } 138 139 public void addHighlightedCell( final Location l, 140 final boolean attack ) { 141 addHighlightedCell( l.x, l.y, attack ); 142 } 143 144 public void addHighlightedCell( final int x, 145 final int y, 146 final boolean attack ) { 147 final byte index = game.toIndex( x, y ); 148 if ( attack ) { 149 attackedCells.set( index ); 150 } else { 151 highlightedCells.set( index ); 152 } 153 repaintCell( x, y ); 154 } 155 156 public void clearHighlightedCells() { 157 highlightedCells.clear(); 158 attackedCells.clear(); 159 repaint(); 160 } 161 162 protected void paint( final Graphics g ) { 163 final int canvasWidth = getWidth(); 164 final int canvasHeight = getHeight(); 165 //clear 166 g.setColor( BG_COLOR ); 167 g.fillRect( 0, 0, canvasWidth, canvasHeight ); 168 169 //paint desk 170 if ( game != null ) { 171 final int nX = game.getWidth(); 172 final int nY = game.getHeight(); 173 174 final int availableWidth = canvasWidth - 2 * MARGIN; 175 final int availableHeight = canvasHeight - 2 * MARGIN; 176 177 final Font font = g.getFont(); 178 final int fontHeight = font.getHeight(); 179 180 //ðåçåðâèðóåì ìåñòî ïîä èíôîðìåðû: 2 * (âûñîòîé rows ñòðîê + îòñòóïû ) 181 //ñ÷èòàåì, ñêîëüêî íóæíî ñòðî÷åê, ÷òîáû íàïèñàòü "Áåëûå: 1235 ñåê <ñïèñîê âçÿòûõ ôèãóð>" 182 final int charWidth = font.charWidth( 'W' ); 183 final int chars = Math.max( 184 Player.WHITE.name().length(), 185 Player.BLACK.name().length() 186 ) + ": 99999 sec".length() + 16/*16 units per player*/; 187 188 final int rows = ( int )Math.floor( availableWidth / ( 1.0f * charWidth * chars ) ) + 1; 189 final int informerHeight = rows * fontHeight; 190 191 final int _availableHeight = availableHeight - 2 * ( informerHeight + MARGIN ); 192 193 //ðåçåðâèðóåì 2 ñòîëáöà è 2 ñòðîêè äëÿ ïîäïèñåé 194 cellSize = Math.min( 195 availableWidth / ( nX + 2 ), 196 _availableHeight / ( nY + 2 ) 197 ); 198 199 if ( cellSize < 2 ) { 200 throw new IllegalStateException( "Too small canvas" ); 201 } 202 203 final int _deskWidth = cellSize * ( nX + 2 ); 204 final int _deskHeight = cellSize * ( nY + 2 ); 205 206 final int vDeskMargin = ( canvasHeight - _deskHeight ) / 2; 207 final int hDeskMargin = ( canvasWidth - _deskWidth ) / 2; 208 vMargin = vDeskMargin + cellSize; 209 hMargin = hDeskMargin + cellSize; 210 211 //draw labels 212 drawLabels( g, nX, nY ); 213 214 //draw desk and units 215 for ( int _y = 0; _y < nY; _y++ ) { 216 for ( int _x = 0; _x < nX; _x++ ) { 217 final int x = toScreenX( _x ); 218 final int y = toScreenY( _y ); 219 220 final int index = game.toIndex( _x, _y ); 221 //A1 -- ÷åðíîå 222 final boolean black = ( _x % 2 == 0 ) ^ ( _y % 2 != 0 ); 223 final boolean highlight = highlightedCells.is( index ); 224 final boolean attack = attackedCells.is( index ); 225 if ( black ) { 226 if ( attack ) { 227 g.setColor( 150, 100, 100 ); 228 } else if ( highlight ) { 229 g.setColor( 100, 150, 100 ); 230 } else { 231 g.setColor( 100, 100, 100 ); 232 } 233 } else { 234 if ( attack ) { 235 g.setColor( 250, 200, 200 ); 236 } else if ( highlight ) { 237 g.setColor( 200, 250, 200 ); 238 } else { 239 g.setColor( 200, 200, 200 ); 240 } 241 } 242 243 g.fillRect( x, y, cellSize, cellSize ); 244 245 final IUnit unit = game.getUnit( _x, _y ); 246 if ( unit != null ) { 247 unit.paint( g, x, y, cellSize, cellSize ); 248 } 249 } 250 } 251 if ( focusedCell != null ) { 252 g.setColor( 0, 150, 0 ); 253 final int x = toScreenX( focusedCell.x ); 254 final int y = toScreenY( focusedCell.y ); 255 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 256 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 257 } 258 if ( selectedCell != null ) { 259 g.setColor( 0, 250, 0 ); 260 final int x = toScreenX( selectedCell.x ); 261 final int y = toScreenY( selectedCell.y ); 262 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 263 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 264 } 265 266 g.setColor( 0 ); 267 g.drawRect( 268 hMargin - cellSize, 269 vMargin - cellSize, 270 _deskWidth, 271 _deskHeight 272 ); 273 g.drawRect( 274 hMargin, 275 vMargin, 276 _deskWidth - cellSize * 2, 277 _deskHeight - cellSize * 2 278 ); 279 280 drawInformers( g, availableWidth, informerHeight ); 281 } 282 } 283 284 private void drawLabels( final Graphics g, 285 final int nX, 286 final int nY ) { 287 //draw labels 288 { 289 g.setColor( LABELS_COLOR ); 290 final int x1 = toScreenX( -1 ); 291 final int x2 = toScreenX( nX ); 292 for ( int i = 0; i < nY; i++ ) { 293 final int y = toScreenY( i ); 294 295 final int baseline = y + cellSize * 3 / 4; 296 g.drawChar( 297 ROWS[i], 298 x1 + cellSize / 2, 299 baseline, 300 Graphics.HCENTER | Graphics.BASELINE 301 ); 302 g.drawChar( 303 ROWS[i], 304 x2 + cellSize / 2, 305 baseline, 306 Graphics.HCENTER | Graphics.BASELINE 307 ); 308 } 309 final int y1 = toScreenY( -1 ); 310 final int y2 = toScreenY( nY ); 311 for ( int i = 0; i < nX; i++ ) { 312 final int x = toScreenX( i ); 313 314 final int baseline1 = y1 + cellSize * 3 / 4; 315 final int baseline2 = y2 + cellSize * 3 / 4; 316 g.drawChar( 317 COLUMNS[i], 318 x + cellSize / 2, 319 baseline1, 320 Graphics.HCENTER | Graphics.BASELINE 321 ); 322 g.drawChar( 323 COLUMNS[i], 324 x + cellSize / 2, 325 baseline2, 326 Graphics.HCENTER | Graphics.BASELINE 327 ); 328 } 329 } 330 } 331 332 private void drawInformers( final Graphics g, 333 final int width, 334 final int height ) { 335 final int x = MARGIN; 336 final int canvasHeight = getHeight(); 337 338 drawInformer( g, 339 x, MARGIN, 340 width, height, 341 Player.BLACK 342 ); 343 344 drawInformer( g, 345 x, canvasHeight - height - 2 * MARGIN, 346 width, height, 347 Player.WHITE 348 ); 349 } 350 351 private void drawInformer( final Graphics g, 352 final int x, 353 final int y, 354 final int width, 355 final int height, 356 final Player player ) { 357 g.drawRect( x, y, width, height ); 358 final String name = game.getOwner() == player ? player.name() + " (you)" : player.name(); 359 final int inset = 2; 360 g.drawString( name, 361 x + inset, 362 y + inset, 363 Graphics.LEFT | Graphics.TOP 364 ); 365 final Font font = g.getFont(); 366 final int nameWidth = font.stringWidth( name ); 367 368 final long timing = game.getTiming( player ); 369 370 final String time = ": " + String.valueOf( timing / 1000 ) + " s"; 371 final int timeWidth = font.stringWidth( time ); 372 g.drawString( time, 373 x + inset + nameWidth + inset, 374 y + inset, 375 Graphics.LEFT | Graphics.TOP 376 ); 377 378 379 final Vector dead = game.getDeadUnits( player.opponent() ); 380 final int length = dead.size(); 381 final int size = 10; 382 final int xStart = x + nameWidth + timeWidth + 3 * inset; 383 for ( int i = 0; i < length; i++ ) { 384 final IUnit u = ( IUnit )dead.elementAt( i ); 385 u.paint( g, 386 xStart + i * size, 387 y + inset, 388 size, size 389 ); 390 } 391 } 392 393 private int toScreenX( final int x ) { 394 return x * cellSize + hMargin; 395 } 396 397 private int toScreenY( final int y ) { 398 return ( game.getHeight() - 1 - y ) * cellSize + vMargin; 399 } 400 401 private void repaintCell( final int x, 402 final int y ) { 403 final int _x = toScreenX( x ); 404 final int _y = toScreenY( y ); 405 repaint( 406 _x - 1, _y - 1, 407 cellSize + 2, cellSize + 2 408 ); 409 //System.out.println( "queried repaint:[" + x + "," + y + "]" ); 410 //repaint(); 411 } 412 413 protected void keyPressed( final int keyCode ) { 414 super.keyPressed( keyCode ); 415 if ( listener != null && isActive() ) { 416 listener.keyPressed( keyCode ); 417 } 418 } 419 420 public void commandAction( final Command c, 421 final Displayable displayable ) { 422 if ( c == midlet.helpCommand ) { 423 midlet.showHelp(); 424 //} else if ( c == surrenderCommand ) { 425 //} else if ( c == askForDrawCommand ) { 426 } else if ( c == midlet.closeCommand ) { 427 midlet.showStartScreen(); 428 } else if ( listener != null && isActive() ) { 429 listener.commandAction( c, displayable ); 430 } 431 } 432 433 public void changed() { 434 if ( isShown() ) { 435 repaint(); 436 } 437 } 438 439 public synchronized void setStatus( final int status ) { 440 this.status = status; 441 switch ( status ) { 442 case ACTIVE: 443 midlet.display.setCurrent( this ); 444 Util.log.println( "status:ACTIVE" ); 445 break; 446 case WAITING_FOR_NETWORK: 447 showWaitMessage( MSG.getMessage( "wait.for-network" ) ); 448 Util.log.println( "status:WAIT_FOR_NETWORK" ); 449 break; 450 case WAITING_FOR_OPPONENT: 451 showWaitMessage( MSG.getMessage( "wait.for-opponent" ) ); 452 Util.log.println( "status:WAIT_FOR_OPPONENT" ); 453 break; 454 case DISCONNECTED: 455 showWaitMessage( MSG.getMessage( "wait.for-connect" ) ); 456 Util.log.println( "status:DISCONNECTED" ); 457 //todo implement 458 break; 459 } 460 } 461 462 private void showWaitMessage( final String message ) { 463 waitScreen.setText( message ); 464 if ( midlet.display.getCurrent() != waitScreen ) { 465 midlet.display.setCurrent( waitScreen ); 466 } 467 } 468 469 public synchronized int getStatus() { 470 return status; 471 } 472 473 public boolean isActive() { 474 return getStatus() == ACTIVE; 475 } 476 477 //public void repaint(){ 478 // this.repaint(); 479 //} 19 public static final int ACTIVE = 0; 20 public static final int WAITING_FOR_NETWORK = 1; 21 public static final int WAITING_FOR_OPPONENT = 2; 22 public static final int NOT_CONNECTED = 3; 23 24 private static final char[] COLUMNS = new char[] { 25 'A', 'B', 'C', 'D', 'E', 'F', 'J', 'H' 26 }; 27 private static final char[] ROWS = new char[] { 28 '1', '2', '3', '4', '5', '6', '7', '8' 29 }; 30 31 protected final ChessMIDlet midlet; 32 33 public final int fireKey; 34 public final int leftKey; 35 public final int rightKey; 36 public final int upKey; 37 public final int downKey; 38 39 //protected Player player; 40 protected IGame game; 41 42 private int status = ACTIVE; 43 44 private Location focusedCell; 45 private Location selectedCell; 46 47 //bitwise masks -- 1 bit per cell 48 private final BitSet64 highlightedCells = new BitSet64(); 49 private final BitSet64 attackedCells = new BitSet64(); 50 51 private IUIListener listener = null; 52 private transient int vMargin; 53 private transient int hMargin; 54 private transient int cellSize; 55 56 57 private static final int MARGIN = 1; 58 //colors 59 private static final int BG_COLOR = 0xFFFFFF; 60 private static final int LABELS_COLOR = 0; 61 62 public final WaitScreen waitScreen; 63 64 public final Command surrenderCommand = new Command( MSG.getMessage( "command.surrender" ), Command.CANCEL, 1 ); 65 public final Command askForDrawCommand = new Command( MSG.getMessage( "command.ask-for-draw" ), Command.SCREEN, 1 ); 66 67 public GameCanvas( final ChessMIDlet midlet ) { 68 this.midlet = midlet; 69 fireKey = getKeyCode( FIRE ); 70 leftKey = getKeyCode( LEFT ); 71 rightKey = getKeyCode( RIGHT ); 72 upKey = getKeyCode( UP ); 73 downKey = getKeyCode( DOWN ); 74 75 waitScreen = new WaitScreen( 76 midlet, 77 MSG.getMessage( "wait.title" ), 78 this 79 ); 80 waitScreen.addCommand( midlet.cancelCommand ); 81 waitScreen.setCommandListener( this ); 82 83 addCommand( midlet.closeCommand ); 84 addCommand( surrenderCommand ); 85 addCommand( askForDrawCommand ); 86 addCommand( midlet.helpCommand ); 87 setCommandListener( this ); 88 } 89 90 public void init( final IGame game ) { 91 if( this.game != null ) { 92 this.game.setListener( null ); 93 } 94 this.game = game; 95 game.setListener( this ); 96 } 97 98 public void setUIListener( final IUIListener listener ) { 99 this.listener = listener; 100 } 101 102 public Location getFocusedCell() { 103 return focusedCell; 104 } 105 106 public void setFocusedCell( final Location l ) { 107 if( focusedCell == null && l == null ) { 108 //nothing 109 } else if( focusedCell == null ) { 110 focusedCell = l; 111 repaintCell( l.x, l.y ); 112 } else if( l == null ) { 113 repaintCell( focusedCell.x, focusedCell.y ); 114 focusedCell = l; 115 } else { 116 final Location old = focusedCell; 117 focusedCell = l; 118 repaintCell( old.x, old.y ); 119 repaintCell( l.x, l.y ); 120 } 121 } 122 123 public Location getSelectedCell() { 124 return selectedCell; 125 } 126 127 public void setSelectedCell( final Location l ) { 128 if( selectedCell == null && l == null ) { 129 //nothing 130 } else if( selectedCell == null ) { 131 selectedCell = l; 132 repaintCell( l.x, l.y ); 133 } else if( l == null ) { 134 repaintCell( selectedCell.x, selectedCell.y ); 135 selectedCell = l; 136 } else { 137 final Location old = selectedCell; 138 selectedCell = l; 139 repaintCell( old.x, old.y ); 140 repaintCell( l.x, l.y ); 141 } 142 } 143 144 public void addHighlightedCell( final Location l, 145 final boolean attack ) { 146 addHighlightedCell( l.x, l.y, attack ); 147 } 148 149 public void addHighlightedCell( final int x, 150 final int y, 151 final boolean attack ) { 152 final byte index = game.toIndex( x, y ); 153 if( attack ) { 154 attackedCells.set( index ); 155 } else { 156 highlightedCells.set( index ); 157 } 158 repaintCell( x, y ); 159 } 160 161 public void clearHighlightedCells() { 162 highlightedCells.clear(); 163 attackedCells.clear(); 164 repaint(); 165 } 166 167 protected void paint( final Graphics g ) { 168 final int canvasWidth = getWidth(); 169 final int canvasHeight = getHeight(); 170 //clear 171 g.setColor( BG_COLOR ); 172 g.fillRect( 0, 0, canvasWidth, canvasHeight ); 173 174 //paint desk 175 if( game != null ) { 176 final int nX = game.getWidth(); 177 final int nY = game.getHeight(); 178 179 final int availableWidth = canvasWidth - 2 * MARGIN; 180 final int availableHeight = canvasHeight - 2 * MARGIN; 181 182 final Font font = g.getFont(); 183 final int fontHeight = font.getHeight(); 184 185 //ðåçåðâèðóåì ìåñòî ïîä èíôîðìåðû: 2 * (âûñîòîé rows ñòðîê + îòñòóïû ) 186 //ñ÷èòàåì, ñêîëüêî íóæíî ñòðî÷åê, ÷òîáû íàïèñàòü "Áåëûå: 1235 ñåê <ñïèñîê âçÿòûõ ôèãóð>" 187 final int charWidth = font.charWidth( 'W' ); 188 final int chars = Math.max( 189 Player.WHITE.name().length(), 190 Player.BLACK.name().length() 191 ) + ": 99999 sec".length() + 16/*16 units per player*/; 192 193 final int rows = ( int ) Math.floor( availableWidth / ( 1.0f * charWidth * chars ) ) + 1; 194 final int informerHeight = rows * fontHeight; 195 196 final int _availableHeight = availableHeight - 2 * ( informerHeight + MARGIN ); 197 198 //ðåçåðâèðóåì 2 ñòîëáöà è 2 ñòðîêè äëÿ ïîäïèñåé 199 cellSize = Math.min( 200 availableWidth / ( nX + 2 ), 201 _availableHeight / ( nY + 2 ) 202 ); 203 204 if( cellSize < 2 ) { 205 throw new IllegalStateException( "Too small canvas" ); 206 } 207 208 final int _deskWidth = cellSize * ( nX + 2 ); 209 final int _deskHeight = cellSize * ( nY + 2 ); 210 211 final int vDeskMargin = ( canvasHeight - _deskHeight ) / 2; 212 final int hDeskMargin = ( canvasWidth - _deskWidth ) / 2; 213 vMargin = vDeskMargin + cellSize; 214 hMargin = hDeskMargin + cellSize; 215 216 //draw labels 217 drawLabels( g, nX, nY ); 218 219 //draw desk and units 220 for( int _y = 0; _y < nY; _y++ ) { 221 for( int _x = 0; _x < nX; _x++ ) { 222 final int x = toScreenX( _x ); 223 final int y = toScreenY( _y ); 224 225 final int index = game.toIndex( _x, _y ); 226 //A1 -- ÷åðíîå 227 final boolean black = ( _x % 2 == 0 ) ^ ( _y % 2 != 0 ); 228 final boolean highlight = highlightedCells.is( index ); 229 final boolean attack = attackedCells.is( index ); 230 if( black ) { 231 if( attack ) { 232 g.setColor( 150, 100, 100 ); 233 } else if( highlight ) { 234 g.setColor( 100, 150, 100 ); 235 } else { 236 g.setColor( 100, 100, 100 ); 237 } 238 } else { 239 if( attack ) { 240 g.setColor( 250, 200, 200 ); 241 } else if( highlight ) { 242 g.setColor( 200, 250, 200 ); 243 } else { 244 g.setColor( 200, 200, 200 ); 245 } 246 } 247 248 g.fillRect( x, y, cellSize, cellSize ); 249 250 final IUnit unit = game.getUnit( _x, _y ); 251 if( unit != null ) { 252 unit.paint( g, x, y, cellSize, cellSize ); 253 } 254 } 255 } 256 if( focusedCell != null ) { 257 g.setColor( 0, 150, 0 ); 258 final int x = toScreenX( focusedCell.x ); 259 final int y = toScreenY( focusedCell.y ); 260 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 261 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 262 } 263 if( selectedCell != null ) { 264 g.setColor( 0, 250, 0 ); 265 final int x = toScreenX( selectedCell.x ); 266 final int y = toScreenY( selectedCell.y ); 267 g.drawRoundRect( x + 1, y + 1, cellSize - 2, cellSize - 2, 2, 2 ); 268 g.drawRoundRect( x, y, cellSize, cellSize, 2, 2 ); 269 } 270 271 g.setColor( 0 ); 272 g.drawRect( 273 hMargin - cellSize, 274 vMargin - cellSize, 275 _deskWidth, 276 _deskHeight 277 ); 278 g.drawRect( 279 hMargin, 280 vMargin, 281 _deskWidth - cellSize * 2, 282 _deskHeight - cellSize * 2 283 ); 284 285 drawInformers( g, availableWidth, informerHeight ); 286 } 287 } 288 289 private void drawLabels( final Graphics g, 290 final int nX, 291 final int nY ) { 292 //draw labels 293 { 294 g.setColor( LABELS_COLOR ); 295 final int x1 = toScreenX( -1 ); 296 final int x2 = toScreenX( nX ); 297 for( int i = 0; i < nY; i++ ) { 298 final int y = toScreenY( i ); 299 300 final int baseline = y + cellSize * 3 / 4; 301 g.drawChar( 302 ROWS[i], 303 x1 + cellSize / 2, 304 baseline, 305 Graphics.HCENTER | Graphics.BASELINE 306 ); 307 g.drawChar( 308 ROWS[i], 309 x2 + cellSize / 2, 310 baseline, 311 Graphics.HCENTER | Graphics.BASELINE 312 ); 313 } 314 final int y1 = toScreenY( -1 ); 315 final int y2 = toScreenY( nY ); 316 for( int i = 0; i < nX; i++ ) { 317 final int x = toScreenX( i ); 318 319 final int baseline1 = y1 + cellSize * 3 / 4; 320 final int baseline2 = y2 + cellSize * 3 / 4; 321 g.drawChar( 322 COLUMNS[i], 323 x + cellSize / 2, 324 baseline1, 325 Graphics.HCENTER | Graphics.BASELINE 326 ); 327 g.drawChar( 328 COLUMNS[i], 329 x + cellSize / 2, 330 baseline2, 331 Graphics.HCENTER | Graphics.BASELINE 332 ); 333 } 334 } 335 } 336 337 private void drawInformers( final Graphics g, 338 final int width, 339 final int height ) { 340 final int x = MARGIN; 341 final int canvasHeight = getHeight(); 342 343 drawInformer( g, 344 x, MARGIN, 345 width, height, 346 Player.BLACK 347 ); 348 349 drawInformer( g, 350 x, canvasHeight - height - 2 * MARGIN, 351 width, height, 352 Player.WHITE 353 ); 354 } 355 356 private void drawInformer( final Graphics g, 357 final int x, 358 final int y, 359 final int width, 360 final int height, 361 final Player player ) { 362 g.drawRect( x, y, width, height ); 363 final String name = game.getOwner() == player ? player.name() + " (you)" : player.name(); 364 final int inset = 2; 365 g.drawString( name, 366 x + inset, 367 y + inset, 368 Graphics.LEFT | Graphics.TOP 369 ); 370 final Font font = g.getFont(); 371 final int nameWidth = font.stringWidth( name ); 372 373 final long timing = game.getTiming( player ); 374 375 final String time = ": " + String.valueOf( timing / 1000 ) + " s"; 376 final int timeWidth = font.stringWidth( time ); 377 g.drawString( time, 378 x + inset + nameWidth + inset, 379 y + inset, 380 Graphics.LEFT | Graphics.TOP 381 ); 382 383 384 final Vector dead = game.getDeadUnits( player.opponent() ); 385 final int length = dead.size(); 386 final int size = 10; 387 final int xStart = x + nameWidth + timeWidth + 3 * inset; 388 for( int i = 0; i < length; i++ ) { 389 final IUnit u = ( IUnit ) dead.elementAt( i ); 390 u.paint( g, 391 xStart + i * size, 392 y + inset, 393 size, size 394 ); 395 } 396 } 397 398 private int toScreenX( final int x ) { 399 return x * cellSize + hMargin; 400 } 401 402 private int toScreenY( final int y ) { 403 return ( game.getHeight() - 1 - y ) * cellSize + vMargin; 404 } 405 406 private void repaintCell( final int x, 407 final int y ) { 408 final int _x = toScreenX( x ); 409 final int _y = toScreenY( y ); 410 repaint( 411 _x - 1, _y - 1, 412 cellSize + 2, cellSize + 2 413 ); 414 //System.out.println( "queried repaint:[" + x + "," + y + "]" ); 415 //repaint(); 416 } 417 418 protected void keyPressed( final int keyCode ) { 419 super.keyPressed( keyCode ); 420 if( listener != null && isActive() ) { 421 listener.keyPressed( keyCode ); 422 } 423 } 424 425 public void commandAction( final Command c, 426 final Displayable displayable ) { 427 if( c == midlet.helpCommand ) { 428 midlet.showHelp(); 429 } else if( c == midlet.closeCommand ) { 430 midlet.showStartScreen(); 431 } else if( listener != null && isActive() ) { 432 listener.commandAction( c, displayable ); 433 } 434 } 435 436 public void changed() { 437 if( isShown() ) { 438 repaint(); 439 } 440 } 441 442 public synchronized void setStatus( final int status ) { 443 this.status = status; 444 switch( status ) { 445 case ACTIVE: 446 midlet.display.setCurrent( this ); 447 Util.log.println( "status:ACTIVE" ); 448 break; 449 case WAITING_FOR_NETWORK: 450 showWaitMessage( MSG.getMessage( "wait.for-network" ) ); 451 Util.log.println( "status:WAIT_FOR_NETWORK" ); 452 break; 453 case WAITING_FOR_OPPONENT: 454 showWaitMessage( MSG.getMessage( "wait.for-opponent" ) ); 455 Util.log.println( "status:WAIT_FOR_OPPONENT" ); 456 break; 457 case NOT_CONNECTED: 458 showWaitMessage( MSG.getMessage( "wait.for-connect" ) ); 459 Util.log.println( "status:NOT_CONNECTED" ); 460 //todo implement 461 break; 462 } 463 } 464 465 private void showWaitMessage( final String message ) { 466 waitScreen.setText( message ); 467 if( midlet.display.getCurrent() != waitScreen ) { 468 midlet.display.setCurrent( waitScreen ); 469 } 470 } 471 472 public synchronized int getStatus() { 473 return status; 474 } 475 476 public boolean isActive() { 477 return getStatus() == ACTIVE; 478 } 479 480 //public void repaint(){ 481 // this.repaint(); 482 //} 480 483 } -
chess/src/chess/ui/JoinGameScreen.java
r22 r25 50 50 setChoiceListener( this ); 51 51 } 52 53 52 54 53 55 public void run() {
Note: See TracChangeset
for help on using the changeset viewer.