Skip to content

Commit

Permalink
Implement clearing of drawables with mouse (#321)
Browse files Browse the repository at this point in the history
* implement clear auto shapes button

* add setting to erase drawables on click

* use `clearShapes` instead of `setShapes` for removing drawables

* improve portuguese translation

* only update shapes if there are shapes

---------

Co-authored-by: Francisco Salgueiro <fgcdbs@gmail.com>
  • Loading branch information
zackschuster and franciscoBSalgueiro authored Jun 29, 2024
1 parent 661f97f commit 2bba1c7
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 22 deletions.
25 changes: 13 additions & 12 deletions src/chessground/Chessground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ export function Chessground(
const moveMethod = useAtomValue(moveMethodAtom);

useEffect(() => {
if (ref?.current && !api) {
if (ref?.current == null) return;
if (api) {
api?.set({
...props,
events: {
change: () => {
if (props.setBoardFen && api) {
props.setBoardFen(api.getFen());
}
},
},
});
} else {
const chessgroundApi = NativeChessground(ref.current, {
...props,
events: {
Expand All @@ -36,17 +48,6 @@ export function Chessground(
},
});
setApi(chessgroundApi);
} else if (ref?.current && api) {
api?.set({
...props,
events: {
change: () => {
if (props.setBoardFen && api) {
props.setBoardFen(api.getFen());
}
},
},
});
}
}, [api, props, ref]);

Expand Down
18 changes: 18 additions & 0 deletions src/components/boards/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
currentTabAtom,
deckAtomFamily,
enableBoardScrollAtom,
eraseDrawablesOnClickAtom,
forcedEnPassantAtom,
moveInputAtom,
showArrowsAtom,
Expand Down Expand Up @@ -46,6 +47,7 @@ import {
IconDeviceFloppy,
IconEdit,
IconEditOff,
IconEraser,
IconPlus,
IconSwitchVertical,
IconTarget,
Expand Down Expand Up @@ -143,6 +145,7 @@ function Board({
const storeMakeMove = useStore(store, (s) => s.makeMove);
const setHeaders = useStore(store, (s) => s.setHeaders);
const deleteMove = useStore(store, (s) => s.deleteMove);
const clearShapes = useStore(store, (s) => s.clearShapes);
const setShapes = useStore(store, (s) => s.setShapes);
const setFen = useStore(store, (s) => s.setFen);

Expand All @@ -152,6 +155,7 @@ function Board({
const showDests = useAtomValue(showDestsAtom);
const showArrows = useAtomValue(showArrowsAtom);
const showConsecutiveArrows = useAtomValue(showConsecutiveArrowsAtom);
const eraseDrawablesOnClick = useAtomValue(eraseDrawablesOnClickAtom);
const autoPromote = useAtomValue(autoPromoteAtom);
const forcedEP = useAtomValue(forcedEnPassantAtom);
const showCoordinates = useAtomValue(showCoordinatesAtom);
Expand Down Expand Up @@ -324,6 +328,17 @@ function Board({
)}
</ActionIcon>
</Tooltip>
{!eraseDrawablesOnClick && (
<Tooltip label="Clear Drawings">
<ActionIcon
variant={editingMode ? "filled" : "default"}
size="lg"
onClick={() => clearShapes()}
>
<IconEraser size="1.3rem" />
</ActionIcon>
</Tooltip>
)}
{!disableVariations && (
<Tooltip label="Edit Position">
<ActionIcon
Expand Down Expand Up @@ -588,6 +603,9 @@ function Board({
}
className={chessboard}
ref={boardRef}
onClick={() => {
eraseDrawablesOnClick && clearShapes();
}}
onWheel={(e) => {
if (enableBoardScroll) {
if (e.deltaY > 0) {
Expand Down
16 changes: 15 additions & 1 deletion src/components/settings/SettingsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Dirs } from "@/App";
import {
autoPromoteAtom,
autoSaveAtom,
enableBoardScrollAtom,
eraseDrawablesOnClickAtom,
forcedEnPassantAtom,
minimumGamesAtom,
moveInputAtom,
Expand Down Expand Up @@ -222,6 +222,20 @@ export default function Page() {
</div>
<SettingsSwitch atom={showConsecutiveArrowsAtom} />
</Group>
<Group
justify="space-between"
wrap="nowrap"
gap="xl"
className={classes.item}
>
<div>
<Text>{t("Settings.EraseDrawablesOnClick")}</Text>
<Text size="xs" c="dimmed">
{t("Settings.EraseDrawablesOnClick.Desc")}
</Text>
</div>
<SettingsSwitch atom={eraseDrawablesOnClickAtom} />
</Group>
<Group
justify="space-between"
wrap="nowrap"
Expand Down
4 changes: 4 additions & 0 deletions src/state/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export const showConsecutiveArrowsAtom = atomWithStorage<boolean>(
"show-consecutive-arrows",
false,
);
export const eraseDrawablesOnClickAtom = atomWithStorage<boolean>(
"erase-drawables-on-click",
false,
);
export const autoPromoteAtom = atomWithStorage<boolean>("auto-promote", true);
export const autoSaveAtom = atomWithStorage<boolean>("auto-save", true);
export const previewBoardOnHoverAtom = atomWithStorage<boolean>(
Expand Down
25 changes: 16 additions & 9 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ export const createTreeStore = (id?: string, initialTree?: TreeState) => {
clearShapes: () =>
set(
produce((state) => {
state.dirty = true;
const node = getNodeAtPath(state.root, state.position);
if (node) {
if (node && node.shapes.length > 0) {
state.dirty = true;
node.shapes = [];
}
}),
Expand Down Expand Up @@ -640,16 +640,23 @@ function promoteVariation(state: TreeState, path: number[]) {
function setShapes(state: TreeState, shapes: DrawShape[]) {
const node = getNodeAtPath(state.root, state.position);
if (!node) return state;
const shape = shapes[0];
const index = node.shapes.findIndex(
(s) => s.orig === shape.orig && s.dest === shape.dest,
);

if (index !== -1) {
node.shapes.splice(index, 1);
const [shape] = shapes;
if (shape) {
const index = node.shapes.findIndex(
(s) => s.orig === shape.orig && s.dest === shape.dest,
);

if (index !== -1) {
node.shapes.splice(index, 1);
} else {
node.shapes.push(shape);
}
} else {
node.shapes.push(shape);
node.shapes = [];
}

return state;
}

function addAnalysis(
Expand Down
3 changes: 3 additions & 0 deletions src/translation/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ export const en_US = {
"Settings.ConsecutiveArrows": "Consecutive Arrows",
"Settings.ConsecutiveArrows.Desc":
"Show multiple arrows for the best line, if it involves moving the same piece several times",
"Settings.EraseDrawablesOnClick": "Erase Drawables On Click",
"Settings.EraseDrawablesOnClick.Desc":
"Clear the board of drawn arrows & circles by left-clicking the mouse",
"Settings.AutoPromition": "Auto Promotion",
"Settings.AutoPromition.Desc":
"Automatically promote to a queen when a pawn reaches the last rank",
Expand Down
3 changes: 3 additions & 0 deletions src/translation/pt_PT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ export const pt_PT = {
"Settings.ConsecutiveArrows": "Setas consecutivas",
"Settings.ConsecutiveArrows.Desc":
"Mostra várias setas para a melhor linha, caso ela consista em mover a mesma peça várias vezes",
"Settings.EraseDrawablesOnClick": "Apagar desenhos ao clicar",
"Settings.EraseDrawablesOnClick.Desc":
"Limpar o tabuleiro das setas e círculos desenhados ao clicar com o botão esquerdo do rato",
"Settings.AutoPromition": "Auto Promoção",
"Settings.AutoPromition.Desc": "Automaticamente promover para rainha",
"Settings.Coordinates": "Coordenadas",
Expand Down
3 changes: 3 additions & 0 deletions src/translation/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export const zh_CN = {
"Settings.ConsecutiveArrows": "连续箭头",
"Settings.ConsecutiveArrows.Desc":
"如果最佳着法中会多次移动同一个棋子,显示多个箭头",
"Settings.EraseDrawablesOnClick": "单击时擦除可绘制对象",
"Settings.EraseDrawablesOnClick.Desc":
"通过单击鼠标左键清除棋盘上绘制的箭头和圆圈",
"Settings.AutoPromition": "自动升变",
"Settings.AutoPromition.Desc": "兵抵达底线时自动升变为后",
"Settings.Coordinates": "坐标",
Expand Down

0 comments on commit 2bba1c7

Please sign in to comment.