DroidFish: Better layout for edit board in landscape mode.

This commit is contained in:
Peter Osterlund
2012-09-22 11:27:12 +00:00
parent 0456c02cf9
commit 87e38eca80
4 changed files with 132 additions and 63 deletions

View File

@@ -34,14 +34,20 @@
<Button <Button
android:text="@android:string/ok" android:text="@android:string/ok"
android:id="@+id/eb_ok" android:id="@+id/eb_ok"
android:layout_width="fill_parent" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"> android:layout_weight="1">
</Button> </Button>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/LinearLayout03"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button <Button
android:text="@string/cancel" android:text="@string/cancel"
android:id="@+id/eb_cancel" android:id="@+id/eb_cancel"
android:layout_width="fill_parent" android:layout_width="0dip"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"> android:layout_weight="1">
</Button> </Button>

View File

@@ -354,6 +354,7 @@ public abstract class ChessBoard extends View {
protected abstract int getSqSizeW(int width); protected abstract int getSqSizeW(int width);
protected abstract int getSqSizeH(int height); protected abstract int getSqSizeH(int height);
protected abstract int getMaxHeightPercentage(); protected abstract int getMaxHeightPercentage();
protected abstract int getMaxWidthPercentage();
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@@ -369,7 +370,8 @@ public abstract class ChessBoard extends View {
int p = getMaxHeightPercentage(); int p = getMaxHeightPercentage();
height = Math.min(getHeight(sqSize), height * p / 100); height = Math.min(getHeight(sqSize), height * p / 100);
} else { } else {
width = Math.min(getWidth(sqSize), width * 65 / 100); int p = getMaxWidthPercentage();
width = Math.min(getWidth(sqSize), width * p / 100);
} }
setMeasuredDimension(width, height); setMeasuredDimension(width, height);
} }
@@ -490,8 +492,7 @@ public abstract class ChessBoard extends View {
} }
} }
protected void drawExtraSquares(Canvas canvas) { abstract protected void drawExtraSquares(Canvas canvas);
}
protected final void drawPiece(Canvas canvas, int xCrd, int yCrd, int p) { protected final void drawPiece(Canvas canvas, int xCrd, int yCrd, int p) {
String psb, psw; String psb, psw;
@@ -601,6 +602,7 @@ public abstract class ChessBoard extends View {
} }
protected abstract int minValidY(); protected abstract int minValidY();
protected abstract int maxValidX();
protected abstract int getSquare(int x, int y); protected abstract int getSquare(int x, int y);
public final Move handleTrackballEvent(MotionEvent event) { public final Move handleTrackballEvent(MotionEvent event) {
@@ -622,7 +624,7 @@ public abstract class ChessBoard extends View {
cursorX += c * event.getX(); cursorX += c * event.getX();
cursorY -= c * event.getY(); cursorY -= c * event.getY();
if (cursorX < 0) cursorX = 0; if (cursorX < 0) cursorX = 0;
if (cursorX > 7) cursorX = 7; if (cursorX > maxValidX()) cursorX = maxValidX();
if (cursorY < minValidY()) cursorY = minValidY(); if (cursorY < minValidY()) cursorY = minValidY();
if (cursorY > 7) cursorY = 7; if (cursorY > 7) cursorY = 7;
invalidate(); invalidate();

View File

@@ -29,13 +29,11 @@ import org.petero.droidfish.gamelogic.TextIO;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.graphics.Canvas;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.widget.Toast; import android.widget.Toast;
/** /** Chess board widget suitable for play mode. */
* Chess board widget suitable for play mode.
* @author petero
*/
public class ChessBoardPlay extends ChessBoard { public class ChessBoardPlay extends ChessBoard {
private PGNOptions pgnOptions = null; private PGNOptions pgnOptions = null;
boolean oneTouchMoves; boolean oneTouchMoves;
@@ -68,6 +66,8 @@ public class ChessBoardPlay extends ChessBoard {
protected int getSqSizeH(int height) { return (height) / 8; } protected int getSqSizeH(int height) { return (height) / 8; }
@Override @Override
protected int getMaxHeightPercentage() { return 75; } protected int getMaxHeightPercentage() { return 75; }
@Override
protected int getMaxWidthPercentage() { return 65; }
@Override @Override
protected void computeOrigin(int width, int height) { protected void computeOrigin(int width, int height) {
@@ -84,8 +84,13 @@ public class ChessBoardPlay extends ChessBoard {
@Override @Override
protected int minValidY() { return 0; } protected int minValidY() { return 0; }
@Override @Override
protected int maxValidX() { return 7; }
@Override
protected int getSquare(int x, int y) { return Position.getSquare(x, y); } protected int getSquare(int x, int y) { return Position.getSquare(x, y); }
@Override
protected void drawExtraSquares(Canvas canvas) {
}
private final boolean myColor(int piece) { private final boolean myColor(int piece) {
return (piece != Piece.EMPTY) && (Piece.isWhite(piece) == pos.whiteMove); return (piece != Piece.EMPTY) && (Piece.isWhite(piece) == pos.whiteMove);

View File

@@ -30,39 +30,69 @@ import android.graphics.Paint;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
/** /** Chess board widget suitable for edit mode. */
* Chess board widget suitable for edit mode.
* @author petero
*/
public class ChessBoardEdit extends ChessBoard { public class ChessBoardEdit extends ChessBoard {
private final boolean landScape;
public ChessBoardEdit(Context context, AttributeSet attrs) { public ChessBoardEdit(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
drawSquareLabels = true; drawSquareLabels = true;
Configuration config = getResources().getConfiguration();
landScape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE);
} }
private final static int gap = 4; private final static int gap = 4;
@Override @Override
protected int getWidth(int sqSize) { return sqSize * 8; } protected int getWidth(int sqSize) {
return landScape ? sqSize * 10 + gap : sqSize * 8;
}
@Override @Override
protected int getHeight(int sqSize) { return sqSize * 10 + gap; } protected int getHeight(int sqSize) {
return landScape ? sqSize * 8 : sqSize * 10 + gap;
}
@Override @Override
protected int getSqSizeW(int width) { return (width) / 8; } protected int getSqSizeW(int width) {
return landScape ? (width - gap) / 10 : width / 8;
}
@Override @Override
protected int getSqSizeH(int height) { return (height - gap) / 10; } protected int getSqSizeH(int height) {
return landScape ? height / 8 : (height - gap) / 10;
}
@Override @Override
protected int getMaxHeightPercentage() { return 85; } protected int getMaxHeightPercentage() { return 85; }
@Override
protected int getMaxWidthPercentage() { return 75; }
@Override @Override
protected void computeOrigin(int width, int height) { protected void computeOrigin(int width, int height) {
x0 = (width - sqSize * 8) / 2; x0 = (width - getWidth(sqSize)) / 2;
Configuration config = getResources().getConfiguration(); y0 = landScape ? 0 : (height - getHeight(sqSize)) / 2;
boolean landScape = (config.orientation == Configuration.ORIENTATION_LANDSCAPE);
y0 = landScape ? 0 : (height - (sqSize * 10 + gap)) / 2;
} }
private final int extraPieces(int x, int y) { private final int extraPieces(int x, int y) {
if (y == -1) { // White pieces if (landScape) {
if (x == 8) {
switch (y) {
case 0: return Piece.WKING;
case 1: return Piece.WQUEEN;
case 2: return Piece.WROOK;
case 3: return Piece.WBISHOP;
case 4: return Piece.WKNIGHT;
case 5: return Piece.WPAWN;
}
} else if (x == 9) {
switch (y) {
case 0: return Piece.BKING;
case 1: return Piece.BQUEEN;
case 2: return Piece.BROOK;
case 3: return Piece.BBISHOP;
case 4: return Piece.BKNIGHT;
case 5: return Piece.BPAWN;
}
}
} else {
if (y == -1) {
switch (x) { switch (x) {
case 0: return Piece.WKING; case 0: return Piece.WKING;
case 1: return Piece.WQUEEN; case 1: return Piece.WQUEEN;
@@ -81,6 +111,7 @@ public class ChessBoardEdit extends ChessBoard {
case 5: return Piece.BPAWN; case 5: return Piece.BPAWN;
} }
} }
}
return Piece.EMPTY; return Piece.EMPTY;
} }
@@ -90,6 +121,9 @@ public class ChessBoardEdit extends ChessBoard {
return Position.getX(sq); return Position.getX(sq);
} else { } else {
int p = -2 - sq; int p = -2 - sq;
if (landScape) {
return Piece.isWhite(p) ? 8 : 9;
} else {
switch (p) { switch (p) {
case Piece.WKING: case Piece.BKING: return 0; case Piece.WKING: case Piece.BKING: return 0;
case Piece.WQUEEN: case Piece.BQUEEN: return 1; case Piece.WQUEEN: case Piece.BQUEEN: return 1;
@@ -101,6 +135,7 @@ public class ChessBoardEdit extends ChessBoard {
} }
} }
} }
}
@Override @Override
protected int getYFromSq(int sq) { protected int getYFromSq(int sq) {
@@ -108,13 +143,25 @@ public class ChessBoardEdit extends ChessBoard {
return Position.getY(sq); return Position.getY(sq);
} else { } else {
int p = -2 - sq; int p = -2 - sq;
if (landScape) {
switch (p) {
case Piece.WKING: case Piece.BKING: return 0;
case Piece.WQUEEN: case Piece.BQUEEN: return 1;
case Piece.WROOK: case Piece.BROOK: return 2;
case Piece.WBISHOP: case Piece.BBISHOP: return 3;
case Piece.WKNIGHT: case Piece.BKNIGHT: return 4;
case Piece.WPAWN: case Piece.BPAWN: return 5;
default: return 6;
}
} else {
return Piece.isWhite(p) ? -1 : -2; return Piece.isWhite(p) ? -1 : -2;
} }
} }
}
@Override @Override
protected int getSquare(int x, int y) { protected int getSquare(int x, int y) {
if (y >= 0) { if ((y >= 0) && (x < 8)) {
return Position.getSquare(x, y); return Position.getSquare(x, y);
} else { } else {
int p = extraPieces(x, y); int p = extraPieces(x, y);
@@ -124,8 +171,12 @@ public class ChessBoardEdit extends ChessBoard {
@Override @Override
protected void drawExtraSquares(Canvas canvas) { protected void drawExtraSquares(Canvas canvas) {
for (int x = 0; x < 8; x++) { int xMin = landScape ? 8 : 0;
for (int y = -2; y < 0; y++) { int xMax = landScape ? 10 : 8;
int yMin = landScape ? 0 : -2;
int yMax = landScape ? 8 : 0;
for (int x = xMin; x < xMax; x++) {
for (int y = yMin; y < yMax; y++) {
final int xCrd = getXCrd(x); final int xCrd = getXCrd(x);
final int yCrd = getYCrd(y); final int yCrd = getYCrd(y);
Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint; Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint;
@@ -156,33 +207,37 @@ public class ChessBoardEdit extends ChessBoard {
} }
@Override @Override
protected int minValidY() { return -2; } protected int minValidY() {
return landScape ? 0 : -2;
}
@Override
protected int maxValidX() {
return landScape ? 9 : 7;
}
@Override @Override
protected int getXCrd(int x) { protected int getXCrd(int x) {
return x0 + sqSize * (flipped ? 7 - x : x); return x0 + sqSize * x + ((x >= 8) ? gap : 0);
} }
@Override @Override
protected int getYCrd(int y) { protected int getYCrd(int y) {
if (y >= 0) { return y0 + sqSize * (7 - y) + ((y < 0) ? gap : 0);
return y0 + sqSize * (flipped ? y : 7 - y);
} else {
return y0 + gap + sqSize * (7 - y);
}
} }
@Override @Override
protected int getXSq(int xCrd) { protected int getXSq(int xCrd) {
int t = (xCrd - x0) / sqSize; return flipped ? 7 - t : t; int x = (xCrd - x0) / sqSize;
if (x < 8)
return x;
return (xCrd - x0 - gap) / sqSize;
} }
@Override @Override
protected int getYSq(int yCrd) { protected int getYSq(int yCrd) {
int t = (yCrd - y0) / sqSize; int y = 7 - (yCrd - y0) / sqSize;
t = flipped ? t : 7 - t; if (y >= 0)
if ((t >= 0) && (t < 8)) return y;
return t;
return 7 - (yCrd - y0 - gap) / sqSize; return 7 - (yCrd - y0 - gap) / sqSize;
} }
@@ -203,7 +258,8 @@ public class ChessBoardEdit extends ChessBoard {
if (sqSize > 0) { if (sqSize > 0) {
int x = getXSq(xCrd); int x = getXSq(xCrd);
int y = getYSq(yCrd); int y = getYSq(yCrd);
if ((x >= 0) && (x < 8) && (y >= -2) && (y < 0)) { if ( landScape && (x >= 0) && (x < 10) && (y >= 0) && (y < 8) ||
!landScape && (x >= 0) && (x < 8) && (y >= -2) && (y < 0)) {
int p = extraPieces(x, y); int p = extraPieces(x, y);
sq = -p - 2; sq = -p - 2;
} }