aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-08-16 09:01:34 -0700
committer3gg <3gg@shellblade.net>2024-08-16 09:01:34 -0700
commitc6afd90485fe19e6b5b3f78df173aae819e2624b (patch)
treeff801fae6ab882b9b05862ee587a6e5a5e86af9b
parent678a4631a36b55face6541c473d5dfb854225547 (diff)
More convenient switch and multiSwitch.
-rw-r--r--Spear/Step.hs24
1 files changed, 11 insertions, 13 deletions
diff --git a/Spear/Step.hs b/Spear/Step.hs
index e767166..a860247 100644
--- a/Spear/Step.hs
+++ b/Spear/Step.hs
@@ -112,9 +112,11 @@ swhen expectedEvent step = Step $ \elapsed dt state maybeEvent a ->
112 in (a', swhen expectedEvent step') 112 in (a', swhen expectedEvent step')
113 else (a, swhen expectedEvent step) 113 else (a, swhen expectedEvent step)
114 114
115-- | Construct a step that switches between two steps based on input. 115-- | Construct a step that switches between two steps based on input events.
116-- 116--
117-- The initial step is the first one. 117-- The current step runs with every 'runStep' even when there are no new events.
118--
119-- The initial step is the identity, 'sid'.
118switch :: 120switch ::
119 Eq e => 121 Eq e =>
120 e -> 122 e ->
@@ -122,16 +124,8 @@ switch ::
122 e -> 124 e ->
123 Step s (Maybe e) a a -> 125 Step s (Maybe e) a a ->
124 Step s (Maybe e) a a 126 Step s (Maybe e) a a
125switch flag1 s1 flag2 s2 = switch' s1 flag1 s1 flag2 s2 127switch = switch' sid
126 128
127switch' ::
128 Eq e =>
129 Step s (Maybe e) a a ->
130 e ->
131 Step s (Maybe e) a a ->
132 e ->
133 Step s (Maybe e) a a ->
134 Step s (Maybe e) a a
135switch' cur flag1 s1 flag2 s2 = Step $ \elapsed dt g e a -> 129switch' cur flag1 s1 flag2 s2 = Step $ \elapsed dt g e a ->
136 case e of 130 case e of
137 Nothing -> 131 Nothing ->
@@ -141,11 +135,15 @@ switch' cur flag1 s1 flag2 s2 = Step $ \elapsed dt g e a ->
141 let next 135 let next
142 | e' == flag1 = s1 136 | e' == flag1 = s1
143 | e' == flag2 = s2 137 | e' == flag2 = s2
144 | otherwise = cur 138 | otherwise = cur
145 (a', s') = runStep next elapsed dt g e a 139 (a', s') = runStep next elapsed dt g e a
146 in (a', switch' s' flag1 s1 flag2 s2) 140 in (a', switch' s' flag1 s1 flag2 s2)
147 141
148-- | Construct a step that switches among multiple steps based on input. 142-- | Construct a step that switches among multiple steps based on input events.
143--
144-- The current step runs with every 'runStep' even when there are no new events.
145--
146-- The initial step is the identity, 'sid'.
149multiSwitch :: (Eq e, Ord e) => [(e, Step s (Maybe e) a a)] -> Step s (Maybe e) a a 147multiSwitch :: (Eq e, Ord e) => [(e, Step s (Maybe e) a a)] -> Step s (Maybe e) a a
150multiSwitch xs = multiSwitch' Nothing sid (Map.fromList xs) 148multiSwitch xs = multiSwitch' Nothing sid (Map.fromList xs)
151 149