#7 Polish selector
This commit is contained in:
parent
cc20b3a5e6
commit
6ca7511514
1 changed files with 12 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
|||
module Selector
|
||||
( Selector (..)
|
||||
, Coordinate
|
||||
, Direction (..)
|
||||
|
||||
, initSelector
|
||||
|
@ -8,6 +9,7 @@ module Selector
|
|||
, select
|
||||
, deselect
|
||||
, toggleSelection
|
||||
, getSelected
|
||||
|
||||
, moveUp
|
||||
, moveDown
|
||||
|
@ -38,7 +40,7 @@ data Selector = Selector {
|
|||
position :: Coordinate,
|
||||
-- The card(s) that the selector currently holds.
|
||||
selected :: Maybe Coordinate
|
||||
} deriving (Show)
|
||||
} deriving (Show, Eq)
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
|
@ -64,16 +66,25 @@ move D = moveBy (diff !! 1)
|
|||
move L = moveBy (diff !! 2)
|
||||
move R = moveBy (diff !! 3)
|
||||
|
||||
-- Select the current position.
|
||||
select :: Selector -> Selector
|
||||
select s@Selector{ position = pos } = s{ selected = Just pos }
|
||||
|
||||
-- Deselect the current selection.
|
||||
deselect :: Selector -> Selector
|
||||
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 s@Selector{ selected = Nothing } = select 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.
|
||||
|
|
Reference in a new issue