From 75033324b977362f9d514c181515784eb1206b2b Mon Sep 17 00:00:00 2001 From: Rene Luria Date: Mon, 31 Aug 2026 09:58:26 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20mapping=20des=20touches=20clavier=20ver?= =?UTF-8?q?s=20les=20actions=20du=20pav=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/livrets/ui/GameKeyMapping.kt | 41 +++++++++++++ .../com/livrets/ui/GameKeyMappingTest.kt | 61 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 sharedUI/src/commonMain/kotlin/com/livrets/ui/GameKeyMapping.kt create mode 100644 sharedUI/src/commonTest/kotlin/com/livrets/ui/GameKeyMappingTest.kt diff --git a/sharedUI/src/commonMain/kotlin/com/livrets/ui/GameKeyMapping.kt b/sharedUI/src/commonMain/kotlin/com/livrets/ui/GameKeyMapping.kt new file mode 100644 index 0000000..3b8b1c7 --- /dev/null +++ b/sharedUI/src/commonMain/kotlin/com/livrets/ui/GameKeyMapping.kt @@ -0,0 +1,41 @@ +package com.livrets.ui + +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType + +sealed interface KeypadAction { + data class Digit(val char: Char) : KeypadAction + data object Delete : KeypadAction + data object Validate : KeypadAction +} + +object GameKeyMapping { + fun actionFor(key: Key, type: KeyEventType): KeypadAction? { + if (type != KeyEventType.KeyDown) return null + return when (key) { + Key.Zero -> KeypadAction.Digit('0') + Key.One -> KeypadAction.Digit('1') + Key.Two -> KeypadAction.Digit('2') + Key.Three -> KeypadAction.Digit('3') + Key.Four -> KeypadAction.Digit('4') + Key.Five -> KeypadAction.Digit('5') + Key.Six -> KeypadAction.Digit('6') + Key.Seven -> KeypadAction.Digit('7') + Key.Eight -> KeypadAction.Digit('8') + Key.Nine -> KeypadAction.Digit('9') + Key.NumPad0 -> KeypadAction.Digit('0') + Key.NumPad1 -> KeypadAction.Digit('1') + Key.NumPad2 -> KeypadAction.Digit('2') + Key.NumPad3 -> KeypadAction.Digit('3') + Key.NumPad4 -> KeypadAction.Digit('4') + Key.NumPad5 -> KeypadAction.Digit('5') + Key.NumPad6 -> KeypadAction.Digit('6') + Key.NumPad7 -> KeypadAction.Digit('7') + Key.NumPad8 -> KeypadAction.Digit('8') + Key.NumPad9 -> KeypadAction.Digit('9') + Key.Enter, Key.NumPadEnter -> KeypadAction.Validate + Key.Backspace -> KeypadAction.Delete + else -> null + } + } +} diff --git a/sharedUI/src/commonTest/kotlin/com/livrets/ui/GameKeyMappingTest.kt b/sharedUI/src/commonTest/kotlin/com/livrets/ui/GameKeyMappingTest.kt new file mode 100644 index 0000000..6a21ef0 --- /dev/null +++ b/sharedUI/src/commonTest/kotlin/com/livrets/ui/GameKeyMappingTest.kt @@ -0,0 +1,61 @@ +package com.livrets.ui + +import androidx.compose.ui.input.key.Key +import androidx.compose.ui.input.key.KeyEventType +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class GameKeyMappingTest { + + @Test + fun `chiffres de la rangee du haut`() { + val expected = mapOf( + Key.Zero to '0', Key.One to '1', Key.Two to '2', Key.Three to '3', + Key.Four to '4', Key.Five to '5', Key.Six to '6', Key.Seven to '7', + Key.Eight to '8', Key.Nine to '9', + ) + for ((key, char) in expected) { + assertEquals(KeypadAction.Digit(char), GameKeyMapping.actionFor(key, KeyEventType.KeyDown)) + } + } + + @Test + fun `chiffres du pave numerique`() { + val expected = mapOf( + Key.NumPad0 to '0', Key.NumPad1 to '1', Key.NumPad2 to '2', Key.NumPad3 to '3', + Key.NumPad4 to '4', Key.NumPad5 to '5', Key.NumPad6 to '6', Key.NumPad7 to '7', + Key.NumPad8 to '8', Key.NumPad9 to '9', + ) + for ((key, char) in expected) { + assertEquals(KeypadAction.Digit(char), GameKeyMapping.actionFor(key, KeyEventType.KeyDown)) + } + } + + @Test + fun `entree valide la reponse`() { + assertEquals(KeypadAction.Validate, GameKeyMapping.actionFor(Key.Enter, KeyEventType.KeyDown)) + } + + @Test + fun `entree du pave numerique valide la reponse`() { + assertEquals(KeypadAction.Validate, GameKeyMapping.actionFor(Key.NumPadEnter, KeyEventType.KeyDown)) + } + + @Test + fun `backspace efface`() { + assertEquals(KeypadAction.Delete, GameKeyMapping.actionFor(Key.Backspace, KeyEventType.KeyDown)) + } + + @Test + fun `relachement de touche ignore`() { + assertNull(GameKeyMapping.actionFor(Key.One, KeyEventType.KeyUp)) + } + + @Test + fun `touches non gerees ignorees`() { + for (key in listOf(Key.A, Key.Escape, Key.Spacebar, Key.Tab)) { + assertNull(GameKeyMapping.actionFor(key, KeyEventType.KeyDown)) + } + } +}