Пример #1
0
 private void emulateDrop(MouseEvent e) {
   while (tileRack.remove(Tile.NONE)) ;
   for (DragTarget target : dragTargets) {
     if (target.rectangle.contains(e.x, e.y)) {
       if (target.y == -1) {
         int x = target.x;
         Tile[] tiles = tileRack.getTiles();
         if (x < tiles.length && !tiles[x].equals(Tile.NONE)) {
           tileRack.add(x, Tile.NONE);
           return;
         }
       }
     }
   }
 }
Пример #2
0
 private boolean beginDrag(MouseEvent e) {
   if (drag != null) {
     doDrop(e);
   }
   for (Rectangle rectangle : dragSources.keySet()) {
     if (rectangle.contains(e.x, e.y)) {
       drag = dragSources.get(rectangle);
       if (drag.y == -1) {
         tileRack.remove(drag.x);
       } else {
         placedTiles.remove(new Point(drag.x, drag.y));
       }
       emulateDrop(e);
       return true;
     }
   }
   return false;
 }
Пример #3
0
 private void doDrop(MouseEvent e) {
   DragSource drag = this.drag;
   this.drag = null;
   if (drag == null) {
     return;
   }
   while (tileRack.remove(Tile.NONE)) ;
   for (DragTarget target : dragTargets) {
     if (target.rectangle.contains(e.x, e.y)) {
       if (target.y == -1) {
         int x = target.x;
         Tile[] tiles = tileRack.getTiles();
         if (x < tiles.length && !tiles[x].equals(Tile.NONE)) {
           tileRack.add(x, drag.tile);
           pushRack();
           return;
         }
       } else {
         IBoard board = game.getBoard();
         Tile current = board.getTile(target.y, target.x);
         if (current.equals(Tile.NONE)) {
           Point point = new Point(target.x, target.y);
           if (!placedTiles.containsKey(point)) {
             placedTiles.put(point, drag.tile);
             updateMoveString();
             return;
           }
         }
       }
     }
   }
   // return to source
   if (drag.y == -1) {
     tileRack.add(drag.tile);
     pushRack();
   } else {
     placedTiles.put(new Point(drag.x, drag.y), drag.tile);
   }
 }
Пример #4
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();
   }
 }