aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport
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
parent9842a86563058cc04dc06cd07fcede688c36d8df (diff)
downloadchelleport-487fc4f6abb243eea4bc1f371fb2d80e4789244e.tar.gz
chelleport-487fc4f6abb243eea4bc1f371fb2d80e4789244e.zip
Refactor update and view
Diffstat (limited to 'src/Chelleport')
-rw-r--r--src/Chelleport/Draw.hs11
-rw-r--r--src/Chelleport/View.hs18
2 files changed, 17 insertions, 12 deletions
diff --git a/src/Chelleport/Draw.hs b/src/Chelleport/Draw.hs
index 1c23ee0..9b30b48 100644
--- a/src/Chelleport/Draw.hs
+++ b/src/Chelleport/Draw.hs
@@ -1,6 +1,7 @@
module Chelleport.Draw where
import Chelleport.Types
+import Chelleport.Utils (intToCInt)
import Data.Text (Text)
import qualified Data.Vector.Storable as Vector
import Data.Word (Word8)
@@ -24,7 +25,7 @@ colorHighlight :: SDL.V4 Word8
colorHighlight = colorAccent
colorGridLines :: SDL.V4 Word8
-colorGridLines = colorGray -- SDL.V4 127 29 29 150
+colorGridLines = colorGray
colorFocusLines :: SDL.V4 Word8
colorFocusLines = colorLightGray
@@ -59,6 +60,14 @@ drawText ctx@(DrawContext {ctxRenderer = renderer}) position color text = do
windowSize :: DrawContext -> IO (SDL.V2 CInt)
windowSize = SDL.get . SDL.windowSize . ctxWindow
+cellSize :: State -> DrawContext -> IO (CInt, CInt)
+cellSize 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)
+
drawHorizontalLine :: DrawContext -> CInt -> IO ()
drawHorizontalLine ctx@(DrawContext {ctxRenderer = renderer}) x = do
(SDL.V2 width _height) <- windowSize ctx
diff --git a/src/Chelleport/View.hs b/src/Chelleport/View.hs
index 4956a9f..8ef852e 100644
--- a/src/Chelleport/View.hs
+++ b/src/Chelleport/View.hs
@@ -16,18 +16,15 @@ render :: State -> DrawContext -> IO ()
render state ctx = do
renderGridLines state ctx
- (SDL.V2 width height) <- windowSize ctx
- let grid = stateGrid state
- let wcell = width `div` intToCInt (length $ head grid)
- let hcell = height `div` intToCInt (length grid)
+ (wcell, hcell) <- cellSize state ctx
- forM_ (zip [0 ..] grid) $ \(rowIndex, row) -> do
+ forM_ (zip [0 ..] $ stateGrid state) $ \(rowIndex, row) -> do
forM_ (zip [0 ..] row) $ \(colIndex, cell) -> do
let py = rowIndex * hcell + 10
let px = colIndex * wcell + wcell `div` 2 - 20
visible <- renderKeySequence ctx (stateKeySequence state) cell (px, py)
when visible $ do
- renderTargetPoints state ctx (rowIndex, colIndex) (wcell, hcell)
+ renderTargetPoints state ctx (rowIndex, colIndex)
renderKeySequence :: DrawContext -> KeySequence -> Cell -> (CInt, CInt) -> IO Bool
renderKeySequence ctx keySequence cell (px, py) = do
@@ -57,10 +54,8 @@ renderKeySequence ctx keySequence cell (px, py) = do
renderGridLines :: State -> DrawContext -> IO ()
renderGridLines state ctx@(DrawContext {ctxRenderer = renderer}) = do
- (SDL.V2 width height) <- windowSize ctx
let grid = stateGrid state
- let wcell = width `div` intToCInt (length $ head grid)
- let hcell = height `div` intToCInt (length grid)
+ (wcell, hcell) <- cellSize state ctx
let rows = intToCInt $ length grid
let columns = intToCInt $ length $ head grid
@@ -79,8 +74,9 @@ renderGridLines state ctx@(DrawContext {ctxRenderer = renderer}) = do
drawHorizontalLine ctx (rows * hcell `div` 2)
drawVerticalLine ctx (columns * wcell `div` 2)
-renderTargetPoints :: State -> DrawContext -> (CInt, CInt) -> (CInt, CInt) -> IO ()
-renderTargetPoints state ctx@(DrawContext {ctxRenderer = renderer}) (row, col) (wcell, hcell) = do
+renderTargetPoints :: State -> DrawContext -> (CInt, CInt) -> IO ()
+renderTargetPoints state ctx@(DrawContext {ctxRenderer = renderer}) (row, col) = do
+ (wcell, hcell) <- cellSize state ctx
let (x, y) = (col * wcell + wcell `div` 2, row * hcell + hcell `div` 2)
SDL.rendererDrawColor renderer $= colorWhite
drawCircle ctx 2 (x, y)