Add improvements
almost playable
This commit is contained in:
@@ -2,34 +2,16 @@ package ch.dissem.yaep.ui.common
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.wrapContentHeight
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.material3.HorizontalDivider
|
||||
import androidx.compose.material3.OutlinedCard
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.unit.dp
|
||||
import ch.dissem.yaep.domain.Clue
|
||||
import ch.dissem.yaep.domain.Grid
|
||||
import ch.dissem.yaep.domain.HorizontalClue
|
||||
import ch.dissem.yaep.domain.ItemClass
|
||||
import ch.dissem.yaep.domain.NeighbourClue
|
||||
import ch.dissem.yaep.domain.OrderClue
|
||||
import ch.dissem.yaep.domain.SameColumnClue
|
||||
import ch.dissem.yaep.domain.TripletClue
|
||||
import ch.dissem.yaep.domain.generateGame
|
||||
import ch.dissem.yaep.domain.*
|
||||
import org.jetbrains.compose.resources.painterResource
|
||||
import yaep.commonui.generated.resources.Res
|
||||
import yaep.commonui.generated.resources.neighbour
|
||||
@@ -66,15 +48,30 @@ fun PuzzleGrid(
|
||||
.fillMaxWidth()
|
||||
.wrapContentHeight()
|
||||
) {
|
||||
val allOptions = row.options
|
||||
for (item in row) {
|
||||
var selection by remember { mutableStateOf(item.selection) }
|
||||
val options = remember { allOptions.map { Toggleable(it, item.options.contains(it)) } }
|
||||
LaunchedEffect(item) {
|
||||
item.removedListeners.add { removed ->
|
||||
options
|
||||
.filter { removed.contains(it.item) }
|
||||
.forEach { it.enabled = false }
|
||||
}
|
||||
item.selectionChangedListeners.add {
|
||||
selection = it
|
||||
}
|
||||
}
|
||||
Selector(
|
||||
modifier = Modifier
|
||||
.padding(8.dp)
|
||||
.weight(1f),
|
||||
category = row.category,
|
||||
options = item.options,
|
||||
selectedItem = item.selection,
|
||||
onSelectItem = { item.selection = it }
|
||||
options = options,
|
||||
selectedItem = selection,
|
||||
onSelectItem = {
|
||||
item.selection = it
|
||||
grid.cleanupOptions()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -169,4 +166,4 @@ fun VerticalClue(modifier: Modifier = Modifier, clue: SameColumnClue<*, *>) {
|
||||
DrawItem(modifier = Modifier.weight(1f), clue.b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package ch.dissem.yaep.ui.common
|
||||
|
||||
import androidx.compose.foundation.Canvas
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.*
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.aspectRatio
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@@ -9,24 +8,28 @@ import androidx.compose.foundation.lazy.grid.GridCells
|
||||
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
|
||||
import androidx.compose.material3.OutlinedCard
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.input.pointer.PointerButton
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.drawText
|
||||
import androidx.compose.ui.text.rememberTextMeasurer
|
||||
import ch.dissem.yaep.domain.Item
|
||||
import ch.dissem.yaep.domain.ItemClass
|
||||
import ch.dissem.yaep.domain.ItemClassCompanion
|
||||
import ch.dissem.yaep.ui.common.theme.emojiFontFamily
|
||||
import kotlin.math.min
|
||||
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
@Composable
|
||||
fun <C : ItemClass<C>> Selector(
|
||||
modifier: Modifier = Modifier,
|
||||
category: ItemClassCompanion<C>,
|
||||
options: List<Item<C>>,
|
||||
options: List<Toggleable<Item<C>>>,
|
||||
selectedItem: Item<C>?,
|
||||
onSelectItem: (Item<C>?) -> Unit
|
||||
onSelectItem: (Item<C>?) -> Unit,
|
||||
) {
|
||||
if (selectedItem != null) {
|
||||
DrawItem(item = selectedItem, modifier = modifier.clickable { onSelectItem(null) })
|
||||
@@ -40,8 +43,19 @@ fun <C : ItemClass<C>> Selector(
|
||||
for (option in options) {
|
||||
item {
|
||||
DrawItem(
|
||||
item = option,
|
||||
modifier = Modifier.clickable { onSelectItem(option) }
|
||||
item = option.item,
|
||||
modifier = Modifier
|
||||
.alpha(if (option.enabled) 1f else 0.1f)
|
||||
.combinedClickable(
|
||||
onClick = { onSelectItem(option.item) },
|
||||
onLongClick = { option.enabled = false }
|
||||
)
|
||||
.onClick(
|
||||
matcher = PointerMatcher.mouse(PointerButton.Secondary),
|
||||
onClick = {
|
||||
option.enabled = false
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -81,7 +95,10 @@ fun <C : ItemClass<C>> DrawItem(
|
||||
drawText(
|
||||
textMeasurer = textMeasurer,
|
||||
text = emoji,
|
||||
style = TextStyle(fontSize = fontSize, fontFamily = emojiFontFamily ?: TextStyle.Default.fontFamily),
|
||||
style = TextStyle(
|
||||
fontSize = fontSize,
|
||||
fontFamily = emojiFontFamily ?: TextStyle.Default.fontFamily
|
||||
),
|
||||
topLeft = offset
|
||||
)
|
||||
}
|
||||
@@ -89,3 +106,23 @@ fun <C : ItemClass<C>> DrawItem(
|
||||
}
|
||||
}
|
||||
|
||||
class Toggleable<T>(val item: T, enabled: Boolean = true) {
|
||||
|
||||
var enabled: Boolean by mutableStateOf(enabled)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (other !is Toggleable<*>) return false
|
||||
|
||||
if (item != other.item) return false
|
||||
if (enabled != other.enabled) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = item.hashCode()
|
||||
result = 31 * result + enabled.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user