private void drawTile(GC gc, Rectangle bounds, Tile tile, boolean highlight) { if (tile == null || tile.equals(Tile.NONE)) return; gc.setClipping(bounds); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; int s = Math.min(w, h); int n = Math.max(s / 12, 1); int x1 = x; int x2 = x1 + n; int x3 = x + s - n; int x4 = x + s; int y1 = y; int y2 = y1 + n; int y3 = y + s - n; int y4 = y + s; int[] p1 = new int[] {x1, y1, x4, y1, x3, y2, x2, y2, x2, y3, x1, y4}; int[] p2 = new int[] {x4, y4, x1, y4, x2, y3, x3, y3, x3, y2, x4, y1}; gc.setBackground(tileFill); gc.fillRectangle(x, y, w, h); gc.setBackground(highlight ? tileLightHighlight : tileLight); gc.fillPolygon(p1); gc.setBackground(highlight ? tileDarkHighlight : tileDark); gc.fillPolygon(p2); if (tile.isWild()) { if (!bounds.contains(mousex, mousey)) { return; } } Font font = new Font(null, tileFont, s / 2 + 1, SWT.BOLD); String string = Character.toString(tile.getLetter()).toUpperCase(); gc.setFont(font); Point extent = gc.stringExtent(string); int sx = x + w / 2 - extent.x / 2 - n; // n * 2; //w / 2 - extent.x / 2 - s/8; int sy = y + h / 2 - extent.y / 2; gc.setForeground(textColor); gc.drawString(string, sx, sy, true); font.dispose(); font = new Font(null, tileFont, s / 5, SWT.BOLD); string = Integer.toString(game.getTileValues().getValue(tile)); gc.setFont(font); extent = gc.stringExtent(string); sx = x + w - n - extent.x - 1; sy = y + h - n - extent.y; gc.setForeground(textColor); gc.drawString(string, sx, sy, true); font.dispose(); }
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(); } }