示例#1
0
 private void keyPressed(KeyEvent e) {
   if (game == null) return;
   if (game.getGameState() != GameState.IN_PROGRESS) return;
   char c = Character.toLowerCase(e.character);
   if (c >= 'a' && c <= 'z') {
     // type letters on the board
     if (arrow != null) {
       IBoard board = game.getBoard();
       if (arrow.x >= board.getWidth()) return;
       if (arrow.y >= board.getHeight()) return;
       Tile boardTile = game.getBoard().getTile(arrow.y, arrow.x);
       if (!boardTile.equals(Tile.NONE)) {
         if (c == boardTile.getLetter()) {
           arrow.advance();
           redraw();
         }
         return;
       }
       Tile tile = new Tile(c);
       boolean wild = false;
       if (!tileRack.contains(tile)) {
         tile = new Tile('?');
         if (!tileRack.contains(tile)) {
           return;
         }
         wild = true;
       }
       tileRack.remove(tile);
       tile = new Tile(c, wild);
       placedTiles.put(new Point(arrow.x, arrow.y), tile);
       arrow.advance();
     }
     updateMoveString();
     redraw();
   } else if (c == SWT.DEL || c == SWT.BS) {
     // backspace a typed character on the board
     if (placedTiles.size() > 0) {
       arrow.backup();
       Point point = new Point(arrow.x, arrow.y);
       Tile tile = placedTiles.get(point);
       if (tile != null) {
         if (tile.isWild()) tile = new Tile('?');
         tileRack.add(tile);
         placedTiles.remove(point);
       } else {
         arrow.advance();
       }
       updateMoveString();
       redraw();
     }
   } else if (c == '1') {
     // toggle coordinates
     showCoordinates = !showCoordinates;
     redraw();
   } else if (e.keyCode == SWT.ESC) {
     // clear typed characters
     resetInput(true);
     redraw();
   } else if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR) {
     // submit move on enter key
     done.run();
   } else if (c == ' ') {
     getLocalTileRack().shuffle();
     resetInput(true);
     redraw();
   }
 }