2024-06-07 20:20:39 +03:00
|
|
|
@file:OptIn(ExperimentalFoundationApi::class, ExperimentalSplitPaneApi::class)
|
|
|
|
|
|
|
|
package space.kscience.controls.demo.collective
|
|
|
|
|
|
|
|
import androidx.compose.foundation.*
|
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
|
import androidx.compose.foundation.layout.Row
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
import androidx.compose.material.Card
|
|
|
|
import androidx.compose.material.Checkbox
|
|
|
|
import androidx.compose.material.Text
|
2024-06-06 16:54:17 +03:00
|
|
|
import androidx.compose.material3.MaterialTheme
|
2024-06-07 20:20:39 +03:00
|
|
|
import androidx.compose.runtime.*
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
import androidx.compose.ui.Modifier
|
2024-06-06 16:54:17 +03:00
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
|
import androidx.compose.ui.res.painterResource
|
2024-06-07 20:20:39 +03:00
|
|
|
import androidx.compose.ui.text.font.FontWeight
|
|
|
|
import androidx.compose.ui.unit.DpSize
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
import androidx.compose.ui.unit.sp
|
2024-06-06 16:54:17 +03:00
|
|
|
import androidx.compose.ui.window.Window
|
|
|
|
import androidx.compose.ui.window.application
|
|
|
|
import io.ktor.client.HttpClient
|
|
|
|
import io.ktor.client.engine.cio.CIO
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
2024-06-07 20:20:39 +03:00
|
|
|
import org.jetbrains.compose.splitpane.ExperimentalSplitPaneApi
|
|
|
|
import org.jetbrains.compose.splitpane.HorizontalSplitPane
|
|
|
|
import org.jetbrains.compose.splitpane.rememberSplitPaneState
|
|
|
|
import space.kscience.controls.compose.conditional
|
2024-06-06 16:54:17 +03:00
|
|
|
import space.kscience.controls.manager.DeviceManager
|
2024-06-07 20:20:39 +03:00
|
|
|
import space.kscience.controls.spec.useProperty
|
2024-06-06 16:54:17 +03:00
|
|
|
import space.kscience.dataforge.context.Context
|
|
|
|
import space.kscience.dataforge.context.request
|
|
|
|
import space.kscience.maps.compose.MapView
|
|
|
|
import space.kscience.maps.compose.OpenStreetMapTileProvider
|
2024-06-07 20:20:39 +03:00
|
|
|
import space.kscience.maps.features.*
|
2024-06-06 16:54:17 +03:00
|
|
|
import java.nio.file.Path
|
|
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun rememberDeviceManager(): DeviceManager = remember {
|
|
|
|
val context = Context {
|
|
|
|
plugin(DeviceManager)
|
|
|
|
}
|
|
|
|
|
|
|
|
context.request(DeviceManager)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun App() {
|
|
|
|
val scope = rememberCoroutineScope()
|
|
|
|
|
|
|
|
|
|
|
|
val deviceManager = rememberDeviceManager()
|
|
|
|
|
|
|
|
|
|
|
|
val collectiveModel = remember {
|
|
|
|
generateModel(deviceManager.context)
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:20:39 +03:00
|
|
|
val devices: Map<DeviceId, CollectiveDevice> = remember {
|
2024-06-06 16:54:17 +03:00
|
|
|
collectiveModel.deviceStates.associate {
|
2024-06-07 20:20:39 +03:00
|
|
|
val device = CollectiveDeviceConstructor(
|
|
|
|
context = deviceManager.context,
|
|
|
|
configuration = it.configuration,
|
|
|
|
position = it.position,
|
|
|
|
velocity = it.velocity
|
|
|
|
) {
|
|
|
|
collectiveModel.locateVisible(it.id).keys
|
|
|
|
}
|
2024-06-06 16:54:17 +03:00
|
|
|
device.moveInCircles()
|
|
|
|
it.id to device
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:20:39 +03:00
|
|
|
var selectedDeviceId by remember { mutableStateOf<DeviceId?>(null) }
|
|
|
|
var showOnlyVisible by remember { mutableStateOf(false) }
|
|
|
|
|
2024-06-06 16:54:17 +03:00
|
|
|
val mapTileProvider = remember {
|
|
|
|
OpenStreetMapTileProvider(
|
|
|
|
client = HttpClient(CIO),
|
|
|
|
cacheDirectory = Path.of("mapCache")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:20:39 +03:00
|
|
|
HorizontalSplitPane(
|
|
|
|
splitPaneState = rememberSplitPaneState(0.9f)
|
2024-06-06 16:54:17 +03:00
|
|
|
) {
|
2024-06-07 20:20:39 +03:00
|
|
|
first(400.dp) {
|
|
|
|
MapView(
|
|
|
|
mapTileProvider = mapTileProvider,
|
|
|
|
config = ViewConfig()
|
|
|
|
) {
|
|
|
|
collectiveModel.deviceStates.forEach { device ->
|
|
|
|
circle(device.position.value, id = device.id + ".position").color(Color.Red)
|
|
|
|
device.position.valueFlow.onEach {
|
|
|
|
circle(device.position.value, id = device.id + ".position", size = 3.dp)
|
|
|
|
.color(Color.Red)
|
|
|
|
.modifyAttribute(AlphaAttribute, 0.5f) // does not work right now
|
|
|
|
}.launchIn(scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
devices.forEach { (id, device) ->
|
|
|
|
device.useProperty(CollectiveDevice.position, scope = scope) { position ->
|
|
|
|
|
|
|
|
val activeDevice = selectedDeviceId?.let { devices[it] }
|
|
|
|
|
|
|
|
if (activeDevice == null || id == selectedDeviceId || !showOnlyVisible || id in activeDevice.listVisible()) {
|
|
|
|
rectangle(
|
|
|
|
position,
|
|
|
|
id = id,
|
|
|
|
size = if (selectedDeviceId == id) DpSize(10.dp, 10.dp) else DpSize(5.dp, 5.dp)
|
|
|
|
).color(if (selectedDeviceId == id) Color.Magenta else Color.Blue)
|
|
|
|
.modifyAttribute(AlphaAttribute, if (selectedDeviceId == id) 1f else 0.5f)
|
|
|
|
.onClick { selectedDeviceId = id }
|
|
|
|
} else {
|
|
|
|
removeFeature(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-06 16:54:17 +03:00
|
|
|
}
|
2024-06-07 20:20:39 +03:00
|
|
|
second(200.dp) {
|
|
|
|
Column {
|
|
|
|
selectedDeviceId?.let { id ->
|
|
|
|
Column(
|
|
|
|
modifier = Modifier
|
|
|
|
.padding(8.dp)
|
|
|
|
.border(2.dp, Color.DarkGray)
|
|
|
|
) {
|
|
|
|
Card(
|
|
|
|
elevation = 16.dp,
|
|
|
|
) {
|
|
|
|
Text(
|
|
|
|
text = "Выбран: $id",
|
|
|
|
fontSize = 16.sp,
|
|
|
|
fontWeight = FontWeight.Bold,
|
|
|
|
modifier = Modifier.padding(10.dp).fillMaxWidth()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
devices[id]?.let {
|
|
|
|
Text(it.meta.toString(), Modifier.padding(10.dp))
|
|
|
|
}
|
|
|
|
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(10.dp).fillMaxWidth()) {
|
|
|
|
Text("Показать только видимые")
|
|
|
|
Checkbox(showOnlyVisible, { showOnlyVisible = it })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Column(
|
|
|
|
modifier = Modifier.verticalScroll(rememberScrollState())
|
|
|
|
) {
|
|
|
|
devices.forEach { (id, device) ->
|
|
|
|
Card(
|
|
|
|
elevation = 16.dp,
|
|
|
|
modifier = Modifier.padding(8.dp).onClick {
|
|
|
|
selectedDeviceId = id
|
|
|
|
}.conditional(id == selectedDeviceId) {
|
|
|
|
border(2.dp, Color.Blue)
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
Text(
|
|
|
|
text = id,
|
|
|
|
fontSize = 16.sp,
|
|
|
|
fontWeight = FontWeight.Bold,
|
|
|
|
modifier = Modifier.padding(10.dp).fillMaxWidth()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-06 16:54:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun main() = application {
|
|
|
|
Window(onCloseRequest = ::exitApplication, title = "Maps-kt demo", icon = painterResource("SPC-logo.png")) {
|
|
|
|
MaterialTheme {
|
|
|
|
App()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|