From 80add34b15855932e9201d7426d9df01aa82c845 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 14 Dec 2024 11:23:35 +0530 Subject: Add key sequence filtering and rendering matched keys --- src/Chelleport/KeySequence.hs | 80 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 src/Chelleport/KeySequence.hs (limited to 'src/Chelleport/KeySequence.hs') diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs new file mode 100644 index 0000000..1d118cd --- /dev/null +++ b/src/Chelleport/KeySequence.hs @@ -0,0 +1,80 @@ +module Chelleport.KeySequence where + +import Data.List (isPrefixOf, nub) +import qualified Data.Map as Map +import qualified SDL + +safeHead :: a -> [a] -> a +safeHead def [] = def +safeHead _ (x : _) = x + +nextChars :: [Char] -> [[[Char]]] -> Maybe [Char] +nextChars keys cells = + case matches of + [] -> Nothing + _ -> Just $ nub result + where + matches = concatMap (filter (isPrefixOf keys)) cells + result = concatMap (take 1 . drop (length keys)) matches + +isValidKey :: SDL.Keycode -> Bool +isValidKey key = Map.member key keycodeMapping + +generateKeyCells :: (Int, Int) -> [Char] -> [[[Char]]] +generateKeyCells (rows, columns) hintKeys = + (\row -> getCellSeq row <$> [1 .. columns]) <$> [1 .. rows] + where + getCellSeq x y = [getPrefix1 x y, getPrefix2 x y, getKey x y] + getKey _row col = hintKeys !! (col `mod` (columns `div` 2)) + getPrefix1 _row col + | col <= (columns `div` 2) = 'H' + | otherwise = 'L' + getPrefix2 row _col + | row <= (rows `div` 2) = 'K' + | otherwise = 'J' + +toKeyChar :: SDL.Keycode -> Maybe Char +toKeyChar key = Map.lookup key keycodeMapping + +eventToKeycode :: SDL.KeyboardEventData -> SDL.Keycode +eventToKeycode = SDL.keysymKeycode . SDL.keyboardEventKeysym + +keycodeMapping :: Map.Map SDL.Keycode Char +keycodeMapping = + Map.fromList + [ (SDL.KeycodeA, 'A'), + (SDL.KeycodeB, 'B'), + (SDL.KeycodeC, 'C'), + (SDL.KeycodeD, 'D'), + (SDL.KeycodeE, 'E'), + (SDL.KeycodeF, 'F'), + (SDL.KeycodeG, 'G'), + (SDL.KeycodeH, 'H'), + (SDL.KeycodeI, 'I'), + (SDL.KeycodeJ, 'J'), + (SDL.KeycodeK, 'K'), + (SDL.KeycodeL, 'L'), + (SDL.KeycodeM, 'M'), + (SDL.KeycodeN, 'N'), + (SDL.KeycodeO, 'O'), + (SDL.KeycodeP, 'P'), + (SDL.KeycodeR, 'R'), + (SDL.KeycodeS, 'S'), + (SDL.KeycodeT, 'T'), + (SDL.KeycodeU, 'U'), + (SDL.KeycodeV, 'V'), + (SDL.KeycodeW, 'W'), + (SDL.KeycodeX, 'X'), + (SDL.KeycodeY, 'Y'), + (SDL.KeycodeZ, 'Z'), + (SDL.Keycode0, '0'), + (SDL.Keycode1, '1'), + (SDL.Keycode2, '2'), + (SDL.Keycode3, '3'), + (SDL.Keycode4, '4'), + (SDL.Keycode5, '5'), + (SDL.Keycode6, '6'), + (SDL.Keycode7, '7'), + (SDL.Keycode8, '8'), + (SDL.Keycode9, '9') + ] -- cgit v1.3.1