logicgate: show the trash zone while pulling a chip out of the tray
- Commit
dd5088c3a0ab36f2b2c22f179f8671be86b2854b- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-01 18:35 UTC (1 month ago)
- Parents
835f3a6
When a chip was pulled up out of the tray, the bottom area gave no feedback until a gate already on the board was being carried, so it was unclear that letting go over the tray would place nothing. Now pulling a chip out arms the same trash zone a carried gate uses: the bottom area turns red with "Release to delete" from the moment the chip is picked up, switches to "Drag here to delete" once the finger is over the board, and back (with a haptic tick) if it returns. Dropping over the bottom area still places nothing; dropping on the board places the chip as before. Implementation: TrashZone.carry(root) tracks a tray drag in root coordinates and reports re-entry; GameScreen feeds it from the tray's drag updates and clears it when the drag ends. Verified with a new CanvasControllerTest case (armed and hovering on pickup, not hovering over the board, re-entry reported, cleared on release), ./gradlew :games:logicgate:testDevUnitTest and ./check games/logicgate.
Diff
games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/GameScreen.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/GameScreen.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/GameScreen.ktindex 2f1f888d21..eede435d26 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/GameScreen.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/GameScreen.kt@@ -45,6 +45,7 @@ import com.vayunmathur.library.ui.Spacing import com.vayunmathur.library.ui.Surface import com.vayunmathur.library.ui.Text import com.vayunmathur.library.ui.appBarScrollBehavior+import com.vayunmathur.library.ui.rememberHaptics import kotlin.math.roundToInt /**@@ -65,6 +66,7 @@ fun GameScreen( val number = LevelRegistry.indexOf(state.level.id) + 1 var trayDrag by remember { mutableStateOf<TrayDrag?>(null) } val trash = remember { TrashZone() }+ val haptics = rememberHaptics() var rootOrigin by remember { mutableStateOf(Offset.Zero) } var canvasBounds by remember { mutableStateOf(Rect.Zero) } @@ -108,7 +110,10 @@ fun GameScreen( ChipTray( chips = state.chips, onTap = { placeNear(it, cameraState.centerWorld()) },- onDragUpdate = { trayDrag = it },+ onDragUpdate = { drag ->+ trayDrag = drag+ if (trash.carry(drag?.root)) haptics.tick()+ }, onDrop = { drop -> if (canvasBounds.contains(drop.root)) { placeNear(drop.chipId, cameraState.camera.toWorld(drop.root - canvasBounds.topLeft))
games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/canvas/TrashZone.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/canvas/TrashZone.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/canvas/TrashZone.ktindex 325df4e257..80e27c2230 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/canvas/TrashZone.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/ui/canvas/TrashZone.kt@@ -9,7 +9,8 @@ import androidx.compose.ui.geometry.Rect /** * The area under the board that a dragged gate can be dropped on to delete it. [armed] while a- * gate is being carried, [hovering] while it is over the zone.+ * gate (or a chip just pulled from the tray) is being carried, [hovering] while it is over the+ * zone, where letting go places nothing. */ @Stable class TrashZone {@@ -23,4 +24,16 @@ class TrashZone { var hovering by mutableStateOf(false) fun contains(canvasPoint: Offset): Boolean = bounds.contains(canvasPoint + canvasOrigin)++ /**+ * Tracks a chip dragged out of the tray at [root] (root coordinates; null when the drag ends).+ * Returns true when it has just moved back over the zone.+ */+ fun carry(root: Offset?): Boolean {+ val over = root != null && bounds.contains(root)+ val entered = over && !hovering && armed+ armed = root != null+ hovering = over+ return entered+ } }
games/logicgate/src/test/java/com/vayunmathur/games/logicgate/ui/canvas/CanvasControllerTest.kt
diff --git a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/ui/canvas/CanvasControllerTest.kt b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/ui/canvas/CanvasControllerTest.ktindex 0edbb96a6c..2b9566d1a7 100644--- a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/ui/canvas/CanvasControllerTest.kt+++ b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/ui/canvas/CanvasControllerTest.kt@@ -63,6 +63,21 @@ class CanvasControllerTest { assertEquals(listOf(gate), actions.moved) } + @Test+ fun aChipPulledFromTheTrayShowsTheTrashUntilItLeavesTheBottomArea() {+ val trash = TrashZone().apply { bounds = Rect(0f, trayTop, 2000f, 1400f) }+ assertFalse(trash.carry(Offset(300f, trayTop + 50f)), "picking up is not re-entering")+ assertTrue(trash.armed)+ assertTrue(trash.hovering)+ trash.carry(Offset(300f, 400f))+ assertTrue(trash.armed)+ assertFalse(trash.hovering)+ assertTrue(trash.carry(Offset(300f, trayTop + 10f)), "coming back over the tray")+ trash.carry(null)+ assertFalse(trash.armed)+ assertFalse(trash.hovering)+ }+ @Test fun levelPinsCannotBeThrownAway() { val (controller, actions, circuit) = setup()