aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Spear/App/Input.hs39
1 files changed, 20 insertions, 19 deletions
diff --git a/Spear/App/Input.hs b/Spear/App/Input.hs
index 07e7189..c8def3a 100644
--- a/Spear/App/Input.hs
+++ b/Spear/App/Input.hs
@@ -8,7 +8,7 @@ module Spear.App.Input
8, Mouse(..) 8, Mouse(..)
9, Input(..) 9, Input(..)
10, ButtonDelay 10, ButtonDelay
11, DelayedMouseState 11, DelayedMouse
12 -- * Input state querying 12 -- * Input state querying
13, newKeyboard 13, newKeyboard
14, getKeyboard 14, getKeyboard
@@ -20,7 +20,8 @@ module Spear.App.Input
20, toggledMouse 20, toggledMouse
21, toggledKeyboard 21, toggledKeyboard
22 -- * Delayed input 22 -- * Delayed input
23, newDMS 23, newDM
24, updateDM
24, delayedMouse 25, delayedMouse
25) 26)
26where 27where
@@ -168,31 +169,31 @@ type ButtonDelay = MouseButton -> Float
168 169
169 170
170-- | Accumulated delays for each mouse button. 171-- | Accumulated delays for each mouse button.
171newtype DelayedMouseState = DelayedMouseState (V.Vector Float) 172data DelayedMouse = DelayedMouse
173 { delayedMouse :: Mouse
174 , delay :: ButtonDelay
175 , accum :: V.Vector Float
176 }
172 177
173 178
174newDMS :: DelayedMouseState 179newDM :: ButtonDelay -- ^ Delay configuration for each button.
175newDMS = DelayedMouseState $ V.replicate (fromEnum (maxBound :: MouseButton)) 0 180 -> DelayedMouse
181newDM delay = DelayedMouse newMouse delay $
182 V.replicate (fromEnum (maxBound :: MouseButton)) 0
176 183
177 184
178delayedMouse :: ButtonDelay -- ^ Delay configuration for each button. 185updateDM :: DelayedMouse -- ^ Current mouse state.
179 -> Mouse -- ^ Current mouse state. 186 -> Float -- ^ Time elapsed since last udpate.
180 -> Float -- ^ Time elapsed since last udpate. 187 -> DelayedMouse
181 -> DelayedMouseState
182 -> (Mouse, DelayedMouseState)
183 188
184delayedMouse delay mouse dt (DelayedMouseState dms) = 189updateDM (DelayedMouse mouse delay accum) dt =
185 let 190 let
186 dms' 191 accum' = V.map (+dt) accum
187 = V.fromList 192 time x = accum' V.! fromEnum x
188 . fmap ((+dt) . (V.!) dms) 193 active x = time x >= delay x
189 $ [0 .. fromEnum (maxBound :: MouseButton)]
190
191 accum x = dms' V.! fromEnum x
192 active x = accum x >= delay x
193 button' x = active x && button mouse x 194 button' x = active x && button mouse x
194 in 195 in
195 (mouse { button = button' }, DelayedMouseState dms') 196 DelayedMouse mouse { button = button' } delay accum'
196 197
197 198
198 199