Skip to content

Commit c6e761f

Browse files
committed
prepare canvas cursor pointer
1 parent 82dc712 commit c6e761f

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

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

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import {
2+
twJoin,
3+
} from "tailwind-merge"
14
import {
25
useEffect,
36
useCallback,
@@ -100,7 +103,6 @@ function Board({gameState, setGameState}) {
100103
let board = gameState.board
101104
let [forbidden_x, forbidden_y] = gameState.forbidden
102105
let canvasRef = useRef()
103-
let countingGroup = !gameHasEnded(gameState) && counting ? getCountingGroup(board, cursor_x, cursor_y) : undefined
104106
let dragging = useLayoutStore(state => state.dragging)
105107
let muted = useMuteStore(state => state.muted)
106108
let howler = useRef()
@@ -138,6 +140,32 @@ function Board({gameState, setGameState}) {
138140
}
139141
}, [setCtrlKeyDown])
140142

143+
let isCursorInBounds = useCallback(() => {
144+
let dim = board.length
145+
let x = cursorXref.current
146+
let y = cursorYref.current
147+
return x >= 0 && x < dim && y >= 0 && y < dim
148+
}, [board.length])
149+
150+
let getCountingGroup = useCallback(() => {
151+
if (gameHasEnded(gameState)) {
152+
return undefined
153+
}
154+
if (!gameState.counting) {
155+
return undefined
156+
}
157+
if (!isCursorInBounds()) {
158+
return undefined
159+
}
160+
let x = cursorXref.current
161+
let y = cursorYref.current
162+
let {has, hasStone} = board[x][y]
163+
if (!hasStone) {
164+
return undefined
165+
}
166+
return has
167+
}, [gameState, board, isCursorInBounds])
168+
141169
let context = useMemo(() => {
142170
let dim = board.length
143171
if (!dim) {
@@ -190,15 +218,12 @@ function Board({gameState, setGameState}) {
190218
ctrlKeyDownRef,
191219
cursorXref,
192220
cursorYref,
193-
isCursorInBounds: function(x, y) {
194-
return x >= 0 && x < dim && y >= 0 && y < dim
195-
},
196221
hoshis: hoshis,
197222
stoneRadius: getRadius(step * 0.475),
198223
territoryRadius: getRadius(step * 0.125),
199224
hoshiRadius: getRadius(step * 0.0625),
200225
}
201-
}, [board.length, canvasRef, zoom])
226+
}, [board, canvasRef, zoom])
202227

