1
Fork 0

#7 Polish selector

This commit is contained in:
Tibo De Peuter 2022-11-15 22:55:33 +01:00
parent cc20b3a5e6
commit 6ca7511514

View file

@ -1,5 +1,6 @@
module Selector module Selector
( Selector (..) ( Selector (..)
, Coordinate
, Direction (..) , Direction (..)
, initSelector , initSelector
@ -8,6 +9,7 @@ module Selector
, select , select
, deselect , deselect
, toggleSelection , toggleSelection
, getSelected
, moveUp , moveUp
, moveDown , moveDown
@ -38,7 +40,7 @@ data Selector = Selector {
position :: Coordinate, position :: Coordinate,
-- The card(s) that the selector currently holds. -- The card(s) that the selector currently holds.
selected :: Maybe Coordinate selected :: Maybe Coordinate
} deriving (Show) } deriving (Show, Eq)
---------------------------------------------------------------------- ----------------------------------------------------------------------
@ -64,16 +66,25 @@ move D = moveBy (diff !! 1)
move L = moveBy (diff !! 2) move L = moveBy (diff !! 2)
move R = moveBy (diff !! 3) move R = moveBy (diff !! 3)
-- Select the current position.
select :: Selector -> Selector select :: Selector -> Selector
select s@Selector{ position = pos } = s{ selected = Just pos } select s@Selector{ position = pos } = s{ selected = Just pos }
-- Deselect the current selection.
deselect :: Selector -> Selector deselect :: Selector -> Selector
deselect s = s{ selected = Nothing } deselect s = s{ selected = Nothing }
-- Toggle the selection of the selector. Deselect if any position is
-- selected, otherwise select the current position.
toggleSelection :: Selector -> Selector toggleSelection :: Selector -> Selector
toggleSelection s@Selector{ selected = Nothing } = select s toggleSelection s@Selector{ selected = Nothing } = select s
toggleSelection s = deselect s toggleSelection s = deselect s
-- Get the selected coordinate, otherwise get (0,0) by default.
getSelected :: Selector -> Coordinate
getSelected s@Selector{ selected = Just c } = c
getSelected s@Selector{ selected = Nothing } = (0,0)
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- Move the selector up one position. -- Move the selector up one position.