示例#1
0
 protected void initBoard() {
   numericBoardView = new TableLayout(this);
   imageBoardView = new TableLayout(this);
   final Board board = getBoardModel();
   board.addMoveListener(this);
   final int boardSize = getBoardSize();
   numericCells = new TextView[boardSize][boardSize];
   imageCells = new ImageView[boardSize][boardSize];
   for (int i = 0; i < boardSize; i++) {
     TableRow numericRow = new TableRow(this);
     TableRow imageRow = new TableRow(this);
     for (int j = 0; j < boardSize; j++) {
       TextView numericCell = new TextView(this);
       ImageView imageCell = new ImageView(this);
       numericCells[i][j] = numericCell;
       imageCells[i][j] = imageCell;
       initNumericCell(numericCell);
       initImageCell(imageCell);
       numericRow.addView(numericCell);
       imageRow.addView(imageCell);
     }
     numericBoardView.addView(numericRow);
     imageBoardView.addView(imageRow);
   }
   countDownCell = new TextView(this);
   initNumericCell(countDownCell);
 }
示例#2
0
 /** Handles user's clicks and taps on the board. */
 public void onClick(View v) {
   final Object tag = v.getTag();
   if (null == previewTimer && tag instanceof Tile) {
     final Board board = getBoardModel();
     final Move move = board.permittedMoveFor((Tile) tag);
     if (null != move) {
       board.move(move);
       if (game.isSolved()) congratulate();
     } else {
       wrongClick();
     }
   }
 }
示例#3
0
 protected void dimImmovableTiles(boolean on) {
   final Board board = getBoardModel();
   final int boardSize = board.getSize();
   final int color = getResources().getColor(R.color.tile_dimmer);
   for (int r = 0; r < boardSize; r++)
     for (int c = 0; c < boardSize; c++) {
       final ImageView cell = imageCells[r][c];
       final Object tag = cell.getTag();
       if (tag instanceof Tile
           && null == board.permittedMoveFor((Tile) tag)
           && 0 != ((Tile) tag).getNumber()) {
         if (on) cell.setColorFilter(color);
         else cell.clearColorFilter();
       }
     }
 }
示例#4
0
  protected void resizeContent(final int screenWidth, final int screenHeight) {
    if (null == game)
      throw new IllegalStateException(this + " must be initialized with onCreate()");
    final float imageRatio = game.isImageSelected() ? game.getImageAspectRatio() : 1f;
    final int boardSize = getBoardSize();
    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    // border width 1 dp rounded up to nearest whole pixels
    final int borderWidth = (int) Math.ceil(metrics.density);
    // calculate spacing allotment
    final int spacing = borderWidth * 2 * boardSize;
    if (screenWidth < spacing + boardSize || screenHeight < spacing + boardSize)
      throw new UnsupportedOperationException(
          "Screen size ("
              + screenWidth
              + " x "
              + screenHeight
              + ") too small for a board of "
              + boardSize
              + " rows");
    final float adjustedScreenRatio = (float) (screenWidth - spacing) / (screenHeight - spacing);

    int width, height;
    if (adjustedScreenRatio > imageRatio) {
      // scale to screen height
      height = screenHeight;
      // fix width = imageRatio * height
      width = (int) (imageRatio * height);
      if (width < spacing + boardSize)
        throw new UnsupportedOperationException(
            "Need a wider image to make a board: scaled to "
                + width
                + " pixels, need "
                + (spacing + boardSize));
    } else {
      // scale to screen width
      width = screenWidth;
      // fix height = width / imageRatio
      height = (int) (width / imageRatio);
      if (height < spacing + boardSize)
        throw new UnsupportedOperationException(
            "Need a taller image to make a board: scaled to "
                + height
                + " pixels, need "
                + (spacing + boardSize));
    }

    // make the dimensions divisible by row/column count
    height -= height % boardSize;
    width -= width % boardSize;
    RelativeLayout.LayoutParams boardLayoutParams = new RelativeLayout.LayoutParams(width, height);
    boardLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    numericBoardView.setLayoutParams(boardLayoutParams);
    imageBoardView.setLayoutParams(boardLayoutParams);

    // load and resize the image
    width = width / boardSize - 2 * borderWidth;
    height = height / boardSize - 2 * borderWidth;
    game.setTileSize(width, height);
    if (game.isImageSelected())
      try {
        game.loadImage(this);
      } catch (ImageProcessingException failure) {
        game.resetSelectedImage();
        Log.e(getClass().getName(), failure.getMessage(), failure);
        hideBoard();
        alert(R.string.image_load_error);
        return;
      }

    // size and fill cell views
    TableRow.LayoutParams cellParams = new TableRow.LayoutParams(width, height);
    cellParams.setMargins(borderWidth, borderWidth, borderWidth, borderWidth);
    float fontSize = width * 4f / 3;
    if (fontSize > height) fontSize = height;
    fontSize *= .5f;
    final Board board = getBoardModel();
    for (int i = 0; i < boardSize; i++) {
      for (int j = 0; j < boardSize; j++) {
        TextView numericCell = numericCells[i][j];
        numericCell.setLayoutParams(cellParams);
        numericCell.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
        ImageView imageCell = imageCells[i][j];
        imageCell.setLayoutParams(cellParams);
        Tile tile = board.getTileAt(i, j);
        assignTile(numericCell, tile);
        assignTile(imageCell, tile);
      }
    }
    countDownCell.setLayoutParams(cellParams);
    countDownCell.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize);
  }