newKeyCallback :: AP.AppDataRef -> GLFW.KeyCallback
newKeyCallback appDataRef = callback
   where
      callback win GLFW.Key'Escape _ GLFW.KeyState'Pressed _ = quit win
      callback win GLFW.Key'Q      _ GLFW.KeyState'Pressed _ = quit win

      callback _ GLFW.Key'S   _ GLFW.KeyState'Pressed _ = do
         appMode <- AP.appMode <$> R.readIORef appDataRef
         when (appMode == AP.EditMode) $ do
            (gdata, saveTo) <- (AP.gameData &&& AP.saveLevelsTo) <$> R.readIORef appDataRef
            writeFile saveTo (show . TF.toFileData $ gdata)
            putStrLn $ "layers: Levels data written to file '" ++ saveTo ++ "'"

      callback win key _ keyState mods = do
         mpos <- CC.mousePosition win appDataRef
         case keyState of
              GLFW.KeyState'Pressed  -> do
                 let keyInfo = KI.KeyInfo key II.Pressed mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              GLFW.KeyState'Released -> do
                 let keyInfo = KI.KeyInfo key II.Released mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              _                      -> return ()

      quit win = GLFW.destroyWindow win >> GLFW.terminate >> exitSuccess
newKeyCallback :: AP.AppDataRef -> GLFW.KeyCallback
newKeyCallback appDataRef = callback
   where
      callback win GLFW.Key'Escape _ GLFW.KeyState'Pressed _ = quit win
      callback win GLFW.Key'Q      _ GLFW.KeyState'Pressed _ = quit win

      callback _ GLFW.Key'S   _ GLFW.KeyState'Pressed _ = do
         appMode <- AP.appMode <$> R.readIORef appDataRef
         when (appMode == AP.EditMode) $ do
            (gdata, saveTo) <- (AP.gameData &&& AP.saveLevelsTo) <$> R.readIORef appDataRef
            writeFile saveTo (show . TF.toFileData $ gdata)
            putStrLn $ "layers: Levels data written to file '" ++ saveTo ++ "'"

      callback win key _ keyState mods = do
         mpos <- CC.mousePosition win appDataRef
         case keyState of
              GLFW.KeyState'Pressed  -> do
                 let keyInfo = KI.KeyInfo key II.Pressed mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              GLFW.KeyState'Released -> do
                 let keyInfo = KI.KeyInfo key II.Released mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              _                      -> return ()

      quit win = GLFW.destroyWindow win >> GLFW.terminate >> exitSuccess
newKeyCallback appDataRef = callback
   where
      callback win GLFW.Key'Escape _ GLFW.KeyState'Pressed _ = quit win
      callback win GLFW.Key'Q      _ GLFW.KeyState'Pressed _ = quit win

      callback _ GLFW.Key'S   _ GLFW.KeyState'Pressed _ = do
         appMode <- AP.appMode <$> R.readIORef appDataRef
         when (appMode == AP.EditMode) $ do
            (gdata, saveTo) <- (AP.gameData &&& AP.saveLevelsTo) <$> R.readIORef appDataRef
            writeFile saveTo (show . TF.toFileData $ gdata)
            putStrLn $ "layers: Levels data written to file '" ++ saveTo ++ "'"

      callback win key _ keyState mods = do
         mpos <- CC.mousePosition win appDataRef
         case keyState of
              GLFW.KeyState'Pressed  -> do
                 let keyInfo = KI.KeyInfo key II.Pressed mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              GLFW.KeyState'Released -> do
                 let keyInfo = KI.KeyInfo key II.Released mpos mods
                 R.modifyIORef appDataRef (AP.handleKeyEvent keyInfo)

              _                      -> return ()

      quit win = GLFW.destroyWindow win >> GLFW.terminate >> exitSuccess