Skip to content

Commit 0805c2f

Browse files
committed
canvas cursor pointer on ctrl key
1 parent c6e761f commit 0805c2f

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/main/client/src/feature/game/Game.jsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ function Board({gameState, setGameState}) {
123123

124124
useEffect(() => {
125125
let onKeyDown = (e) => {
126-
if (e.ctrlKey) {
126+
if (e.ctrlKey || e.shiftKey) {
127127
setCtrlKeyDown(true)
128128
}
129129
}
130130
let onKeyUp = (e) => {
131-
if (!e.ctrlKey) {
131+
if (!e.shiftKey && !e.shiftKey) {
132132
setCtrlKeyDown(false)
133133
}
134134
}
@@ -151,20 +151,24 @@ function Board({gameState, setGameState}) {
151151
if (gameHasEnded(gameState)) {
152152
return undefined
153153
}
154-
if (!gameState.counting) {
154+
if (!counting) {
155155
return undefined
156156
}
157157
if (!isCursorInBounds()) {
158158
return undefined
159159
}
160160
let x = cursorXref.current
161161
let y = cursorYref.current
162-
let {has, hasStone} = board[x][y]
162+
let {has, hasStone} = board[y][x]
163163
if (!hasStone) {
164164
return undefined
165165
}
166166
return has
167-
}, [gameState, board, isCursorInBounds])
167+
}, [gameState, counting, board, isCursorInBounds])
168+
169+
let showMoveNumbers = useCallback(() => {
170+
return ctrlKeyDownRef.current && (isKibitz(gameState, auth) || gameHasEnded(gameState))
171+
}, [gameState, auth])
168172

169173
let context = useMemo(() => {
170174
let dim = board.length
@@ -215,7 +219,6 @@ function Board({gameState, setGameState}) {
215219
step,
216220
grid,
217221
canvasRef,
218-
ctrlKeyDownRef,
219222
cursorXref,
220223
cursorYref,
221224
hoshis: hoshis,
@@ -233,7 +236,7 @@ function Board({gameState, setGameState}) {
233236
return
234237
}
235238
let dim = board.length
236-
setCtrlKeyDown(e.ctrlKey)
239+
setCtrlKeyDown(e.shiftKey || e.ctrlKey)
237240
let cursor_x = Math.round((e.nativeEvent.offsetX - context.margin) / context.step)
238241
let cursor_y = Math.round((e.nativeEvent.offsetY - context.margin) / context.step)
239242
if (cursor_x >= 0 && cursor_x < dim && cursor_y >= 0 && cursor_y < dim) {
@@ -299,16 +302,15 @@ function Board({gameState, setGameState}) {
299302
return
300303
}
301304
paintGrid(context)
302-
let showMoveNumbers = ctrlKeyDownRef.current && (isKibitz(gameState, auth) || gameHasEnded(gameState))
303305
if (counting && !isReviewing(gameState)) {
304-
paintStonesCounting(context, board, getCountingGroup())
305-
if (showMoveNumbers) {
306+
paintStonesCounting(context, board, getCountingGroup(), showMoveNumbers())
307+
if (showMoveNumbers()) {
306308
paintMoveNumbers(context, board)
307309
}
308310
return
309311
}
310-
paintStones(context, board, showMoveNumbers)
311-
if (showMoveNumbers) {
312+
paintStones(context, board, showMoveNumbers())
313+
if (showMoveNumbers()) {
312314
paintMoveNumbers(context, board)
313315
} else {
314316
paintLastMove(context, lastMove)
@@ -329,7 +331,7 @@ function Board({gameState, setGameState}) {
329331
return
330332
}
331333
paintShadow(context, cursor_x, cursor_y, currentColor(gameState))
332-
}, [gameState, context, cursor_x, cursor_y, ctrlKeyDown, canvasRef, auth, board, counting, forbidden_x, forbidden_y, lastMove, isCursorInBounds, getCountingGroup])
334+
}, [gameState, context, cursor_x, cursor_y, ctrlKeyDown, canvasRef, auth, board, counting, forbidden_x, forbidden_y, lastMove, isCursorInBounds, getCountingGroup, showMoveNumbers])
333335

334336
useEffect(() => {
335337
if (id === gameId && queueStatus === "up_to_date") {
@@ -359,7 +361,11 @@ function Board({gameState, setGameState}) {
359361

360362
return (
361363
<div className="grid h-full">
362-
<canvas className={twJoin("place-self-center")} ref={canvasRef}
364+
<canvas className={twJoin(
365+
"place-self-center",
366+
isCursorInBounds() && showMoveNumbers() && board[cursor_y][cursor_x].hasStone && "cursor-pointer",
367+
)}
368+
ref={canvasRef}
363369
onMouseLeave={() => {
364370
setCursor_x(-1)
365371
setCursor_y(-1)

src/main/client/src/feature/game/paint.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ export function paintMoveNumbers(context, board) {
108108
}
109109
}
110110

111-
export function paintStonesCounting(context, board, countingGroup) {
111+
export function paintStonesCounting(context, board, countingGroup, showMoveNumbers) {
112+
let cursor_x = context.cursorXref.current
113+
let cursor_y = context.cursorYref.current
112114
for (let grid_y = 0; grid_y < board.length; grid_y++) {
113115
for (let grid_x = 0; grid_x < board.length; grid_x++) {
114-
let { hasStone, color } = board[grid_y][grid_x]
116+
let {hasStone, color} = board[grid_y][grid_x]
115117
if (hasStone) {
116118
if (countingGroup && countingGroup(grid_x, grid_y)) {
117119
paintShadow(context, grid_x, grid_y, color)
120+
} else if (showMoveNumbers && grid_x === cursor_x && grid_y === cursor_y) {
121+
paintShadow(context, grid_x, grid_y, color)
118122
} else {
119123
paintStone(context, grid_x, grid_y, color)
120124
}

0 commit comments

Comments
 (0)