@@ -7,6 +7,7 @@ import Control.Concurrent (threadDelay)
77import Control.Monad (forM_ , when )
88import Control.Monad.IO.Class (liftIO )
99import Data.IntSet qualified as IS
10+ import Data.Maybe (isNothing )
1011import Language.Javascript.JSaddle (liftJSM , FromJSVal (.. ), ToJSVal (.. ))
1112import Miso
1213import Miso.Canvas as Canvas
@@ -49,6 +50,7 @@ data Action
4950 | ActionAskTime
5051 | ActionSetTime Double
5152 | ActionPointer PointerEvent
53+ | ActionUndo
5254
5355-------------------------------------------------------------------------------
5456-- update
@@ -93,11 +95,19 @@ updateModel (ActionPointer event) = do
9395 (False , _, True ) -> doPlayMove $ playMove MoveRight
9496 (False , _, False ) -> doPlayMove $ playMove MoveLeft
9597
98+ updateModel ActionUndo = do
99+ previous <- use modelPrevious
100+ forM_ previous $ \ g -> do
101+ modelPrevious .= Nothing
102+ modelGame .= g
103+
96104doPlayMove :: (Game -> Maybe Game ) -> Transition Model Action
97105doPlayMove f = do
98- mg <- f <$> use modelGame
99- forM_ mg $ \ g -> do
100- modelGame .= g
106+ g0 <- use modelGame
107+ let mg1 = f g0
108+ forM_ mg1 $ \ g1 -> do
109+ modelPrevious .= Just g0
110+ modelGame .= g1
101111 modelNbMoves += 1
102112
103113-------------------------------------------------------------------------------
@@ -147,6 +157,7 @@ viewModel m@Model{..} =
147157 ]
148158 , p_ []
149159 [ select_ [ onChange ActionAskLevel ] (map fmtOption [1 .. length allWorlds])
160+ , button_ (undoOpts ++ [ onClick ActionUndo ]) [ " undo" ]
150161 , button_ [ onClick (ActionSetLevel _modelLevel) ] [ " reset" ]
151162 , button_ [ onClick (ActionSetLevel (1 + _modelLevel)) ] [ " next level" ]
152163 ]
@@ -162,6 +173,8 @@ viewModel m@Model{..} =
162173 ]
163174
164175 where
176+ undoOpts = [ disabled_ | isNothing _modelPrevious ]
177+
165178 (w, h) = ij2xy $ getNiNj _modelGame
166179
167180 status = if computeRunning _modelGame then " " else " , done !!!"
0 commit comments