aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-12-28 21:36:39 +0530
committerAkshay Nair <phenax5@gmail.com>2024-12-28 21:36:39 +0530
commitd000cbb52821dc33c5d2f3d676dc6efa54f3b25f (patch)
tree7d4d51082b3b7ddf4f3b94879383efb003a87394 /src
parentb305546950a6742f25023e2ffece423905e1bda8 (diff)
downloadchelleport-d000cbb52821dc33c5d2f3d676dc6efa54f3b25f.tar.gz
chelleport-d000cbb52821dc33c5d2f3d676dc6efa54f3b25f.zip
Move hints mode state inside ModeHints constructor
Diffstat (limited to 'src')
-rw-r--r--src/Chelleport/AppState.hs46
-rw-r--r--src/Chelleport/Draw.hs6
-rw-r--r--src/Chelleport/Types.hs21
-rw-r--r--src/Chelleport/View.hs17
4 files changed, 47 insertions, 43 deletions
diff --git a/src/Chelleport/AppState.hs b/src/Chelleport/AppState.hs
index 9b69404..a304c96 100644
--- a/src/Chelleport/AppState.hs
+++ b/src/Chelleport/AppState.hs
@@ -15,13 +15,11 @@ import qualified Text.Fuzzy as Fuzzy
initialState :: (Monad m) => Configuration -> m (State, Maybe AppAction)
initialState config = do
- let cells = either error id $ generateGrid 0 (rows, columns) hintKeys
let action = Just $ SetMode $ configMode config
- pure (def {stateGrid = cells}, action)
+ pure (def {stateGridRows = rows, stateGridCols = columns}, action)
where
rows = 9
columns = 16
- hintKeys = ['A' .. 'Z']
update :: (MonadAppShell m, MonadDraw m, MonadControl m, MonadOCR m) => Update m State AppAction
-- Chain clicks
@@ -32,20 +30,20 @@ update _ state (ChainMouseClick btn) = do
pure (state {stateRepetition = 1}, Just ResetKeys)
-- HINTS MODE: Act on key inputs
-update _ state@(State {stateMode = ModeHints {}}) (HandleKeyInput keycode) = do
+update _ state@(State {stateMode = ModeHints hintsData}) (HandleKeyInput keycode) = do
case (toKeyChar keycode, validNextKeys) of
(Just keyChar, Just validChars')
- | stateIsMatched state && keyChar `elem` ("HJKL" :: String) -> do
+ | stateIsMatched hintsData && keyChar `elem` ("HJKL" :: String) -> do
pure (state, Just $ MoveMouseInDirection $ hjklDirection keyChar)
| keyChar `elem` validChars' -> do
- let newKeySequence = stateKeySequence state ++ [keyChar]
- let matchPosition = findMatchPosition newKeySequence $ stateGrid state
- let state' = state {stateKeySequence = newKeySequence, stateIsMatched = isJust matchPosition}
+ let newKeySequence = stateKeySequence hintsData ++ [keyChar]
+ let matchPosition = findMatchPosition newKeySequence $ stateGrid hintsData
+ let updatedHintsData = hintsData {stateKeySequence = newKeySequence, stateIsMatched = isJust matchPosition}
action <- traverse (fmap MoveMousePosition . screenPositionFromCellPosition state) matchPosition
- pure (state', action)
+ pure (state {stateMode = ModeHints updatedHintsData}, action)
_ -> pure (state, Nothing)
where
- validNextKeys = nextChars (stateKeySequence state) (stateGrid state)
+ validNextKeys = nextChars (stateKeySequence hintsData) (stateGrid hintsData)
-- SEARCH MODE: Act on key inputs
update _ state@(State {stateMode = ModeSearch (ModeSearchData {searchWords, searchInputText})}) (HandleKeyInput keycode) = do
@@ -125,25 +123,25 @@ update _ state (MoveMousePosition (x, y)) = do
-- Reset entered key sequence and state
update _ state ResetKeys = do
- pure
- ( state
- { stateKeySequence = [],
- stateIsMatched = False,
- stateRepetition = 1,
- stateMode = resetMode (stateMode state)
- },
- Nothing
- )
+ let nextState =
+ state
+ { stateRepetition = 1,
+ stateMode = resetMode (stateMode state)
+ }
+ pure (nextState, Nothing)
where
- resetMode mode@(ModeHints {}) = mode
+ resetMode (ModeHints hintsData) = ModeHints $ hintsData {stateKeySequence = [], stateIsMatched = False}
resetMode (ModeSearch searchData@(ModeSearchData {searchWords})) =
- ModeSearch (searchData {searchWords = searchWords, searchFilteredWords = searchWords})
+ ModeSearch $ searchData {searchWords = searchWords, searchFilteredWords = searchWords}
-- Initialize current mode
update flush state InitializeMode =
case stateMode state of
- ModeHints {} -> pure (state {stateIsModeInitialized = True}, Nothing)
- ModeSearch {} -> do
+ ModeHints hintsData -> do
+ let cells = either error id $ generateGrid 0 (stateGridRows state, stateGridCols state) ['A' .. 'Z']
+ let updateHintsData = hintsData {stateGrid = cells}
+ pure (state {stateMode = ModeHints updateHintsData, stateIsModeInitialized = True}, Nothing)
+ ModeSearch searchData -> do
position <- windowPosition
size <- windowSize
hideWindow
@@ -151,7 +149,7 @@ update flush state InitializeMode =
showWindow
flush
matches <- getWordsInImage screenshot
- let updatedSearchData = (modeSearchData $ stateMode state) {searchWords = matches, searchFilteredWords = matches}
+ let updatedSearchData = searchData {searchWords = matches, searchFilteredWords = matches}
pure (state {stateMode = ModeSearch updatedSearchData, stateIsModeInitialized = True}, Nothing)
-- Set mode
diff --git a/src/Chelleport/Draw.hs b/src/Chelleport/Draw.hs
index dbda1c1..afb72ca 100644
--- a/src/Chelleport/Draw.hs
+++ b/src/Chelleport/Draw.hs
@@ -78,10 +78,10 @@ fillRectVertices :: (MonadDraw m) => (CInt, CInt) -> (CInt, CInt) -> m ()
fillRectVertices (x1, y1) (x2, y2) = fillRect (x1, y1) (x2 - x1, y2 - y1)
cellSize :: (MonadDraw m) => State -> m (CInt, CInt)
-cellSize (State {stateGrid}) = do
+cellSize (State {stateGridRows, stateGridCols}) = do
(width, height) <- windowSize
- let wcell = width `div` intToCInt (length $ head stateGrid)
- let hcell = height `div` intToCInt (length stateGrid)
+ let wcell = width `div` intToCInt stateGridCols
+ let hcell = height `div` intToCInt stateGridRows
pure (wcell, hcell)
pointerPositionIncrement :: (MonadDraw m) => State -> m (CInt, CInt)
diff --git a/src/Chelleport/Types.hs b/src/Chelleport/Types.hs
index 3224e6f..ca4742e 100644
--- a/src/Chelleport/Types.hs
+++ b/src/Chelleport/Types.hs
@@ -36,10 +36,19 @@ instance Default ModeSearchData where
}
data ModeHintsData = ModeHintsData
+ { stateGrid :: KeyGrid,
+ stateKeySequence :: KeySequence,
+ stateIsMatched :: Bool
+ }
deriving (Show, Eq)
instance Default ModeHintsData where
- def = ModeHintsData
+ def =
+ ModeHintsData
+ { stateGrid = [],
+ stateKeySequence = "",
+ stateIsMatched = False
+ }
data Mode
= ModeHints {modeHintsData :: ModeHintsData}
@@ -47,9 +56,8 @@ data Mode
deriving (Show, Eq)
data State = State
- { stateGrid :: KeyGrid,
- stateKeySequence :: KeySequence,
- stateIsMatched :: Bool,
+ { stateGridRows :: Int,
+ stateGridCols :: Int,
stateIsShiftPressed :: Bool,
stateIsDragging :: Bool,
stateRepetition :: Int,
@@ -61,9 +69,8 @@ data State = State
instance Default State where
def =
State
- { stateGrid = [],
- stateKeySequence = "",
- stateIsMatched = False,
+ { stateGridRows = 0,
+ stateGridCols = 0,
stateIsShiftPressed = False,
stateIsDragging = False,
stateRepetition = 1,
diff --git a/src/Chelleport/View.hs b/src/Chelleport/View.hs
index 03c12d3..8cb6094 100644
--- a/src/Chelleport/View.hs
+++ b/src/Chelleport/View.hs
@@ -11,7 +11,7 @@ import Foreign.C (CInt)
render :: (MonadDraw m) => State -> m ()
render state = case stateMode state of
- ModeHints _ -> renderHintsView state
+ ModeHints modeHintsData -> renderHintsView state modeHintsData
ModeSearch modeSearchData -> renderSearchView state modeSearchData
getSearchText :: State -> ModeSearchData -> String
@@ -35,19 +35,19 @@ renderSearchView state searchData@(ModeSearchData {searchFilteredWords, searchHi
(w, h) <- windowSize
void $ drawText (w `div` 2, h `div` 2) colorWhite FontSM (Text.pack $ getSearchText state searchData)
-renderHintsView :: (MonadDraw m) => State -> m ()
-renderHintsView state = do
+renderHintsView :: (MonadDraw m) => State -> ModeHintsData -> m ()
+renderHintsView state (ModeHintsData {stateGrid, stateKeySequence, stateIsMatched}) = do
renderGridLines state
(wcell, hcell) <- cellSize state
- forM_ (zip [0 ..] $ stateGrid state) $ \(rowIndex, row) -> forM_ (zip [0 ..] row) $ \(colIndex, cell) -> do
+ forM_ (zip [0 ..] stateGrid) $ \(rowIndex, row) -> forM_ (zip [0 ..] row) $ \(colIndex, cell) -> do
let py = rowIndex * hcell + 10
let px = colIndex * wcell + wcell `div` 2 - 20
- visible <- renderKeySequence (stateKeySequence state) cell (px, py)
+ visible <- renderKeySequence stateKeySequence cell (px, py)
when visible $ do
renderTargetMarker state (rowIndex, colIndex)
- when (stateIsMatched state) $ do
+ when stateIsMatched $ do
renderGranularGrid state (rowIndex, colIndex)
renderKeySequence :: (MonadDraw m) => KeySequence -> Cell -> (CInt, CInt) -> m Bool
@@ -75,11 +75,10 @@ renderKeySequence keySequence cell (px, py) = do
renderGridLines :: (MonadDraw m) => State -> m ()
renderGridLines state = do
- let grid = stateGrid state
(wcell, hcell) <- cellSize state
+ let rows = intToCInt $ stateGridRows state
+ let columns = intToCInt $ stateGridCols state
- let rows = intToCInt $ length grid
- let columns = intToCInt $ length $ head grid
forM_ [0 .. rows] $ \rowIndex -> do
setDrawColor colorFocusLines
drawHorizontalLine (rowIndex * hcell + hcell `div` 2)