aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Chelleport.hs27
-rw-r--r--src/Chelleport/Control.hs5
-rw-r--r--src/Chelleport/Types.hs4
3 files changed, 27 insertions, 9 deletions
diff --git a/src/Chelleport.hs b/src/Chelleport.hs
index cb0ed84..e50ecdd 100644
--- a/src/Chelleport.hs
+++ b/src/Chelleport.hs
@@ -1,14 +1,14 @@
module Chelleport where
import Chelleport.AppShell (Action (AppAction, SysQuit), EventHandler, Update, hideWindow, setupAppShell)
-import Chelleport.Control (isKeyPress, isKeyPressWith, moveMouse, triggerMouseLeftClick)
+import Chelleport.Control (currentMousePosition, isKeyPress, isKeyPressWith, moveMouse, triggerMouseLeftClick)
import Chelleport.Draw (windowSize)
import Chelleport.KeySequence (eventToKeycode, findMatchPosition, generateGrid, isValidKey, nextChars, toKeyChar)
import Chelleport.Types
import Chelleport.Utils (intToCInt)
import qualified Chelleport.View
import Data.List ((\\))
-import Data.Maybe (fromMaybe)
+import Data.Maybe (fromMaybe, isJust)
import qualified SDL
open :: IO ()
@@ -17,7 +17,7 @@ open = setupAppShell initialState update eventToAction Chelleport.View.render
initialState :: DrawContext -> IO State
initialState _ctx = do
let cells = fromMaybe (pure undefined) $ generateGrid 0 (rows, columns) hintKeys
- pure $ State {stateGrid = cells, stateKeySequence = []}
+ pure $ State {stateGrid = cells, stateKeySequence = [], stateIsMatched = False}
where
rows = 12
columns = 12
@@ -27,16 +27,27 @@ update :: Update State AppAction
update state _ctx (FilterSequence key) =
case liftA2 (,) (toKeyChar key) validChars of
Just (keyChar, validChars')
+ | stateIsMatched state && keyChar `elem` ("HJKL" :: String) -> do
+ let incr = 10
+ let action = IncrementMouseCursor $ incrementCursor keyChar incr
+ pure (state, Just . AppAction $ action)
| keyChar `elem` validChars' -> do
let newKeySequence = stateKeySequence state ++ [keyChar]
let matchPosition = findMatchPosition newKeySequence $ stateGrid state
- pure
- ( state {stateKeySequence = newKeySequence},
- AppAction . MoveMousePosition <$> matchPosition
- )
+ let state' = state {stateKeySequence = newKeySequence, stateIsMatched = isJust matchPosition}
+ pure (state', AppAction . MoveMousePosition <$> matchPosition)
_ -> pure (state, Nothing)
where
validChars = nextChars (stateKeySequence state) (stateGrid state)
+ incrementCursor 'H' inc = (-inc, 0)
+ incrementCursor 'L' inc = (inc, 0)
+ incrementCursor 'K' inc = (0, -inc)
+ incrementCursor 'J' inc = (0, inc)
+ incrementCursor _ _ = undefined
+update state ctx (IncrementMouseCursor (incx, incy)) = do
+ (SDL.V2 curx cury) <- currentMousePosition ctx
+ moveMouse ctx (curx + intToCInt incx) (cury + intToCInt incy)
+ pure (state, Nothing)
update state ctx (MoveMousePosition (row, col)) = do
(x, y) <- getPosition
moveMouse ctx x y
@@ -54,7 +65,7 @@ update state ctx (MoveMousePosition (row, col)) = do
let x = (wcell `div` 2) + wcell * intToCInt col
let y = (hcell `div` 2) + hcell * intToCInt row
pure (x, y)
-update state _ctx ResetKeys = pure (state {stateKeySequence = []}, Nothing)
+update state _ctx ResetKeys = pure (state {stateKeySequence = [], stateIsMatched = False}, Nothing)
update state ctx TriggerLeftClick = do
hideWindow ctx
triggerMouseLeftClick ctx
diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs
index c915ae8..7b58fdf 100644
--- a/src/Chelleport/Control.hs
+++ b/src/Chelleport/Control.hs
@@ -18,6 +18,11 @@ moveMouse :: DrawContext -> CInt -> CInt -> IO ()
moveMouse _ x y = do
SDL.warpMouse SDL.WarpGlobal (SDL.P $ SDL.V2 x y)
+currentMousePosition :: DrawContext -> IO (SDL.V2 CInt)
+currentMousePosition _ctx = do
+ (SDL.P p) <- SDL.getAbsoluteMouseLocation
+ pure p
+
isKeyPress :: SDL.KeyboardEventData -> Bool
isKeyPress = (== SDL.Pressed) . SDL.keyboardEventKeyMotion
diff --git a/src/Chelleport/Types.hs b/src/Chelleport/Types.hs
index 8c86d03..2238019 100644
--- a/src/Chelleport/Types.hs
+++ b/src/Chelleport/Types.hs
@@ -12,7 +12,8 @@ type KeyGrid = [[Cell]]
data State = State
{ stateGrid :: KeyGrid,
- stateKeySequence :: KeySequence
+ stateKeySequence :: KeySequence,
+ stateIsMatched :: Bool
}
data AppAction
@@ -20,6 +21,7 @@ data AppAction
| MoveMousePosition (Int, Int)
| ResetKeys
| TriggerLeftClick
+ | IncrementMouseCursor (Int, Int)
data DrawContext = DrawContext
{ ctxWindow :: SDL.Window,