203228
let onMouseMove = useCallback((e) => {
204229
if (dragging) {
@@ -207,10 +232,11 @@ function Board({gameState, setGameState}) {
207232
if (!board.length) {
208233
return
209234
}
235+
let dim = board.length
210236
setCtrlKeyDown(e.ctrlKey)
211237
let cursor_x = Math.round((e.nativeEvent.offsetX - context.margin) / context.step)
212238
let cursor_y = Math.round((e.nativeEvent.offsetY - context.margin) / context.step)
213-
if (context.isCursorInBounds(cursor_x, cursor_y)) {
239+
if (cursor_x >= 0 && cursor_x < dim && cursor_y >= 0 && cursor_y < dim) {
214240
setCursor_x(cursor_x + 0)
215241
setCursor_y(cursor_y + 0)
216242
} else {
@@ -219,16 +245,16 @@ function Board({gameState, setGameState}) {
219245
}
220246
}, [context, board.length, dragging])
221247

222-
let onClick = useCallback((e) => {
248+
let onClick = useCallback(() => {
249+
let cursor_x = cursorXref.current
250+
let cursor_y = cursorYref.current
223251
if (gameHasEnded(gameState)) {
224252
return
225253
}
226254
if (!board.length) {
227255
return
228256
}
229-
let cursor_x = Math.round((e.nativeEvent.offsetX - context.margin) / context.step)
230-
let cursor_y = Math.round((e.nativeEvent.offsetY - context.margin) / context.step)
231-
if (!context.isCursorInBounds(cursor_x, cursor_y)) {
257+
if (!isCursorInBounds()) {
232258
return
233259
}
234260
if (counting) {
@@ -259,7 +285,7 @@ function Board({gameState, setGameState}) {
259285
destination: "/app/game/move",
260286
body: JSON.stringify(move),
261287
})
262-
}, [gameState, setGameState, context, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound])
288+
}, [gameState, setGameState, auth, board, stompClient, counting, forbidden_x, forbidden_y, movesLength, myColor, playClickSound, isCursorInBounds])
263289

264290
useEffect(() => {
265291
if (!board.length) {
@@ -275,7 +301,7 @@ function Board({gameState, setGameState}) {
275301
paintGrid(context)
276302
let showMoveNumbers = ctrlKeyDownRef.current && (isKibitz(gameState, auth) || gameHasEnded(gameState))
277303
if (counting && !isReviewing(gameState)) {
278-
paintStonesCounting(context, board, countingGroup)
304+
paintStonesCounting(context, board, getCountingGroup())
279305
if (showMoveNumbers) {
280306
paintMoveNumbers(context, board)
281307
}
@@ -290,7 +316,7 @@ function Board({gameState, setGameState}) {
290316
if (currentPlayer(gameState) !== auth.name) {
291317
return
292318
}
293-
if (!context.isCursorInBounds(cursor_x, cursor_y)) {
319+
if (!isCursorInBounds()) {
294320
return
295321
}
296322
if (board[cursor_y][cursor_x].hasStone) {
@@ -303,7 +329,7 @@ function Board({gameState, setGameState}) {
303329
return
304330
}
305331
paintShadow(context, cursor_x, cursor_y, currentColor(gameState))
306-
}, [gameState, cursor_x, cursor_y, context, ctrlKeyDown, canvasRef, auth, board, counting, countingGroup, forbidden_x, forbidden_y, lastMove])
332+
}, [gameState, context, cursor_x, cursor_y, ctrlKeyDown, canvasRef, auth, board, counting, forbidden_x, forbidden_y, lastMove, isCursorInBounds, getCountingGroup])
307333

308334
useEffect(() => {
309335
if (id === gameId && queueStatus === "up_to_date") {
@@ -333,7 +359,7 @@ function Board({gameState, setGameState}) {
333359

334360
return (
335361
<div className="grid h-full">
336-
<canvas className="place-self-center" ref={canvasRef}
362+
<canvas className={twJoin("place-self-center")} ref={canvasRef}
337363
onMouseLeave={() => {
338364
setCursor_x(-1)
339365
setCursor_y(-1)
@@ -354,20 +380,6 @@ function getRadius(radius) {
354380
return diameter / 2
355381
}
356382

357-
function getCountingGroup(board, cursor_x, cursor_y) {
358-
if (cursor_x < 0 ||
359-
cursor_x >= board.length ||
360-
cursor_y < 0 ||
361-
cursor_y >= board.length ) {
362-
return undefined
363-
}
364-
let {has, hasStone} = board[cursor_y][cursor_x]
365-
if (!hasStone) {
366-
return undefined
367-
}
368-
return has
369-
}
370-
371383
function MuteIcon() {
372384
let toggleMuted = useMuteStore((state) => state.toggleMuted)
373385
let muted = useMuteStore(state => state.muted)

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,11 @@ function paintMoveNumber({stoneRadius, canvasRef, grid}, grid_x, grid_y, color,
167167
ctx.fillText(n, x, y)
168168
}
169169

170-
export function paintLastMove({isCursorInBounds, canvasRef, grid, stoneRadius}, lastMove) {
170+
export function paintLastMove({canvasRef, grid, stoneRadius}, lastMove) {
171171
if (!lastMove) {
172172
return
173173
}
174174
let {x: grid_x, y: grid_y, color} = lastMove
175-
if (!isCursorInBounds(grid_x, grid_y)) {
176-
return
177-
}
178175
let style = color === BLACK ?
179176
"rgba(255,255,255)" :
180177
"rgba(0,0,0)"

0 commit comments

Comments
 (0)