diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-14 13:04:36 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-14 13:04:36 +0530 |
| commit | 9368c616e202fc67c15d6128a2110d05998f4290 (patch) | |
| tree | ba30236841d12e24e9b8b84b42579d2148c1445d /src/Chelleport.hs | |
| parent | 7ab99bd80e30d0cc2cf21e4cda47870821b8fd47 (diff) | |
| download | chelleport-9368c616e202fc67c15d6128a2110d05998f4290.tar.gz chelleport-9368c616e202fc67c15d6128a2110d05998f4290.zip | |
Move the mouse to the cell position after key sequence match
Diffstat (limited to 'src/Chelleport.hs')
| -rw-r--r-- | src/Chelleport.hs | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/src/Chelleport.hs b/src/Chelleport.hs index 74c61e7..4f6aa7b 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -2,11 +2,12 @@ module Chelleport where import Chelleport.AppShell (Action (AppAction, SysQuit), DrawContext (ctxWindow), setupAppShell) import Chelleport.Draw (colorLightGray, colorWhite, renderText) -import Chelleport.KeySequence (eventToKeycode, generateKeyCells, isValidKey, nextChars, toKeyChar) +import Chelleport.KeySequence (eventToKeycode, findMatchPosition, generateKeyCells, isValidKey, nextChars, toKeyChar) import Control.Monad (forM_, unless, void) import Data.IORef (modifyIORef', newIORef, readIORef) import Data.List (isPrefixOf) import qualified Data.Text as Text +import Foreign.C (CInt (CInt)) import qualified SDL import Unsafe.Coerce (unsafeCoerce) @@ -20,12 +21,6 @@ data AppAction = FilterSequence SDL.Keycode | SetupGrid open :: IO () open = setupAppShell initialState update eventToAction render -padded :: Int -> a -> [a] -> [a] -padded 0 _ ls = ls -padded n x ls - | length ls > n = ls - | otherwise = padded (n - 1) x (ls ++ [x]) - initialState :: DrawContext -> IO State initialState _ctx = do let cells = generateKeyCells (rows, columns) hintKeys @@ -64,11 +59,22 @@ render state ctx = do update :: State -> DrawContext -> AppAction -> IO State update state _ctx SetupGrid = pure state -update state _ctx (FilterSequence key) = +update state ctx (FilterSequence key) = case validChars >>= (\chars -> (,chars) <$> toKeyChar key) of Just (keyChar, validChars') - | keyChar `elem` validChars' -> - pure state {stateKeySequence = stateKeySequence state ++ [keyChar]} + | keyChar `elem` validChars' -> do + (SDL.V2 width height) <- SDL.get $ SDL.windowSize $ ctxWindow ctx :: IO (SDL.V2 CInt) + let newKeySequence = stateKeySequence state ++ [keyChar] + let rows = stateCells state + let wcell = width `div` unsafeCoerce (length $ head rows) + let hcell = height `div` unsafeCoerce (length rows) + case findMatchPosition newKeySequence rows of + Just (row, col) -> do + let x = wcell * unsafeCoerce col + let y = hcell * unsafeCoerce row + SDL.warpMouse SDL.WarpGlobal (SDL.P $ SDL.V2 x y) + Nothing -> pure () + pure state {stateKeySequence = newKeySequence} _ -> pure state where validChars = nextChars (stateKeySequence state) (stateCells state) |
