DroidFish: In edit board mode, use the same orientation as in game play.

This commit is contained in:
Peter Osterlund 2016-12-17 11:23:51 +01:00
parent e030f68f7e
commit ae5f947b13
4 changed files with 78 additions and 50 deletions

View File

@ -202,10 +202,12 @@ public abstract class ChessBoard extends View {
private void drawAnimPiece(Canvas canvas, int piece, int from, int to, double animState) { private void drawAnimPiece(Canvas canvas, int piece, int from, int to, double animState) {
if (piece == Piece.EMPTY) if (piece == Piece.EMPTY)
return; return;
final int xCrd1 = getXCrd(Position.getX(from)); XYCoord crd1 = sqToPix(Position.getX(from), Position.getY(from));
final int yCrd1 = getYCrd(Position.getY(from)); final int xCrd1 = crd1.x;
final int xCrd2 = getXCrd(Position.getX(to)); final int yCrd1 = crd1.y;
final int yCrd2 = getYCrd(Position.getY(to)); XYCoord crd2 = sqToPix(Position.getX(to), Position.getY(to));
final int xCrd2 = crd2.x;
final int yCrd2 = crd2.y;
final int xCrd = xCrd1 + (int)Math.round((xCrd2 - xCrd1) * animState); final int xCrd = xCrd1 + (int)Math.round((xCrd2 - xCrd1) * animState);
final int yCrd = yCrd1 + (int)Math.round((yCrd2 - yCrd1) * animState); final int yCrd = yCrd1 + (int)Math.round((yCrd2 - yCrd1) * animState);
drawPiece(canvas, xCrd, yCrd, piece); drawPiece(canvas, xCrd, yCrd, piece);
@ -323,7 +325,7 @@ public abstract class ChessBoard extends View {
} }
} }
/** Set/clear the board flipped status. */ /** Set/clear the drawSquareLabels status. */
final public void setDrawSquareLabels(boolean drawSquareLabels) { final public void setDrawSquareLabels(boolean drawSquareLabels) {
if (this.drawSquareLabels != drawSquareLabels) { if (this.drawSquareLabels != drawSquareLabels) {
this.drawSquareLabels = drawSquareLabels; this.drawSquareLabels = drawSquareLabels;
@ -398,8 +400,9 @@ public abstract class ChessBoard extends View {
computeOrigin(width, height); computeOrigin(width, height);
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
final int xCrd = getXCrd(x); XYCoord crd = sqToPix(x, y);
final int yCrd = getYCrd(y); final int xCrd = crd.x;
final int yCrd = crd.y;
Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint; Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint;
canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint); canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint);
@ -423,15 +426,17 @@ public abstract class ChessBoard extends View {
int selX = getXFromSq(selectedSquare); int selX = getXFromSq(selectedSquare);
int selY = getYFromSq(selectedSquare); int selY = getYFromSq(selectedSquare);
selectedSquarePaint.setStrokeWidth(sqSize/(float)16); selectedSquarePaint.setStrokeWidth(sqSize/(float)16);
int x0 = getXCrd(selX); XYCoord crd = sqToPix(selX, selY);
int y0 = getYCrd(selY); int x0 = crd.x;
int y0 = crd.y;
canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, selectedSquarePaint); canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, selectedSquarePaint);
} }
if (cursorVisible) { if (cursorVisible) {
int x = Math.round(cursorX); int x = Math.round(cursorX);
int y = Math.round(cursorY); int y = Math.round(cursorY);
int x0 = getXCrd(x); XYCoord crd = sqToPix(x, y);
int y0 = getYCrd(y); int x0 = crd.x;
int y0 = crd.y;
cursorSquarePaint.setStrokeWidth(sqSize/(float)16); cursorSquarePaint.setStrokeWidth(sqSize/(float)16);
canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, cursorSquarePaint); canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, cursorSquarePaint);
} }
@ -459,10 +464,12 @@ public abstract class ChessBoard extends View {
Move m = moveHints.get(i); Move m = moveHints.get(i);
if ((m == null) || (m.from == m.to)) if ((m == null) || (m.from == m.to))
continue; continue;
float x0 = getXCrd(Position.getX(m.from)) + h; XYCoord crd0 = sqToPix(Position.getX(m.from), Position.getY(m.from));
float y0 = getYCrd(Position.getY(m.from)) + h; XYCoord crd1 = sqToPix(Position.getX(m.to), Position.getY(m.to));
float x1 = getXCrd(Position.getX(m.to)) + h; float x0 = crd0.x + h;
float y1 = getYCrd(Position.getY(m.to)) + h; float y0 = crd0.y + h;
float x1 = crd1.x + h;
float y1 = crd1.y + h;
float x2 = (float)(Math.hypot(x1 - x0, y1 - y0) + d); float x2 = (float)(Math.hypot(x1 - x0, y1 - y0) + d);
float y2 = 0; float y2 = 0;
@ -562,10 +569,17 @@ public abstract class ChessBoard extends View {
canvas.drawText(s, xCrd, yCrd, labelPaint); canvas.drawText(s, xCrd, yCrd, labelPaint);
} }
protected abstract int getXCrd(int x); protected static class XYCoord {
protected abstract int getYCrd(int y); public int x;
protected abstract int getXSq(int xCrd); public int y;
protected abstract int getYSq(int yCrd); public XYCoord(int x, int y) { this.x = x; this.y = y; }
}
/** Convert square coordinates to pixel coordinates. */
protected abstract XYCoord sqToPix(int x, int y);
/** Convert pixel coordinates to square coordinates. */
protected abstract XYCoord pixToSq(int xCrd, int yCrd);
/** /**
* Compute the square corresponding to the coordinates of a mouse event. * Compute the square corresponding to the coordinates of a mouse event.
@ -578,8 +592,9 @@ public abstract class ChessBoard extends View {
int sq = -1; int sq = -1;
if (sqSize > 0) { if (sqSize > 0) {
int x = getXSq(xCrd); XYCoord xy = pixToSq(xCrd, yCrd);
int y = getYSq(yCrd); int x = xy.x;
int y = xy.y;
if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8)) { if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8)) {
sq = Position.getSquare(x, y); sq = Position.getSquare(x, y);
} }
@ -674,8 +689,9 @@ public abstract class ChessBoard extends View {
if (((1L << sq) & decorated) != 0) if (((1L << sq) & decorated) != 0)
continue; continue;
decorated |= 1L << sq; decorated |= 1L << sq;
int xCrd = getXCrd(Position.getX(sq)); XYCoord crd = sqToPix(Position.getX(sq), Position.getY(sq));
int yCrd = getYCrd(Position.getY(sq)); int xCrd = crd.x;
int yCrd = crd.y;
String s = null; String s = null;
int wdl = sd.tbData.wdl; int wdl = sd.tbData.wdl;

View File

@ -48,13 +48,18 @@ public class ChessBoardPlay extends ChessBoard {
} }
@Override @Override
protected int getXCrd(int x) { return x0 + sqSize * (flipped ? 7 - x : x); } protected XYCoord sqToPix(int x, int y) {
int xPix = x0 + sqSize * (flipped ? 7 - x : x);
int yPix = y0 + sqSize * (flipped ? y : 7 - y);
return new XYCoord(xPix, yPix);
}
@Override @Override
protected int getYCrd(int y) { return y0 + sqSize * (flipped ? y : 7 - y); } protected XYCoord pixToSq(int xCrd, int yCrd) {
@Override int x = (xCrd - x0) / sqSize; if (flipped) x = 7 - x;
protected int getXSq(int xCrd) { int t = (xCrd - x0) / sqSize; return flipped ? 7 - t : t; } int y = (yCrd - y0) / sqSize; if (!flipped) y = 7 - y;
@Override return new XYCoord(x, y);
protected int getYSq(int yCrd) { int t = (yCrd - y0) / sqSize; return flipped ? t : 7 - t; } }
@Override @Override
protected int getWidth(int sqSize) { return sqSize * 8; } protected int getWidth(int sqSize) { return sqSize * 8; }

View File

@ -179,8 +179,9 @@ public class ChessBoardEdit extends ChessBoard {
int yMax = landScape ? 8 : 0; int yMax = landScape ? 8 : 0;
for (int x = xMin; x < xMax; x++) { for (int x = xMin; x < xMax; x++) {
for (int y = yMin; y < yMax; y++) { for (int y = yMin; y < yMax; y++) {
final int xCrd = getXCrd(x); XYCoord crd = sqToPix(x, y);
final int yCrd = getYCrd(y); final int xCrd = crd.x;
final int yCrd = crd.y;
Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint; Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint;
canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint); canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint);
int p = extraPieces(x, y); int p = extraPieces(x, y);
@ -218,29 +219,31 @@ public class ChessBoardEdit extends ChessBoard {
} }
@Override @Override
protected int getXCrd(int x) { protected XYCoord sqToPix(int x, int y) {
return x0 + sqSize * x + ((x >= 8) ? getGap(sqSize) : 0); if (flipped && (x >= 0) && (x < 8) && (y >= 0) && (y < 8)) {
x = 7 - x;
y = 7 - y;
}
int xPix = x0 + sqSize * x + ((x >= 8) ? getGap(sqSize) : 0);
int yPix = y0 + sqSize * (7 - y) + ((y < 0) ? getGap(sqSize) : 0);
return new XYCoord(xPix, yPix);
} }
@Override @Override
protected int getYCrd(int y) { protected XYCoord pixToSq(int xCrd, int yCrd) {
return y0 + sqSize * (7 - y) + ((y < 0) ? getGap(sqSize) : 0);
}
@Override
protected int getXSq(int xCrd) {
int x = (xCrd - x0) / sqSize; int x = (xCrd - x0) / sqSize;
if (x < 8) if (x >= 8)
return x; x = (xCrd - x0 - getGap(sqSize)) / sqSize;
return (xCrd - x0 - getGap(sqSize)) / sqSize;
}
@Override
protected int getYSq(int yCrd) {
int y = 7 - (yCrd - y0) / sqSize; int y = 7 - (yCrd - y0) / sqSize;
if (y >= 0) if (y < 0)
return y; y = 7 - (yCrd - y0 - getGap(sqSize)) / sqSize;
return 7 - (yCrd - y0 - getGap(sqSize)) / sqSize;
if (flipped && (x >= 0) && (x < 8) && (y >= 0) && (y < 8)) {
x = 7 - x;
y = 7 - y;
}
return new XYCoord(x, y);
} }
/** /**
@ -258,8 +261,9 @@ public class ChessBoardEdit extends ChessBoard {
int yCrd = (int)(evt.getY()); int yCrd = (int)(evt.getY());
if (sqSize > 0) { if (sqSize > 0) {
int x = getXSq(xCrd); XYCoord xy = pixToSq(xCrd, yCrd);
int y = getYSq(yCrd); int x = xy.x;
int y = xy.y;
if ( landScape && (x >= 0) && (x < 10) && (y >= 0) && (y < 8) || if ( landScape && (x >= 0) && (x < 10) && (y >= 0) && (y < 8) ||
!landScape && (x >= 0) && (x < 8) && (y >= -2) && (y < 0)) { !landScape && (x >= 0) && (x < 8) && (y >= -2) && (y < 0)) {
int p = extraPieces(x, y); int p = extraPieces(x, y);

View File

@ -85,6 +85,7 @@ public class EditBoard extends Activity {
private boolean egtbHints; private boolean egtbHints;
private boolean autoScrollTitle; private boolean autoScrollTitle;
private boolean boardFlipped;
private TextView whiteFigText; private TextView whiteFigText;
private TextView blackFigText; private TextView blackFigText;
private Typeface figNotation; private Typeface figNotation;
@ -102,6 +103,7 @@ public class EditBoard extends Activity {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
egtbHints = settings.getBoolean("tbHintsEdit", false); egtbHints = settings.getBoolean("tbHintsEdit", false);
autoScrollTitle = settings.getBoolean("autoScrollTitle", true); autoScrollTitle = settings.getBoolean("autoScrollTitle", true);
boardFlipped = settings.getBoolean("boardFlipped", false);
initUI(); initUI();
@ -142,6 +144,7 @@ public class EditBoard extends Activity {
View firstTitleLine = findViewById(R.id.first_title_line); View firstTitleLine = findViewById(R.id.first_title_line);
View secondTitleLine = findViewById(R.id.second_title_line); View secondTitleLine = findViewById(R.id.second_title_line);
cb = (ChessBoardEdit)findViewById(R.id.eb_chessboard); cb = (ChessBoardEdit)findViewById(R.id.eb_chessboard);
cb.setFlipped(boardFlipped);
status = (TextView)findViewById(R.id.eb_status); status = (TextView)findViewById(R.id.eb_status);
okButton = (Button)findViewById(R.id.eb_ok); okButton = (Button)findViewById(R.id.eb_ok);
cancelButton = (Button)findViewById(R.id.eb_cancel); cancelButton = (Button)findViewById(R.id.eb_cancel);