aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport.hs
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-12-16 00:13:13 +0530
committerAkshay Nair <phenax5@gmail.com>2024-12-16 00:13:13 +0530
commit487fc4f6abb243eea4bc1f371fb2d80e4789244e (patch)
tree5fe9a3bda5bf347b9c9ed8315198a85fde397c74 /src/Chelleport.hs
parent9842a86563058cc04dc06cd07fcede688c36d8df (diff)
downloadchelleport-487fc4f6abb243eea4bc1f371fb2d80e4789244e.tar.gz
chelleport-487fc4f6abb243eea4bc1f371fb2d80e4789244e.zip
Refactor update and view
Diffstat (limited to 'src/Chelleport.hs')
-rw-r--r--src/Chelleport.hs39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/Chelleport.hs b/src/Chelleport.hs
index dba304d..8bd938e 100644
--- a/src/Chelleport.hs
+++ b/src/Chelleport.hs
@@ -2,7 +2,7 @@ module Chelleport where
import Chelleport.AppShell (Action (AppAction, SysQuit), EventHandler, Update, hideWindow, setupAppShell)
import Chelleport.Control (currentMousePosition, eventToKeycode, isKeyPress, isKeyPressWith, isKeyReleaseWith, moveMouse, triggerMouseLeftClick)
-import Chelleport.Draw (windowSize)
+import Chelleport.Draw (cellSize)
import Chelleport.KeySequence (findMatchPosition, generateGrid, isValidKey, nextChars, toKeyChar)
import Chelleport.Types
import Chelleport.Utils (cIntToInt, intToCInt)
@@ -30,21 +30,22 @@ initialState _ctx = do
columns = 12
hintKeys = ['A' .. 'Z'] \\ "Q"
-cellDimensions :: State -> DrawContext -> IO (CInt, CInt)
-cellDimensions state ctx = do
- (SDL.V2 width height) <- windowSize ctx
- let rows = stateGrid state
- let wcell = width `div` intToCInt (length $ head rows)
- let hcell = height `div` intToCInt (length rows)
- pure (wcell, hcell)
+directionalIncrement :: (CInt, CInt) -> Char -> (Int, Int)
+directionalIncrement (incx, incy) = \case
+ 'H' -> (-cIntToInt incx, 0)
+ 'L' -> (cIntToInt incx, 0)
+ 'K' -> (0, -cIntToInt incy)
+ 'J' -> (0, cIntToInt incy)
+ _ -> undefined
update :: Update State AppAction
+-- Act on key inputs
update state ctx (FilterSequence key) =
case liftA2 (,) (toKeyChar key) validChars of
Just (keyChar, validChars')
| stateIsMatched state && keyChar `elem` ("HJKL" :: String) -> do
incr <- incrementValue
- let action = IncrementMouseCursor $ incrementCursor keyChar incr
+ let action = IncrementMouseCursor $ directionalIncrement incr keyChar
pure (state, Just . AppAction $ action)
| keyChar `elem` validChars' -> do
let newKeySequence = stateKeySequence state ++ [keyChar]
@@ -55,36 +56,40 @@ update state ctx (FilterSequence key) =
where
validChars = nextChars (stateKeySequence state) (stateGrid state)
incrementValue = do
- (wcell, hcell) <- cellDimensions state ctx
+ (wcell, hcell) <- cellSize state ctx
if stateIsShiftPressed state
then pure (wcell `div` 4, hcell `div` 4)
else pure (wcell `div` 16, hcell `div` 16)
- incrementCursor :: Char -> (CInt, CInt) -> (Int, Int)
- incrementCursor 'H' (incx, _incy) = (-cIntToInt incx, 0)
- incrementCursor 'L' (incx, _incy) = (cIntToInt incx, 0)
- incrementCursor 'K' (_incx, incy) = (0, -cIntToInt incy)
- incrementCursor 'J' (_incx, incy) = (0, cIntToInt incy)
- incrementCursor _ _ = undefined
+
+-- Move mouse incrementally
update state ctx (IncrementMouseCursor (incx, incy)) = do
(SDL.V2 curx cury) <- currentMousePosition ctx
moveMouse ctx (curx + intToCInt incx) (cury + intToCInt incy)
pure (state, Nothing)
+
+-- Move mouse to given position
update state ctx (MoveMousePosition (row, col)) = do
(x, y) <- getPosition
moveMouse ctx x y
pure (state, Nothing)
where
getPosition = do
- (wcell, hcell) <- cellDimensions state ctx
+ (wcell, hcell) <- cellSize state ctx
let x = (wcell `div` 2) + wcell * intToCInt col
let y = (hcell `div` 2) + hcell * intToCInt row
pure (x, y)
+
+-- Reset entered key sequence and state
update state _ctx ResetKeys = do
pure (state {stateKeySequence = [], stateIsMatched = False}, Nothing)
+
+-- Trigger left click
update state ctx TriggerLeftClick = do
hideWindow ctx
triggerMouseLeftClick ctx
pure (state, Just SysQuit)
+
+-- Set/unset whether shift is pressed
update state _ctx (UpdateShiftState shift) = pure (state {stateIsShiftPressed = shift}, Nothing)
eventToAction :: EventHandler State AppAction