BoardCellContainerView.java :  » Game » tmfshogi » at » fanninger » android » game » shogi » lib » Android Open Source

Android Open Source » Game » tmfshogi 
tmfshogi » at » fanninger » android » game » shogi » lib » BoardCellContainerView.java
package at.fanninger.android.game.shogi.lib;

import android.content.Context;
import android.view.ViewGroup;

public class BoardCellContainerView extends ViewGroup {
  
  private BoardCellView[][] boardCells;
  
  public BoardCellContainerView(Context context, int cellSize, int columns, int rows) {
    super(context);
    
    this.boardCells = new BoardCellView[columns][rows];
    for(int x=0; x<columns; x++){
      for(int y=0; y<rows; y++){
        this.boardCells[x][y] = new BoardCellView(this.getContext());
        this.boardCells[x][y].setCellSize( cellSize );
      }
    }
    
    this.setViewSize(cellSize, columns, rows);
  }
  
  
  public void setViewSize(int cellSize, int columns, int rows){
    int measuredWidth, measuredHeight;
    
    measuredWidth = cellSize * columns;
    measuredHeight = cellSize * rows;
    
    this.setMeasuredDimension( measuredWidth, measuredHeight);
  }
  
  private void positionItems(){
    int width = 0, height = 0, left = 0, top = 0;
    
    for(int x=0; x<this.boardCells.length; x++){
      left= 0;
      for(int y=0; y<this.boardCells[x].length; y++){
        width = this.boardCells[x][y].getMeasuredWidth();
              height = this.boardCells[x][y].getMeasuredHeight();
              
              this.boardCells[x][y].layout(left, top, left + width, top + height);
        
        left += width;
      }
      top += height;
    }
  }
  
  public void setCellSize(int cellSize){
    for(int x=0; x<this.boardCells.length; x++){
      for(int y=0; y<this.boardCells[x].length; y++){
        this.boardCells[x][y].setCellSize( cellSize );
      }
    }
    this.invalidate();
  }
  
  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if(changed){
      int count = 0;
      
      this.removeAllViews();
      
      for(int x=0; x<this.boardCells.length; x++){
        for(int y=0; y<this.boardCells[x].length; y++){
          this.boardCells[x][y].setId( count );
          this.addView( this.boardCells[x][y], count );
          count++;
        }
      }
    }
    
    this.positionItems();
    this.invalidate();
  }
  
  public void getCellForPosition(int xValue, int yValue){
    
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.