Skip to content

Commit 485f4d3

Browse files
committed
✨ (sudoku): add click with highlight
1 parent a35be7d commit 485f4d3

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

internal/layout/click-handler.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package layout
2+
3+
import (
4+
"github.com/hajimehoshi/ebiten/v2"
5+
"github.com/hajimehoshi/ebiten/v2/inpututil"
6+
"github.com/leetcode-golang-classroom/sudoku-game/internal/game"
7+
)
8+
9+
func (gameLayout *GameLayout) detectClickCell() {
10+
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
11+
xPos, yPos := ebiten.CursorPosition()
12+
yPos -= PanelHeight
13+
// range check
14+
if (xPos >= 0 && xPos <= ScreenWidth) &&
15+
(yPos >= 0 && yPos <= ScreenHeight) {
16+
row := yPos / cellSize
17+
col := xPos / cellSize
18+
gameLayout.updateCursor(row, col)
19+
}
20+
}
21+
}
22+
23+
func (gameLayout *GameLayout) updateCursor(row, col int) {
24+
if row >= 0 && row < game.BoardSize {
25+
gameLayout.gameInstance.Board.CursorRow = row
26+
}
27+
if col >= 0 && col < game.BoardSize {
28+
gameLayout.gameInstance.Board.CursorCol = col
29+
}
30+
}

internal/layout/high-light.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package layout
2+
3+
import (
4+
"image/color"
5+
6+
"github.com/hajimehoshi/ebiten/v2"
7+
"github.com/hajimehoshi/ebiten/v2/vector"
8+
"github.com/leetcode-golang-classroom/sudoku-game/internal/game"
9+
)
10+
11+
func (gameLayout *GameLayout) drawHighLightCell(screen *ebiten.Image, row, col int) {
12+
board := gameLayout.gameInstance.Board
13+
cursorRow := board.CursorRow
14+
cursorCol := board.CursorCol
15+
16+
// 跳過自己
17+
if row == cursorRow && col == cursorCol {
18+
return
19+
}
20+
// highLight relative row, col
21+
if row == cursorRow || col == cursorCol {
22+
gameLayout.drawHighLightCover(screen, row, col)
23+
return
24+
}
25+
boxSize := game.BoxSize
26+
boxRow := (cursorRow / boxSize) * boxSize
27+
boxCol := (cursorCol / boxSize) * boxSize
28+
// highLight Box value check
29+
for r := 0; r < boxSize; r++ {
30+
for c := 0; c < boxSize; c++ {
31+
br := boxRow + r
32+
bc := boxCol + c
33+
if br == row && bc == col {
34+
gameLayout.drawHighLightCover(screen, row, col)
35+
break
36+
}
37+
}
38+
}
39+
}
40+
41+
func (gameLayout *GameLayout) drawHighLightCover(screen *ebiten.Image, row, col int) {
42+
xPos := col * cellSize
43+
yPos := PanelHeight + row*cellSize
44+
// stable blue
45+
bgColor := color.RGBA{0x6a, 0x5a, 0xcd, 128}
46+
if col%3 == 0 {
47+
xPos += thinkLineWidth
48+
} else {
49+
xPos += leanLineWidth
50+
}
51+
if row%3 == 0 {
52+
yPos += thinkLineWidth
53+
} else {
54+
yPos += leanLineWidth
55+
}
56+
vector.DrawFilledRect(
57+
screen,
58+
float32(xPos),
59+
float32(yPos),
60+
cellSize-1,
61+
cellSize-1,
62+
bgColor,
63+
false,
64+
)
65+
}

internal/layout/layout.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (gameLayout *GameLayout) Update() error {
3333
if gameLayout.isPlayerWin {
3434
return nil
3535
}
36+
gameLayout.detectClickCell()
3637
gameLayout.elapsedSeconds = gameLayout.gameInstance.GetElaspedTime()
3738
gameLayout.DetectCursor()
3839
gameLayout.DetectInput()
@@ -105,6 +106,8 @@ func (gameLayout *GameLayout) drawCellValuesOnBoard(screen *ebiten.Image) {
105106
getTileColor(board.Cells[row][col].Type),
106107
)
107108
}
109+
// highlight background
110+
gameLayout.drawHighLightCell(screen, row, col)
108111
}
109112
}
110113
}

0 commit comments

Comments
 (0)