diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-21 22:18:50 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-21 22:25:42 +0530 |
| commit | f51fbc728e4b731372e39ba19c38a35ef58fe71a (patch) | |
| tree | ada2dbc2a16b6df46d446cfd9fc787d4c5a18879 /src | |
| parent | 1ade68e7252dda79c365cc9ec187c2bea3513cae (diff) | |
| download | chelleport-f51fbc728e4b731372e39ba19c38a35ef58fe71a.tar.gz chelleport-f51fbc728e4b731372e39ba19c38a35ef58fe71a.zip | |
Add action repetition
Diffstat (limited to '')
| -rw-r--r-- | src/Chelleport.hs | 39 | ||||
| -rw-r--r-- | src/Chelleport/Config.hs | 2 | ||||
| -rw-r--r-- | src/Chelleport/Context.hs | 1 | ||||
| -rw-r--r-- | src/Chelleport/KeySequence.hs | 43 | ||||
| -rw-r--r-- | src/Chelleport/Types.hs | 4 |
5 files changed, 57 insertions, 32 deletions
diff --git a/src/Chelleport.hs b/src/Chelleport.hs index df3433e..9b96bbc 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -13,10 +13,11 @@ import Chelleport.Control withShift, ) import Chelleport.Draw (MonadDraw (windowPosition), cellSize) -import Chelleport.KeySequence (findMatchPosition, generateGrid, isValidKey, nextChars, toKeyChar) +import Chelleport.KeySequence (findMatchPosition, generateGrid, isKeycodeDigit, isValidKey, keycodeToInt, nextChars, toKeyChar) import Chelleport.Types import Chelleport.Utils (intToCInt) import qualified Chelleport.View +import Control.Monad (forM_) import Control.Monad.IO.Class (MonadIO) import Control.Monad.Reader (ReaderT (runReaderT)) import Data.Maybe (fromMaybe, isJust) @@ -45,7 +46,8 @@ initialState = do stateKeySequence = [], stateIsMatched = False, stateIsShiftPressed = False, - stateIsDragging = False + stateIsDragging = False, + stateRepetition = 1 } where rows = 9 @@ -63,6 +65,8 @@ eventHandler event = if withShift ev then Just $ ChainMouseClick RightClick else Just $ TriggerMouseClick RightClick + | isKeycodeDigit (eventToKeycode ev) -> + Just $ UpdateRepetition (fromMaybe 0 $ keycodeToInt $ eventToKeycode ev) | isKeyPressWith ev SDL.KeycodeSpace -> if withShift ev then Just $ ChainMouseClick LeftClick @@ -105,8 +109,9 @@ update state (HandleKeyInput key) = do -- Move mouse incrementally update state (IncrementMouseCursor (incX, incY)) = do (curX, curY) <- getMousePointerPosition - moveMousePointer (curX + intToCInt incX) (curY + intToCInt incY) - pure (state, Nothing) + let count = intToCInt $ case stateRepetition state of 0 -> 1; n -> n + moveMousePointer (curX + count * intToCInt incX) (curY + count * intToCInt incY) + pure (state {stateRepetition = 1}, Nothing) -- Move mouse to given position update state (MoveMousePosition (row, col)) = do @@ -123,20 +128,24 @@ update state (MoveMousePosition (row, col)) = do -- Reset entered key sequence and state update state ResetKeys = do - pure (state {stateKeySequence = [], stateIsMatched = False}, Nothing) + pure (state {stateKeySequence = [], stateIsMatched = False, stateRepetition = 1}, Nothing) -- Trigger click update state (TriggerMouseClick btn) = do hideWindow - clickMouseButton btn - pure (state, Just ShutdownApp) + let count = case stateRepetition state of 0 -> 1; n -> n + forM_ [1 .. count] $ \_ -> do + clickMouseButton btn + pure (state {stateRepetition = 1}, Just ShutdownApp) -- Chain clicks update state (ChainMouseClick btn) = do hideWindow - clickMouseButton btn + let count = case stateRepetition state of 0 -> 1; n -> n + forM_ [1 .. count] $ \_ -> do + clickMouseButton btn showWindow - pure (state, Just ResetKeys) + pure (state {stateRepetition = 1}, Just ResetKeys) -- Cleanup everything and exit update state ShutdownApp = do @@ -146,21 +155,25 @@ update state ShutdownApp = do -- Mouse dragging update state MouseDragToggle | stateIsDragging state = pure (state {stateIsDragging = False}, Just MouseDragEnd) - | otherwise = pure (state {stateIsDragging = True}, Just MouseDragStart) --- + | otherwise = do pure (state {stateIsDragging = True}, Just MouseDragStart) + -- Mouse button press update state MouseDragStart = do hideWindow pressMouseButton showWindow - pure (state, Nothing) + pure (state {stateRepetition = 1}, Nothing) -- Mouse button release update state MouseDragEnd = do hideWindow releaseMouseButton showWindow - pure (state, Nothing) + pure (state {stateRepetition = 1}, Nothing) + +-- Set/unset whether shift is pressed +update state (UpdateRepetition count) = do + pure (state {stateRepetition = count}, Nothing) -- Set/unset whether shift is pressed update state (UpdateShiftState shift) = diff --git a/src/Chelleport/Config.hs b/src/Chelleport/Config.hs index 65b7077..d8b4132 100644 --- a/src/Chelleport/Config.hs +++ b/src/Chelleport/Config.hs @@ -35,7 +35,7 @@ colorFineGrainGrid :: Color colorFineGrainGrid = SDL.V4 55 52 65 100 windowOpacity :: CFloat -windowOpacity = 0.4 +windowOpacity = 0.5 fontSize :: Int fontSize = 24 diff --git a/src/Chelleport/Context.hs b/src/Chelleport/Context.hs index b721812..45c90d4 100644 --- a/src/Chelleport/Context.hs +++ b/src/Chelleport/Context.hs @@ -7,7 +7,6 @@ import Chelleport.Config import Chelleport.Types import Data.ByteString (ByteString) import Data.FileEmbed (embedFileRelative) -import Foreign.C (CFloat) import qualified Graphics.X11 as X11 import SDL (($=)) import qualified SDL diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs index dc5f603..9f1f26a 100644 --- a/src/Chelleport/KeySequence.hs +++ b/src/Chelleport/KeySequence.hs @@ -3,8 +3,9 @@ module Chelleport.KeySequence where import Chelleport.Types (KeyGrid, KeySequence) import Chelleport.Utils (findWithIndex, uniq) import Control.Monad (guard) -import Data.List (isPrefixOf) +import Data.List (elemIndex, isPrefixOf) import qualified Data.Map as Map +import Data.Maybe (fromMaybe, isJust) import qualified SDL nextChars :: KeySequence -> KeyGrid -> Maybe [Char] @@ -23,7 +24,7 @@ findMatchPosition keySequence = findWithIndex searchRows 0 searchInRow = guard . (== keySequence) isValidKey :: SDL.Keycode -> Bool -isValidKey = (`Map.member` keycodeMapping) +isValidKey = (`Map.member` keycodeCharMapping) -- Linear Congruential Generator lcg :: Int -> Int @@ -51,10 +52,10 @@ generateGrid seed (rows, columns) hintKeys ] toKeyChar :: SDL.Keycode -> Maybe Char -toKeyChar = (`Map.lookup` keycodeMapping) +toKeyChar = (`Map.lookup` keycodeCharMapping) -keycodeMapping :: Map.Map SDL.Keycode Char -keycodeMapping = +keycodeCharMapping :: Map.Map SDL.Keycode Char +keycodeCharMapping = Map.fromList [ (SDL.KeycodeA, 'A'), (SDL.KeycodeB, 'B'), @@ -81,15 +82,25 @@ keycodeMapping = (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') + (SDL.KeycodeZ, 'Z') ] + +keycodeToInt :: SDL.Keycode -> Maybe Int +keycodeToInt = (`elemIndex` digitKeycodes) + +isKeycodeDigit :: SDL.Keycode -> Bool +isKeycodeDigit = isJust . keycodeToInt + +digitKeycodes :: [SDL.Keycode] +digitKeycodes = + [ SDL.Keycode0, + SDL.Keycode1, + SDL.Keycode2, + SDL.Keycode3, + SDL.Keycode4, + SDL.Keycode5, + SDL.Keycode6, + SDL.Keycode7, + SDL.Keycode8, + SDL.Keycode9 + ] diff --git a/src/Chelleport/Types.hs b/src/Chelleport/Types.hs index b114189..3c52909 100644 --- a/src/Chelleport/Types.hs +++ b/src/Chelleport/Types.hs @@ -19,7 +19,8 @@ data State = State stateKeySequence :: KeySequence, stateIsMatched :: Bool, stateIsShiftPressed :: Bool, - stateIsDragging :: Bool + stateIsDragging :: Bool, + stateRepetition :: Int } deriving (Show, Eq) @@ -35,6 +36,7 @@ data AppAction | ShutdownApp | TriggerMouseClick MouseButtonType | UpdateShiftState Bool + | UpdateRepetition Int deriving (Show, Eq) data DrawContext = DrawContext |
