Skip to content

Commit

Permalink
Implements new, modern design of the grid
Browse files Browse the repository at this point in the history
Uses a path with round corners around cages and avoids further borders of the whole grid.

Adaption to different screens is still in progress.
  • Loading branch information
meikpiep committed Oct 3, 2023
1 parent 11c3f54 commit 3d08297
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 422 deletions.
42 changes: 36 additions & 6 deletions holoken-app/src/main/kotlin/com/holokenmod/ui/grid/GridCageUI.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.holokenmod.ui.grid

import android.graphics.Canvas
import android.graphics.Path
import com.holokenmod.creation.cage.BorderInfo
import com.holokenmod.grid.GridCage

class GridCageUI(
Expand All @@ -12,10 +14,7 @@ class GridCageUI(
private var westPixel: Float = 0f
private var northPixel: Float = 0f

fun onDraw(canvas: Canvas, cellSize: Float, padding: Pair<Int, Int>) {
westPixel = padding.first + cellSize * cage.getCell(0).column + GridUI.BORDER_WIDTH
northPixel = padding.second + cellSize * cage.getCell(0).row + GridUI.BORDER_WIDTH

fun onDraw(canvas: Canvas, cellSize: Float) {
drawCageText(canvas, cellSize)
}

Expand All @@ -32,10 +31,41 @@ class GridCageUI(

canvas.drawText(
cage.cageText,
westPixel + 4,
northPixel + cageTextSize,
westPixel + 4 + 8,
northPixel + cageTextSize + 8,
paint
)
}

fun drawCageBackground(canvas: Canvas, cellSize: Float, padding: Pair<Int, Int>) {
westPixel = padding.first + cellSize * cage.getCell(0).column + GridUI.BORDER_WIDTH
northPixel = padding.second + cellSize * cage.getCell(0).row + GridUI.BORDER_WIDTH

val paint = paintHolder.gridPaint(cage, grid.grid, cellSize)

val offsetDistance = 5

var pixelX = westPixel + offsetDistance
var pixelY = northPixel + offsetDistance

val path = Path()
path.moveTo(pixelX, pixelY)

cage.cageType.borderInfos.forEach{
val length = it.length * cellSize - it.offset * offsetDistance

when (it.direction) {
BorderInfo.Direction.UP -> pixelY -= length
BorderInfo.Direction.DOWN -> pixelY += length
BorderInfo.Direction.LEFT -> pixelX -= length
BorderInfo.Direction.RIGHT -> pixelX += length
}

path.lineTo(pixelX, pixelY)
}

path.close()

canvas.drawPath(path, paint)
}
}
55 changes: 42 additions & 13 deletions holoken-app/src/main/kotlin/com/holokenmod/ui/grid/GridCellUI.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.holokenmod.ui.grid

import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.RectF
import com.holokenmod.grid.GridCell

class GridCellUI(
Expand All @@ -17,7 +19,6 @@ class GridCellUI(
val eastPixel: Float
get() = westPixel + cellSize

private val borderDrawer = GridCellUIBorderDrawer(this, paintHolder)
private val possibleNumbersDrawer = GridCellUIPossibleNumbersDrawer(this, paintHolder)
private var cellSize = 0f

Expand All @@ -33,13 +34,32 @@ class GridCellUI(
.userValue + ">"
}

fun onDraw(canvas: Canvas, cellSize: Float, padding: Pair<Int, Int>) {
fun onDraw(
canvas: Canvas,
grid: GridUI,
cellSize: Float,
padding: Pair<Int, Int>
) {
this.cellSize = cellSize
this.westPixel = padding.first + cellSize * cell.column + GridUI.BORDER_WIDTH
this.northPixel = padding.second + cellSize * cell.row + GridUI.BORDER_WIDTH

drawCellBackground(canvas)
borderDrawer.drawBorders(canvas)

if (cell.cage() == grid.grid.getCage(cell.row, cell.column + 1)) {
canvas.drawLine(westPixel + cellSize,
northPixel + 8,
westPixel + cellSize,
northPixel + cellSize - 8,
paintHolder.innerGridPaint(cellSize))
}
if (cell.cage() == grid.grid.getCage(cell.row + 1, cell.column)) {
canvas.drawLine(westPixel + 8,
northPixel + cellSize,
westPixel + cellSize - 8,
northPixel + cellSize,
paintHolder.innerGridPaint(cellSize))
}

drawCellValue(canvas, cellSize)

Expand Down Expand Up @@ -74,14 +94,23 @@ class GridCellUI(
}

private fun drawCellBackground(canvas: Canvas) {
paintHolder.cellBackgroundPaint(cell)?.let {
canvas.drawRect(
westPixel + 1,
northPixel + 1,
eastPixel - 1,
southPixel - 1,
it
)
}
val paint = paintHolder.cellBackgroundPaint(cell) ?: return

val offsetDistance = 5

paint.strokeJoin = Paint.Join.ROUND
paint.style = Paint.Style.STROKE
paint.strokeWidth = 5f
paint.pathEffect = CornerPathEffect(0.21f * cellSize)

canvas.drawRect(
RectF(
westPixel + offsetDistance,
northPixel + offsetDistance,
eastPixel - offsetDistance,
southPixel - offsetDistance
),
paint
)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ class GridCellUIPossibleNumbersDrawer(
}

private fun drawPossibleNumbersDynamically(canvas: Canvas, cellSize: Float, paint: Paint) {
if (cell.possibles.isEmpty()) return

val possiblesLines = calculatePossibleLines(paint, cellSize)

var index = 0
val metrics = paint.fontMetricsInt
val lineHeigth = -metrics.ascent + metrics.leading + metrics.descent

possiblesLines.forEach {
canvas.drawText(
it,
cellUI.westPixel + 13,
cellUI.northPixel + cellSize - 15 - lineHeigth * index,
paint
)
index++
}
}

private fun calculatePossibleLines(
paint: Paint,
cellSize: Float
): List<String> {
if (cellSize < 35) {
return listOf("...")
}

paint.textSize = (cellSize / 4).toInt().toFloat()
val possiblesLines = mutableListOf<MutableSet<Int>>()

Expand All @@ -33,10 +60,10 @@ class GridCellUIPossibleNumbersDrawer(
possiblesLines += currentLine
var currentLineText = getPossiblesLineText(currentLine)

while (paint.measureText(currentLineText) > cellSize - 8) {
while (paint.measureText(currentLineText) > cellSize - 26) {
val newLine = mutableSetOf<Int>()
possiblesLines += newLine
while (paint.measureText(currentLineText) > cellSize - 8) {
while (paint.measureText(currentLineText) > cellSize - 26) {
val firstDigitOfCurrentLine = currentLine.first()
newLine.add(firstDigitOfCurrentLine)
currentLine.remove(firstDigitOfCurrentLine)
Expand All @@ -46,19 +73,7 @@ class GridCellUIPossibleNumbersDrawer(
currentLineText = getPossiblesLineText(currentLine)
}

var index = 0
val metrics = paint.fontMetricsInt
val lineHeigth = -metrics.ascent + metrics.leading + metrics.descent

possiblesLines.forEach {
canvas.drawText(
getPossiblesLineText(it),
cellUI.westPixel + 4,
cellUI.northPixel + cellSize - 6 - lineHeigth * index,
paint
)
index++
}
return possiblesLines.map { getPossiblesLineText(it) }
}

private fun drawPossibleNumbersWithFixedGrid(canvas: Canvas, cellSize: Float, paint: Paint) {
Expand Down
Loading

0 comments on commit 3d08297

Please sign in to comment.