feat: mapping des touches clavier vers les actions du pavé
This commit is contained in:
2 files changed
+102
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user