UNPKG

shogiground

Version:
138 lines 5.04 kB
import { allKeys, colors } from './constants.js'; export const pos2key = (pos) => allKeys[pos[0] + 16 * pos[1]]; export const key2pos = (k) => { if (k.length > 2) return [k.charCodeAt(1) - 39, k.charCodeAt(2) - 97]; else return [k.charCodeAt(0) - 49, k.charCodeAt(1) - 97]; }; export function memo(f) { let v; const ret = () => { if (v === undefined) v = f(); return v; }; ret.clear = () => { v = undefined; }; return ret; } export function callUserFunction(f, ...args) { if (f) setTimeout(() => f(...args), 1); } export const opposite = (c) => (c === 'sente' ? 'gote' : 'sente'); export const sentePov = (o) => o === 'sente'; export const distanceSq = (pos1, pos2) => { const dx = pos1[0] - pos2[0], dy = pos1[1] - pos2[1]; return dx * dx + dy * dy; }; export const samePiece = (p1, p2) => p1.role === p2.role && p1.color === p2.color; const posToTranslateBase = (pos, dims, asSente, xFactor, yFactor) => [ (asSente ? dims.files - 1 - pos[0] : pos[0]) * xFactor, (asSente ? pos[1] : dims.ranks - 1 - pos[1]) * yFactor, ]; export const posToTranslateAbs = (dims, bounds) => { const xFactor = bounds.width / dims.files, yFactor = bounds.height / dims.ranks; return (pos, asSente) => posToTranslateBase(pos, dims, asSente, xFactor, yFactor); }; export const posToTranslateRel = (dims) => (pos, asSente) => posToTranslateBase(pos, dims, asSente, 100, 100); export const translateAbs = (el, pos, scale) => { el.style.transform = `translate(${pos[0]}px,${pos[1]}px) scale(${scale}`; }; export const translateRel = (el, percents, scaleFactor, scale) => { el.style.transform = `translate(${percents[0] * scaleFactor}%,${percents[1] * scaleFactor}%) scale(${scale || scaleFactor})`; }; export const setDisplay = (el, v) => { el.style.display = v ? '' : 'none'; }; export const eventPosition = (e) => { var _a; if (e.clientX || e.clientX === 0) return [e.clientX, e.clientY]; if ((_a = e.targetTouches) === null || _a === void 0 ? void 0 : _a[0]) return [e.targetTouches[0].clientX, e.targetTouches[0].clientY]; return; // touchend has no position! }; export const isRightButton = (e) => e.buttons === 2 || e.button === 2; export const isMiddleButton = (e) => e.buttons === 4 || e.button === 1; export const createEl = (tagName, className) => { const el = document.createElement(tagName); if (className) el.className = className; return el; }; export function pieceNameOf(piece) { return `${piece.color} ${piece.role}`; } export function parsePieceName(pieceName) { const splitted = pieceName.split(' ', 2); return { color: splitted[0], role: splitted[1] }; } export function isPieceNode(el) { return el.tagName === 'PIECE'; } export function isSquareNode(el) { return el.tagName === 'SQ'; } export function computeSquareCenter(key, asSente, dims, bounds) { const pos = key2pos(key); if (asSente) { pos[0] = dims.files - 1 - pos[0]; pos[1] = dims.ranks - 1 - pos[1]; } return [ bounds.left + (bounds.width * pos[0]) / dims.files + bounds.width / (dims.files * 2), bounds.top + (bounds.height * (dims.ranks - 1 - pos[1])) / dims.ranks + bounds.height / (dims.ranks * 2), ]; } export function domSquareIndexOfKey(key, asSente, dims) { const pos = key2pos(key); let index = dims.files - 1 - pos[0] + pos[1] * dims.files; if (!asSente) index = dims.files * dims.ranks - 1 - index; return index; } export function isInsideRect(rect, pos) { return (rect.left <= pos[0] && rect.top <= pos[1] && rect.left + rect.width > pos[0] && rect.top + rect.height > pos[1]); } export function getKeyAtDomPos(pos, asSente, dims, bounds) { let file = Math.floor((dims.files * (pos[0] - bounds.left)) / bounds.width); if (asSente) file = dims.files - 1 - file; let rank = Math.floor((dims.ranks * (pos[1] - bounds.top)) / bounds.height); if (!asSente) rank = dims.ranks - 1 - rank; return file >= 0 && file < dims.files && rank >= 0 && rank < dims.ranks ? pos2key([file, rank]) : undefined; } export function getHandPieceAtDomPos(pos, roles, bounds) { for (const color of colors) { for (const role of roles) { const piece = { color, role }, pieceRect = bounds.get(pieceNameOf(piece)); if (pieceRect && isInsideRect(pieceRect, pos)) return piece; } } return; } export function posOfOutsideEl(left, top, asSente, dims, boardBounds) { const sqW = boardBounds.width / dims.files, sqH = boardBounds.height / dims.ranks; if (!sqW || !sqH) return; let xOff = (left - boardBounds.left) / sqW; if (asSente) xOff = dims.files - 1 - xOff; let yOff = (top - boardBounds.top) / sqH; if (!asSente) yOff = dims.ranks - 1 - yOff; return [xOff, yOff]; } //# sourceMappingURL=util.js.map