diff --git a/DroidFish/src/org/petero/droidfish/ChessBoard.java b/DroidFish/src/org/petero/droidfish/ChessBoard.java index db8491d..1e20b72 100644 --- a/DroidFish/src/org/petero/droidfish/ChessBoard.java +++ b/DroidFish/src/org/petero/droidfish/ChessBoard.java @@ -202,10 +202,12 @@ public abstract class ChessBoard extends View { private void drawAnimPiece(Canvas canvas, int piece, int from, int to, double animState) { if (piece == Piece.EMPTY) return; - final int xCrd1 = getXCrd(Position.getX(from)); - final int yCrd1 = getYCrd(Position.getY(from)); - final int xCrd2 = getXCrd(Position.getX(to)); - final int yCrd2 = getYCrd(Position.getY(to)); + XYCoord crd1 = sqToPix(Position.getX(from), Position.getY(from)); + final int xCrd1 = crd1.x; + final int yCrd1 = crd1.y; + 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 yCrd = yCrd1 + (int)Math.round((yCrd2 - yCrd1) * animState); 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) { if (this.drawSquareLabels != drawSquareLabels) { this.drawSquareLabels = drawSquareLabels; @@ -398,8 +400,9 @@ public abstract class ChessBoard extends View { computeOrigin(width, height); for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { - final int xCrd = getXCrd(x); - final int yCrd = getYCrd(y); + XYCoord crd = sqToPix(x, y); + final int xCrd = crd.x; + final int yCrd = crd.y; Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint; canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint); @@ -423,15 +426,17 @@ public abstract class ChessBoard extends View { int selX = getXFromSq(selectedSquare); int selY = getYFromSq(selectedSquare); selectedSquarePaint.setStrokeWidth(sqSize/(float)16); - int x0 = getXCrd(selX); - int y0 = getYCrd(selY); + XYCoord crd = sqToPix(selX, selY); + int x0 = crd.x; + int y0 = crd.y; canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, selectedSquarePaint); } if (cursorVisible) { int x = Math.round(cursorX); int y = Math.round(cursorY); - int x0 = getXCrd(x); - int y0 = getYCrd(y); + XYCoord crd = sqToPix(x, y); + int x0 = crd.x; + int y0 = crd.y; cursorSquarePaint.setStrokeWidth(sqSize/(float)16); canvas.drawRect(x0, y0, x0 + sqSize, y0 + sqSize, cursorSquarePaint); } @@ -459,10 +464,12 @@ public abstract class ChessBoard extends View { Move m = moveHints.get(i); if ((m == null) || (m.from == m.to)) continue; - float x0 = getXCrd(Position.getX(m.from)) + h; - float y0 = getYCrd(Position.getY(m.from)) + h; - float x1 = getXCrd(Position.getX(m.to)) + h; - float y1 = getYCrd(Position.getY(m.to)) + h; + XYCoord crd0 = sqToPix(Position.getX(m.from), Position.getY(m.from)); + XYCoord crd1 = sqToPix(Position.getX(m.to), Position.getY(m.to)); + float x0 = crd0.x + 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 y2 = 0; @@ -562,10 +569,17 @@ public abstract class ChessBoard extends View { canvas.drawText(s, xCrd, yCrd, labelPaint); } - protected abstract int getXCrd(int x); - protected abstract int getYCrd(int y); - protected abstract int getXSq(int xCrd); - protected abstract int getYSq(int yCrd); + protected static class XYCoord { + public int x; + public int y; + 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. @@ -578,8 +592,9 @@ public abstract class ChessBoard extends View { int sq = -1; if (sqSize > 0) { - int x = getXSq(xCrd); - int y = getYSq(yCrd); + XYCoord xy = pixToSq(xCrd, yCrd); + int x = xy.x; + int y = xy.y; if ((x >= 0) && (x < 8) && (y >= 0) && (y < 8)) { sq = Position.getSquare(x, y); } @@ -674,8 +689,9 @@ public abstract class ChessBoard extends View { if (((1L << sq) & decorated) != 0) continue; decorated |= 1L << sq; - int xCrd = getXCrd(Position.getX(sq)); - int yCrd = getYCrd(Position.getY(sq)); + XYCoord crd = sqToPix(Position.getX(sq), Position.getY(sq)); + int xCrd = crd.x; + int yCrd = crd.y; String s = null; int wdl = sd.tbData.wdl; diff --git a/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java b/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java index 20f22f8..820aa30 100644 --- a/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java +++ b/DroidFish/src/org/petero/droidfish/ChessBoardPlay.java @@ -48,13 +48,18 @@ public class ChessBoardPlay extends ChessBoard { } @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 - protected int getYCrd(int y) { return y0 + sqSize * (flipped ? y : 7 - y); } - @Override - protected int getXSq(int xCrd) { int t = (xCrd - x0) / sqSize; return flipped ? 7 - t : t; } - @Override - protected int getYSq(int yCrd) { int t = (yCrd - y0) / sqSize; return flipped ? t : 7 - t; } + protected XYCoord pixToSq(int xCrd, int yCrd) { + int x = (xCrd - x0) / sqSize; if (flipped) x = 7 - x; + int y = (yCrd - y0) / sqSize; if (!flipped) y = 7 - y; + return new XYCoord(x, y); + } @Override protected int getWidth(int sqSize) { return sqSize * 8; } diff --git a/DroidFish/src/org/petero/droidfish/activities/ChessBoardEdit.java b/DroidFish/src/org/petero/droidfish/activities/ChessBoardEdit.java index 6147e48..957386f 100644 --- a/DroidFish/src/org/petero/droidfish/activities/ChessBoardEdit.java +++ b/DroidFish/src/org/petero/droidfish/activities/ChessBoardEdit.java @@ -179,8 +179,9 @@ public class ChessBoardEdit extends ChessBoard { 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 yCrd = getYCrd(y); + XYCoord crd = sqToPix(x, y); + final int xCrd = crd.x; + final int yCrd = crd.y; Paint paint = Position.darkSquare(x, y) ? darkPaint : brightPaint; canvas.drawRect(xCrd, yCrd, xCrd+sqSize, yCrd+sqSize, paint); int p = extraPieces(x, y); @@ -218,29 +219,31 @@ public class ChessBoardEdit extends ChessBoard { } @Override - protected int getXCrd(int x) { - return x0 + sqSize * x + ((x >= 8) ? getGap(sqSize) : 0); + protected XYCoord sqToPix(int x, int y) { + 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 - protected int getYCrd(int y) { - return y0 + sqSize * (7 - y) + ((y < 0) ? getGap(sqSize) : 0); - } - - @Override - protected int getXSq(int xCrd) { + protected XYCoord pixToSq(int xCrd, int yCrd) { int x = (xCrd - x0) / sqSize; - if (x < 8) - return x; - return (xCrd - x0 - getGap(sqSize)) / sqSize; - } + if (x >= 8) + x = (xCrd - x0 - getGap(sqSize)) / sqSize; - @Override - protected int getYSq(int yCrd) { int y = 7 - (yCrd - y0) / sqSize; - if (y >= 0) - return y; - return 7 - (yCrd - y0 - getGap(sqSize)) / sqSize; + if (y < 0) + y = 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()); if (sqSize > 0) { - int x = getXSq(xCrd); - int y = getYSq(yCrd); + XYCoord xy = pixToSq(xCrd, yCrd); + int x = xy.x; + int y = xy.y; if ( landScape && (x >= 0) && (x < 10) && (y >= 0) && (y < 8) || !landScape && (x >= 0) && (x < 8) && (y >= -2) && (y < 0)) { int p = extraPieces(x, y); diff --git a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java index 7676e7a..76e6708 100644 --- a/DroidFish/src/org/petero/droidfish/activities/EditBoard.java +++ b/DroidFish/src/org/petero/droidfish/activities/EditBoard.java @@ -85,6 +85,7 @@ public class EditBoard extends Activity { private boolean egtbHints; private boolean autoScrollTitle; + private boolean boardFlipped; private TextView whiteFigText; private TextView blackFigText; private Typeface figNotation; @@ -102,6 +103,7 @@ public class EditBoard extends Activity { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); egtbHints = settings.getBoolean("tbHintsEdit", false); autoScrollTitle = settings.getBoolean("autoScrollTitle", true); + boardFlipped = settings.getBoolean("boardFlipped", false); initUI(); @@ -142,6 +144,7 @@ public class EditBoard extends Activity { View firstTitleLine = findViewById(R.id.first_title_line); View secondTitleLine = findViewById(R.id.second_title_line); cb = (ChessBoardEdit)findViewById(R.id.eb_chessboard); + cb.setFlipped(boardFlipped); status = (TextView)findViewById(R.id.eb_status); okButton = (Button)findViewById(R.id.eb_ok); cancelButton = (Button)findViewById(R.id.eb_cancel);