module Input where
import qualified Graphics.UI.SDL as SDL
-- 入力
data Input = Input {
inputKey :: [SDL.SDLKey],
inputMouse :: (Bool, Bool, Bool)
}
emptyInput = Input [] (False, False, False)
hasKey :: Input -> SDL.SDLKey -> Bool
hasKey input key = elem key (inputKey input)
addKey :: Input -> SDL.SDLKey -> Input
addKey i@Input { inputKey = keys } key = if elem key keys then i else i { inputKey = key:keys }
eraseKey :: Input -> SDL.SDLKey -> Input
eraseKey i@Input { inputKey = keys } key = i { inputKey = filter (/= key) keys }
-- イベントによる更新
updateInput :: Input -> SDL.Event -> Input
updateInput input (SDL.KeyDown (SDL.Keysym key mod ucode)) = addKey input key
updateInput input (SDL.KeyUp (SDL.Keysym key mod ucode)) = eraseKey input key
updateInput input SDL.Quit = addKey input SDL.SDLK_ESCAPE
updateInput input _ = input