Android Open Source - abalone-android Tutorial Board View






From Project

Back to project page abalone-android.

License

The source code is released under:

GNU General Public License

If you think the Android project abalone-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
* Copyright (c) 2010-2011 Yaroslav Geryatovich, Alexander Yakushev
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*//*  ww w  . ja  va2  s.  co  m*/
package com.bytopia.abalone;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.bytopia.abalone.mechanics.Board;
import com.bytopia.abalone.mechanics.Cell;
import com.bytopia.abalone.mechanics.Group;
import com.bytopia.abalone.mechanics.Move;
import com.bytopia.abalone.mechanics.MoveType;

public class TutorialBoardView extends View {

  int size = 0;
  public Activity parent;

  public void setParent(Activity parent) {
    this.parent = parent;
  }

  private Board board;

  private BoardRenderer renderer;

  public TutorialBoardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TutorialBoardView(Context context) {
    super(context);
    init();
  }
  
  public TutorialBoardView(Context context, Board initialBoard) {
    super(context);
    board = initialBoard;
    init();
  }

  private void init() {
    setFocusable(false);
    renderer = new BoardRenderer(this);
    renderer.rescale(getWidth(), getWidth());
  }

  private int measure(int measureSpec) {
    int result = 0;

    // Decode the measurement specifications.
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.UNSPECIFIED) {
      // Return a default size of 200 if no bounds are specified.
      result = 200;
    } else {
      // As you want to fill the available space
      // always return the full available bounds.
      result = specSize;
    }
    Log.d("screen", "result=" + result);
    return result;
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int measuredWidth = measure(widthMeasureSpec);
    int measuredHeight = measure(heightMeasureSpec);
    int borderSize = renderer.getBorderSize();
    float PROP = BoardRenderer.PROP;

    if ((measuredWidth - 2 * borderSize) * PROP + 2 * borderSize < measuredHeight) {
      measuredHeight = (int) (((measuredWidth - 2 * borderSize) * PROP) + 2 * borderSize);

    } else {
      measuredWidth = (int) (((measuredHeight - 2 * borderSize) / PROP) + 2 * borderSize);
    }
    Log.d("screen", measuredWidth + "x" + measuredHeight);
    setMeasuredDimension(measuredWidth, measuredHeight);
    size = measuredWidth;
    renderer.rescale(getWidth(), size);
    renderer.updateBoard();
  }

  @Override
  protected void onDraw(Canvas canvas) {
    renderer.renderBoard(canvas);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    measure(MeasureSpec.AT_MOST);
    renderer.rescale(w, size);
    updateView();
    Log.d("screen", "screen changed " + h + " " + w);
  }
  
  public void setBoard(Board b) {
    board = b;
    updateView();
  }
  
  public void setSelected(Group g) {
    renderer.setSelected(g);
  }
  
  public void setHighlighted(Group g) {
    renderer.setHighlighted(g);
  }
  
  public void updateView() {
    if (board != null) {
      renderer.updateBoard(board);
    }
  }
  
  public void makeMove(Move move) {
    MoveType moveType = board.getMoveType(move);
    doAnimation(moveType, move.getDirection());
    board.makeMove(move);
    updateView();
  }

  public void doAnimation(MoveType moveType, byte direction) {
    renderer.animate(moveType, direction);
  }
  
}




Java Source Code List

com.bytopia.abalone.BoardRenderer.java
com.bytopia.abalone.BoardView.java
com.bytopia.abalone.GameActivity.java
com.bytopia.abalone.GameOptionsActivity.java
com.bytopia.abalone.LoseBallsView.java
com.bytopia.abalone.MainMenuActivity.java
com.bytopia.abalone.Options.java
com.bytopia.abalone.Scenario.java
com.bytopia.abalone.SelectLayoutActivity.java
com.bytopia.abalone.SplashAcitvity.java
com.bytopia.abalone.TutorialActivity.java
com.bytopia.abalone.TutorialBoardView.java
com.bytopia.abalone.mechanics.AiAnn.java
com.bytopia.abalone.mechanics.AiBeatrice.java
com.bytopia.abalone.mechanics.AiCharlotte.java
com.bytopia.abalone.mechanics.AiDeborah.java
com.bytopia.abalone.mechanics.ArtificialIntilligence.java
com.bytopia.abalone.mechanics.BelgianLayout.java
com.bytopia.abalone.mechanics.Board.java
com.bytopia.abalone.mechanics.Cell.java
com.bytopia.abalone.mechanics.ClassicLayout.java
com.bytopia.abalone.mechanics.ConsoleWatcher.java
com.bytopia.abalone.mechanics.Debug.java
com.bytopia.abalone.mechanics.Direction.java
com.bytopia.abalone.mechanics.EmptyLayout.java
com.bytopia.abalone.mechanics.Game.java
com.bytopia.abalone.mechanics.GermanLayout.java
com.bytopia.abalone.mechanics.Group.java
com.bytopia.abalone.mechanics.Layout.java
com.bytopia.abalone.mechanics.MoveType.java
com.bytopia.abalone.mechanics.Move.java
com.bytopia.abalone.mechanics.Player.java
com.bytopia.abalone.mechanics.Side.java
com.bytopia.abalone.mechanics.TestLayout.java
com.bytopia.abalone.mechanics.Watcher.java