X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=client%2Fsrc%2Futils%2FprintDiagram.js;h=ef0d2b55f528b823823450c8c87bd3760a71697f;hb=2a2936023cda0888e215ff699d420090372899b0;hp=f3f77d8a8d621023a89302d4158f0e422497f7ec;hpb=8d61fc4ab7373b4a576f3f9108cdf7768ae27096;p=vchess.git diff --git a/client/src/utils/printDiagram.js b/client/src/utils/printDiagram.js index f3f77d8a..ef0d2b55 100644 --- a/client/src/utils/printDiagram.js +++ b/client/src/utils/printDiagram.js @@ -1,108 +1,134 @@ +import { ArrayFun } from "@/utils/array"; +import { store } from "@/store"; + // Turn (human) marks into coordinates -function getMarkArray(marks) -{ - if (!marks || marks == "-") - return []; - let markArray = doubleArray(V.size.x, V.size.y, false); - const squares = marks.split(","); - for (let i=0; i= 0) - { - // Shadow a range of squares, horizontally or vertically - const firstLastSq = squares[i].split("-"); - const range = - [ - V.SquareToCoords(firstLastSq[0]), - V.SquareToCoords(firstLastSq[1]) - ]; - const step = - [ - range[1].x == range[0].x - ? 0 - : (range[1].x - range[0].x) / Math.abs(range[1].x - range[0].x), - range[1].y == range[0].y - ? 0 - : (range[1].y - range[0].y) / Math.abs(range[1].y - range[0].y) - ]; - // Convention: range always from smaller to larger number - for (let x=range[0].x, y=range[0].y; x <= range[1].x && y <= range[1].y; - x += step[0], y += step[1]) - { - shadowArray[x][y] = true; - } - continue; - } - // Shadow just one square: - const coords = V.SquareToCoords(squares[i]); - shadowArray[coords.x][coords.y] = true; - } - return shadowArray; +function getShadowArray(shadow) { + if (!shadow || shadow == "-") return []; + let shadowArray = ArrayFun.init(V.size.x, V.size.y, false); + const squares = shadow.split(","); + for (let i = 0; i < squares.length; i++) { + const rownum = V.size.x - parseInt(squares[i], 10); + if (!isNaN(rownum)) { + // Shadow a full row + for (let i = 0; i < V.size.y; i++) shadowArray[rownum][i] = true; + continue; + } + if (squares[i].length == 1) { + // Shadow a full column + const colnum = V.ColumnToCoord(squares[i]); + for (let i = 0; i < V.size.x; i++) shadowArray[i][colnum] = true; + continue; + } + if (squares[i].indexOf("-") >= 0) { + // Shadow a range of squares, horizontally or vertically + const firstLastSq = squares[i].split("-"); + const range = [ + V.SquareToCoords(firstLastSq[0]), + V.SquareToCoords(firstLastSq[1]) + ]; + const step = [ + range[1].x == range[0].x + ? 0 + : (range[1].x - range[0].x) / Math.abs(range[1].x - range[0].x), + range[1].y == range[0].y + ? 0 + : (range[1].y - range[0].y) / Math.abs(range[1].y - range[0].y) + ]; + // Convention: range always from smaller to larger number + for ( + let x = range[0].x, y = range[0].y; + x <= range[1].x && y <= range[1].y; + x += step[0], y += step[1] + ) { + shadowArray[x][y] = true; + } + continue; + } + // Shadow just one square: + const coords = V.SquareToCoords(squares[i]); + shadowArray[coords.x][coords.y] = true; + } + return shadowArray; } // args: object with position (mandatory), and // orientation, marks, shadow (optional) -export function getDiagram(args) -{ - // Obtain the array of pieces images names: - const board = V.GetBoard(args.position); - const orientation = args.orientation || "w"; - const markArray = getMarkArray(args.marks); - const shadowArray = getShadowArray(args.shadow); - let boardDiv = ""; - const [startX,startY,inc] = orientation == 'w' - ? [0, 0, 1] - : [V.size.x-1, V.size.y-1, -1]; - for (let i=startX; i>=0 && i=0 && j 0 && shadowArray[i][j] ? " in-shadow" : "") + - "'>"; - if (board[i][j] != V.EMPTY) - { - boardDiv += ""; - } - if (markArray.length > 0 && markArray[i][j]) - boardDiv += ""; - boardDiv += ""; - } - boardDiv += ""; - } - return boardDiv; +// TODO: in time, find a strategy to draw middle lines (weiqi, xianqi...) +// and maybe also some diagonals (fanorona...) +// https://stackoverflow.com/questions/40697231/horizontal-line-in-the-middle-of-divs +// + CSS rotate? +export function getDiagram(args) { + // Obtain the array of pieces images names: + const board = V.GetBoard(args.position); + const orientation = args.orientation || "w"; + const darkBottomRight = !!args.darkBottomRight; + const markArray = getMarkArray(args.marks); + const shadowArray = getShadowArray(args.shadow); + const vr = new V(); //just for pieces images paths + let boardDiv = ""; + const [startX, startY, inc] = + orientation == "w" ? [0, 0, 1] : [V.size.x - 1, V.size.y - 1, -1]; + for (let i = startX; i >= 0 && i < V.size.x; i += inc) { + boardDiv += "
"; + for (let j = startY; j >= 0 && j < V.size.y; j += inc) { + boardDiv += "
"; + if (board[i][j] != V.EMPTY) { + boardDiv += + ""; + } + if (markArray.length > 0 && markArray[i][j]) + boardDiv += ""; + boardDiv += "
"; + } + boardDiv += "
"; + } + return boardDiv; +} + +// Method to replace diagrams in loaded HTML +export function replaceByDiag(match, p1, p2) { + const diagParts = p2.split(" "); + return getDiagram({ + position: diagParts[0], + marks: diagParts[1], + orientation: diagParts[2], + shadow: diagParts[3] + }); }