private void drawCoordinates(IBoard b, GC gc, int xo, int yo, int size, int csize) { gc.setBackground(light); gc.setForeground(coordinateColor); gc.setFont(coordinateFont); int xs = size / b.getWidth(); int ys = size / b.getHeight(); int pdx = 4 + gc.stringExtent("X").x; int pdy = 4; for (int y = 0; y < b.getHeight(); y++) { String s = Character.toString((char) (y + 'A')); int x = 0; int px = xo + x * xs - gc.stringExtent(s).x / 2 - pdx; int py = yo + y * ys + ys / 2 - gc.stringExtent(s).y / 2; gc.drawString(s, px, py, true); } for (int x = 0; x < b.getWidth(); x++) { String s = Integer.toString(x + 1); int y = 0; int px = xo + x * xs + xs / 2 - gc.stringExtent(s).x / 2; int py = yo + y * ys - gc.stringExtent(s).y - pdy; gc.drawString(s, px, py, true); } }
private void drawContainer(GC gc, Rectangle bounds) { gc.setClipping(bounds); dragSources.clear(); dragTargets.clear(); // gc.setTextAntialias(SWT.ON); // gc.setAntialias(SWT.OFF); // gc.setAdvanced(false); gc.setBackground(light); gc.fillRectangle(bounds); if (game == null) { return; } IBoard board = game.getBoard(); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; int coord = showCoordinates ? coordinates : 0; // compute tile size int height = bounds.height - (4 * border + 3 * padding + 1 * coord); int width = bounds.width - (2 * border + 2 * padding + 1 * coord); tileSize = Math.min(width / board.getWidth(), height / (board.getHeight() + 1)); // compute header size and position int hh = tileSize + border * 2; int hw = tileSize * board.getWidth() + border * 2; int hx = (w - hw + coord) / 2; int hy = y + padding; drawHeader(gc, new Rectangle(hx, hy, hw, hh)); // compute board area size and position int cx = x + padding; int cy = y + hh + padding * 2; int cw = w - padding * 2; int ch = h - hh - padding * 3; drawCenter(gc, new Rectangle(cx, cy, cw, ch)); }
private void drawBoard(IBoard b, GC gc, int xo, int yo, int size) { squareLocations.clear(); int tileSize = size / b.getWidth(); // TODO handle non-square boards for (int y = 0; y < b.getHeight(); y++) { for (int x = 0; x < b.getWidth(); x++) { int px = xo + x * tileSize; int py = yo + y * tileSize; Rectangle rectangle = new Rectangle(px, py, tileSize, tileSize); DragTarget target = new DragTarget(); target.rectangle = rectangle; target.x = x; target.y = y; dragTargets.add(target); squareLocations.add(new Square(x, y, rectangle)); drawTile(b, gc, x, y, px, py, tileSize - 1); } } }
private void drawCenter(GC gc, Rectangle bounds) { gc.setClipping(bounds); int x = bounds.x; int y = bounds.y; int w = bounds.width; int h = bounds.height; gc.setFont(coordinateFont); int coord = showCoordinates ? coordinates : 0; int n = coord + border * 2; IBoard board = game.getBoard(); int t = Math.min((w - n) / board.getWidth(), (h - n) / board.getHeight()); int bw = t * board.getWidth() + n; int bh = t * board.getHeight() + n; int px = (w - bw) / 2; int py = (h - bh) / 2; int rx = (w - bw) % 2; int ry = (h - bh) % 2; drawBoardArea(gc, new Rectangle(x + px, y + 0, w - px * 2 - rx, h - py * 2 - ry), coord); }
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(); } }