Created
August 14, 2012 19:58
-
-
Save Oshuma/3352280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.nerdno.rpc.view; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.util.AttributeSet; | |
import android.util.Log; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.GridView; | |
import de.nerdno.rpc.model.Tile; | |
public final class Chessboard extends View { | |
private static final String TAG = Chessboard.class.getSimpleName(); | |
private static final int COLS = 8; | |
private static final int ROWS = 8; | |
private final Tile[][] mTiles; | |
private int x0 = 0; | |
private int y0 = 0; | |
private int squareSize = 0; | |
/** 'true' if black is facing player. */ | |
private boolean flipped = false; | |
public Chessboard(final Context context, final AttributeSet attrs) { | |
super(context, attrs); | |
this.mTiles = new Tile[COLS][ROWS]; | |
setFocusable(true); | |
buildTiles(); | |
} | |
private void buildTiles() { | |
for (int c = 0; c < COLS; c++) { | |
for (int r = 0; r < ROWS; r++) { | |
mTiles[c][r] = new Tile(c, r); | |
} | |
} | |
} | |
@Override | |
protected void onDraw(final Canvas canvas) { | |
final int width = getWidth(); | |
final int height = getHeight(); | |
this.squareSize = Math.min( | |
getSquareSizeWidth(width), | |
getSquareSizeHeight(height) | |
); | |
computeOrigins(width, height); | |
for (int c = 0; c < COLS; c++) { | |
for (int r = 0; r < ROWS; r++) { | |
final int xCoord = getXCoord(c); | |
final int yCoord = getYCoord(r); | |
final Rect tileRect = new Rect( | |
xCoord, // left | |
yCoord, // top | |
xCoord + squareSize, // right | |
yCoord + squareSize // bottom | |
); | |
mTiles[c][r].setTileRect(tileRect); | |
mTiles[c][r].draw(canvas); | |
} | |
} | |
} | |
@Override | |
public boolean onTouchEvent(final MotionEvent event) { | |
final int x = (int) event.getX(); | |
final int y = (int) event.getY(); | |
Tile tile; | |
for (int c = 0; c < COLS; c++) { | |
for (int r = 0; r < ROWS; r++) { | |
tile = mTiles[c][r]; | |
if (tile.isTouched(x, y)) | |
tile.handleTouch(); | |
} | |
} | |
return true; | |
} | |
private int getSquareSizeWidth(final int width) { | |
return width / 8; | |
} | |
private int getSquareSizeHeight(final int height) { | |
return height / 8; | |
} | |
private int getXCoord(final int x) { | |
return x0 + squareSize * (flipped ? 7 - x : x); | |
} | |
private int getYCoord(final int y) { | |
return y0 + squareSize * (flipped ? y : 7 - y); | |
} | |
private void computeOrigins(final int width, final int height) { | |
this.x0 = (width - squareSize * 8) / 2; | |
this.y0 = (height - squareSize * 8) / 2; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.nerdno.rpc.model; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.util.Log; | |
public final class Tile { | |
private static final String TAG = Tile.class.getSimpleName(); | |
private final int col; | |
private final int row; | |
private final Paint squareColor; | |
private Rect tileRect; | |
public Tile(final int col, final int row) { | |
this.col = col; | |
this.row = row; | |
this.squareColor = new Paint(); | |
squareColor.setColor(isDark() ? Color.BLACK : Color.WHITE); | |
} | |
public void draw(final Canvas canvas) { | |
canvas.drawRect(tileRect, squareColor); | |
} | |
public String getColumnString() { | |
switch (col) { | |
case 0: return "A"; | |
case 1: return "B"; | |
case 2: return "C"; | |
case 3: return "D"; | |
case 4: return "E"; | |
case 5: return "F"; | |
case 6: return "G"; | |
case 7: return "H"; | |
default: return null; | |
} | |
} | |
public String getRowString() { | |
// To get the actual row, add 1 since 'row' is 0 indexed. | |
return String.valueOf(row + 1); | |
} | |
public void handleTouch() { | |
Log.d(TAG, "handleTouch(): col: " + col); | |
Log.d(TAG, "handleTouch(): row: " + row); | |
} | |
public boolean isDark() { | |
return (col + row) % 2 == 0; | |
} | |
public boolean isTouched(final int x, final int y) { | |
return tileRect.contains(x, y); | |
} | |
public void setTileRect(final Rect tileRect) { | |
this.tileRect = tileRect; | |
} | |
public String toString() { | |
final String column = getColumnString(); | |
final String row = getRowString(); | |
return "<Tile " + column + row + ">"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code is very clean and readable. Good job!