-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
87 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/space/taran/arkretouch/presentation/edit/TransparencyChessBoard.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package space.taran.arkretouch.presentation.edit | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Canvas | ||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.background | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Paint | ||
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | ||
import androidx.compose.ui.graphics.graphicsLayer | ||
|
||
private class TransparencyChessBoard { | ||
fun create(boardSize: Size, canvas: Canvas) { | ||
val numberOfBoxesOnHeight = (boardSize.height / SQUARE_SIZE).toInt() | ||
val numberOfBoxesOnWidth = (boardSize.width / SQUARE_SIZE).toInt() | ||
var color = DARK | ||
val paint = Paint().also { | ||
it.color = color | ||
} | ||
|
||
0.rangeTo(numberOfBoxesOnWidth).forEach { i -> | ||
0.rangeTo(numberOfBoxesOnHeight).forEach { j -> | ||
val offsetX = SQUARE_SIZE * i | ||
val offsetY = SQUARE_SIZE * j | ||
val offset = Offset(offsetX, offsetY) | ||
val box = Rect(offset, Size(SQUARE_SIZE, SQUARE_SIZE)) | ||
if (j == 0) { | ||
if (color == paint.color) { | ||
switchPaintColor(paint) | ||
} | ||
color = paint.color | ||
} | ||
switchPaintColor(paint) | ||
canvas.drawRect(box, paint) | ||
} | ||
} | ||
} | ||
|
||
private fun switchPaintColor(paint: Paint) { | ||
if (paint.color == DARK) | ||
paint.color = LIGHT | ||
else paint.color = DARK | ||
} | ||
|
||
companion object { | ||
private const val SQUARE_SIZE = 100f | ||
private val LIGHT = Color.White | ||
private val DARK = Color.LightGray | ||
} | ||
} | ||
|
||
private fun transparencyChessBoard(canvas: Canvas, size: Size) { | ||
TransparencyChessBoard().create(size, canvas) | ||
} | ||
|
||
@Composable | ||
fun TransparencyChessBoardCanvas(modifier: Modifier) { | ||
Canvas( | ||
modifier | ||
.background(Color.Transparent) | ||
.graphicsLayer(alpha = 0.99f) | ||
) { | ||
drawIntoCanvas { canvas -> | ||
transparencyChessBoard(canvas, size) | ||
} | ||
} | ||
} |