Skip to content

Commit

Permalink
feat(Board): highlight notes with a value of selected number
Browse files Browse the repository at this point in the history
  • Loading branch information
kaajjo committed Nov 17, 2024
1 parent b27cf66 commit d6e1b1c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ fun Board(
questions: Boolean = false,
renderNotes: Boolean = true,
cellsToHighlight: List<Cell>? = null,
notesToHighlight: List<Note> = emptyList(),
zoomable: Boolean = false,
boardColors: SudokuBoardColors = LocalBoardColors.current,
crossHighlight: Boolean = false,
Expand Down Expand Up @@ -112,8 +113,8 @@ fun Board(
val horThick by remember(size) { mutableIntStateOf(ceil(sqrt(size.toFloat())).toInt()) }

var fontSizePx = with(LocalDensity.current) { mainTextSize.toPx() }
var noteSizePx = with(LocalDensity.current) { noteTextSize.toPx() }
var killerSumSizePx = with(LocalDensity.current) { noteTextSize.toPx() * 0.9f }
val noteSizePx = with(LocalDensity.current) { (cellSizeDivWidth * 0.8f).toSp().toPx() }
val killerSumSizePx = with(LocalDensity.current) { noteSizePx * 1.1f }

val thinLineWidth = with(LocalDensity.current) { 1.3.dp.toPx() }
val thickLineWidth = with(LocalDensity.current) { 1.3.dp.toPx() }
Expand Down Expand Up @@ -163,6 +164,15 @@ fun Board(
)
}

val noteHighlightPaint by remember {
mutableStateOf(
Paint().apply {
color = highlightColor.copy(alpha = 0.3f).toArgb()
isAntiAlias = true
}
)
}

var killerSumPaint by remember {
mutableStateOf(
Paint().apply {
Expand All @@ -180,16 +190,6 @@ fun Board(
mainTextSize.value,
context.resources.displayMetrics
)
noteSizePx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
noteTextSize.value,
context.resources.displayMetrics
)
killerSumSizePx = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
noteTextSize.value * 0.9f,
context.resources.displayMetrics
)
textPaint = Paint().apply {
color = foregroundColor.toArgb()
isAntiAlias = true
Expand Down Expand Up @@ -397,7 +397,9 @@ fun Board(
drawNotes(
size = size,
paint = notePaint,
highlightPaint = noteHighlightPaint,
notes = notes,
notesToHighlight = notesToHighlight,
cellSize = cellSize,
cellSizeDivWidth = cellSizeDivWidth,
killerSumBounds = killerSumBounds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ fun DrawScope.drawRoundCell(
fun DrawScope.drawNotes(
size: Int,
paint: Paint,
highlightPaint: Paint,
notes: List<Note>,
notesToHighlight: List<Note>,
cellSize: Float,
cellSizeDivWidth: Float,
killerSumBounds: android.graphics.Rect,
Expand All @@ -61,13 +63,29 @@ fun DrawScope.drawNotes(
val noteTextMeasure = paint.measureText(textToDraw)

val noteCol = getNoteColumnNumber(note.value, size)
val noteRow = getNoteRowNumber(note.value, size)



val horizontalPadding =
if (noteRow == 0) noteTextMeasure / 3f
else if (noteRow == 1) 0f
else if (noteRow == 2 && note.value > 9) 0f
else -(noteTextMeasure / 3f)


if (notesToHighlight.contains(note)) {
canvas.nativeCanvas.drawCircle(
note.col * cellSize + cellSizeDivWidth / 2f + (cellSizeDivWidth * noteRow) + horizontalPadding,
note.row * cellSize + noteBounds.height() * 1.5f + killerSumBounds.height() + (cellDivHeight * noteCol) - (noteBounds.height() * 0.5f),
noteTextMeasure * 1.1f,
highlightPaint
)
}

canvas.nativeCanvas.drawText(
textToDraw,
note.col * cellSize + cellSizeDivWidth / 2f + (cellSizeDivWidth * getNoteRowNumber(
note.value,
size
)) - noteTextMeasure / 2f,
note.col * cellSize + cellSizeDivWidth / 2f + (cellSizeDivWidth * noteRow) - noteTextMeasure / 2f + horizontalPadding,
note.row * cellSize + noteBounds.height() * 1.5f + killerSumBounds.height() + (cellDivHeight * noteCol),
paint
)
Expand Down Expand Up @@ -135,10 +153,11 @@ fun roundRectForCell(
rect: Rect,
cornerRadius: CornerRadius
): RoundRect {
val topLeft = if (row == 0 && col == 0) cornerRadius else CornerRadius.Zero
val topRight = if (row == 0 && col == gameSize - 1)cornerRadius else CornerRadius.Zero
val topLeft = if (row == 0 && col == 0) cornerRadius else CornerRadius.Zero
val topRight = if (row == 0 && col == gameSize - 1) cornerRadius else CornerRadius.Zero
val bottomLeft = if (row == gameSize - 1 && col == 0) cornerRadius else CornerRadius.Zero
val bottomRight = if (row == gameSize - 1 && col == gameSize - 1) cornerRadius else CornerRadius.Zero
val bottomRight =
if (row == gameSize - 1 && col == gameSize - 1) cornerRadius else CornerRadius.Zero

return RoundRect(
rect = rect,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ fun GameScreen(
identicalNumbersHighlight = highlightIdentical,
errorsHighlight = errorHighlight != 0,
positionLines = positionLines,
notesToHighlight = if (viewModel.digitFirstNumber > 0) {
viewModel.notes.filter { it.value == viewModel.digitFirstNumber }
} else {
emptyList()
},
enabled = viewModel.gamePlaying && !viewModel.endGame,
questions = !(viewModel.gamePlaying || viewModel.endGame) && SDK_INT < Build.VERSION_CODES.R,
renderNotes = renderNotes && !viewModel.showSolution,
Expand Down

0 comments on commit d6e1b1c

Please sign in to comment.