如何在 Haskell Gloss 中隐藏鼠标光标

How to hide the mouse cursor in Haskell Gloss

是否可以在 Haskell 中使用 Gloss 隐藏光标?我们想用鼠标位置替换播放器。

此功能未通过 gloss 公开:如果 Internal 模块公开(通过使用例如 playWithBackendIO and some custom instances of Backend to hide the cursor). If you really want this functionality, it'd be best to fork the project 并添加您想要的功能。


虽然有一些不修改原始包的解决方法:当使用任何 ...IO 函数时,我们可以在我们传入的函数中执行任意 IO 操作,这允许我们修改后端状态。

为了简单起见,我将在此处使用 displayIO,但这适用于任何模式,例如 playIO

import Prelude hiding (init)
import Control.Monad
import Data.IORef
import Data.StateVar
import Graphics.Gloss
import Graphics.Gloss.Interface.IO.Display
import qualified Graphics.UI.GLUT.Window as Glut

init :: IO ()
init = Glut.cursor $= Glut.None

render :: IORef Bool -> IO Picture
render doneInit = do
    needsInit <- not <$> readIORef doneInit
    when needsInit $ do
        init
        writeIORef doneInit True
    return $ color white $ circle 30

controllerCallback :: Controller -> IO ()
controllerCallback _ = return ()

main :: IO ()
main = do
    let disp = InWindow "test" (800, 600) (0, 0)
    initVar <- newIORef False
    displayIO disp black (render initVar) controllerCallback

重要的部分是 Glut.cursor $= Glut.None,它使用 StateVar 的 $= 函数设置 GLUT 的 cursor 值并自动更新过剩上下文。我们只想 运行 这一次,所以我们使用 IORef 来跟踪我们之前是否 运行 它。

最后,这不适用于 GLFW 后端。 gloss 使用 very old version of GLFW-b which doesn't support anything to do with cursor modes. More modern versions do,但我无法找到获取 Window 实例的方法,因为 Gloss 只是丢弃它。