Moved MapCanvas to another file

This commit is contained in:
Alexey 2025-07-28 11:16:20 +03:00
commit 0448677b67
2 changed files with 60 additions and 60 deletions

View file

@ -5,12 +5,6 @@ import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@ -19,26 +13,18 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Build import androidx.compose.material.icons.filled.Build
import androidx.compose.material3.FloatingActionButton import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MaterialTheme.colorScheme import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import com.mirenkov.ktheightmap.ui.theme.KtHeightMapTheme import com.mirenkov.ktheightmap.ui.theme.KtHeightMapTheme
const val TAG = "KtHeightMap" const val TAG = "KtHeightMap"
const val TILE_SIZE: Float = 128F const val TILE_SIZE: Float = 256F
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -64,51 +50,6 @@ class MainActivity : ComponentActivity() {
} }
} }
@Composable
fun MapCanvas(
backColor: Color,
gridColor: Color,
modifier: Modifier = Modifier
) {
val offsetX = rememberSaveable { mutableFloatStateOf(0F) }
val offsetY = rememberSaveable { mutableFloatStateOf(0F) }
Canvas(
modifier = modifier.fillMaxSize()
.pointerInput(Unit) {
detectDragGestures { _, distance ->
offsetX.floatValue += distance.x
offsetY.floatValue += distance.y
}
}
) {
drawRect(
color = backColor,
size = size
)
val strippedOffsetX = offsetX.floatValue % TILE_SIZE
val strippedOffsetY = offsetY.floatValue % TILE_SIZE
val offset = Offset(strippedOffsetX, strippedOffsetY)
val grid = size / TILE_SIZE
val gridWidth = grid.width.toInt()
val gridHeight = grid.height.toInt()
for (cellX in 0 .. gridWidth + 1) {
val offsetX = TILE_SIZE * (cellX - 1)
for (cellY in 0 .. gridHeight + 1) {
val offsetY = TILE_SIZE * (cellY - 1)
drawRect(
color = gridColor,
size = Size(TILE_SIZE, TILE_SIZE),
topLeft = offset + Offset(offsetX, offsetY),
style = Stroke(width = 4F)
)
}
}
}
}
@Composable @Composable
fun ToolButton() { fun ToolButton() {
FloatingActionButton( FloatingActionButton(

View file

@ -0,0 +1,59 @@
package com.mirenkov.ktheightmap
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.input.pointer.pointerInput
@Composable
fun MapCanvas(
backColor: Color,
gridColor: Color,
modifier: Modifier = Modifier
) {
val offsetX = rememberSaveable { mutableFloatStateOf(0F) }
val offsetY = rememberSaveable { mutableFloatStateOf(0F) }
Canvas(
modifier = modifier.fillMaxSize()
.pointerInput(Unit) {
detectDragGestures { _, distance ->
offsetX.floatValue += distance.x
offsetY.floatValue += distance.y
}
}
) {
drawRect(
color = backColor,
size = size
)
val strippedOffsetX = offsetX.floatValue % TILE_SIZE
val strippedOffsetY = offsetY.floatValue % TILE_SIZE
val offset = Offset(strippedOffsetX, strippedOffsetY)
val grid = size / TILE_SIZE
val gridWidth = grid.width.toInt()
val gridHeight = grid.height.toInt()
for (cellX in 0 .. gridWidth + 2) {
val offsetX = TILE_SIZE * (cellX - 1)
for (cellY in 0 .. gridHeight + 2) {
val offsetY = TILE_SIZE * (cellY - 1)
drawRect(
color = gridColor,
size = Size(TILE_SIZE, TILE_SIZE),
topLeft = offset + Offset(offsetX, offsetY),
style = Stroke(width = 4F)
)
}
}
}
}