Add pausing functionality
This commit is contained in:
parent
977a1f355e
commit
3f0dcbb1aa
|
@ -10,6 +10,8 @@ data Command
|
|||
| -- | A command to change the tick speed of the simulation,
|
||||
-- given by a function on the period between ticks.
|
||||
ChangeSpeed (Time -> Time)
|
||||
| -- | A command to play/pause the simulation.
|
||||
PlayPause
|
||||
|
||||
getResize :: Command -> Maybe (Int, Int)
|
||||
getResize (Resize size) = Just size
|
||||
|
@ -19,5 +21,9 @@ getChangeSpeed :: Command -> Maybe (Time -> Time)
|
|||
getChangeSpeed (ChangeSpeed f) = Just f
|
||||
getChangeSpeed _ = Nothing
|
||||
|
||||
isPlayPause :: Command -> Bool
|
||||
isPlayPause PlayPause = True
|
||||
isPlayPause _ = False
|
||||
|
||||
-- | An event signalling that a command has been given by the user.
|
||||
type CommandEvent = Event Command
|
|
@ -39,6 +39,7 @@ processEvent :: InputEvent -> CommandEvent
|
|||
processEvent = event noEvent $ \case
|
||||
EventKey (Char '-') Down _ _ -> Event $ ChangeSpeed (* 1.25)
|
||||
EventKey (Char '+') Down _ _ -> Event $ ChangeSpeed (* 0.8)
|
||||
EventKey (SpecialKey KeySpace) Down _ _ -> Event PlayPause
|
||||
EventKey {} -> noEvent
|
||||
EventMotion {} -> noEvent
|
||||
EventResize size -> Event $ Resize size
|
||||
|
@ -49,9 +50,11 @@ run st =
|
|||
in proc inp -> do
|
||||
let cmdev = processEvent inp
|
||||
|
||||
playing <- accumHoldBy (const . not) True -< filterE isPlayPause cmdev
|
||||
time <- accum 0.2 -< mapFilterE getChangeSpeed cmdev
|
||||
windowSize <- hold (100, 100) -< mapFilterE getResize cmdev
|
||||
|
||||
space <- engine initSpace <<< tickSignal -< time
|
||||
tick <- tickSignal -< time
|
||||
space <- engine initSpace -< gate tick playing
|
||||
|
||||
returnA -< drawGrid windowSize white (getSpace space)
|
Loading…
Reference in a new issue