Fix cleanup

This commit is contained in:
2024-07-24 23:03:11 +02:00
parent 75a0789fb4
commit e673b3cb4d
4 changed files with 18 additions and 9 deletions

View File

@@ -71,9 +71,10 @@ fun PuzzleGrid(
val allOptions = row.options val allOptions = row.options
for (item in row) { for (item in row) {
var selection by remember { mutableStateOf(item.selection) } var selection by remember { mutableStateOf(item.selection) }
val options = remember { allOptions.map { Toggleable(it, item.options.contains(it)) } } val options =
remember { allOptions.map { Toggleable(it, item.options.contains(it)) } }
LaunchedEffect(item) { LaunchedEffect(item) {
item.removedListeners.add { removed -> item.optionsRemovedListeners.add { removed ->
options options
.filter { removed.contains(it.item) } .filter { removed.contains(it.item) }
.forEach { it.enabled = false } .forEach { it.enabled = false }
@@ -88,10 +89,14 @@ fun PuzzleGrid(
.padding(8.dp) .padding(8.dp)
.weight(1f), .weight(1f),
options = options, options = options,
onOptionRemoved = {
item.options.remove(it)
row.cleanupOptions()
},
selectedItem = selection, selectedItem = selection,
onSelectItem = { onSelectItem = {
item.selection = it item.selection = it
grid.cleanupOptions() row.cleanupOptions()
} }
) )
} }

View File

@@ -28,6 +28,7 @@ import kotlin.math.min
fun <C : ItemClass<C>> Selector( fun <C : ItemClass<C>> Selector(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
options: List<Toggleable<Item<C>>>, options: List<Toggleable<Item<C>>>,
onOptionRemoved: (Item<C>?) -> Unit,
selectedItem: Item<C>?, selectedItem: Item<C>?,
onSelectItem: (Item<C>?) -> Unit, onSelectItem: (Item<C>?) -> Unit,
) { ) {
@@ -54,6 +55,7 @@ fun <C : ItemClass<C>> Selector(
matcher = PointerMatcher.mouse(PointerButton.Secondary), matcher = PointerMatcher.mouse(PointerButton.Secondary),
onClick = { onClick = {
option.enabled = false option.enabled = false
onOptionRemoved(option.item)
} }
) )
) )

View File

@@ -6,14 +6,14 @@ class GameCell<C : ItemClass<C>>(
options: Collection<Item<C>> options: Collection<Item<C>>
) { ) {
val selectionChangedListeners = mutableListOf<(Item<C>?) -> Unit>() val selectionChangedListeners = mutableListOf<(Item<C>?) -> Unit>()
val removedListeners = mutableListOf<(Collection<Item<C>>) -> Unit>() val optionsRemovedListeners = mutableListOf<(Collection<Item<C>>) -> Unit>()
var selection: Item<C>? = selection var selection: Item<C>? = selection
set(value) { set(value) {
field = value field = value
selectionChangedListeners.forEach { listener -> listener(value) } selectionChangedListeners.forEach { listener -> listener(value) }
} }
val options = ObservableSet(options.toMutableSet()) { removedListeners.forEach { listener -> listener(it) } } val options = ObservableSet(options.toMutableSet()) { optionsRemovedListeners.forEach { listener -> listener(it) } }
} }
fun <C : ItemClass<C>> GameCell<C>?.mayBe(item: Item<C>, mayHaveSelection: Boolean = true) = fun <C : ItemClass<C>> GameCell<C>?.mayBe(item: Item<C>, mayHaveSelection: Boolean = true) =

View File

@@ -11,12 +11,14 @@ class GameRow<C : ItemClass<C>>(
cells.forEach { cleanupOptions(it) } cells.forEach { cleanupOptions(it) }
} }
private fun cleanupOptions(cell: GameCell<C>) { private fun cleanupOptions(cell: GameCell<C>, firstCall: Boolean = true) {
if (cell.options.size == 1 && cell.selection == null) { if ((firstCall && cell.selection != null) || (cell.options.size == 1 && cell.selection == null)) {
if (cell.selection == null) {
cell.selection = cell.options.first() cell.selection = cell.options.first()
}
filter { otherCell -> otherCell != cell && otherCell.selection == null }.forEach { otherCell -> filter { otherCell -> otherCell != cell && otherCell.selection == null }.forEach { otherCell ->
otherCell.options.remove(cell.selection) otherCell.options.remove(cell.selection)
cleanupOptions(otherCell) cleanupOptions(otherCell, false)
} }
} }
} }