Scale slider

This commit is contained in:
Alexey 2025-07-30 15:39:50 +03:00
commit 783a700f04
2 changed files with 42 additions and 6 deletions

View file

@ -7,23 +7,34 @@ 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.background
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.absoluteOffset
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawingPadding import androidx.compose.foundation.layout.safeDrawingPadding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons 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.colorScheme import androidx.compose.material3.MaterialTheme.colorScheme
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Slider
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.draw.rotate
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
@ -62,19 +73,35 @@ class MainActivity : ComponentActivity() {
@Composable @Composable
fun Main(vm: TileViewModel = viewModel()) { fun Main(vm: TileViewModel = viewModel()) {
var sliderValue = rememberSaveable { mutableFloatStateOf(1F) }
KtHeightMapTheme { KtHeightMapTheme {
Surface { Box(modifier = Modifier.background(color = colorScheme.background)) {
Scaffold( Scaffold(
modifier = Modifier.fillMaxSize() modifier = Modifier.fillMaxSize()
.safeDrawingPadding(), .safeDrawingPadding()
.align(Alignment.Center),
floatingActionButton = { ToolButton() } floatingActionButton = { ToolButton() }
) { innerPadding -> ) { innerPadding ->
MapCanvas( MapCanvas(
gridColor = colorScheme.primary, gridColor = colorScheme.primary,
backColor = colorScheme.background, backColor = colorScheme.background,
scale = sliderValue,
modifier = Modifier.padding(innerPadding), modifier = Modifier.padding(innerPadding),
) )
} }
Box(modifier = Modifier.safeDrawingPadding()
.align(Alignment.CenterEnd)
.rotate(-90F)
.size(240.dp, 40.dp)
.offset(0.dp, 80.dp)
) {
Slider(
value = sliderValue.floatValue,
onValueChange = { sliderValue.floatValue = it },
valueRange = 1F..14F,
modifier = Modifier.align(Alignment.CenterStart)
)
}
} }
} }
} }

View file

@ -2,8 +2,11 @@ package com.mirenkov.ktheightmap
import androidx.compose.foundation.Canvas import androidx.compose.foundation.Canvas
import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.gestures.rememberTransformableState
import androidx.compose.foundation.gestures.transformable
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableFloatState
import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -17,10 +20,12 @@ import androidx.compose.ui.input.pointer.pointerInput
fun MapCanvas( fun MapCanvas(
backColor: Color, backColor: Color,
gridColor: Color, gridColor: Color,
scale: MutableFloatState,
modifier: Modifier = Modifier modifier: Modifier = Modifier
) { ) {
val offsetX = rememberSaveable { mutableFloatStateOf(0F) } val offsetX = rememberSaveable { mutableFloatStateOf(0F) }
val offsetY = rememberSaveable { mutableFloatStateOf(0F) } val offsetY = rememberSaveable { mutableFloatStateOf(0F) }
Canvas( Canvas(
modifier = modifier.fillMaxSize() modifier = modifier.fillMaxSize()
.pointerInput(Unit) { .pointerInput(Unit) {
@ -39,17 +44,21 @@ fun MapCanvas(
val strippedOffsetX = offsetX.floatValue % TILE_SIZE val strippedOffsetX = offsetX.floatValue % TILE_SIZE
val strippedOffsetY = offsetY.floatValue % TILE_SIZE val strippedOffsetY = offsetY.floatValue % TILE_SIZE
val level = scale.floatValue.toInt()
val strippedScale = scale.floatValue + 1 - level
val actualTileSize = TILE_SIZE * strippedScale
val offset = Offset(strippedOffsetX, strippedOffsetY) val offset = Offset(strippedOffsetX, strippedOffsetY)
val grid = size / TILE_SIZE val grid = size / actualTileSize
val gridWidth = grid.width.toInt() val gridWidth = grid.width.toInt()
val gridHeight = grid.height.toInt() val gridHeight = grid.height.toInt()
for (cellX in 0 .. gridWidth + 2) { for (cellX in 0 .. gridWidth + 2) {
val offsetX = TILE_SIZE * (cellX - 1) val offsetX = actualTileSize * (cellX - 1)
for (cellY in 0 .. gridHeight + 2) { for (cellY in 0 .. gridHeight + 2) {
val offsetY = TILE_SIZE * (cellY - 1) val offsetY = actualTileSize * (cellY - 1)
drawRect( drawRect(
color = gridColor, color = gridColor,
size = Size(TILE_SIZE, TILE_SIZE), size = Size(actualTileSize, actualTileSize),
topLeft = offset + Offset(offsetX, offsetY), topLeft = offset + Offset(offsetX, offsetY),
style = Stroke(width = 4F) style = Stroke(width = 4F)
) )