X-Git-Url: https://git.auder.net/?p=vchess.git;a=blobdiff_plain;f=public%2Fjavascripts%2Futils%2FprintDiagram.js;h=9f7a0203d1a9af62d4d988a88e3d0ce336ddc288;hp=ef750490ed02ad88c3af54663ac3de12606e78b1;hb=b6487fb9c41705187cf97215fc9e8f86a59057c7;hpb=da06a6eb0237123ce43fdb01cb06246b8b57f5e5 diff --git a/public/javascripts/utils/printDiagram.js b/public/javascripts/utils/printDiagram.js index ef750490..9f7a0203 100644 --- a/public/javascripts/utils/printDiagram.js +++ b/public/javascripts/utils/printDiagram.js @@ -1,42 +1,104 @@ -// Assuming V(ariantRules) class is loaded. -// args: object with position (mandatory), orientation, marks (optional) -function getDiagram(args) +// Turn (human) marks into coordinates +function getMarkArray(marks) { - const [sizeX,sizeY] = [V.size.x,V.size.y]; - // Obtain array of pieces images names - const board = VariantRules.GetBoard(args.position); - const orientation = args.orientation || "w"; - let markArray = []; - if (!!args.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; +} + +// args: object with position (mandatory), and +// orientation, marks, shadow (optional) +function getDiagram(args) +{ + // Obtain the array of pieces images names: + const board = VariantRules.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] - : [sizeX-1, sizeY-1, -1]; - for (let i=startX; i>=0 && i=0 && i=0 && j=0 && j"; + boardDiv += "
"; if (board[i][j] != V.EMPTY) { boardDiv += ""; } - if (!!args.marks && markArray[i][j]) + if (markArray.length > 0 && markArray[i][j]) boardDiv += ""; boardDiv += "
"; }